#Adapté à Python 3 et traduit par Richard Fortier 

import RPi.GPIO as GPIO  
import time  

BIT0 = 3   #pour selection digit 4 le plus a droite
BIT1 = 5   #pour selection digit 3
BIT2 = 24  #pour selection digit 2
BIT3 = 26  #pour selection digit 1

segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]  #nombre de 0~9  
pins = [11,12,13,15,16,18,22,7,3,5,24,26]  
bits = [BIT0, BIT1, BIT2, BIT3]  

def print_msg():  
	print ('le programme est démarré...')  
	print ('Peser sur Ctrl+C pour terminer le programme...')  

def digitalWriteByte(val):  
	GPIO.output(11, val & (0x01 << 0)) #Segment a du digit
	GPIO.output(12, val & (0x01 << 1)) #Segment b du digit 
	GPIO.output(13, val & (0x01 << 2)) #Segment c du digit 
	GPIO.output(15, val & (0x01 << 3)) #Segment d du digit 
	GPIO.output(16, val & (0x01 << 4)) #Segment e du digit 
	GPIO.output(18, val & (0x01 << 5)) #Segment f du digit 
	GPIO.output(22, val & (0x01 << 6)) #Segment g du digit 
	GPIO.output(7,  val & (0x01 << 7)) #dp ou le point du digit 

def display_1():  #Affiche 0 à 9 sur digit 4
	GPIO.output(BIT0, GPIO.LOW)   
	for i in range(10):  
		digitalWriteByte(segCode[i])  
		time.sleep(0.5)  

def display_2():  #Affiche 0 à 9 sur les 4 digit
        for bit in bits:
                GPIO.output(bit, GPIO.LOW)
        for i in range(10):
                digitalWriteByte(segCode[i])
                time.sleep(0.5)
        for bit in bits:
                GPIO.output(bit, GPIO.HIGH)

def display_3(num):  #Affiche un nombre 0 à 9999
        b0 = int(num % 10)
        b1 = int(num % 100 / 10)
        b2 = int(num % 1000 / 100)
        b3 = int(num / 1000)
        if num < 10:
                #Si nombre plus petit que 10 agit sur digit 4 seulement
                GPIO.output(BIT0, GPIO.LOW)
                GPIO.output(BIT1, GPIO.HIGH)
                GPIO.output(BIT2, GPIO.HIGH)
                GPIO.output(BIT3, GPIO.HIGH)
                digitalWriteByte(segCode[b0])
                time.sleep(0.004)
                GPIO.output(BIT0, GPIO.HIGH)
        elif num >= 10 and num < 100:
                GPIO.output(BIT0, GPIO.LOW)
                digitalWriteByte(segCode[b0])
                time.sleep(0.002)
                GPIO.output(BIT0, GPIO.HIGH)
                GPIO.output(BIT1, GPIO.LOW)
                digitalWriteByte(segCode[b1])
                time.sleep(0.002)
                GPIO.output(BIT1, GPIO.HIGH)
        elif num >= 100 and num < 1000:
                GPIO.output(BIT0, GPIO.LOW)
                digitalWriteByte(segCode[b0])
                time.sleep(0.002)
                GPIO.output(BIT0, GPIO.HIGH)
                GPIO.output(BIT1, GPIO.LOW)
                digitalWriteByte(segCode[b1])
                time.sleep(0.002)
                GPIO.output(BIT1, GPIO.HIGH)
                GPIO.output(BIT2, GPIO.LOW)
                digitalWriteByte(segCode[b2])
                time.sleep(0.002)
                GPIO.output(BIT2, GPIO.HIGH)
        elif num >= 1000 and num < 10000:
                GPIO.output(BIT0, GPIO.LOW)
                digitalWriteByte(segCode[b0])
                time.sleep(0.002)
                GPIO.output(BIT0, GPIO.HIGH)
                GPIO.output(BIT1, GPIO.LOW)
                digitalWriteByte(segCode[b1])
                time.sleep(0.002)
                GPIO.output(BIT1, GPIO.HIGH)
                GPIO.output(BIT2, GPIO.LOW)
                digitalWriteByte(segCode[b2])
                time.sleep(0.002)
                GPIO.output(BIT2, GPIO.HIGH)
                GPIO.output(BIT3, GPIO.LOW)
                digitalWriteByte(segCode[b3])
                time.sleep(0.002)
                GPIO.output(BIT3, GPIO.HIGH)
        else:
                print ('Out of range, num should be 0~9999 !')  

def setup():  #initialisation GPIO de la raspberry pi
	GPIO.setmode(GPIO.BOARD)   
	for pin in pins: #Dans la liste pins va chercher les 12 pin utilisées 
		GPIO.setup(pin, GPIO.OUT)    # Place les pins utilisées en output  
		GPIO.output(pin, GPIO.HIGH)  # Place les pins utilisées à 1 pour éteindre le display  

def loop():
        while True:
                print_msg()
                display_1()
                time.sleep(1)
                display_2()
                time.sleep(1)
                while True:
                        tmp = int(input('Entrer un nombre de (0~9999):'))
                        if tmp <10000:
                                for i in range(500):
                                        display_3(tmp)
                                time.sleep(1)
                        else:
                                print ('un nombre de 0~9999 S.V.P !')
                
                
def destroy():   #Arrêt du programme   
	for pin in pins:    
		GPIO.output(pin, GPIO.LOW) #Place les pins utilisées à 0 (0V)   
	GPIO.cleanup()             # relache la ressource GPIO

if __name__ == '__main__': #Programme commence ici   
	setup()   
	try:  
		loop()    
	except KeyboardInterrupt:    
		destroy()    
