#clavier matrice 4 X 4 donc 16 touches et display 7segments
import RPi.GPIO as GPIO
import time

pins = [11,12,13,15,16,18,22,7]

dats = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]

def setup(): #initialisation GPIO de la raspberry pi
	GPIO.setmode(GPIO.BOARD)
	for pin in pins: #Dans la liste pins va chercher les 8 pin utilisées
		GPIO.setup(pin, GPIO.OUT)   # Place les pins utilisées en output
		GPIO.output(pin, GPIO.LOW)  # Place les pins utilisées à 0 pour éteindre le display

def writeOneByte(val): #val contient le code hexa
        GPIO.output(11, val & (0x01 << 0))  #(0x01 << 0)=1
        GPIO.output(12, val & (0x01 << 1))  #(0x01 << 1)=2
        GPIO.output(13, val & (0x01 << 2))  #(0x01 << 2)=4
        GPIO.output(15, val & (0x01 << 3))  #(0x01 << 3)=8  
        GPIO.output(16, val & (0x01 << 4))  #(0x01 << 4)=16  
        GPIO.output(18, val & (0x01 << 5))  #(0x01 << 5)=32  
        GPIO.output(22, val & (0x01 << 6))  #(0x01 << 6)=64  
        GPIO.output(7,  val & (0x01 << 7))  #(0x01 << 7)=128
        #avec le code hexa 3f les pins 11,12,13,15,16,18,22,7 seront =11111100
        #ce qui affichera le 0 sur le display 7 segments
        #et ainsi de suite avec les codes hexa 06,5b, jusqu'au code 80.
        
        
class keypad():
    # CONSTANTS   
    KEYPAD = [
    [1,2,3,"A"],
    [4,5,6,"B"],
    [7,8,9,"C"],
    ["*",0,"#","D"]
    ]
     
    ROW         = [37,36,33,32] # du livre [11,12,13,15]
    COLUMN      = [26,24,31,29] # du livre [16,18,22,7]
    
    def __init__(self):
        GPIO.setmode(GPIO.BOARD) #définie GPIO par numéro de pin
     
    def getKey(self):
         
        # Place les pins colone en sortie et à 0
        for j in range(len(self.COLUMN)):
            GPIO.setup(self.COLUMN[j], GPIO.OUT) 
            GPIO.output(self.COLUMN[j], GPIO.LOW)
         
        # Place les pins rangé en entrée
        for i in range(len(self.ROW)):
            GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
         
        # Scan rangé pour voir si une touche est enfoncée
        # une touche enfoncé valide donnera un valeur "rowVal"  entre 0 et 3.
        rowVal = -1
        for i in range(len(self.ROW)):
            tmpRead = GPIO.input(self.ROW[i])
            if tmpRead == 0:
                rowVal = i
                 
        # if rowVal is not 0 thru 3 then no button was pressed and we can exit
        if rowVal < 0 or rowVal > 3:
            self.exit()
            return
         
        # Convert columns to input
        for j in range(len(self.COLUMN)):
            GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
         
        # Switch the i-th row found from scan to output
        GPIO.setup(self.ROW[rowVal], GPIO.OUT)
        GPIO.output(self.ROW[rowVal], GPIO.HIGH)
 
        # Scan columns for still-pushed key/button
        # A valid key press should set "colVal"  between 0 and 2.
        colVal = -1
        for j in range(len(self.COLUMN)):
            tmpRead = GPIO.input(self.COLUMN[j])
            if tmpRead == 1:
                colVal=j
                 
        # if colVal is not 0 thru 2 then no button was pressed and we can exit
        if colVal < 0 or colVal > 3:
            self.exit()
            return
 
        # Return the value of the key pressed
        self.exit()
        return self.KEYPAD[rowVal][colVal]
         
    def exit(self):
        # Reinitialize all rows and columns as input at exit
        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)



def loop():
        # Initialise la class keypad()
        kp = keypad()
        # fait toujour cette boucle pour afficher si une touche a été enfoncé
        while True:
            digit = None
            while digit == None:
                digit = kp.getKey()    
            # affiche le résultat
            print (digit)
            if digit=="A":
                digit= 10
            if digit=="B":
                digit= 11
            if digit=="C":
                digit= 12
            if digit=="D":
                digit= 13
            if digit=="*":
                digit= 14
            if digit=="#":
                digit= 15    
            dat=dats[digit]
            writeOneByte(dat)
            time.sleep(1.0) # delais de 0.5 seconde
        

def destroy():   #Arrêt du programme
         GPIO.cleanup()                     # relache la ressource GPIO
	

if __name__ == '__main__': # Le programme commence ici
    setup()                #Lance la fonction setup()
    try:  
            loop()    
    except KeyboardInterrupt:    
            destroy()
    
