Skip to content

Commit 8561bed

Browse files
committed
reduce amount of pio code strings
1 parent c86c6c0 commit 8561bed

File tree

1 file changed

+11
-60
lines changed

1 file changed

+11
-60
lines changed

adafruit_neopxl8.py

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import adafruit_pixelbuf
2020
import rp2pio
2121

22-
_PROGRAM8 = """
22+
_PROGRAM = """
2323
.program piopixl8
2424
top:
2525
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
@@ -32,7 +32,7 @@
3232
bitloop:
3333
pull ifempty [1] ; don't start outputting HIGH unless data is available (always-low part)
3434
mov pins, ~ null [3] ; always-high part
35-
out pins, 8 [4] ; variable part
35+
{} ; variable part
3636
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
3737
3838
jmp y--, bitloop ; always-low part
@@ -46,60 +46,6 @@
4646
jmp top
4747
"""
4848

49-
_PROGRAMN = """
50-
.program piopixl8
51-
top:
52-
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
53-
pull block ; wait for fresh data
54-
out y, 32 ; get count of NeoPixel bits
55-
56-
; NeoPixels are 800khz bit streams. We are choosing zeros as <312ns hi, 936 lo>
57-
; and ones as <700 ns hi, 546 ns lo> and a clock of 16*800kHz, so the always-high
58-
; time is 4 cycles, the variable time is 5 cycles, and the always-low time is 7 cycles
59-
bitloop:
60-
pull ifempty [1] ; don't start outputting HIGH unless data is available (always-low part)
61-
mov pins, ~ null [3] ; always-high part
62-
out pins, {} [3] ; variable part
63-
out x, {} ; variable part
64-
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
65-
66-
jmp y--, bitloop ; always-low part
67-
68-
; A minimum delay is required so that the next pixel starts refreshing the front of the strands
69-
pull block
70-
out y, 32
71-
72-
wait_reset:
73-
jmp y--, wait_reset
74-
jmp top
75-
"""
76-
77-
_PROGRAM1 = """
78-
.program piopixl8
79-
top:
80-
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
81-
pull block ; wait for fresh data
82-
out y, 32 ; get count of NeoPixel bits
83-
84-
; NeoPixels are 800khz bit streams. We are choosing zeros as <312ns hi, 936 lo>
85-
; and ones as <700 ns hi, 546 ns lo> and a clock of 16*800kHz, so the always-high
86-
; time is 4 cycles, the variable time is 5 cycles, and the always-low time is 7 cycles
87-
bitloop:
88-
pull ifempty [1] ; don't start outputting HIGH unless data is available (always-low part)
89-
mov pins, ~ null [3] ; always-high part
90-
out pins, 1 [4] ; variable part
91-
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
92-
93-
jmp y--, bitloop ; always-low part
94-
95-
; A minimum delay is required so that the next pixel starts refreshing the front of the strands
96-
pull block
97-
out y, 32
98-
99-
wait_reset:
100-
jmp y--, wait_reset
101-
jmp top
102-
"""
10349

10450
# Pixel color order constants
10551
RGB = "RGB"
@@ -199,12 +145,17 @@ def __init__(
199145
self._num_strands = num_strands
200146

201147
if num_strands == 8:
202-
assembled = adafruit_pioasm.assemble(_PROGRAM8)
148+
variable_part = "out pins, 8 [4] ; variable part"
203149
elif num_strands == 1:
204-
assembled = adafruit_pioasm.assemble(_PROGRAM1)
150+
variable_part = "out pins, 1 [4] ; variable part"
205151
else:
206-
program = _PROGRAMN.format(num_strands, 8 - num_strands)
207-
assembled = adafruit_pioasm.assemble(program)
152+
variable_part = f"""
153+
out pins, {num_strands} [3] ; variable part
154+
out x, {8-num_strands} ; variable part
155+
"""
156+
157+
program = _PROGRAM.format(variable_part)
158+
assembled = adafruit_pioasm.assemble(program)
208159

209160
self._sm = rp2pio.StateMachine(
210161
assembled,

0 commit comments

Comments
 (0)