1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| # coding=utf-8
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
class keypad():
def __init__(self, columnCount = 7):
GPIO.setmode(GPIO.BCM)
# Définition des paramètres
if columnCount is 7:
self.KEYPAD = [
[0,5,10,15,20,25,29],
[1,6,11,16,21,26,30],
[2,7,12,17,22,27,31],
[3,8,13,18,23,28,32],
[4,9,14,19,24,90,91],
]
# Affectation des broches
self.ROW = [25,13,12,6,5]
self.COLUMN = [17,4,18,27,22,23,24]
else:
return
def getKey(self):
# Définition des colonnes en sorties
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.OUT)
GPIO.output(self.COLUMN[j], GPIO.LOW)
# Définition des lignes en entrées
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Regarde si un bouton est poussé
rowVal = -1
for i in range(len(self.ROW)):
tmpRead = GPIO.input(self.ROW[i])
if tmpRead == 0:
rowVal = i
if rowVal <0 or rowVal >4:
self.exit()
return
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.ROW[rowVal], GPIO.OUT)
GPIO.output(self.ROW[rowVal], GPIO.HIGH)
colVal = -1
for j in range(len(self.COLUMN)):
tmpRead = GPIO.input(self.COLUMN[j])
if tmpRead == 1:
colVal=j
if colVal <0 or colVal >4:
self.exit()
return
# Retourne la position du bouton pressé
self.exit()
return self.KEYPAD[rowVal][colVal]
def exit(self):
# Retourne le nom du bouton pressé
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
if __name__ == '__main__':
# Initialisation du clavier
kp = keypad()
print ('--- Clavier prêt ---')
# Boucle principale
while True:
digit = None
while digit == None:
digit = kp.getKey()
# Affiche le nom de la touche enfoncée
print (digit) |
Partager