#!/usr/bin/env python3 """Load demo animations for the simulated bridge rig (or any 2-column rig) into led-controller. python3 tools/sim_demo.py # controller at localhost:8080, simulator at :8765 python3 tools/sim_demo.py --host 10.1.1.180 --sim http://localhost:8765 Creates (again, if run twice) a "Sim demo" profile and makes it current, with: - groups: Rig (all bars), Left, Right, Row 1-8 - presets: the field patterns (sweep, ripple, spin, grow, field_wave) and hit / keyframes, timed in beats - sequences: row chase, column ping-pong, a two-lane canon, a looks medley - animations (one-shot clips over whatever plays): flash, rows drop, centre burst - a zone "Bridge" over the whole rig with all of the above as tiles Drivers restart their pattern on every beat, so every preset here lasts one beat (beat_time, delay 4 quarter beats) and loops cleanly with the clock. """ import argparse import sys import requests PROFILE = "Sim demo" class Api: def __init__(self, base): self.base = base.rstrip("/") self.s = requests.Session() def __call__(self, method, path, body=None, ok=(200, 201)): r = self.s.request(method, self.base + path, json=body, timeout=15) if r.status_code not in ok: sys.exit(f"{method} {path}: {r.status_code} {r.text[:200]}") return r.json() if r.content else {} def new_id(reply): if isinstance(reply, dict) and "id" in reply and not isinstance(reply["id"], dict): return str(reply["id"]) return str(next(iter(reply))) def rig_bars(sim_url): """[(mac, row, col)] from the simulator's strips.""" d = requests.get(sim_url.rstrip("/") + "/api/pixels", timeout=5).json() return [(s["mac"].replace(":", ""), s["row"], s["col"]) for s in d["strips"]], d["layout"] def main(): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--host", default="localhost:8080", help="controller address (default localhost:8080)") ap.add_argument("--sim", default="http://localhost:8765", help="simulator URL, for the bars' rows and columns") a = ap.parse_args() api = Api(a.host if a.host.startswith("http") else "http://" + a.host) bars, layout = rig_bars(a.sim) rows = layout["rows"] # Start over: drop an earlier demo profile and the groups it owned profiles = api("GET", "/profiles") for pid, p in (profiles.get("profiles") or profiles).items(): if isinstance(p, dict) and p.get("name") == PROFILE: for gid, g in api("GET", "/groups").items(): if isinstance(g, dict) and g.get("profile_id") == pid: api("DELETE", f"/groups/{gid}") api("DELETE", f"/profiles/{pid}") pid = new_id(api("POST", "/profiles", {"name": PROFILE})) api("POST", f"/profiles/{pid}/apply") # A new profile comes with a default zone and presets; the zone goes, so "Bridge" is the only one for zid in api("GET", "/zones").get("zone_order", []): api("DELETE", f"/zones/{zid}") def group(name, macs): return new_id(api("POST", "/groups", {"name": name, "devices": macs, "profile_scoped": True})) g = {"Rig": group("Rig", [m for m, _, _ in bars])} g["Left"] = group("Left", [m for m, _, c in bars if c == 0]) g["Right"] = group("Right", [m for m, _, c in bars if c == 1]) for r in range(rows): g[f"Row {r + 1}"] = group(f"Row {r + 1}", [m for m, row, _ in bars if row == r]) def preset(name, pattern, colors, **kw): body = {"name": name, "pattern": pattern, "colors": colors, "brightness": 255, "delay": 4, "beat_time": True, "auto": True} body.update(kw) return new_id(api("POST", "/presets", body)) P = { "Sweep →": preset("Sweep →", "sweep", ["#ff0040", "#ffa000"], n1=0, n2=250, n3=400), "Sweep ↑": preset("Sweep ↑", "sweep", ["#00c0ff", "#8000ff"], n1=90, n2=200, n3=300), "Wipe ←": preset("Wipe ←", "sweep", ["#40ff80", "#00a0ff"], n1=180, n2=100, n4=1), "Ripple": preset("Ripple", "ripple", ["#ff2060", "#20a0ff", "#40ff80"], n1=250, n2=2), "Ripple in": preset("Ripple in", "ripple", ["#ffffff", "#ff8000"], n1=200, n3=1), "Radar": preset("Radar", "spin", ["#00ff60", "#004010"], n1=1, n2=25, n3=120), "Pinwheel": preset("Pinwheel", "spin", ["#ff0080", "#0080ff"], n1=4, n2=20, n3=40, n4=1), "Grow out": preset("Grow out", "grow", ["#ffd000", "#ff2000"], n1=0, n2=1), "Grow round": preset("Grow round", "grow", ["#00ffd0", "#0040ff"], n1=2, n2=2), "Plasma": preset("Plasma", "field_wave", ["#ff0060", "#6000ff", "#00c0ff"], n3=3, n2=800), "Swirl": preset("Swirl", "field_wave", ["#ff8000", "#ff0040", "#8000ff"], n3=2), "Wave": preset("Wave", "field_wave", ["#0040ff", "#00ffc0"], n1=0, n2=600, n3=0), "Hit": preset("Hit", "hit", ["#ffffff", "#ff0040", "#00a0ff"], auto=False, n1=0, n2=1, n3=2), "Fade": preset("Fade", "keyframes", ["#ff0000", "#00ff00", "#0000ff"], n1=1), "Red": preset("Red", "on", ["#ff0000"], beat_time=False, delay=100), "Blue": preset("Blue", "on", ["#0040ff"], beat_time=False, delay=100), "White": preset("White", "on", ["#ffffff"], beat_time=False, delay=100), } row_targets = [{"name": f"Row {r + 1}", "group_ids": [g[f"Row {r + 1}"]]} for r in range(rows)] def sequence(name, kind="sequence", **body): body = {"name": name, "kind": kind, "loop": kind != "animation", "advance_mode": "beats", **body} return new_id(api("POST", "/sequences", body)) seqs = { "Rows chase": sequence( "Rows chase", targets=row_targets, lanes=[[{"preset_id": P["Ripple"], "beats": 0.5}] * rows], lane_opts=[{"targets": list(range(rows)), "chase": {"order": "bounce", "width": 1}}], ), "Ping-pong": sequence( "Ping-pong", targets=[{"name": "Left", "group_ids": [g["Left"]]}, {"name": "Right", "group_ids": [g["Right"]]}], lanes=[[{"preset_id": P["Sweep →"], "beats": 1, "targets": [0], "others": "off"}, {"preset_id": P["Wipe ←"], "beats": 1, "targets": [1], "others": "off"}]], lane_opts=[{"targets": [0, 1]}], ), "Canon": sequence( "Canon", targets=[{"name": f"L{r + 1}", "devices": [m for m, row, c in bars if row == r and c == 0]} for r in range(rows)] + [{"name": f"R{r + 1}", "devices": [m for m, row, c in bars if row == r and c == 1]} for r in range(rows)], lanes=[[{"preset_id": P["Hit"], "beats": 0.5}] * rows, [{"preset_id": P["Hit"], "beats": 0.5}] * rows], lane_opts=[{"targets": list(range(rows)), "chase": {"order": "forward"}}, {"targets": list(range(rows, 2 * rows)), "chase": {"order": "forward"}, "offset": 2}], ), "Looks": sequence( "Looks", group_ids=[g["Rig"]], lanes=[[{"preset_id": P[k], "beats": 4} for k in ("Ripple", "Sweep →", "Radar", "Grow out", "Plasma", "Pinwheel", "Swirl", "Sweep ↑")]], ), } anims = { "Flash": sequence("Flash", kind="animation", group_ids=[g["Rig"]], lanes=[[{"preset_id": P["Hit"], "beats": 1}]]), "Rows drop": sequence( "Rows drop", kind="animation", targets=row_targets, lanes=[[{"preset_id": P["White"], "beats": 0.25}] * rows], lane_opts=[{"targets": list(range(rows)), "chase": {"order": "forward", "width": 1}}], ), "Burst": sequence("Burst", kind="animation", group_ids=[g["Rig"]], lanes=[[{"preset_id": P["Ripple in"], "beats": 1}, {"preset_id": P["Ripple"], "beats": 1}]]), } names = list(P) zone = new_id(api("POST", "/zones", { "name": "Bridge", "group_ids": [g["Rig"]], "presets": [[P[n] for n in names[i:i + 6]] for i in range(0, len(names), 6)], "sequence_ids": list(seqs.values()), "animation_ids": list(anims.values()), })) api("POST", f"/zones/{zone}/set-current") api("POST", f"/zones/{zone}/play", {"preset_id": P["Ripple"]}) print(f"profile {pid} '{PROFILE}', zone {zone} 'Bridge': {len(g)} groups, {len(P)} presets, " f"{len(seqs)} sequences ({', '.join(seqs)}), {len(anims)} animations ({', '.join(anims)}). Playing Ripple.") if __name__ == "__main__": main()