# SPDX-FileCopyrightText: 2022 Jamon Terrell <[email protected]>
# SPDX-License-Identifier: MIT
from rp2 import PIO, StateMachine, asm_pio
from machine import Pin
import utime
@asm_pio(autopush=True, push_thresh=32)
def encoder():
label("start")
wait(0, pin, 0) # Wait for CLK to go low
jmp(pin, "WAIT_HIGH") # if Data is low
mov(x, invert(x)) # Increment X
jmp(x_dec, "nop1")
label("nop1")
mov(x, invert(x))
label("WAIT_HIGH") # else
jmp(x_dec, "nop2") # Decrement X
label("nop2")
wait(1, pin, 0) # Wait for CLK to go high
jmp(pin, "WAIT_LOW") # if Data is low
jmp(x_dec, "nop3") # Decrement X
label("nop3")
label("WAIT_LOW") # else
mov(x, invert(x)) # Increment X
jmp(x_dec, "nop4")
label("nop4")
mov(x, invert(x))
wrap()
sm1 = StateMachine(1, encoder, freq=125_000_000, in_base=Pin(3), jmp_pin=Pin(2))
sm1.active(1)
while(True):
utime.sleep(1)
sm1.exec("in_(x, 32)")
x = sm1.get()
print(x)