Files
led-controller/src/engine/mpcompat.py
T
jimmyandClaude Opus 5.5 a5a7fc575f feat(engine): Pi-side animation engine with protocol outputs and sequencer
Add src/engine, modelled on technicalkiwi/portal's player and sequence runner:

- Pixel generator: runs the unchanged led-driver patterns under CPython
  (utime shim + NeoPixel-like buffer) on a per-zone render thread and
  streams frames to pixel outputs, with keepalive for static frames.
- Output drivers: DDP, Art-Net (ArtDMX/ArtSync), sACN/E1.31, USB-DMX
  (Enttec Pro / Open DMX), plus an ESP32 preset output that reuses the
  existing preset + select delivery path. Registry in db/output.json,
  CRUD and test pattern at /outputs.
- Sequencer: timed preset chains per zone (portal step shape), deadline
  scheduling, loop/repeat, resume after restart; legacy sequence records
  are migrated. Zone play/stop, sequence and scene play/stop endpoints.
- CLI (python -m engine.cli), Outputs and Sequences modals, preset tiles
  drive pixel outputs, API/README docs, engine test suite.

ESP32 firmware is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:24:14 +13:00

87 lines
2.3 KiB
Python

"""MicroPython compatibility for running led-driver patterns under CPython.
Installs a ``utime`` module whose clock is chosen per thread:
* ``RealClock`` — monotonic wall clock, ``sleep_ms`` really sleeps (render threads).
* ``VirtualClock`` — manual time; ``sleep_ms`` just advances it (tests, offline render).
Patterns only use ``ticks_ms``/``ticks_diff``/``ticks_add``/``sleep_ms``; tick
values never wrap here, so the diff/add helpers are plain arithmetic.
"""
import sys
import threading
import time
import types
class RealClock:
def __init__(self):
self._t0 = time.monotonic()
def ticks_ms(self):
return int((time.monotonic() - self._t0) * 1000)
def ticks_us(self):
return int((time.monotonic() - self._t0) * 1000000)
def sleep_ms(self, ms):
if ms > 0:
time.sleep(ms / 1000.0)
class VirtualClock:
def __init__(self, start_ms=0):
self.now_us = int(start_ms) * 1000
def ticks_ms(self):
return self.now_us // 1000
def ticks_us(self):
return self.now_us
def advance_ms(self, ms):
self.now_us += int(ms * 1000)
def sleep_ms(self, ms):
if ms > 0:
self.advance_ms(ms)
_local = threading.local()
_default_clock = RealClock()
def current_clock():
return getattr(_local, "clock", None) or _default_clock
def set_thread_clock(clock):
"""Use ``clock`` for ``utime`` calls made on the current thread (None = real)."""
_local.clock = clock
def _build_utime():
mod = types.ModuleType("utime")
mod.ticks_ms = lambda: current_clock().ticks_ms()
mod.ticks_us = lambda: current_clock().ticks_us()
mod.ticks_cpu = mod.ticks_us
mod.ticks_diff = lambda a, b: a - b
mod.ticks_add = lambda a, b: a + b
mod.sleep_ms = lambda ms: current_clock().sleep_ms(ms)
mod.sleep_us = lambda us: current_clock().sleep_ms(us / 1000.0)
mod.sleep = lambda s: current_clock().sleep_ms(s * 1000.0)
mod.time = time.time
mod.__led_controller_shim__ = True
return mod
def install():
"""Register the shim as ``utime`` (idempotent; never replaces a real utime)."""
existing = sys.modules.get("utime")
if existing is not None:
return existing
mod = _build_utime()
sys.modules["utime"] = mod
return mod