Raspberry Pi
Tutorial de Raspberry Pi en español.
- Juan Antonio Villalpando -
Volver al índice del tutorial
____________________________
6.- Teclado keypad 4x4 con bus I2C. Pantalla LCD con bus I2C. Librería WiringPi.
GPIO2 es SDA
GPIO3 es SCL
______________________________
1.- Conexión.
- La pantalla LCD tiene la dirección 27
- El módulo I2C del keypad tiene la dirección 20
- Para ver las direcciones I2C utilizamos la orden:
i2cdetect -y 1
- Para que funcionen los I2C deben estar conectados. Primero conectaremos solamente el teclado. En el segundo ejemplo conectaremos el teclado y la pantalla LCD.
- Recuerda que si no están conectados no funcionará el código.
______________________________
2.- Teclado Keypad 4x4.
- Seguimos este tutorial para instalar Wiring Pi: http://wiringpi.com/download-and-install/ (vamos al Plan B)
- Vamos a
https://git.drogon.net/?p=wiringPi;a=summary y bajamos el último snapshot.
- wiringPi-36fb7f1.tar.gz
- Creamos una carpeta:
$ sudo su
# mkdir /home/pi/teclado
# cd /home/pi/teclado
- Copiamos el archivo en esa carpeta, lo descomprimos y generamos la librería:
# cp /home/pi/Downloads/wiringPi-36fb7f1.tar.gz /home/pi/teclado/
# tar xfz wiringPi-36fb7f1.tar.gz
# cd /home/pi/teclado/wiringPi-36fb7f1
# ./build
- Se generará la librería wiringPi.
Con esta librería podemos trabajar con los terminales GPIO.
# gpio -v
# gpio readall
- Por ejemplo, encender y apagar un LED en el GPIO26:
# /usr/local/bin/gpio -g mode 26 out
# /usr/local/bin/gpio -g write 26 0
# /usr/local/bin/gpio -g write 26 1
http://wiringpi.com/the-gpio-utility/ |
- Creamos el archivo teclado.py y lo ejecutamos. Observa la dirección el módulo I2C del keypad 0x20
- python teclado.py
|
#!/usr/bin/env python
import smbus
import sys
import time
""" this is an example on how to create a keypad using the pcf8475
I use an old telephone keypad and this is the layout of it
Pin4(P4) Pin6(P5) Pin8(P6)
| | |
Pin5(P0) --- 1 ------- 2 -------- 3
| | |
Pin7(P1) --- 4 ------- 5 -------- 6
| | |
Pin2(P2) --- 7 ------- 8 -------- 9
| | |
Pin3(P3) --- * ------- 0 -------- #
"""
class MyKeyboard:
#Modificado por Juan A. Villalpando http://kio4.com
KeyPadTable= [['D','C','B','A'] , ['#','9','6','3'], ['0','8','5','2'], ['*','7','4','1']]
RowID=[0,0,0,0,0,0,0,4,0,0,0,3,0,2,1,0]
CurrentKey=None
def __init__(self,I2CBus=1, I2CAddress=0x20):
self.I2CAddress = I2CAddress
self.I2CBus = I2CBus
#open smbus pcf8574
self.bus = smbus.SMBus(self.I2CBus)
#set pcf to input
self.bus.write_byte(self.I2CAddress,0xff)
#ReadRawKey
#this function will scan and return a key press
#with no debouncing.
# it will return None if no or more than a key is pressed on the same row
def ReadRawKey(self):
#set P4 Low First
OutPin= 0x10
for Column in range(4):
#scan first row to see if we have something
self.bus.write_byte(self.I2CAddress,~OutPin)
#read the key now
key = self.RowID[self.bus.read_byte(self.I2CAddress) & 0x0f]
if key >0 :
return self.KeyPadTable[key-1][Column]
OutPin = OutPin * 2
return None
#ReadKey return current key once and debounce it
def ReadKey(self):
LastKey= self.CurrentKey;
while True:
NewKey= self.ReadRawKey()
if NewKey != LastKey:
time.sleep(0.01)
LastKey= NewKey
else:
break
#if LastValue is the same than CurrentValue
#just return None
if LastKey==self.CurrentKey:
return None
#ok put Lastvalue to be CurrentValue
self.CurrentKey=LastKey
return self.CurrentKey
if __name__ == "__main__":
test = MyKeyboard()
while True:
V = test.ReadKey()
if V != None:
sys.stdout.write(V)
#print(V)
sys.stdout.flush()
else:
time.sleep(0.001)
|
- También podemos ver la tecla pulsada mediante
print(V)
______________________________
3.- Teclado Keypad 4x4 y pantalla LCD.
- Añadido:
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()
mylcd.lcd_display_string(V, 1)
- El archivo de librería I2C_LCD_driver, debe estar en la misma carpeta que el tecladoLCD.py
- El archivo I2C_LCD_driver.py lo puedes obtener en el tutorial anterior sobre LCD. En ese archivo es donde se establece la dirección de la LCD, en nuestro caso la 0x27
|
#!/usr/bin/env python
import smbus
import sys
import time
import I2C_LCD_driver
class MyKeyboard:
#Modificado por Juan A. Villalpando http://kio4.com
KeyPadTable= [['D','C','B','A'] , ['#','9','6','3'], ['0','8','5','2'], ['*','7','4','1']]
RowID=[0,0,0,0,0,0,0,4,0,0,0,3,0,2,1,0]
CurrentKey=None
def __init__(self,I2CBus=1, I2CAddress=0x20):
self.I2CAddress = I2CAddress
self.I2CBus = I2CBus
self.bus = smbus.SMBus(self.I2CBus)
self.bus.write_byte(self.I2CAddress,0xff)
def ReadRawKey(self):
OutPin= 0x10
for Column in range(4):
self.bus.write_byte(self.I2CAddress,~OutPin)
key = self.RowID[self.bus.read_byte(self.I2CAddress) & 0x0f]
if key >0 :
return self.KeyPadTable[key-1][Column]
OutPin = OutPin * 2
return None
def ReadKey(self):
LastKey= self.CurrentKey;
while True:
NewKey= self.ReadRawKey()
if NewKey != LastKey:
time.sleep(0.01)
LastKey= NewKey
else:
break
if LastKey==self.CurrentKey:
return None
self.CurrentKey=LastKey
return self.CurrentKey
if __name__ == "__main__":
test = MyKeyboard()
mylcd = I2C_LCD_driver.lcd()
while True:
V = test.ReadKey()
if V != None:
sys.stdout.write(V)
mylcd.lcd_display_string(V, 1)
#print(V)
sys.stdout.flush()
else:
time.sleep(0.001)
|
______________________________
4.- Teclado Keypad 4x4 y pantalla LCD. Escribir una tanda de varios números.
- Vamos a escribir varios números, por ejemplo 382 y pulsar la tecla #, para indicar el final de la cifra.
- Será una modificación del código anterior.
- Dispondrá de la variable todo, en donde se irá acumulando cada uno de los caracteres pulsados.
|
#!/usr/bin/env python
import smbus
import sys
import time
import I2C_LCD_driver
class MyKeyboard:
#Modificado por Juan A. Villalpando http://kio4.com
KeyPadTable= [['D','C','B','A'] , ['#','9','6','3'], ['0','8','5','2'], ['*','7','4','1']]
RowID=[0,0,0,0,0,0,0,4,0,0,0,3,0,2,1,0]
CurrentKey=None
def __init__(self,I2CBus=1, I2CAddress=0x20):
self.I2CAddress = I2CAddress
self.I2CBus = I2CBus
self.bus = smbus.SMBus(self.I2CBus)
self.bus.write_byte(self.I2CAddress,0xff)
def ReadRawKey(self):
OutPin= 0x10
for Column in range(4):
self.bus.write_byte(self.I2CAddress,~OutPin)
key = self.RowID[self.bus.read_byte(self.I2CAddress) & 0x0f]
if key >0 :
return self.KeyPadTable[key-1][Column]
OutPin = OutPin * 2
return None
def ReadKey(self):
LastKey= self.CurrentKey;
while True:
NewKey= self.ReadRawKey()
if NewKey != LastKey:
time.sleep(0.01)
LastKey= NewKey
else:
break
if LastKey==self.CurrentKey:
return None
self.CurrentKey=LastKey
return self.CurrentKey
if __name__ == "__main__":
test = MyKeyboard()
mylcd = I2C_LCD_driver.lcd()
todo = ""
while True:
V = test.ReadKey()
if V != None:
todo = todo + V
mylcd.lcd_clear()
mylcd.lcd_display_string(todo, 1)
if V == '#':
sys.stdout.write(todo)
mylcd.lcd_display_string(todo[:-1], 1)
#print(V)
sys.stdout.flush()
todo = ""
else:
time.sleep(0.001)
|
___________________________________________________
|