Host gateway that takes led-controller-style commands and fans out DMX512 universes to Art-Net devices and serial ports. Fixtures are patched onto outputs and a led-driver-style pattern engine writes channel roles into them. Includes the vendored microdot/utemplate libs, tests and the README. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
# Host pyserial DMX512 TX (break + 513-byte frame). No-op without pyserial.
|
|
|
|
try:
|
|
from time import sleep
|
|
except ImportError: # pragma: no cover
|
|
def sleep(_s):
|
|
pass
|
|
|
|
try:
|
|
import serial as pyserial
|
|
except ImportError:
|
|
pyserial = None
|
|
|
|
|
|
class SerialDMX:
|
|
"""Raw DMX512 over a serial port (8N2, break, start code 0 + 512 slots)."""
|
|
|
|
def __init__(self, path, baud=250000):
|
|
self.path = path
|
|
self.baud = int(baud or 250000)
|
|
self.ser = None
|
|
self.open()
|
|
|
|
def open(self):
|
|
self.close()
|
|
if pyserial is None:
|
|
print("serial_dmx: pyserial not available, skip", self.path)
|
|
return
|
|
try:
|
|
self.ser = pyserial.Serial(
|
|
self.path,
|
|
baudrate=self.baud,
|
|
bytesize=pyserial.EIGHTBITS,
|
|
parity=pyserial.PARITY_NONE,
|
|
stopbits=pyserial.STOPBITS_TWO,
|
|
timeout=0,
|
|
write_timeout=0.5,
|
|
)
|
|
except Exception as e:
|
|
print("serial_dmx open failed", self.path, e)
|
|
self.ser = None
|
|
|
|
def close(self):
|
|
if self.ser is None:
|
|
return
|
|
try:
|
|
self.ser.close()
|
|
except Exception:
|
|
pass
|
|
self.ser = None
|
|
|
|
def send(self, channels):
|
|
"""Send one universe. ``channels`` is 512 bytes (ch1 at [0]) or a 513-byte frame."""
|
|
if self.ser is None:
|
|
return
|
|
if channels is None:
|
|
return
|
|
if len(channels) == 513:
|
|
frame = channels
|
|
else:
|
|
frame = bytearray(513)
|
|
n = len(channels)
|
|
if n > 512:
|
|
n = 512
|
|
frame[1 : 1 + n] = channels[:n]
|
|
try:
|
|
# DMX break ≥88 µs; pyserial send_break duration is in seconds.
|
|
self.ser.send_break(duration=0.0001)
|
|
sleep(0.000012) # mark after break
|
|
self.ser.write(frame)
|
|
except Exception as e:
|
|
print("serial_dmx send failed", self.path, e)
|