If anyone is up for giving my code some constructive criticism (including my comments - maybe I have some misunderstanding) it would be greatly appreciated.
Say I have NUM_BUTTONS = 5
in my CP code. Is there a way to reference NUM_BUTTONS in my assembler code?
Here is my code:
Code: Select all
# Debounce up to 8 buttons in parallel
import board
import rp2pio
import adafruit_pioasm
debounce = """
.program debounce
pull block ; wait for write (and ignore value)
in pins, 8 ; set ISR to the state of the button
mov x, isr ; save ISR to x
loop:
out y, 20 ; loop counter for sample loop
sample_loop:
nop [31] ; wait a bit before taking another sample
mov osr, y ; save y (loop counter) into OSR
mov y, x ; save current ISR value for next comparison
in pins, 8 ; take annother sample
mov x, isr ; save ISR to x for testing
jmp x!=y loop ; state changed - start again
mov y, osr ; reload loop counter into y for decrement/testing
jmp y-- sample_loop
in x,8 ; the state of the buttons has been stable
push ; put x into ISR and output
"""
assembled = adafruit_pioasm.assemble(debounce)
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
first_in_pin=board.GP16,
in_pin_count = 8,
)
def buttons_state():
""" Return a list of booleans representing the state of the buttons"""
b = bytearray(1)
sm.write_readinto(bytes((0,)), b)
state = b[0]
return [(state & (1 << i)) != 0 for i in range(8)]