#!/usr/bin/env python3 """Send Art-Net (ArtDmx) to the simulated rig, or any Art-Net node. The sim controller listens on UDP 6454 (LED_SIM_ARTNET_PORT moves it) and passes each universe to the /sim/ page, which draws the room's `artnet` fixtures from it: pixel strips (RGB from `address`, then 170 pixels a universe from channel 1), par cans and movers. python3 tools/artnet_send.py pixels --universe 5 --leds 60 --effect rainbow # the fixtures room's art-1 python3 tools/artnet_send.py pixels --universe 1 --leds 119 --effect chase --color ff8000 python3 tools/artnet_send.py pixels --universe 5 --leds 60 --effect solid --color 0000ff --seconds 0 python3 tools/artnet_send.py channels --universe 0 88=255 89=255 90=0 91=0 # raw channels (1-based) python3 tools/artnet_send.py blackout --universe 0 --universe 1 --universe 5 python3 tools/artnet_send.py pixels --host 10.1.1.50 --universe 0 --leds 170 --seconds 0 sends one frame (repeated a few times, as Art-Net is UDP) and exits; the default runs until Ctrl+C. """ import argparse import colorsys import math import socket import struct import sys import time ARTNET_PORT = 6454 PIXELS_PER_UNIVERSE = 170 def art_dmx(universe, data, seq): """One ArtDmx packet: universe is the 15-bit port address, data up to 512 channels (padded to even).""" if len(data) % 2: data = data + b"\0" return (b"Art-Net\0" + struct.pack("H", 14) + bytes([seq & 0xFF, 0]) + struct.pack("H", len(data)) + data) def split_pixels(universe, address, rgb): """Lay RGB bytes out as the sim reads them: from `address` in the first universe, then from channel 1.""" out = {} u, a0, px, n = universe, address - 1, 0, len(rgb) // 3 while px < n: take = min((512 - a0) // 3, n - px) buf = bytearray(out.get(u, bytes(512))) buf[a0:a0 + take * 3] = rgb[px * 3:(px + take) * 3] out[u] = bytes(buf) px += take u += 1 a0 = 0 return out def parse_color(s): s = s.lstrip("#") if len(s) != 6: raise argparse.ArgumentTypeError("colour is RRGGBB hex") return bytes.fromhex(s) def effect_frame(effect, leds, t, color, speed): """RGB bytes for `leds` pixels at time t (seconds).""" if effect == "solid": return color * leds if effect == "rainbow": out = bytearray() for i in range(leds): r, g, b = colorsys.hsv_to_rgb((i / leds + t * speed * 0.25) % 1.0, 1.0, 1.0) out += bytes((int(r * 255), int(g * 255), int(b * 255))) return bytes(out) if effect == "chase": head = (t * speed * leds * 0.5) % leds out = bytearray() for i in range(leds): d = (head - i) % leds k = max(0.0, 1.0 - d / 8.0) # an 8-pixel tail out += bytes(int(c * k) for c in color) return bytes(out) if effect == "pulse": k = 0.5 - 0.5 * math.cos(t * speed * math.pi) return bytes(int(c * k) for c in color) * leds raise ValueError(effect) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) common = argparse.ArgumentParser(add_help=False) common.add_argument("--host", default="127.0.0.1", help="Art-Net node (default 127.0.0.1, the local sim)") common.add_argument("--port", type=int, default=ARTNET_PORT) common.add_argument("--fps", type=float, default=30.0) common.add_argument("--seconds", type=float, default=None, help="how long to run (0: one frame; default: until Ctrl+C)") sub = ap.add_subparsers(dest="cmd", required=True) p = sub.add_parser("pixels", parents=[common], help="an RGB pixel strip") p.add_argument("--universe", type=int, default=0) p.add_argument("--address", type=int, default=1, help="first channel (1-512)") p.add_argument("--leds", type=int, default=PIXELS_PER_UNIVERSE) p.add_argument("--effect", choices=("rainbow", "chase", "solid", "pulse"), default="rainbow") p.add_argument("--color", type=parse_color, default=bytes((255, 255, 255)), help="RRGGBB (chase, solid, pulse)") p.add_argument("--speed", type=float, default=1.0) c = sub.add_parser("channels", parents=[common], help="set raw DMX channels: CH=VALUE ...") c.add_argument("--universe", type=int, default=0) c.add_argument("set", nargs="+", metavar="CH=VALUE") b = sub.add_parser("blackout", parents=[common], help="all channels 0") b.add_argument("--universe", type=int, action="append", default=None) a = ap.parse_args() if a.cmd == "pixels" and not 1 <= a.address <= 510: ap.error("--address is 1-510") def frame(t): if a.cmd == "pixels": return split_pixels(a.universe, a.address, effect_frame(a.effect, a.leds, t, a.color, a.speed)) if a.cmd == "channels": buf = bytearray(512) for kv in a.set: ch, _, v = kv.partition("=") ch, v = int(ch), int(v) if not (1 <= ch <= 512 and 0 <= v <= 255): sys.exit(f"bad channel setting {kv!r}: CH 1-512, VALUE 0-255") buf[ch - 1] = v return {a.universe: bytes(buf)} return {u: bytes(512) for u in (a.universe or [0])} static = a.cmd != "pixels" or a.effect == "solid" seconds = a.seconds if a.seconds is not None else (0 if a.cmd == "blackout" else None) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) dest = (a.host, a.port) period = 1.0 / a.fps t0 = time.monotonic() seq, sent = 1, 0 unis = sorted(frame(0)) print(f"Art-Net to {a.host}:{a.port}, universe{'s' if len(unis) > 1 else ''} {', '.join(map(str, unis))}" + ("" if seconds == 0 else f", {a.fps:g} fps (Ctrl+C to stop)")) try: while True: t = time.monotonic() - t0 for u, data in frame(t).items(): sock.sendto(art_dmx(u, data, seq), dest) seq = seq % 255 + 1 sent += 1 if seconds == 0 and sent >= 3: # one frame, a few copies for UDP break if seconds and t >= seconds: break time.sleep(period if not static else max(period, 0.25)) except KeyboardInterrupt: pass print(f"sent {sent} frame{'s' if sent != 1 else ''}") if __name__ == "__main__": main()