#This code is based on the debouncer from https://github.com/GitJer/Some_RPI-Pico_stuff/tree/main/Button-debouncer
import time
import rp2
import machine as m
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
@rp2.asm_pio(in_shiftdir=rp2.PIO.SHIFT_LEFT)
def debounce():
wrap_target()
jmp(pin,"isone") #executed only once: is the pin currently 0 or 1?
label("iszero")
wait(1,pin,0) # the pin is 0, wait for it to become 1
set(x,31) # prepare to test the pin for 31 times
label("checkzero")
jmp(pin,"stillone") # check if the pin is still 1
jmp("iszero") # if the pin has returned to 0, start over
label("stillone")
jmp(x_dec,"checkzero") # decrease the time to wait, or the pin has definitively become 1
set(y,0)
in_(y,32)
push()
irq(block,rel(0))
label("isone")
wait(0,pin,0) # the pin is 1, wait for it to become 0
set(x,31) # prepare to test the pin for 31 times
label("checkone")
jmp(pin,"isone") # if the pin has returned to 1, start over
jmp(x_dec,"checkone") # decrease the time to wait
set(y,1)
in_(y,32)
push()
irq(block,rel(0))
jmp("iszero") # the pin has definitively become 0
wrap()
cnt = 0
def sm_irq(stm):
global cnt
print("hi")
if stm.rx_fifo():
cnt += 1
print(str(cnt) + "received "+str(stm.get()))
my_pin = m.Pin(10,m.Pin.IN, m.Pin.PULL_UP)
sm = rp2.StateMachine(0, debounce, freq=10000, in_base=my_pin, jmp_pin=my_pin)
sm.irq(handler=sm_irq)
sm.active(1)