# led-simulator A whole LED rig in the browser, served by **led-controller itself**. The host build of the controller (`sim-controller`, and the desktop build) serves the rig at **`/sim/`**: - Every LED bar is its own led-driver: the real **C++ firmware** (`led-driver/main`) built to **WebAssembly** (`../sim-driver/`), one module worker per bar. The worker runs what the board's pattern task and ESP-NOW link task run (`patterns_frame`, `espnow_link_poll`) from a timer, so there are no threads, no `SharedArrayBuffer`, and the page works on any address. Only the hardware is replaced: NVS is kept by the controller, ESP-NOW goes through the page, and the WS2812 strip is the canvas. - The controller reaches the bars over **ESP-NOW**, through its **`browser` bridge**. The frames it would hand to a USB or network bridge board go to the page over a WebSocket (`/sim/ws`), and the page delivers them to each bar by MAC. Beats, sequences, groups and the reliable link all run as on hardware. - **Rooms** say where the bars are. One room per JSON file, edited on the page. The bars' places go to the controller's rig layout. - **`/sim/gallery`** shows every pattern side by side, from the same C++ engine, with each preset field editable. One program and one port. There is no Python server and no UDP bridge. ## Run From the repository root: ```bash make sim-web # builds the drivers (Emscripten in Docker), the controller for the host, and runs it ``` Then open **http://localhost:8080/sim/** in Chrome, Edge or Firefox. The controller's own UI is on http://localhost:8080/. Any address works, a phone on the LAN included (`http://:8080/sim/`): the drivers need no cross-origin isolation. The bars register with the controller from their announces when they power on, and the page gives the controller their places from the room. Only one tab runs the rig: opening it in a second tab powers the first one's bars off. `make desktop` embeds the rig too (after `make sim-wasm`). There, add **`browser`** to the bridges, under Settings > Network > Bridges or with the page's *Add the browser bridge* button. `make sim-web` starts the host build with `LED_SIM_BRIDGE=browser`. With no bridge configured, `browser` is the default. ## Rooms The spatial config follows the stage maps of the qlcplus simulator: one file per room, a size in metres, strip types in `definitions`, and `fixtures` keyed by id, each placed by position and heading. ```json { "name": "studio", "units": "metres", "width": 7.0, "depth": 5.0, "origin": "X=0 at the room centre, +X right; Y=0 at the front edge, +Y up the screen; headings clockwise from +Y", "definitions": { "bar-119": { "leds": 119, "pitch_m": 0.01667 } }, "fixtures": { "sim-01": { "label": "sim-01", "def": "bar-119", "mac": "02:51:4d:00:00:01", "x": -0.1, "y": 2.55, "direction": -90 } } } ``` - A **fixture** is one bar, so one simulated driver. `x`, `y` (metres) is **pixel 0**, the data-in end. The bar runs from there along **`direction`**: degrees clockwise from +Y, so `90` runs right and `-90` runs left. It is `leds × pitch_m` long. `leds` on a fixture overrides its type's count (1–512). - **`mac`** identifies the driver. Each bar's flash is kept per MAC, so a bar in two rooms (a copied room, say) is the same bar with the same presets. New bars get a MAC no room uses yet. - A fixture's **`type`** is `espnow` (the default: a bar with its own driver), `ddp` or `artnet`; see [Network fixtures](#network-fixtures). - The first run makes the `default` room: 16 bars × 119 LEDs in 2 columns × 8 rows, pixel 0 at the centre line, `sim-01` … `sim-16` = `02:51:4d:00:00:01` … `…:10`. The rig's bridge is `02:51:4d:00:00:00`. Everything is kept in the controller's store folder (`LED_SIM_DB`; `sim-controller/data` for `make sim-web`): | | | | --- | --- | | `sim/rooms/.json` | the rooms | | `sim/flash/.json` | each bar's NVS store, `{namespace: {key: [type, value]}}` | | `sim/sim.json` | the active room | On every boot a bar's `led.num_leds` is set from its room. The first boot also takes its hostname from the fixture's label. Delete `sim/flash/` to factory-reset every bar. On the page, pick the room in the header. **Edit room** changes it, and every change is saved and sent to the controller's rig layout: - Click the floor to add a bar. Drag a bar to move it, which snaps to 5 cm; hold Alt to move it freely. - Drag the circle at its far end to turn it, which snaps to 15°; hold Shift to turn it freely. Delete removes the selected bar. - The side panel edits a bar's label, type, LED count, position and heading. - **Size & strip types…** sets the room's size and adds or changes strip types. - **New room…** makes an empty room or a copy of the current one. **Delete room** removes the room's file. A bar whose LED count changes keeps running with the old count until it is power-cycled, as `num_leds` is read at boot. ### Network fixtures Besides the ESP-NOW bars, a room can hold fixtures that something sends to over the network: the controller's own Outputs, QLC+, or any DDP or Art-Net sender. The controller's sim receives them over UDP (`sim_net.c`: Art-Net on 6454, DDP on each port the active room's DDP strips use, 4048 by default) and passes each changed universe or DDP buffer to the page at up to 40 frames a second. It answers ArtPoll, so QLC+ lists the node. If QLC+ on the same machine already holds 6454, set `LED_SIM_ARTNET_PORT` to another port (`0` turns Art-Net off). | `type` | `kind` | Fields | What it is | | --- | --- | --- | --- | | `ddp` | | `def`, `leds`, `port` (4048), `offset` (first pixel), `color_order` | an LED strip fed by DDP | | `artnet` | `pixels` | `def`, `leds`, `universe`, `address` (1–512), `color_order` | an LED strip fed by Art-Net: the first universe from `address`, then 170 pixels a universe from channel 1, as the controller's Art-Net output sends them | | `artnet` | `can` | `dmx_def`, `universe`, `address`, `direction` | a par can | | `artnet` | `mover` | `dmx_def`, `universe`, `address`, `zero`, `pan_reverse` | a moving head; `zero` is its heading at pan centre | Cans and movers behave as in the qlcplus stage simulator (technicalkiwi/qlcplus `simulator/`). Their channels come from the room's **`dmx_definitions`**: `{name: {manufacturer, model, mode, channels: [name, ...]}}`, the same shape as a qlcplus `stage/map.json`. The channel names choose what each slot does: `Dimmer`, `Strobe`, `Red`, `Green`, `Blue`, `White`, `Macro`, `Speed` for cans; `Pan`, `Pan Fine`, `Tilt`, `Tilt Fine`, `Pan/Tilt Speed`, `Zoom`, `Focus`, `Lens Rotate`, `Center Brightness`, `Center Strobe`, `Red 1` … `White 7`, `Colour Macro`, `Colour Temperature`, `Static Effect`, `Dynamic Effect` for movers (`web/dmx.js`). Art-Net fixtures may not share DMX channels; the room is refused if they do. On the page, in Edit mode: - Pick the type beside **Add** (ESP-NOW bar, DDP strip, Art-Net pixel strip, DMX can, DMX mover). A click on the floor adds that type. New DDP strips start after the last one on port 4048, Art-Net pixel strips on the first free universe, and cans and movers at the first free DMX address. - The side panel edits the port and first pixel, the universe and address, the colour order, the DMX definition, and a mover's pan zero and reversal. For a can or mover it lists the channels with their live values. - **Create controller output** (DDP and Art-Net pixel strips) adds an Output to the controller aimed at the sim (`127.0.0.1`), and **Send test** runs its red, green, blue, white, black test. Outputs send from pixel 0 and channel 1, so the button needs First pixel 0 or DMX address 1. - **Size, strip types & DMX…** also lists the DMX definitions: add a preset (the qlcplus beamZ BCC145 and Small Eye 7 mover), write one (one channel name per line), import a QLC+ fixture definition (`.qxf`, one definition per mode), or import a qlcplus `stage/map.json` (its definitions, and its cans and movers placed in the room). ### API (on the controller) | | | | --- | --- | | `GET /sim/api/config` | the active room, the room list and the bridge state | | `GET /sim/api/flash/` | a bar's NVS store | | `GET /sim/api/rooms` | `{current, rooms}` | | `POST /sim/api/rooms` | `{name, width, depth, copy_from?}` makes a room | | `GET`, `PUT`, `DELETE /sim/api/rooms/` | one room: read it, replace it (validated), delete it (not the active one) | | `POST /sim/api/room` | `{name}` switches the rig to that room | | `POST /sim/api/next-mac` | `{taken: [mac, ...]}`: a MAC no room or the page uses | | `GET /sim/api/dmx[?data=1]` | what the Art-Net and DDP receiver has had: packets per universe and port, the last sender; `data=1` adds the bytes as hex | | `WS /sim/ws` | the page's radio. Down, binary: `0x00 + dst_mac + packet` (ESP-NOW), `0x01 + u16 universe + 512 bytes` (Art-Net), `0x02 + u16 port + u32 offset + RGB` (DDP), little-endian. Up: binary `src_mac + packet`; text `{"t":"flash"}` | ## How it fits together ``` led-controller (host build) ──"browser" bridge──▶ WS /sim/ws ──▶ page ──postMessage──▶ driver worker × N rel, beats, sequences ◀── src_mac+pkt ───── ◀── tx, px, flash ── (led_driver.wasm: /sim/ page + rooms (sim_rig.c) sim_tick: link + patterns) ``` | Where | What | | --- | --- | | `sim-controller/components/sim/sim_rig.c` | serves the page and the wasm from `www_dist/sim.pack`, the page's WebSocket, rooms and flash | | `sim-controller/components/sim/sim_net.c` | receives Art-Net and DDP for the network fixtures and passes them to the page | | `main/bridge.c` | the `browser` bridge kind next to UDP and serial | | `tools/build_sim_web.sh` | packs `web/` and `web/wasm/` into `www_dist/sim.pack`. It is empty when the drivers are not built, so `make desktop` works without Emscripten. | | `../sim-driver/` | the Emscripten build of the driver (`led_driver`) and the pattern engine for the gallery (`led_fx`) | | `web/app.js` | the room canvas, editor, power control, packet routing by MAC, and placements for `POST /devices/layout` | | `web/driver-worker.js` | loads `led_driver.mjs` and boots one bar | | `web/dmx.js`, `web/dmx-draw.js` | the DMX decoder and the can and mover drawing, ported from the qlcplus stage simulator | | `web/gallery.js` | one `led_fx` instance per pattern, each stepped every 10 ms of its own clock | Not simulated: the drivers' Wi-Fi, their own web UI and HTTP API, DDP to the bars themselves, sACN and USB. Bars do not hear each other's ESP-NOW. ## Headless (older) `python3 led-simulator/bridge_sim.py` ran each bar as a CPython process with the MicroPython driver (`led-driver/src`, now removed from led-driver) behind a UDP bridge, so it no longer has drivers to run. Its grid comes from `layout` in `sim.json` (`layout.py`, `rig_seed.py`).