"""Physical strip layout for led-simulator (server side; the page draws it in web/layout.js). The default rig is 16 strips of 119 LEDs in 2 columns x 8 rows. Each strip runs along its row, and pixel 0 sits next to the centre line between the columns: left-column strips run right-to-left, right-column strips run left-to-right. """ from __future__ import annotations from dataclasses import dataclass DEFAULT_LAYOUT = { "rows": 8, "columns": 2, "leds_per_strip": 119, # "row": strip 1 = row 1 left, strip 2 = row 1 right, strip 3 = row 2 left, ... # "column": strips 1-8 = left column top to bottom, 9-16 = right column. "order": "row", # "center": pixel 0 next to the middle of the columns (default rig). # "left": every strip starts at its left end. "right": at its right end. "start": "center", } @dataclass(frozen=True) class StripPlace: index: int # 0-based strip number row: int # 0-based, top row first col: int # 0-based, left column first leds: int reversed: bool # True: pixel 0 is at the strip's right end @property def number(self) -> int: return self.index + 1 def normalise_layout(raw: dict | None) -> dict: lay = dict(DEFAULT_LAYOUT) if isinstance(raw, dict): lay.update({k: v for k, v in raw.items() if v is not None}) lay["rows"] = max(1, int(lay["rows"])) lay["columns"] = max(1, int(lay["columns"])) lay["leds_per_strip"] = max(1, min(2048, int(lay["leds_per_strip"]))) lay["order"] = "column" if str(lay["order"]).lower() == "column" else "row" start = str(lay["start"]).lower() lay["start"] = start if start in ("center", "left", "right") else "center" return lay def strip_count(layout: dict) -> int: lay = normalise_layout(layout) return lay["rows"] * lay["columns"] def _reversed_for_column(col: int, columns: int, start: str) -> bool: if start == "left": return False if start == "right": return True # centre-out: columns left of the middle run leftwards from their right end. # With an odd column count the middle column runs left-to-right. return col < columns // 2 def strip_places(layout: dict) -> list[StripPlace]: lay = normalise_layout(layout) rows, cols = lay["rows"], lay["columns"] out = [] for i in range(rows * cols): if lay["order"] == "column": row, col = i % rows, i // rows else: row, col = i // cols, i % cols out.append( StripPlace( index=i, row=row, col=col, leds=lay["leds_per_strip"], reversed=_reversed_for_column(col, cols, lay["start"]), ) ) return out def side_name(col: int, columns: int) -> str: if columns == 2: return "left" if col == 0 else "right" return "c%d" % (col + 1) # Rig coordinates for led-controller's rig layout (POST /devices/layout): the same 1000 x 700 canvas as its # rig-layout editor, y up. Each strip runs along its row inside its column; x0,y0 is pixel 0's end. RIG_W = 1000 RIG_H = 700 RIG_GAP = 20 # space between a strip's end and its column's edge def rig_placements(layout: dict) -> tuple[dict[int, dict], dict]: """({strip index: {x0, y0, x1, y1}}, centre {x, y}) for the controller's rig layout.""" lay = normalise_layout(layout) rows, cols = lay["rows"], lay["columns"] col_w = RIG_W / cols row_h = RIG_H / rows out = {} for p in strip_places(lay): y = round(RIG_H - (p.row + 0.5) * row_h, 1) # row 1 at the top left = round(p.col * col_w + RIG_GAP, 1) right = round((p.col + 1) * col_w - RIG_GAP, 1) x0, x1 = (right, left) if p.reversed else (left, right) out[p.index] = {"x0": x0, "y0": y, "x1": x1, "y1": y} return out, {"x": RIG_W / 2, "y": RIG_H / 2}