Files
qlcplus/tests/test_simulator.py
jimmyandClaude Opus 5.5 fc8abf9c99 Set the movers' zeros and pan directions, swap M1/M2's patch, and model tilt in the simulator.
The front right mover is on DMX 1 and the front left on 45, so M1 and M2
swap addresses in the stage map and test.qxw. Measured on the rig, pan 0
faces forward on the front pair and into the middle on the back pair, back
at 85. M1 and M4 turn anticlockwise, so movers get a pan_reverse flag.

Tilt 127 now points straight down: the simulator draws a spot under the
head, and past 127 the beam flips behind the pan heading.

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

302 lines
11 KiB
Python

"""Stage simulator: Art-Net decode and the XCHC patch."""
from __future__ import annotations
import json
import threading
from pathlib import Path
from urllib.request import urlopen
from simulator.artnet import ArtNetNode, build_artdmx, parse_artdmx, parse_opcode
from simulator.decode import apply_cto, decode_can, decode_mover, lens_angle, lens_spin, mix_rgbw, view_stage
from simulator.server import Simulator, make_server
from simulator.stage import apply_show, load_stage, set_heading, wrap_degrees
ROOT = Path(__file__).resolve().parents[1]
def test_artdmx_roundtrip():
frame = bytearray(512)
frame[87] = 255
frame[89] = 200
packet = build_artdmx(frame, universe=0, sequence=3)
parsed = parse_artdmx(packet)
assert parsed is not None
universe, data = parsed
assert universe == 0
assert data[87] == 255
assert data[89] == 200
assert parse_opcode(packet) == 0x5000
def test_poll_is_not_dmx():
packet = b"Art-Net\x00" + bytes([0x00, 0x20]) + bytes(10)
assert parse_opcode(packet) == 0x2000
assert parse_artdmx(packet) is None
def test_xchc_patch_matches_the_stage_map():
stage = load_stage(ROOT / "stage" / "map.json")
apply_show(stage, ROOT / "shows" / "xchc.qxw")
by_id = {fixture.id: fixture for fixture in stage.fixtures}
assert by_id["#1"].address == 87
assert by_id["#1"].dmx == 88
assert by_id["#8"].address == 143
assert by_id["M1"].address == 44
assert by_id["M1"].dmx == 45
assert by_id["M2"].address == 0
assert by_id["M1"].channels == 43
assert by_id["#3"].kind == "can"
assert stage.output_ip == "192.168.1.228"
assert stage.name == "xchc"
assert by_id["#9"].x == -3.5 and by_id["#9"].y == -0.8
assert by_id["#11"].y == -6.8 and by_id["#12"].x == 3.5
assert by_id["#13"].x == -3.5 and by_id["#15"].x == 3.5
assert by_id["#14"].x == 0.0 and by_id["#14"].y == -7.65
assert by_id["#1"].address == 87 and by_id["#9"].address == 151
assert by_id["#15"].address == 199 and by_id["M2"].address == 0
assert by_id["M1"].address == 44 and by_id["#15"].channels == 8
assert by_id["M3"].address == 257 and by_id["M3"].channels == 43
assert by_id["M3"].x == -1.75 and by_id["M3"].y == -7.65
assert by_id["M4"].address == 300 and by_id["M4"].x == 1.75
assert by_id["#1"].direction == 71 and by_id["#8"].direction == -72
assert by_id["#3"].direction == 0 and by_id["#14"].direction == -179
assert by_id["M2"].zero == -90 and by_id["M4"].zero == 0
assert by_id["M1"].zero == 90 and by_id["M1"].pan_reverse and not by_id["M2"].pan_reverse
assert by_id["M4"].pan_reverse and not by_id["M3"].pan_reverse
assert stage.warnings == []
def test_can_red_and_white():
assert mix_rgbw(0, 0, 0, 255, 255) == (255, 255, 255)
viewed = decode_can([255, 0, 255, 0, 0, 0, 0, 0])
assert (viewed["r"], viewed["g"], viewed["b"]) == (255, 0, 0)
assert viewed["function"] == "mix"
assert viewed["strobe_hz"] == 0
assert decode_can([255, 255, 255, 0, 0, 0, 0, 0])["strobe_hz"] > 0
assert decode_can([128, 0, 0, 0, 0, 0, 120, 0])["effect"] is True
def test_can_macro_functions():
assert decode_can([255, 0, 0, 0, 0, 0, 51, 0])["function"] == "fixed"
assert decode_can([255, 0, 0, 0, 0, 0, 51, 0])["r"] == 255
assert decode_can([0, 0, 0, 0, 0, 0, 80, 0])["r"] == 0
jump = decode_can([255, 0, 0, 0, 0, 0, 120, 200])
assert jump["function"] == "jump"
assert jump["speed_hz"] > 1
assert jump["palette"][0] == [255, 0, 0]
pulse = decode_can([255, 0, 255, 80, 0, 0, 170, 0])
assert pulse["function"] == "pulse"
assert pulse["r"] == 255 and pulse["g"] == 80
assert decode_can([255, 0, 0, 0, 0, 0, 220, 0])["function"] == "gradient"
sound = decode_can([255, 0, 0, 0, 0, 0, 255, 0])
assert sound["function"] == "sound" and sound["sound"] == 2
def test_mover_home_points_at_the_audience():
channels = [0] * 43
channels[0] = 128 # pan coarse, fine 0 → 32768, the centre of 540°
channels[2] = 64
channels[8] = 255
channels[10] = 255 # red on the first pixel
viewed = decode_mover(channels)
assert viewed["pan_deg"] == 0
assert viewed["r"] == 255
assert viewed["beam_m"] > 2
def test_mover_tilt_goes_through_straight_down():
def reach(tilt):
channels = [0] * 43
channels[2] = tilt
return decode_mover(channels)["beam_m"]
assert reach(0) == 4.7 # forward and up
assert abs(reach(127)) < 0.2 # a spot under the head
assert reach(255) < -4.6 # behind the pan heading
assert reach(64) > 0 > reach(192)
assert lens_spin(0) == 0
assert lens_spin(160) < 0
assert lens_spin(230) > 0
assert lens_angle(0) == 0
assert 25 < lens_angle(64) < 35
def test_mover_functions():
channels = [0] * 43
channels[5] = 230
channels[6] = 255
channels[8] = 255
channels[39] = 20
macro = decode_mover(channels)
assert macro["function"] == "macro"
assert macro["r"] == 255 and macro["b"] == 0
assert macro["zoom_deg"] == 45
assert macro["spin_dps"] > 0
channels[40] = 103
static = decode_mover(channels)
assert static["function"] == "static"
assert static["pixels"][3][0] == 255
assert static["pixels"][0] == [0, 0, 0]
channels[41] = 30
dynamic = decode_mover(channels)
assert dynamic["function"] == "dynamic"
assert dynamic["dynamic"] == "chase"
assert dynamic["pixels"][0][0] == 255
assert dynamic["speed_hz"] > 0
warm = [0] * 43
warm[8] = 255
warm[10] = warm[11] = warm[12] = 255
warm[38] = 255
tinted = decode_mover(warm)
assert tinted["r"] == 255
assert tinted["b"] < apply_cto(255, 255, 255, 0)[2]
assert tinted["b"] < 100
strobe = [0] * 43
strobe[9] = 240
assert decode_mover(strobe)["strobe_mode"] == "random"
assert decode_mover([0] * 43)["strobe_mode"] == "open"
def test_map_definition_selects_the_channels():
stage = load_stage(ROOT / "stage" / "map.json")
mover = next(fixture for fixture in stage.fixtures if fixture.id == "M1")
can = next(fixture for fixture in stage.fixtures if fixture.id == "#1")
assert can.channel_names[:3] == ["Dimmer", "Strobe", "Red"]
assert mover.channel_names[5] == "Lens Rotate"
assert mover.channel_names[6] == "Zoom"
assert mover.mode == "43 Channel"
frame = bytearray(512)
frame[mover.address + mover.channel_names.index("Pan")] = 128
frame[mover.address + mover.channel_names.index("Zoom")] = 255
frame[mover.address + mover.channel_names.index("Center Brightness")] = 255
frame[mover.address + mover.channel_names.index("Red 1")] = 255
frame[mover.address + mover.channel_names.index("Lens Rotate")] = 230
live = view_stage(stage, {0: bytes(frame)})["M1"]
assert live["zoom_deg"] == 45
assert live["spin_dps"] > 0
assert live["r"] == 255
assert live["pan_deg"] == 0
def test_front_movers_face_forward_at_pan_zero_and_back_at_85():
stage = load_stage(ROOT / "stage" / "map.json")
by_id = {fixture.id: fixture for fixture in stage.fixtures}
def heading(pan):
frame = bytearray(512)
for mover in (by_id["M1"], by_id["M2"]):
frame[mover.address + mover.channel_names.index("Pan")] = pan
live = view_stage(stage, {0: bytes(frame)})
return {key: wrap_degrees(by_id[key].zero + live[key]["pan_deg"]) for key in ("M1", "M2")}
assert heading(0) == {"M1": 0, "M2": 0}
back = heading(85)
assert abs(abs(back["M1"]) - 180) <= 2 and abs(abs(back["M2"]) - 180) <= 2
# M2 turns clockwise and M1 anticlockwise.
assert heading(20)["M2"] > 0 > heading(20)["M1"]
def test_back_movers_face_the_middle_at_pan_zero():
stage = load_stage(ROOT / "stage" / "map.json")
by_id = {fixture.id: fixture for fixture in stage.fixtures}
def heading(pan):
frame = bytearray(512)
for mover in (by_id["M3"], by_id["M4"]):
frame[mover.address + mover.channel_names.index("Pan")] = pan
live = view_stage(stage, {0: bytes(frame)})
return {key: wrap_degrees(by_id[key].zero + live[key]["pan_deg"]) for key in ("M3", "M4")}
# M3 is on the left (-X) so the middle is +X, 90°. M4 mirrors it.
assert heading(0) == {"M3": 90, "M4": -90}
back = heading(85)
assert abs(back["M3"] + 90) <= 2 and abs(back["M4"] - 90) <= 2
# M3 turns clockwise and M4 anticlockwise, so both swing toward the back wall first.
assert heading(20)["M3"] > 90 and heading(20)["M4"] < -90
def test_view_reads_universe_bytes():
stage = load_stage(ROOT / "stage" / "map.json")
apply_show(stage, ROOT / "shows" / "xchc.qxw")
frame = bytearray(512)
frame[87] = 255
frame[89] = 0
frame[90] = 255
live = view_stage(stage, {0: bytes(frame)})
assert (live["#1"]["r"], live["#1"]["g"], live["#1"]["b"]) == (0, 255, 0)
assert live["#2"]["dimmer"] == 0
def test_http_state_follows_artnet():
stage = load_stage(ROOT / "stage" / "map.json")
apply_show(stage, ROOT / "shows" / "xchc.qxw")
node = ArtNetNode("127.0.0.1", 0)
node.start()
server = make_server(Simulator(stage, node), "127.0.0.1", 0)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
frame = bytearray(512)
frame[0] = 128
frame[8] = 255
frame[10] = 255
node.write(0, frame)
port = server.server_address[1]
with urlopen(f"http://127.0.0.1:{port}/api/state") as response:
payload = json.load(response)
assert payload["packets"] == 1
assert payload["fixtures"]["M2"]["r"] == 255
with urlopen(f"http://127.0.0.1:{port}/api/stage") as response:
described = json.load(response)
assert described["fixtures"][0]["x"] == -3.5
finally:
server.shutdown()
node.close()
def test_heading_is_stored_in_the_map(tmp_path):
target = tmp_path / "map.json"
target.write_text((ROOT / "stage" / "map.json").read_text())
before = json.loads(target.read_text())["fixtures"]
stage = load_stage(target)
assert wrap_degrees(270) == -90
set_heading(stage, "#1", direction=45)
set_heading(stage, "M1", zero=-30)
saved = json.loads(target.read_text())
assert saved["fixtures"]["#1"]["direction"] == 45
assert saved["fixtures"]["#2"]["direction"] == before["#2"]["direction"]
assert saved["fixtures"]["M1"]["zero"] == -30
assert saved["fixtures"]["M2"]["zero"] == -90
again = load_stage(target)
by_id = {fixture.id: fixture for fixture in again.fixtures}
assert by_id["#1"].direction == 45
assert by_id["M1"].zero == -30
def test_switch_stage_lists_and_loads_maps_in_the_same_folder(tmp_path):
first = tmp_path / "map.json"
first.write_text((ROOT / "stage" / "map.json").read_text())
data = json.loads(first.read_text())
data["name"] = "movers only"
data["fixtures"] = {key: value for key, value in data["fixtures"].items() if key.startswith("M")}
(tmp_path / "movers.json").write_text(json.dumps(data))
sim = Simulator(load_stage(first), node=None, show=ROOT / "shows" / "xchc.qxw")
assert sim.stages() == {"current": "map.json", "stages": ["map.json", "movers.json"]}
stage = sim.switch("movers.json")
assert stage.name == "movers only"
assert stage.show == "xchc.qxw"
assert sim.stages()["current"] == "movers.json"
for bad in ("../map.json", "missing.json"):
try:
sim.switch(bad)
except KeyError:
pass
else:
raise AssertionError(bad)