1+ # https://docs.micropython.org/en/latest/library/rp2.html
2+
3+ import time
4+ import rp2
5+ from machine import Pin
6+
7+ @rp2 .asm_pio (set_init = rp2 .PIO .OUT_LOW , out_shiftdir = rp2 .PIO .SHIFT_LEFT )
8+ def neo_prog ():
9+ pull () # osr <= number of pixels - 1
10+ mov (y , osr ) # y <= number of pixels - 1
11+ label ("loop_pixel" )
12+ mov (isr , y ) # isr (pixel counter) <= y
13+ pull () # sor <= 24 bits GRB
14+ set (x , 23 ) # x (bit counter) <= 23
15+ label ("loop_pixel_bit" )
16+ out (y , 1 ) # y <= left-most 1 bit of sor
17+ jmp (not_y , "bit_0" )
18+ set (pins , 1 ).delay (15 )
19+ set (pins , 0 ).delay (8 )
20+ jmp ("bit_end" )
21+ label ("bit_0" )
22+ set (pins , 1 ).delay (7 )
23+ set (pins , 0 ).delay (16 )
24+ label ("bit_end" )
25+ jmp (x_dec , "loop_pixel_bit" ) # x is bit counter
26+ mov (y , isr ) # y <= isr (pixel counter)
27+ jmp (y_dec , "loop_pixel" ) # y is pixel counter
28+ label ("debug" )
29+ set (y , 8 )
30+ label ("debug_2" )
31+ mov (isr , y )
32+ push ()
33+
34+ sm = rp2 .StateMachine (0 , neo_prog , freq = 20_000_000 , set_base = Pin (22 ))
35+ sm .active (1 )
36+
37+ def ShowNeoPixels (* pixels ):
38+ '''
39+ each pixel RGB is the tuple (r, g, b)
40+ '''
41+ pixel_count = len (pixels )
42+ sm .put (pixel_count - 1 )
43+ for i in range (pixel_count ):
44+ pixel = pixels [i ]
45+ if pixel :
46+ (r , g , b ) = pixel
47+ else :
48+ (r , g , b ) = (0 , 0 , 0 )
49+ grb = (g << 16 ) + (r << 8 ) + b # the order is G R B
50+ #print(f". [{i}] = {pixel} ({grb})")
51+ sm .put (grb , 8 )
52+ time .sleep_us (300 )
53+ res = sm .get ()
54+ print (f"got result { res } " )
55+
56+ NUM_PIXELS = 4
57+ Pixels = []
58+ for i in range (NUM_PIXELS ):
59+ Pixels .append (None )
60+
61+ Pixels [0 ] = (128 , 0 , 0 )
62+ Pixels [1 ] = (0 , 128 , 0 )
63+ Pixels [2 ] = (0 , 0 , 128 )
64+ Pixels [3 ] = (32 , 32 , 32 )
65+ ShowNeoPixels (* Pixels )
66+
0 commit comments