"""Stand-in for the led-driver ``Presets`` object that patterns draw into. Same surface the patterns use on the ESP32 (``n[i]``, ``n.write()``, ``n.fill``, ``num_leds``, ``step``, ``fill``, ``off``, ``on``, ``apply_brightness``), but ``write()`` hands the frame to a callback instead of a NeoPixel strip. """ class PixelBuffer: """NeoPixel-compatible buffer of RGB tuples.""" def __init__(self, num_leds, on_write=None): self.n = int(num_leds) self._px = [(0, 0, 0)] * self.n self.on_write = on_write def __len__(self): return self.n def __setitem__(self, i, color): self._px[i] = (int(color[0]) & 0xFF, int(color[1]) & 0xFF, int(color[2]) & 0xFF) def __getitem__(self, i): return self._px[i] def fill(self, color): c = (int(color[0]) & 0xFF, int(color[1]) & 0xFF, int(color[2]) & 0xFF) self._px = [c] * self.n def to_bytes(self): out = bytearray(self.n * 3) for i, (r, g, b) in enumerate(self._px): j = i * 3 out[j] = r out[j + 1] = g out[j + 2] = b return bytes(out) def write(self): if self.on_write is not None: self.on_write(self.to_bytes()) class FrameDriver: def __init__(self, num_leds, on_write=None): self.num_leds = int(num_leds) self.n = PixelBuffer(self.num_leds, on_write) self.step = 0 # Global brightness is applied per output at send time, not here. self.b = 255 def apply_brightness(self, color, brightness_override=None): local = brightness_override if brightness_override is not None else 255 effective_brightness = int(local * self.b / 255) return tuple(int(c * effective_brightness / 255) for c in color) def fill(self, color=None): self.n.fill(color if color is not None else (0, 0, 0)) self.n.write() def off(self, preset=None): self.fill((0, 0, 0)) def on(self, preset): colors = preset.c color = colors[0] if colors else (255, 255, 255) self.fill(self.apply_brightness(color, preset.b))