import machine
from machine import Pin, Timer
import time
#difinimos tipos
NADA=0
EVENTO=1
ESPERA=2
Verde=Pin(4,Pin.IN, Pin.PULL_UP)
Rojo=Pin(6,Pin.IN, Pin.PULL_UP)
Amarillo=Pin(5,Pin.IN, Pin.PULL_UP)
Azul=Pin(7,Pin.IN, Pin.PULL_UP)
estadoVerde = NADA #Estado del pulsador
estadoAzul = NADA
estadoRojo = NADA
estadoAmarillo = NADA
ARRIBA = 1
ABAJO = 2
DERECHA = 3
IZQUIERDA = 4
estadoFSM=0
def FSM(estadoActual,entrada):
#Esta FSM recibe el estado actual y una entrada
#que puede ser 1 (arriba), 2(abajo), 3 (izq), 4 (der)
#y reconoce la secuencia Arrriba, Der, Abajo, Izq
if estadoActual==0 and entrada==ARRIBA:
return 1
if estadoActual==1 and entrada==ARRIBA:
return 1 #caso de dos arriba seguidos
if estadoActual==1 and entrada==DERECHA:
return 2
if estadoActual==2 and entrada==ABAJO:
return 3
if estadoActual==3 and entrada==IZQUIERDA:
return 4
return 0
def procesaEntrada(estadoActual,valorPin):
if estadoActual==NADA and valorPin==False:
return EVENTO
if estadoActual==EVENTO:
return ESPERA
if estadoActual==ESPERA and valorPin==False:
return ESPERA
return NADA
while True:
time.sleep_ms(5) #Dejamos pasar tiempo para eliminar bouncing
#Procesamos la entrada a ver si hay evento
estadoVerde = procesaEntrada(estadoVerde, Verde.value())
if (estadoVerde == EVENTO):
estadoFSM=FSM(estadoFSM,ABAJO)
estadoAzul = procesaEntrada(estadoAzul, Azul.value())
if (estadoAzul == EVENTO):
estadoFSM=FSM(estadoFSM,ARRIBA)
estadoRojo = procesaEntrada(estadoRojo, Rojo.value())
if (estadoRojo == EVENTO):
estadoFSM=FSM(estadoFSM,IZQUIERDA)
estadoAmarillo = procesaEntrada(estadoAmarillo, Amarillo.value())
if (estadoAmarillo == EVENTO):
estadoFSM=FSM(estadoFSM,DERECHA)
if (estadoFSM==4):
print("Konami")
estadoFSM=0