1- # https://docs.micropython.org/en/latest/library/rp2.html
1+ # PIO reference: https://docs.micropython.org/en/latest/library/rp2.html
22
33import time
44import rp2
1818# - 1: 16 cycles high + 9 cycles low
1919# afterward, delay for 300us
2020
21+
22+ NUM_PIXELS = 4
2123NEO_PIXELS_IN_PIN = 22
2224
2325@rp2 .asm_pio (set_init = rp2 .PIO .OUT_LOW , out_shiftdir = rp2 .PIO .SHIFT_LEFT ) # SHIFT_LEFT: i.e. most significant bit first
@@ -26,10 +28,10 @@ def neo_prog():
2628 mov (y , osr ) # y <= number of pixels - 1
2729 label ("loop_pixel" )
2830 mov (isr , y ) # isr (pixel counter) <= y
29- pull () # sor <= 24 bits GRB
31+ pull () # osr <= 24 bits GRB
3032 set (x , 23 ) # x (bit counter) <= 23
3133 label ("loop_pixel_bit" )
32- out (y , 1 ) # y <= left-most 1 bit of sor
34+ out (y , 1 ) # y <= left-most 1 bit of osr
3335 jmp (not_y , "bit_0" )
3436 set (pins , 1 ).delay (15 ) # 1: high (16 cycles)
3537 set (pins , 0 ).delay (8 ) # 1: low (9 cycles)
@@ -41,15 +43,11 @@ def neo_prog():
4143 jmp (x_dec , "loop_pixel_bit" ) # x is bit counter
4244 mov (y , isr ) # y <= isr (pixel counter)
4345 jmp (y_dec , "loop_pixel" ) # y is pixel counter
44- # label("debug")
45- # set(y, 8)
46- # label("debug_2")
47- # mov(isr, y)
48- # push()
4946
5047sm = rp2 .StateMachine (0 , neo_prog , freq = 20_000_000 , set_base = Pin (NEO_PIXELS_IN_PIN ))
5148sm .active (1 )
5249
50+
5351def ShowNeoPixels (* pixels ):
5452 '''
5553 each pixel is the tuple (r, g, b)
@@ -63,25 +61,14 @@ def ShowNeoPixels(*pixels):
6361 else :
6462 (r , g , b ) = (0 , 0 , 0 )
6563 grb = (g << 16 ) + (r << 8 ) + b # the order is G R B
66- #print(f". [{i}] = {pixel} ({grb})")
6764 sm .put (grb , 8 ) # a word is 32 bits, so, pre-shift out (discard) 8 bits, leaving 24 bits of the GRB
6865 time .sleep_us (300 ) # make sure the NeoPixels is reset for the next round
69- # res = sm.get()
70- # print(f"got result {res}")
7166
7267
73- NUM_PIXELS = 4
7468Pixels = []
7569for i in range (NUM_PIXELS ):
7670 Pixels .append (None )
7771
78- # Pixels[0] = (128, 0, 0)
79- # Pixels[1] = (0, 128, 0)
80- # Pixels[2] = (0, 0, 128)
81- # Pixels[3] = (32, 32, 32)
82- # ShowNeoPixels(*Pixels)
83-
84-
8572rgb = 0
8673i = 0
8774while True :
@@ -97,4 +84,5 @@ def ShowNeoPixels(*pixels):
9784 Pixels [i ] = None
9885 ShowNeoPixels (* Pixels )
9986 rgb = (rgb + 1 ) % 3
100- i = (i + 1 ) % NUM_PIXELS
87+ i = (i + 1 ) % NUM_PIXELS
88+
0 commit comments