Merges led-simulator (sim-bridge, 4d8aeb5) under led-simulator/ with its history,
in place of the submodule.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
"""Host NeoPixel with MicroPython's semantics: a ``buf`` bytearray in ``ORDER`` (GRB), which led-driver
|
|
writes directly. ``write()`` sends the frame to the simulator as RGB."""
|
|
|
|
import _simlink
|
|
|
|
# Optional extra hook: callable(list[tuple[int, int, int]]) -> None
|
|
PIXEL_SINK = None
|
|
|
|
|
|
class NeoPixel:
|
|
ORDER = (1, 0, 2, 3) # as MicroPython: buf holds G, R, B (, W)
|
|
|
|
def __init__(self, pin, n, bpp=3, timing=1):
|
|
self.pin = pin
|
|
self.n = n
|
|
self.bpp = bpp
|
|
self.timing = timing
|
|
self.buf = bytearray(n * bpp)
|
|
|
|
def __len__(self):
|
|
return self.n
|
|
|
|
def __setitem__(self, index, value):
|
|
if index < 0:
|
|
index += self.n
|
|
off = index * self.bpp
|
|
for j in range(self.bpp):
|
|
self.buf[off + self.ORDER[j]] = int(value[j]) & 255
|
|
|
|
def __getitem__(self, index):
|
|
if index < 0:
|
|
index += self.n
|
|
off = index * self.bpp
|
|
return tuple(self.buf[off + self.ORDER[j]] for j in range(self.bpp))
|
|
|
|
def fill(self, color):
|
|
px = bytearray(self.bpp)
|
|
for j in range(self.bpp):
|
|
px[self.ORDER[j]] = int(color[j]) & 255
|
|
self.buf[:] = px * self.n
|
|
|
|
def _rgb(self):
|
|
b, bpp, o = self.buf, self.bpp, self.ORDER
|
|
return [(b[i + o[0]], b[i + o[1]], b[i + o[2]]) for i in range(0, self.n * bpp, bpp)]
|
|
|
|
def write(self):
|
|
if _simlink.connected() or PIXEL_SINK is not None:
|
|
px = self._rgb()
|
|
if _simlink.connected():
|
|
_simlink.push_pixels(px)
|
|
if PIXEL_SINK is not None:
|
|
PIXEL_SINK(px)
|