diff --git a/ch-6/0-pio-basic/pio_led_test.py b/ch-6/0-pio-basic/pio_led_test.py new file mode 100644 index 0000000..206a57b --- /dev/null +++ b/ch-6/0-pio-basic/pio_led_test.py @@ -0,0 +1,25 @@ +import time +import board +import rp2pio +import adafruit_pioasm + +led_flash = """ +.program led_flash + pull + out pins, 1 +""" + +assembled = adafruit_pioasm.assemble(led_flash) + +sm = rp2pio.StateMachine( + assembled, + frequency=2000, + first_out_pin=board.LED, +) +print("real frequency", sm.frequency) + +while True: + sm.write(bytes((1,))) + time.sleep(0.5) + sm.write(bytes((0,))) + time.sleep(0.5) diff --git a/ch-6/1-simple-input/pio_one_encoder_on_off.py b/ch-6/1-simple-input/pio_one_encoder_on_off.py new file mode 100644 index 0000000..fa77c96 --- /dev/null +++ b/ch-6/1-simple-input/pio_one_encoder_on_off.py @@ -0,0 +1,29 @@ +"""Send this code, run and watch the repl. +Then turn the wheel slowly to see the change""" +import board +import rp2pio +import adafruit_pioasm +import time + +pio_input = """ +.program pio_input + in pins, 1 ; read in pin (into ISR) + push noblock ; put this into input FIFO +""" + +assembled = adafruit_pioasm.assemble(pio_input) + +## set up a statemachine +sm = rp2pio.StateMachine( + assembled, + frequency=2000, + first_in_pin=board.GP20 +) + +buffer = bytearray(1) # an array of bytes to read into - we are just asking for a byte + +while True: + # read data from the fifo + data = sm.readinto(buffer) + # print it. + print("{:08b}".format(buffer[0])) diff --git a/ch-6/1-simple-input/pio_one_encoder_on_off_2_pins.py b/ch-6/1-simple-input/pio_one_encoder_on_off_2_pins.py new file mode 100644 index 0000000..16f8645 --- /dev/null +++ b/ch-6/1-simple-input/pio_one_encoder_on_off_2_pins.py @@ -0,0 +1,30 @@ +"""Send this code, run and watch the repl. +Then turn the wheel slowly to see the change""" +import board +import rp2pio +import adafruit_pioasm +import time + +pio_input = """ +.program pio_input + in pins, 2 ; read in two pins (into ISR) + push noblock ; put ISR into input FIFO +""" + +assembled = adafruit_pioasm.assemble(pio_input) + +## set up a statemachine +sm = rp2pio.StateMachine( + assembled, + frequency=2000, + first_in_pin=board.GP20, + in_pin_count=2 +) + +buffer = bytearray(1) # an array of bytes to read into - we are just asking for a byte + +while True: + # read data from the fifo + data = sm.readinto(buffer) + # print it. + print("{:08b}".format(buffer[0]))