import time
from machine import Pin
import utime
import machine
# ----------------[Initialisation du programme]----------------
def Init_Syst():
TabEtat=[1,2,3]
Init_Bp1 = Pin(0, Pin.IN)
TabEtat[0] = Pin(0, Pin.IN)
Init_ValeurBp1 = 'Aucune carte'
Init_Bp2 = Pin(1, Pin.IN)
TabEtat[1] = Pin(1, Pin.IN)
Init_ValeurBp2 = 'Aucune carte'
Init_Bp3 = Pin(2, Pin.IN)
TabEtat[2] = Pin(2, Pin.IN)
Init_ValeurBp3 = 'Aucune carte'
return Init_Bp1, Init_ValeurBp1, Init_Bp2, Init_ValeurBp2, Init_Bp3, Init_ValeurBp3
Bp1, ValeurBp1, Bp2, ValeurBp2,Bp3,ValeurBp3 = Init_Syst()
debounce_time = 50 # Temps pour l'anti-rebond
TabCCP2=[]
# ----------------[Fonctions qui test toutes les entrée une première fois]----------------
def PremTest(TabEtatFonct):
TabEtatFonct.append(Pin(0, Pin.IN))
TabEtatFonct.append(Pin(1, Pin.IN))
TabEtatFonct.append(Pin(2, Pin.IN))
TabEtatFonct.append(Pin(3, Pin.IN))
TabEtatFonct.append(Pin(4, Pin.IN))
TabEtatFonct.append(Pin(5, Pin.IN))
TabEtatFonct.append(Pin(6, Pin.IN))
TabEtatFonct.append(Pin(7, Pin.IN))
TabEtatFonct.append(Pin(8, Pin.IN))
TabEtatFonct.append(Pin(9, Pin.IN))
for i in range(len(TabEtatFonct)):
Bp_Etat = TabEtatFonct[i].value() # lire l'état du bouton
if Bp_Etat == 0:
ValeurBp = 'Input'
elif Bp_Etat == 1:
ValeurBp = 'Output'
TabEtatFonct[i] = ValeurBp
time.sleep(0.5)
print("Broche " + str(i) + " est : " + str(TabEtatFonct[i]))
# ----------------[Fonctions Interruptions]----------------
# Fonction de rappel pour la broche BP1
def Interruption_bp1(pin):
global ValeurBp1, last_interrupt_time, Cpt, Last_state_bp1
current_time = time.ticks_ms()
# Vérifier si le temps écoulé depuis la dernière interruption est supérieur au temps d'anti-rebond
if (current_time - last_interrupt_time) > debounce_time:
Etat_Bp1 = pin.value()
if pin.value() != Last_state_bp1:
if Etat_Bp1 == 0:
ValeurBp1 = 'Input'
Cpt += 1
elif Etat_Bp1 == 1:
ValeurBp1 = 'Output'
print("-----------------------------------------------------")
print("Interrupt BP1 : ", ValeurBp1)
print("-----------------------------------------------------")
print("Notre compteur a une valeur de : " + str(Cpt))
last_interrupt_time = current_time
Last_state_bp1 = Etat_Bp1
TabCCP2[0]=ValeurBp1
def Interruption_bp2(pin):
global ValeurBp2, last_interrupt_time, Cpt, Last_state_bp2
current_time = time.ticks_ms()
# Vérifier si le temps écoulé depuis la dernière interruption est supérieur au temps d'anti-rebond
if (current_time - last_interrupt_time) > debounce_time:
Etat_Bp2 = pin.value()
if Etat_Bp2 != Last_state_bp2:
if Etat_Bp2 == 0:
ValeurBp2 = 'Input'
Cpt += 1
elif Etat_Bp2 == 1:
ValeurBp2 = 'Output'
print("-----------------------------------------------------")
print("Interrupt BP2 : ", ValeurBp2)
print("-----------------------------------------------------")
print("Notre compteur a une valeur de : " + str(Cpt))
last_interrupt_time = current_time
Last_state_bp2 = Etat_Bp2
TabCCP2[1]=ValeurBp2
def Interruption_bp3(pin):
global ValeurBp3, last_interrupt_time, Cpt, Last_state_bp3
current_time = time.ticks_ms()
# Vérifier si le temps écoulé depuis la dernière interruption est supérieur au temps d'anti-rebond
if (current_time - last_interrupt_time) > debounce_time:
Etat_Bp3 = pin.value()
if Etat_Bp3 != Last_state_bp3:
if Etat_Bp3 == 0:
ValeurBp3 = 'Input'
Cpt += 1
elif Etat_Bp3 == 1:
ValeurBp3 = 'Output'
print("-----------------------------------------------------")
print("Interrupt BP3 : ", ValeurBp3)
print("-----------------------------------------------------")
print("Notre compteur a une valeur de : " + str(Cpt))
last_interrupt_time = current_time
Last_state_bp3 = Etat_Bp3
TabCCP2[2]=ValeurBp3
# ----------------[Initialisations pour les Interruptions]----------------
# Attache la fonction de rappel à la broche BP1 pour les deux niveaux de signal
# le AND permet de gérer l'interuption lorsque nous avons un niveau bas et haut
Bp1.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=Interruption_bp1)
Bp2.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=Interruption_bp2)
Bp3.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=Interruption_bp3)
#Présent pour éviter un rebond lors de la mise en place de la carte
Last_state_bp1 = Bp1.value()
Last_state_bp2= Bp2.value()
Last_state_bp3= Bp3.value()
last_interrupt_time = time.ticks_ms()
Cpt = 0
#Fonction pour réaliser un premier test des entrée lors du démarage du prototype
PremTest(TabCCP2)
# ----------------[Partie "main" du programme]----------------
while True:
# Lecture de l'état du bouton Bp1
time.sleep(5)
print("----------------")
print(TabCCP2)
print("----------------")
for i in range(len(TabCCP2)):
time.sleep(0.5)
print("Broche " + str(i) + " est : " + str(TabCCP2[i]))