Simplify and organise
This commit is contained in:
parent
b5b6bb0627
commit
503462bea4
@ -3,7 +3,6 @@ Then turn the wheel slowly to see the change"""
|
||||
import board
|
||||
import rp2pio
|
||||
import adafruit_pioasm
|
||||
import time
|
||||
|
||||
pio_input = """
|
||||
.program pio_input
|
||||
|
||||
@ -3,7 +3,6 @@ Then turn the wheel slowly to see the change"""
|
||||
import board
|
||||
import rp2pio
|
||||
import adafruit_pioasm
|
||||
import time
|
||||
|
||||
pio_input = """
|
||||
.program pio_input
|
||||
|
||||
41
ch-6/2-change-conditions/pio_one_encoder_when_changed.py
Normal file
41
ch-6/2-change-conditions/pio_one_encoder_when_changed.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""Send this code, run and watch the repl.
|
||||
Then turn the wheel slowly to see the change"""
|
||||
import board
|
||||
import rp2pio
|
||||
import adafruit_pioasm
|
||||
|
||||
pio_input = """
|
||||
.program pio_input
|
||||
set y, 0 ; clear y
|
||||
read:
|
||||
mov x, y ; store old Y in x
|
||||
in null, 31 ; Clear ISR
|
||||
in pins, 2 ; read in two pins (into ISR)
|
||||
mov y, isr ; store ISR in y
|
||||
jmp x!=y different ; Jump if its different
|
||||
jmp read ; otherwise loop back to read
|
||||
|
||||
different:
|
||||
push noblock ; put ISR into input FIFO
|
||||
jmp read ; loop back
|
||||
"""
|
||||
|
||||
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 (are we though? It might be 4 bytes)
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
# print (sm.in_waiting)
|
||||
if sm.in_waiting:
|
||||
data = sm.readinto(buffer)
|
||||
# print it.
|
||||
print("{:08b}".format(buffer[0]))
|
||||
56
ch-6/2-change-conditions/pio_two_encoders_when_changed.py
Normal file
56
ch-6/2-change-conditions/pio_two_encoders_when_changed.py
Normal file
@ -0,0 +1,56 @@
|
||||
"""Send this code, run and watch the repl.
|
||||
Then turn the wheel slowly to see the change"""
|
||||
import board
|
||||
import rp2pio
|
||||
import adafruit_pioasm
|
||||
|
||||
pio_input = """
|
||||
.program pio_input
|
||||
set y, 0 ; clear y
|
||||
read:
|
||||
mov x, y ; store old Y in x
|
||||
in null, 31 ; Clear ISR
|
||||
in pins, 2 ; read in two pins (into ISR)
|
||||
mov y, isr ; store ISR in y
|
||||
jmp x!=y different ; Jump if its different
|
||||
jmp read ; otherwise loop back to read
|
||||
|
||||
different:
|
||||
push noblock ; put ISR into input FIFO
|
||||
jmp read ; loop back
|
||||
"""
|
||||
|
||||
assembled = adafruit_pioasm.assemble(pio_input)
|
||||
|
||||
## set up a statemachine
|
||||
left_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP20,
|
||||
in_pin_count=2
|
||||
)
|
||||
|
||||
right_enc = rp2pio.StateMachine(
|
||||
assembled,
|
||||
frequency=0,
|
||||
first_in_pin=board.GP26,
|
||||
in_pin_count=2
|
||||
)
|
||||
|
||||
buffer = bytearray(1) # an array of bytes to read into - we are just asking for a byte (are we though? It might be 4 bytes)
|
||||
|
||||
left_data = 0
|
||||
right_data = 0
|
||||
|
||||
while True:
|
||||
# read data from the fifo
|
||||
if left_enc.in_waiting:
|
||||
left_enc.readinto(buffer)
|
||||
left_data = buffer[0]
|
||||
# print it.
|
||||
print("{:08b} {:08b}".format(left_data, right_data))
|
||||
if right_enc.in_waiting:
|
||||
right_enc.readinto(buffer)
|
||||
right_data = buffer[0]
|
||||
# print it.
|
||||
print("{:08b} {:08b}".format(left_data, right_data))
|
||||
Loading…
x
Reference in New Issue
Block a user