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>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-10-22 13:24:14 +13:00
1 parent 827eb97203
commit a5a7fc575f
42 files changed
+4117 -75

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
"""sACN / E1.31 output — UDP 5568, multicast 239.255.<hi>.<lo> by default.
One data packet per universe (170 RGB pixels each), 126-byte header +
start code + up to 512 slots. Layer lengths are 12-bit, OR'd with 0x7000
(flags).
"""
import socket
import struct
import uuid
from engine.outputs.base import UdpFrameOutput, _bool, _int, split_universes
SACN_PORT = 5568
ACN_PACKET_ID = b"ASC-E1.17\x00\x00\x00"
VECTOR_ROOT_E131_DATA = 0x00000004
VECTOR_E131_DATA_PACKET = 0x00000002
VECTOR_DMP_SET_PROPERTY = 0x02
DEFAULT_PRIORITY = 100
def multicast_address(universe):
return f"239.255.{(universe >> 8) & 0xFF}.{universe & 0xFF}"
def stable_cid(seed):
"""Deterministic 16-byte CID from a name, so receivers see one source."""
return uuid.uuid5(uuid.NAMESPACE_URL, f"led-controller/sacn/{seed}").bytes
def build_e131(universe, data, sequence, cid, source_name="led-controller", priority=DEFAULT_PRIORITY):
slots = b"\x00" + bytes(data[:512]) # start code 0 + DMX
prop_count = len(slots)
dmp_len = 10 + prop_count
framing_len = 77 + dmp_len
root_len = 22 + framing_len
name = source_name.encode("utf-8")[:63].ljust(64, b"\x00")
root = (
struct.pack(">HH", 0x0010, 0x0000)
+ ACN_PACKET_ID
+ struct.pack(">HI", 0x7000 | root_len, VECTOR_ROOT_E131_DATA)
+ bytes(cid)
)
framing = (
struct.pack(">HI", 0x7000 | framing_len, VECTOR_E131_DATA_PACKET)
+ name
+ struct.pack(">BHBBH", priority & 0xFF, 0, sequence & 0xFF, 0, universe & 0xFFFF)
)
dmp = (
struct.pack(">HBBHHH", 0x7000 | dmp_len, VECTOR_DMP_SET_PROPERTY, 0xA1, 0x0000, 0x0001, prop_count)
+ slots
)
return root + framing + dmp
class SacnOutput(UdpFrameOutput):
protocol = "sacn"
default_port = SACN_PORT
def __init__(self, cfg):
super().__init__(cfg)
self.universe = _int(self.cfg.get("universe"), 1, 1, 63999)
self.priority = _int(self.cfg.get("priority"), DEFAULT_PRIORITY, 0, 200)
self.multicast = not self.host or _bool(self.cfg.get("multicast"), False)
self.source_name = str(self.cfg.get("source_name") or f"led-controller {self.name}")
self.cid = stable_cid(self.cfg.get("cid") or self.cfg.get("id") or self.name)
self._sequences = {}
def _make_socket(self):
sock = super()._make_socket()
if self.multicast:
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 8)
return sock
def _next_sequence(self, universe):
seq = (self._sequences.get(universe, -1) + 1) & 0xFF
self._sequences[universe] = seq
return seq
def packets_for(self, payload):
out = []
for i, chunk in enumerate(split_universes(payload)):
u = self.universe + i
pkt = build_e131(u, chunk, self._next_sequence(u), self.cid, self.source_name, self.priority)
addr = (multicast_address(u), self.port) if self.multicast else self.destination()
out.append((pkt, addr))
return out
async def _send_prepared(self, payload):
for packet, addr in self.packets_for(payload):
self.sendto(packet, addr)