"""Turn a DMX frame into colours, beams, and strobe rates. Channel order comes from the stage map's fixture definitions. Role names such as Dimmer, Red, Pan, and Zoom select the slots. """ from __future__ import annotations import re from simulator.stage import Fixture, Stage _PIXEL = re.compile(r"^(red|green|blue|white) (\d+)$", re.IGNORECASE) PAN_DEGREES = 540.0 ZOOM_MIN = 5.0 ZOOM_MAX = 45.0 # BCC145 8ch: strobe 0–7 is open. Small Eye centre strobe 0–3 is open. CAN_STROBE_OPEN = 7 MOVER_STROBE_OPEN = 3 # beamZ BCC145 8ch macro, matching fixtures/beamZ-BCC145.qxf. # 0–50 RGBW mix, 51–100 fixed colour, 101–150 jump, 151–200 pulse, # 201–250 gradient, 251–255 sound. Speed is slow to fast. _PAR_PALETTE = ( (255, 0, 0), (255, 96, 0), (255, 200, 0), (255, 255, 0), (0, 255, 0), (0, 255, 160), (0, 64, 255), (140, 0, 255), (255, 0, 160), (255, 255, 255), ) # Small Eye 43ch colour macros, static masks, and dynamic chases. # Ranges match fixtures/Small-Eye-7-Mover.qxf: 0–9 is no function. _MOVER_MACROS = ( (255, 0, 0), (255, 80, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255), (0, 40, 255), (160, 0, 255), (255, 0, 180), (255, 255, 255), (255, 180, 80), ) _STATIC_MASKS = ( ("all", (1, 1, 1, 1, 1, 1, 1)), ("odds", (1, 0, 1, 0, 1, 0, 1)), ("evens", (0, 1, 0, 1, 0, 1, 0)), ("center", (0, 0, 0, 1, 0, 0, 0)), ("ends", (1, 0, 0, 0, 0, 0, 1)), ("left", (1, 1, 1, 1, 0, 0, 0)), ("right", (0, 0, 0, 1, 1, 1, 1)), ("one", None), ) _DYNAMIC = ("chase", "reverse", "rainbow", "bounce", "pulse", "pairs") _DYNAMIC_LABELS = { "chase": "chase", "reverse": "reverse chase", "rainbow": "rainbow", "bounce": "bounce", "pulse": "pixel pulse", "pairs": "pairs", } MOVER_43 = [ "Pan", "Pan Fine", "Tilt", "Tilt Fine", "Pan/Tilt Speed", "Lens Rotate", "Zoom", "Focus", "Center Brightness", "Center Strobe", "Red 1", "Green 1", "Blue 1", "White 1", "Red 2", "Green 2", "Blue 2", "White 2", "Red 3", "Green 3", "Blue 3", "White 3", "Red 4", "Green 4", "Blue 4", "White 4", "Red 5", "Green 5", "Blue 5", "White 5", "Red 6", "Green 6", "Blue 6", "White 6", "Red 7", "Green 7", "Blue 7", "White 7", "Colour Temperature", "Colour Macro", "Static Effect", "Dynamic Effect", "Reset", ] def _find(names: list[str], wanted: str) -> int | None: wanted = wanted.lower() for index, name in enumerate(names): if name.lower() == wanted: return index return None def _value(channels: list[int], names: list[str], wanted: str, default: int = 0) -> int: index = _find(names, wanted) if index is None or index >= len(channels): return default return channels[index] def _pixels(channels: list[int], names: list[str], dimmer: int) -> list[list[int]]: groups: dict[int, dict[str, int]] = {} for index, name in enumerate(names): match = _PIXEL.match(name.strip()) if match is None: continue groups.setdefault(int(match.group(2)), {})[match.group(1).lower()] = index def at(group: dict[str, int], color: str) -> int: index = group.get(color) if index is None or index >= len(channels): return 0 return channels[index] return [ list(mix_rgbw(at(groups[number], "red"), at(groups[number], "green"), at(groups[number], "blue"), at(groups[number], "white"), dimmer)) for number in sorted(groups) ] def slots_at(frame: bytes, address: int, count: int) -> list[int]: out: list[int] = [] for offset in range(count): index = address + offset out.append(frame[index] if 0 <= index < len(frame) else 0) return out def mix_rgbw(red: int, green: int, blue: int, white: int, dimmer: int) -> tuple[int, int, int]: """White is a fourth emitter. Dimmer scales the whole mix.""" level = max(0, min(255, dimmer)) / 255.0 return ( int(round(min(255, red + white) * level)), int(round(min(255, green + white) * level)), int(round(min(255, blue + white) * level)), ) def strobe_hz(value: int, open_max: int) -> float: if value <= open_max: return 0.0 span = max(1, 255 - open_max) return round(1.0 + (value - open_max) / span * 18.0, 2) def lens_angle(value: int) -> float: """Indexed lens position, 0–60°. Spin ranges are not an index.""" if value <= 127: return round(value / 127.0 * 60.0, 1) return 0.0 def lens_spin(value: int) -> float: """Degrees per second. Negative is counter-clockwise. 0–127 index, 128–188 CCW fast to slow, 189–193 stop, 194–255 CW slow to fast. """ if 128 <= value <= 188: rate = (188 - value) / 60.0 return round(-(40.0 + rate * 320.0), 1) if 194 <= value <= 255: rate = (value - 194) / 61.0 return round(40.0 + rate * 320.0, 1) return 0.0 def _beam_color(pixels: list[list[int]]) -> tuple[int, int, int]: """Brightness-weighted mix, so one lit pixel stays its own colour.""" red = green = blue = weight = 0.0 for pr, pg, pb in pixels: w = pr + pg + pb red += pr * w green += pg * w blue += pb * w weight += w if weight <= 0: return 0, 0, 0 return int(round(red / weight)), int(round(green / weight)), int(round(blue / weight)) def _scale(colour: tuple[int, int, int], dimmer: int) -> tuple[int, int, int]: level = max(0, min(255, dimmer)) / 255.0 return tuple(int(round(channel * level)) for channel in colour) def _speed_hz(speed: int, *, slow: float = 0.35, fast: float = 8.0) -> float: return round(slow + max(0, min(255, speed)) / 255.0 * (fast - slow), 2) def _can_mode(macro: int) -> str: if macro <= 50: return "mix" if macro <= 100: return "fixed" if macro <= 150: return "jump" if macro <= 200: return "pulse" if macro <= 250: return "gradient" return "sound" def _range_pick(value: int, start: int, count: int) -> tuple[int, float] | None: """Map value onto `count` bands from `start` through 255. Returns index and 0–1 position.""" if value < start or count <= 0: return None span = 256 - start index = min(count - 1, (value - start) * count // span) band = span / count frac = ((value - start) - index * band) / band return index, max(0.0, min(0.999, frac)) def apply_cto(red: int, green: int, blue: int, amount: int) -> tuple[int, int, int]: """0 leaves the colour alone. Higher values warm it, the way a CTO does.""" if amount <= 0: return red, green, blue warm = max(0, min(255, amount)) / 255.0 return ( red, int(round(green * (1 - 0.35 * warm))), int(round(blue * (1 - 0.75 * warm))), ) def _can_view( dimmer: int, strobe: int, red: int, green: int, blue: int, white: int, macro: int, speed: int, channels: list[int], ) -> dict: mode = _can_mode(macro) mixed = mix_rgbw(red, green, blue, white, dimmer) peak = mix_rgbw(red, green, blue, white, 255) if peak == (0, 0, 0): peak = (255, 255, 255) shown = mixed palette = None sound = 0 hz = 0.0 if mode == "fixed": span = 50 index = min(len(_PAR_PALETTE) - 1, max(0, macro - 51) * len(_PAR_PALETTE) // span) shown = _scale(_PAR_PALETTE[index], dimmer) elif mode == "pulse": shown = _scale(peak, dimmer) hz = _speed_hz(speed, slow=0.4, fast=6.0) elif mode in ("jump", "gradient", "sound"): palette = [list(colour) for colour in _PAR_PALETTE] hz = _speed_hz(speed) shown = _scale(_PAR_PALETTE[0], dimmer) if mode == "sound": sound = 2 if macro >= 254 else 1 labels = { "mix": "RGBW", "fixed": "fixed colour", "jump": "colour jump", "pulse": "colour pulse", "gradient": "colour gradient", "sound": "sound 2" if sound == 2 else "sound 1", } viewed = { "r": shown[0], "g": shown[1], "b": shown[2], "dimmer": dimmer, "strobe_hz": strobe_hz(strobe, CAN_STROBE_OPEN), "strobe_mode": "strobe" if strobe > CAN_STROBE_OPEN else "open", "effect": mode != "mix", "function": mode, "function_label": labels[mode], "speed_hz": hz, "sound": sound, "channels": channels, } if palette is not None: viewed["palette"] = palette return viewed def decode_can(channels: list[int]) -> dict: def at(index: int) -> int: return channels[index] if index < len(channels) else 0 return _can_view(at(0), at(1), at(2), at(3), at(4), at(5), at(6), at(7), channels) def decode_named(channels: list[int], names: list[str], *, kind: str) -> dict: """Read a fixture using the channel names from the stage map.""" if kind == "mover" or (_find(names, "Pan") is not None and _find(names, "Tilt") is not None): return _decode_mover_named(channels, names) return _decode_can_named(channels, names) def _decode_can_named(channels: list[int], names: list[str]) -> dict: dimmer = 255 if _find(names, "Dimmer") is None else _value(channels, names, "Dimmer") return _can_view( dimmer, _value(channels, names, "Strobe"), _value(channels, names, "Red"), _value(channels, names, "Green"), _value(channels, names, "Blue"), _value(channels, names, "White"), _value(channels, names, "Macro"), _value(channels, names, "Speed"), channels, ) def _mover_strobe(value: int) -> tuple[float, str]: """Centre strobe: open, linear strobe, then three random-strobe bands.""" if value <= MOVER_STROBE_OPEN: return 0.0, "open" if value <= 200: return strobe_hz(value, MOVER_STROBE_OPEN), "strobe" if value <= 215: return 6.0, "random" if value <= 234: return 12.0, "random" return 18.0, "random" def _decode_mover_named(channels: list[int], names: list[str]) -> dict: pan = (_value(channels, names, "Pan") << 8) | _value(channels, names, "Pan Fine") tilt = (_value(channels, names, "Tilt") << 8) | _value(channels, names, "Tilt Fine") zoom = _value(channels, names, "Zoom") rotate = _value(channels, names, "Lens Rotate") dimmer = 255 if _find(names, "Center Brightness") is None else _value(channels, names, "Center Brightness") strobe = _value(channels, names, "Center Strobe") pixels = _pixels(channels, names, dimmer) macro_value = _value(channels, names, "Colour Macro") static_value = _value(channels, names, "Static Effect") dynamic_value = _value(channels, names, "Dynamic Effect") cto = _value(channels, names, "Colour Temperature") macro = _range_pick(macro_value, 10, len(_MOVER_MACROS)) static = _range_pick(static_value, 10, len(_STATIC_MASKS)) dynamic = _range_pick(dynamic_value, 10, len(_DYNAMIC)) source = [list(pixel) for pixel in pixels] function = "pixels" label = "pixels" dynamic_name = None speed = 0.0 if macro is not None: colour = list(_scale(_MOVER_MACROS[macro[0]], dimmer)) source = [colour[:] for _ in range(7)] function = "macro" label = "colour macro" if static is not None or dynamic is not None: if not any(sum(pixel) > 0 for pixel in source): white = list(_scale((255, 255, 255), dimmer)) source = [white[:] for _ in range(7)] if static is not None and dynamic is None: name, mask = _STATIC_MASKS[static[0]] if name == "one": pixel = min(6, int(static[1] * 7)) mask = tuple(1 if index == pixel else 0 for index in range(7)) source = [pixel if mask[index] else [0, 0, 0] for index, pixel in enumerate(source)] function = "static" label = "one pixel" if name == "one" else f"static {name}" if dynamic is not None: dynamic_name = _DYNAMIC[dynamic[0]] speed = round(0.4 + dynamic[1] * 6.0, 2) function = "dynamic" label = _DYNAMIC_LABELS[dynamic_name] tinted = [list(apply_cto(pixel[0], pixel[1], pixel[2], cto)) for pixel in source] red, green, blue = _beam_color(tinted) rate, strobe_mode = _mover_strobe(strobe) reset = _value(channels, names, "Reset") pan_speed = _value(channels, names, "Pan/Tilt Speed") pan_deg = (pan - 32768) / 65536.0 * PAN_DEGREES zoom_deg = ZOOM_MIN + (zoom / 255.0) * (ZOOM_MAX - ZOOM_MIN) # Long enough to read on the plan at tilt zero. Tilt stretches the throw. beam_m = 2.2 + (tilt / 65535.0) * 2.5 return { "r": red, "g": green, "b": blue, "dimmer": dimmer, "strobe_hz": rate, "strobe_mode": strobe_mode, "effect": function != "pixels", "function": function, "function_label": label, "speed_hz": speed, "dynamic": dynamic_name, "pan_deg": round(pan_deg, 2), "zoom_deg": round(zoom_deg, 2), "beam_m": round(beam_m, 3), "pixels": tinted, "spin_dps": lens_spin(rotate), "lens_deg": lens_angle(rotate), "focus": round(_value(channels, names, "Focus") / 255.0, 3), "move": round(1 - pan_speed / 255.0, 3), "cto": cto, "reset": 200 <= reset <= 205, "channels": channels, } def decode_mover(channels: list[int]) -> dict: return _decode_mover_named(channels, MOVER_43) def view_fixture(fixture: Fixture, universes: dict[int, bytes]) -> dict: if fixture.address is None or fixture.universe is None: return {"patched": False, "r": 0, "g": 0, "b": 0, "dimmer": 0, "strobe_hz": 0, "channels": []} count = len(fixture.channel_names) or fixture.channels or (43 if fixture.kind == "mover" else 8) frame = universes.get(fixture.universe, bytes(512)) channels = slots_at(frame, fixture.address, count) if fixture.channel_names: viewed = decode_named(channels, fixture.channel_names, kind=fixture.kind) elif fixture.kind == "mover": viewed = decode_mover(channels) else: viewed = decode_can(channels) viewed["patched"] = True return viewed def view_stage(stage: Stage, universes: dict[int, bytes]) -> dict[str, dict]: return {fixture.id: view_fixture(fixture, universes) for fixture in stage.fixtures}