Skip to content

Commit 4d15f21

Browse files
committed
Support fewer than 8 parallel strips
This requires slightly different PIO code, because a variable number of bits have to be thrown away
1 parent ade06ed commit 4d15f21

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

adafruit_neopxl8.py

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

22-
_PROGRAM = """
22+
_PROGRAM8 = """
2323
.program piopixl8
2424
top:
2525
mov pins, null ; always-low part (last cycle is the 'pull ifempty' after wrap)
@@ -46,7 +46,33 @@
4646
jmp top
4747
"""
4848

49-
_ASSEMBLED = adafruit_pioasm.assemble(_PROGRAM)
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+
"""
5076

5177
# Pixel color order constants
5278
RGB = "RGB"
@@ -135,11 +161,19 @@ def __init__(
135161

136162
self._num_strands = num_strands
137163

164+
if num_strands == 8:
165+
assembled = adafruit_pioasm.assemble(_PROGRAM8)
166+
else:
167+
program = _PROGRAMN.format(num_strands, 8 - num_strands)
168+
print(program)
169+
assembled = adafruit_pioasm.assemble(program)
170+
171+
print("num strands is", num_strands)
138172
self._sm = rp2pio.StateMachine(
139-
_ASSEMBLED,
173+
assembled,
140174
frequency=800_000 * 16,
141175
first_out_pin=data0,
142-
out_pin_count=8,
176+
out_pin_count=num_strands,
143177
first_set_pin=data0,
144178
auto_pull=False,
145179
out_shift_right=True,

0 commit comments

Comments
 (0)