'''
References:
- https://youtu.be/YafifJLNr6I?si=sgi9Dr56twcclH-1
- https://github.com/micropython/micropython/blob/master/ports/rp2/modules/rp2.py
'''

from machine import Pin
from rp2 import PIO, StateMachine, asm_pio
from time import sleep

'''
Reserve 8 contiguous output ports with initial state HIGH.
Define OSR direction to output: right shift will output the 8 LSBs(least significant bits).
Automatically refill the OSR from FIFO.
When OSR reaches 16 bits refill the OSR.
'''
@asm_pio(out_init=(rp2.PIO.OUT_HIGH,) * 8,  # (1) * 8 -> int vs (1,) * 8 -> tuple.
         out_shiftdir=PIO.SHIFT_RIGHT,
         autopull=True, pull_thresh=16)
def output_data():
    pull()  # Pull data from FIFO.
    out(dest=pins, data=8)  # Send to output pins 8 bits from OSR. Use "data" instead of "bit_count".

sm = StateMachine(0, output_data, freq=1_000_000, out_base=Pin(0))
sm.active(1)

steps = [2 ** x for x in range(8)] + [255]

while True:
    sleep(1)
    # for i in range(256):
    for i in steps:
        sm.put(i)  # Send data to FIFO.
        print(i)
        sleep(0.5)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT