Skip to content

Commit 7e9076d

Browse files
committed
switch bit count from 16 bits to 32 bits, explain how auto_write behaves
1 parent 082e0a1 commit 7e9076d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

examples/pioasm_neopixel_bg.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
77
The NeoPixelBackground class defined here is largely compatible with the
88
standard NeoPixel class, except that the ``show()`` method returns immediately,
9-
writing data to the LEDs in the background.
9+
writing data to the LEDs in the background, and setting `auto_write` to true
10+
causes the data to be continuously sent to the LEDs all the time.
1011
1112
Writing the LED data in the background will allow more time for your
1213
Python code to run, so it may be possible to slightly increase the refresh
@@ -43,7 +44,7 @@
4344
.side_set 1 opt
4445
.wrap_target
4546
pull block side 0
46-
out y, 16 side 0 ; get count of NeoPixel bits
47+
out y, 32 side 0 ; get count of NeoPixel bits
4748
4849
bitloop:
4950
pull ifempty side 0 ; drive low
@@ -56,8 +57,8 @@
5657
jmp y--, bitloop side 0 [4] ; drive low for a zero (short pulse)
5758
5859
end_sequence:
59-
pull block side 0 ; get fresh 16 bit delay value
60-
out y, 16 side 0 ; get delay count
60+
pull block side 0 ; get fresh delay value
61+
out y, 32 side 0 ; get delay count
6162
wait_reset:
6263
jmp y--, wait_reset side 0 ; wait until delay elapses
6364
.wrap
@@ -79,21 +80,18 @@ def __init__(
7980

8081
byte_count = bpp * n
8182
bit_count = byte_count * 8
82-
padding_count = byte_count % 2
83+
padding_count = -byte_count % 4
8384

84-
if bit_count > 65536:
85-
raise ValueError("Too many pixels")
86-
87-
# backwards, so that ulab byteswap corrects it!
88-
header = struct.pack(">H", (bit_count - 1) & 0xFFFF)
89-
trailer = b"\0" * padding_count + struct.pack(">H", 3840)
85+
# backwards, so that dma byteswap corrects it!
86+
header = struct.pack(">L", bit_count - 1)
87+
trailer = b"\0" * padding_count + struct.pack(">L", 3840)
9088

9189
self._sm = StateMachine(
9290
_program.assembled,
9391
auto_pull=False,
9492
first_sideset_pin=pin,
9593
out_shift_right=False,
96-
pull_threshold=16,
94+
pull_threshold=32,
9795
frequency=12_800_000,
9896
**_program.pio_kwargs,
9997
)
@@ -128,10 +126,10 @@ def auto_write(self, value):
128126
def _transmit(self, buf):
129127
if self._auto_write:
130128
if not self._auto_writing:
131-
self._sm.background_write(loop=memoryview(buf).cast("H"), swap=True)
129+
self._sm.background_write(loop=memoryview(buf).cast("L"), swap=True)
132130
self._auto_writing = True
133131
else:
134-
self._sm.background_write(memoryview(buf).cast("H"), swap=True)
132+
self._sm.background_write(memoryview(buf).cast("L"), swap=True)
135133

136134

137135
if __name__ == "__main__":

0 commit comments

Comments
 (0)