|
12 | 12 | Python code to run, so it may be possible to slightly increase the refresh
|
13 | 13 | rate of your LEDs or do more complicated processing.
|
14 | 14 |
|
| 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 | +
|
15 | 19 | The demonstration code, under ``if __name__ == '__main__':`` is intended
|
16 | 20 | for the Adafruit MacroPad, with 12 NeoPixel LEDs. It shows a cycling rainbow
|
17 | 21 | pattern across all the LEDs.
|
18 | 22 | """
|
19 | 23 |
|
20 | 24 | import struct
|
21 | 25 | import adafruit_pixelbuf
|
22 |
| -from ulab import numpy as np |
23 | 26 | from rp2pio import StateMachine
|
24 | 27 | from adafruit_pioasm import Program
|
25 | 28 |
|
@@ -95,28 +98,51 @@ def __init__(
|
95 | 98 | **_program.pio_kwargs,
|
96 | 99 | )
|
97 | 100 |
|
| 101 | + self._first = True |
98 | 102 | super().__init__(
|
99 | 103 | n,
|
100 | 104 | brightness=brightness,
|
101 | 105 | byteorder=pixel_order,
|
102 |
| - auto_write=auto_write, |
| 106 | + auto_write=False, |
103 | 107 | header=header,
|
104 | 108 | trailer=trailer,
|
105 | 109 | )
|
106 | 110 |
|
| 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 | + |
107 | 128 | 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) |
109 | 135 |
|
110 | 136 |
|
111 | 137 | if __name__ == "__main__":
|
112 | 138 | import board
|
113 | 139 | import rainbowio
|
114 |
| - import time |
| 140 | + import supervisor |
115 | 141 |
|
116 | 142 | NEOPIXEL = board.NEOPIXEL
|
117 | 143 | NUM_PIXELS = 12
|
118 | 144 | pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS)
|
119 | 145 | i = 0
|
120 | 146 | 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