"""Minimal Art-Net output + a background runner that plays a pattern on the real heads.""" from __future__ import annotations import socket import threading import time from typing import Any from .efx import Efx, Path as PathPattern, clamp8 from .qxw import Workspace ARTNET_PORT = 6454 _HEADER = b"Art-Net\x00" _OP_DMX = 0x5000 def build_artdmx(frame: bytes, universe: int, sequence: int) -> bytes: data = bytearray(frame[:512]) if len(data) & 1: data.append(0) n = len(data) pkt = bytearray(18 + n) pkt[0:8] = _HEADER pkt[8:10] = bytes((_OP_DMX & 0xFF, _OP_DMX >> 8)) pkt[10], pkt[11] = 0, 14 pkt[12] = sequence & 0xFF pkt[13] = 0 pkt[14] = universe & 0xFF pkt[15] = (universe >> 8) & 0x7F pkt[16], pkt[17] = (n >> 8) & 0xFF, n & 0xFF pkt[18:] = data return bytes(pkt) class LivePreview: """Runs one pattern at a time, ~40 Hz, until stopped. Thread-safe.""" def __init__(self): self._lock = threading.Lock() self._thread: threading.Thread | None = None self._stop = threading.Event() self.status: dict[str, Any] = {"running": False} def start(self, ws: Workspace, pattern: Efx | PathPattern, ip: str, universe: int, hz: float = 40.0): self.stop() movers = {f.id: f for f in ws.fixtures if f.is_mover} # Universe → frame; static channel values that light the head. frames: dict[int, bytearray] = {} for fx in pattern.fixtures: f = movers[fx.id] frame = frames.setdefault(f.universe, bytearray(512)) for ch, v in f.mode.preview_channels().items(): # type: ignore[union-attr] if f.address + ch < 512: frame[f.address + ch] = v # QLC+ Art-Net plugin sends each QLC+ universe as `universe` + offset; we # mirror that: QLC+ universe N goes to Art-Net universe (base + N - first). base_uni = universe first_uni = min(frames) if frames else 0 self._stop.clear() self.status = { "running": True, "ip": ip, "universe": universe, "fixtures": [fx.id for fx in pattern.fixtures], "kind": "efx" if isinstance(pattern, Efx) else "path", } def run(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) seq = 1 period = 1.0 / hz t0 = time.monotonic() try: while not self._stop.is_set(): elapsed_ms = (time.monotonic() - t0) * 1000.0 for serial, fx in enumerate(pattern.fixtures): f = movers[fx.id] m = f.mode pt = pattern.live_point(serial, elapsed_ms) if pt is None or m is None: continue pan, tilt = pt frame = frames[f.universe] if m.pan_msb is not None: frame[f.address + m.pan_msb] = clamp8(pan) if m.tilt_msb is not None: frame[f.address + m.tilt_msb] = clamp8(tilt) if m.pan_lsb is not None: frame[f.address + m.pan_lsb] = clamp8((pan - int(pan)) * 255) if m.tilt_lsb is not None: frame[f.address + m.tilt_lsb] = clamp8((tilt - int(tilt)) * 255) for uni, frame in frames.items(): sock.sendto(build_artdmx(bytes(frame), base_uni + (uni - first_uni), seq), (ip, ARTNET_PORT)) seq = 1 if seq >= 255 else seq + 1 time.sleep(period) # Blackout the channels we touched so the heads do not stay lit. for uni, frame in frames.items(): for _ in range(3): sock.sendto(build_artdmx(bytes(512), base_uni + (uni - first_uni), seq), (ip, ARTNET_PORT)) time.sleep(period) finally: sock.close() with self._lock: self.status = {"running": False} with self._lock: self._thread = threading.Thread(target=run, daemon=True) self._thread.start() def stop(self): self._stop.set() t = self._thread if t and t.is_alive(): t.join(timeout=2.0) self._thread = None with self._lock: self.status = {"running": False}