Skip to content

Commit 082e0a1

Browse files
committed
enhance examples
Works in conjunction with adafruit/circuitpython#6360 * get rid of the need for 'filler' in morse demo * make neopixels work super fast without explicit byteswapping
1 parent 079fe9e commit 082e0a1

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

examples/pioasm_background_morse.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
https://en.wikipedia.org/wiki/Morse_code
1818
"""
1919

20-
import time
2120
import array
21+
import time
2222
from board import LED
2323
from rp2pio import StateMachine
2424
from adafruit_pioasm import Program
@@ -62,13 +62,6 @@
6262
SOS = S + O + S + WORD_SPACE
6363
TEST = T + E + S + T + WORD_SPACE
6464

65-
# 8 slots of the shortest possible led-off time
66-
# A background write is 'complete' as soon as all data has been placed
67-
# in the StateMachine's FIFO. This FIFO has either 4 or 8 entries.
68-
# By adding 8 very short "led off" times, we can have our 'sm.writing' test
69-
# tell us when the actual letter data has all been
70-
FILLER = array.array("H", [LED_OFF | 1]) * 8
71-
7265
sm = StateMachine(
7366
pio_code.assembled,
7467
frequency=1_000_000,
@@ -94,11 +87,11 @@
9487
("TEST", TEST),
9588
):
9689
print(f"Sending out {plain}", end="")
97-
sm.background_write(morse + FILLER)
98-
while sm.writing:
90+
sm.background_write(morse)
91+
sm.clear_txstall()
92+
while not sm.txstall:
9993
print(end=".")
10094
time.sleep(0.1)
10195
print()
102-
print("Message all sent to StateMachine")
103-
time.sleep(1)
96+
print("Message all sent to StateMachine (including emptying FIFO)")
10497
print()

examples/pioasm_neopixel_bg.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
Python code to run, so it may be possible to slightly increase the refresh
1313
rate of your LEDs or do more complicated processing.
1414
15+
Because the pixelbuf storage is also being written out 'live', it is possible
16+
(even with auto-show 'false') to experience tearing, where the LEDs are a
17+
combination of old and new values at the same time.
18+
1519
The demonstration code, under ``if __name__ == '__main__':`` is intended
1620
for the Adafruit MacroPad, with 12 NeoPixel LEDs. It shows a cycling rainbow
1721
pattern across all the LEDs.
1822
"""
1923

2024
import struct
2125
import adafruit_pixelbuf
22-
from ulab import numpy as np
2326
from rp2pio import StateMachine
2427
from adafruit_pioasm import Program
2528

@@ -95,28 +98,51 @@ def __init__(
9598
**_program.pio_kwargs,
9699
)
97100

101+
self._first = True
98102
super().__init__(
99103
n,
100104
brightness=brightness,
101105
byteorder=pixel_order,
102-
auto_write=auto_write,
106+
auto_write=False,
103107
header=header,
104108
trailer=trailer,
105109
)
106110

111+
self._auto_write = False
112+
self._auto_writing = False
113+
self.auto_write = auto_write
114+
115+
@property
116+
def auto_write(self):
117+
return self._auto_write
118+
119+
@auto_write.setter
120+
def auto_write(self, value):
121+
self._auto_write = bool(value)
122+
if not value and self._auto_writing:
123+
self._sm.background_write()
124+
self._auto_writing = False
125+
elif value:
126+
self.show()
127+
107128
def _transmit(self, buf):
108-
self._sm.background_write(np.frombuffer(buf, dtype=np.uint16).byteswap())
129+
if self._auto_write:
130+
if not self._auto_writing:
131+
self._sm.background_write(loop=memoryview(buf).cast("H"), swap=True)
132+
self._auto_writing = True
133+
else:
134+
self._sm.background_write(memoryview(buf).cast("H"), swap=True)
109135

110136

111137
if __name__ == "__main__":
112138
import board
113139
import rainbowio
114-
import time
140+
import supervisor
115141

116142
NEOPIXEL = board.NEOPIXEL
117143
NUM_PIXELS = 12
118144
pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS)
119145
i = 0
120146
while True:
121-
pixels.fill(rainbowio.colorwheel(i := (i + 1) % 256))
122-
time.sleep(0.01)
147+
# Around 1 cycle per second
148+
pixels.fill(rainbowio.colorwheel(supervisor.ticks_ms() // 4))

0 commit comments

Comments
 (0)