feat: DMX gateway: Art-Net and serial out, fixtures and a pattern engine
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>
This commit is contained in:
1 parent
072f95e041
commit
d2ffcb1969
30 files changed
+5490
-1
No files matched your search
@@ -0,0 +1,250 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Desktop tests for Art-Net, fixtures, patterns, and upload.
|
||||
|
||||
Run from dmx-driver repo root::
|
||||
|
||||
PYTHONPATH=src:lib python3 tests/artnet_dmx.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import socket
|
||||
|
||||
|
||||
def _bootstrap_import_path():
|
||||
try:
|
||||
here = __file__.rsplit("/", 1)[0]
|
||||
except NameError:
|
||||
here = "."
|
||||
parent = here.rsplit("/", 1)[0] if "/" in here else ".."
|
||||
for p in (parent + "/lib", parent + "/src", "lib", "src", ".", ".."):
|
||||
if p and p not in sys.path:
|
||||
sys.path.insert(0, p)
|
||||
|
||||
|
||||
_bootstrap_import_path()
|
||||
|
||||
from artnet import build_artdmx, parse # noqa: E402
|
||||
from serial_dmx import SerialDMX # noqa: E402
|
||||
from outputs import OutputManager # noqa: E402
|
||||
from controller import process_data # noqa: E402
|
||||
from settings import Settings # noqa: E402
|
||||
from preset import Preset # noqa: E402
|
||||
from player import PatternPlayer # noqa: E402
|
||||
|
||||
|
||||
def _assert_eq(label, got, expect):
|
||||
if got != expect:
|
||||
raise AssertionError("%s: got %r expected %r" % (label, got, expect))
|
||||
|
||||
|
||||
def test_build_artdmx():
|
||||
channels = bytearray(512)
|
||||
channels[0] = 10
|
||||
channels[1] = 20
|
||||
channels[511] = 99
|
||||
pkt = build_artdmx(channels, universe=0, sequence=1)
|
||||
kind = parse(pkt)
|
||||
_assert_eq("kind", kind[0], "dmx")
|
||||
_assert_eq("ch1", kind[3][0], 10)
|
||||
_assert_eq("ch2", kind[3][1], 20)
|
||||
_assert_eq("ch512", kind[3][511], 99)
|
||||
print("PASS build_artdmx")
|
||||
|
||||
|
||||
def test_artnet_send_loopback():
|
||||
recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
recv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
recv.bind(("127.0.0.1", 0))
|
||||
host, port = recv.getsockname()
|
||||
recv.settimeout(1.0)
|
||||
|
||||
outs = OutputManager()
|
||||
outs.configure(
|
||||
[
|
||||
{
|
||||
"id": "loop",
|
||||
"type": "artnet",
|
||||
"enabled": True,
|
||||
"host": host,
|
||||
"port": port,
|
||||
"universe": 0,
|
||||
}
|
||||
]
|
||||
)
|
||||
outs.set_rgb(1, 2, 3, 4, output_id="loop")
|
||||
outs.tick()
|
||||
|
||||
data, _addr = recv.recvfrom(2048)
|
||||
recv.close()
|
||||
outs.close()
|
||||
|
||||
kind = parse(data)
|
||||
_assert_eq("kind", kind[0], "dmx")
|
||||
_assert_eq("ch1", kind[3][0], 1)
|
||||
_assert_eq("ch2", kind[3][1], 2)
|
||||
_assert_eq("ch3", kind[3][2], 3)
|
||||
_assert_eq("ch4", kind[3][3], 4)
|
||||
print("PASS artnet_send_loopback")
|
||||
|
||||
|
||||
def test_fixture_patch():
|
||||
settings = Settings()
|
||||
settings["outputs"] = [
|
||||
{
|
||||
"id": "a1",
|
||||
"type": "artnet",
|
||||
"enabled": True,
|
||||
"host": "127.0.0.1",
|
||||
"port": 6454,
|
||||
"universe": 0,
|
||||
}
|
||||
]
|
||||
settings["fixtures"] = settings._normalize_fixtures(
|
||||
[
|
||||
{
|
||||
"id": "f1",
|
||||
"name": "Par 1",
|
||||
"output_id": "a1",
|
||||
"start_channel": 5,
|
||||
"profile": "parcan",
|
||||
}
|
||||
],
|
||||
settings["outputs"],
|
||||
)
|
||||
outs = OutputManager()
|
||||
outs.configure(settings["outputs"])
|
||||
fix = settings["fixtures"][0]
|
||||
outs.write_fixture(fix, {"dimmer": 200, "red": 10, "green": 20, "blue": 30})
|
||||
buf = outs.buffer("a1")
|
||||
# start_channel 5 → index 4
|
||||
_assert_eq("dimmer", buf[4], 200)
|
||||
_assert_eq("red", buf[5], 10)
|
||||
_assert_eq("green", buf[6], 20)
|
||||
_assert_eq("blue", buf[7], 30)
|
||||
outs.close()
|
||||
print("PASS fixture_patch")
|
||||
|
||||
|
||||
def test_pattern_blink_and_select():
|
||||
settings = Settings()
|
||||
settings["name"] = "dmx-gateway"
|
||||
settings["outputs"] = [
|
||||
{
|
||||
"id": "a1",
|
||||
"type": "artnet",
|
||||
"enabled": True,
|
||||
"host": "127.0.0.1",
|
||||
"port": 6454,
|
||||
"universe": 0,
|
||||
}
|
||||
]
|
||||
settings["fixtures"] = settings._normalize_fixtures(
|
||||
[
|
||||
{
|
||||
"id": "f1",
|
||||
"name": "Par 1",
|
||||
"output_id": "a1",
|
||||
"start_channel": 1,
|
||||
"profile": "parcan",
|
||||
}
|
||||
],
|
||||
settings["outputs"],
|
||||
)
|
||||
outs = OutputManager()
|
||||
outs.configure(settings["outputs"])
|
||||
player = PatternPlayer(settings, outs)
|
||||
_assert_eq("has_blink", "blink" in player.patterns, True)
|
||||
|
||||
process_data(
|
||||
{
|
||||
"v": "1",
|
||||
"presets": {
|
||||
"wash": {
|
||||
"pattern": "solid",
|
||||
"colors": ["#FF0000"],
|
||||
"brightness": 200,
|
||||
}
|
||||
},
|
||||
"select": {"f1": ["wash"]},
|
||||
},
|
||||
settings,
|
||||
outs,
|
||||
player=player,
|
||||
)
|
||||
player.tick()
|
||||
buf = outs.buffer("a1")
|
||||
_assert_eq("dimmer", buf[0], 200)
|
||||
_assert_eq("red", buf[1], 255)
|
||||
_assert_eq("green", buf[2], 0)
|
||||
_assert_eq("blue", buf[3], 0)
|
||||
|
||||
preset = Preset({"pattern": "blink", "colors": ["#00FF00"], "brightness": 255, "delay": 500})
|
||||
player.select(preset, targets=["f1"], preset_id="b")
|
||||
player.tick()
|
||||
buf = outs.buffer("a1")
|
||||
_assert_eq("blink_green", buf[2], 255)
|
||||
outs.close()
|
||||
print("PASS pattern_blink_and_select")
|
||||
|
||||
|
||||
def test_pattern_upload(tmp_path=None):
|
||||
settings = Settings()
|
||||
settings["outputs"] = []
|
||||
settings["fixtures"] = []
|
||||
outs = OutputManager()
|
||||
player = PatternPlayer(settings, outs)
|
||||
code = (
|
||||
"class UploadProbe:\\n"
|
||||
" id = 'upload_probe'\\n"
|
||||
" def run(self, preset, write_roles):\\n"
|
||||
" while True:\\n"
|
||||
" write_roles({'dimmer': 1, 'red': 2, 'green': 3, 'blue': 4})\\n"
|
||||
" yield\\n"
|
||||
)
|
||||
# Write via the same path upload uses
|
||||
try:
|
||||
os.mkdir("patterns")
|
||||
except OSError:
|
||||
pass
|
||||
path = os.path.join("patterns", "upload_probe.py")
|
||||
with open(path, "w") as f:
|
||||
f.write(code.replace("\\n", "\n"))
|
||||
try:
|
||||
player.reload_patterns()
|
||||
_assert_eq("uploaded", "upload_probe" in player.patterns, True)
|
||||
finally:
|
||||
try:
|
||||
os.remove(path)
|
||||
except OSError:
|
||||
pass
|
||||
outs.close()
|
||||
print("PASS pattern_upload")
|
||||
|
||||
|
||||
def test_serial_frame_layout():
|
||||
outs = OutputManager()
|
||||
outs.set_rgb(10, 20, 30, 40)
|
||||
outs.frame[0] = 0
|
||||
outs.frame[1:513] = outs.channels
|
||||
_assert_eq("start_code", outs.frame[0], 0)
|
||||
_assert_eq("frame_ch1", outs.frame[1], 10)
|
||||
ser = SerialDMX("/dev/null_does_not_exist_dmx_test", baud=250000)
|
||||
_assert_eq("ser_closed", ser.ser is None, True)
|
||||
ser.close()
|
||||
outs.close()
|
||||
print("PASS serial_frame_layout")
|
||||
|
||||
|
||||
def main():
|
||||
test_build_artdmx()
|
||||
test_artnet_send_loopback()
|
||||
test_serial_frame_layout()
|
||||
test_fixture_patch()
|
||||
test_pattern_blink_and_select()
|
||||
test_pattern_upload()
|
||||
print("PASS")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user