Add host simulator scaffolding, examples, and docs so led-driver main can run end-to-end with MicroPython module stubs. Co-authored-by: Cursor <cursoragent@cursor.com>
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
"""Host NeoPixel: keeps RGB buffer and notifies the GUI on `write()`."""
|
|
|
|
# Set by led-simulator main: callable(list[tuple[int,int,int]]) -> None
|
|
PIXEL_SINK = None
|
|
|
|
|
|
class NeoPixel:
|
|
def __init__(self, pin, n):
|
|
self.pin = pin
|
|
self.n = n
|
|
self._buf = [(0, 0, 0)] * n
|
|
|
|
def __setitem__(self, index, value):
|
|
if isinstance(value, (list, tuple)) and len(value) >= 3:
|
|
r, g, b = (int(value[0]), int(value[1]), int(value[2]))
|
|
self._buf[index] = (
|
|
max(0, min(255, r)),
|
|
max(0, min(255, g)),
|
|
max(0, min(255, b)),
|
|
)
|
|
|
|
def __getitem__(self, index):
|
|
return self._buf[index]
|
|
|
|
def fill(self, color):
|
|
c = (0, 0, 0)
|
|
if isinstance(color, (list, tuple)) and len(color) >= 3:
|
|
c = (
|
|
max(0, min(255, int(color[0]))),
|
|
max(0, min(255, int(color[1]))),
|
|
max(0, min(255, int(color[2]))),
|
|
)
|
|
self._buf = [c] * self.n
|
|
|
|
def write(self):
|
|
if PIXEL_SINK is not None:
|
|
PIXEL_SINK(list(self._buf))
|