Files
jimmyandClaude Opus 5.5 581b751904 feat: browser simulator served by the controller, desktop build, browser bridge
- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:46 +13:00

106 lines
3.4 KiB
JavaScript

/* One simulated led-driver: the C++ firmware (led-driver/main) built to WebAssembly (sim-driver/ in led-controller).
*
* A module worker. The page posts {t: 'boot', mac, nvs, fps} once; the worker instantiates
* wasm/led_driver.mjs and calls sim_boot. There are no threads (so no SharedArrayBuffer, and the page works
* on any address): every TICK_MS the worker calls sim_tick, which runs what the board's two tasks run, a pass
* of the ESP-NOW link and, every 10 ms, a pattern frame. Packets arrive as plain messages between ticks.
* Power-off is Worker.terminate().
*
* Page -> worker:
* {t:'boot', mac, nvs, fps} mac: 12 hex digits; nvs: the strip's flash (JSON text)
* {t:'rx', data} Uint8Array src_mac(6) + ESP-NOW packet
* Worker -> page:
* {t:'state', state} loading | running
* {t:'log', text} driver serial output (one line)
* {t:'tx', data} Uint8Array dest_mac(6) + ESP-NOW packet
* {t:'px', data} Uint8Array RGB frame, pixel 0 first
* {t:'flash', data} the NVS store after a commit (JSON text)
* {t:'exit', error, reset} the driver stopped (reset: esp_restart())
*/
import LedDriver from './wasm/led_driver.mjs';
const FRAME_CAP = 512 * 3; // FX_LEDS_MAX
const TICK_MS = 4;
const RX_CAP = 6 + 250;
let mod = null;
let rxPtr = 0;
function log(text) {
self.postMessage({ t: 'log', text: String(text) });
}
function exit(error, reset = false) {
self.postMessage({ t: 'exit', error: String(error), reset });
}
function startPixels(fps) {
const ptr = mod._malloc(FRAME_CAP);
let last = null;
setInterval(() => {
const n = mod._sim_frame(ptr, FRAME_CAP);
const frame = mod.HEAPU8.slice(ptr, ptr + n * 3);
if (last && last.length === frame.length && frame.every((v, i) => v === last[i])) {
return; // unchanged: nothing to redraw
}
last = frame;
const out = frame.slice();
self.postMessage({ t: 'px', data: out }, [out.buffer]);
}, 1000 / Math.max(1, Math.min(60, Number(fps) || 30)));
}
async function boot(m) {
self.postMessage({ t: 'state', state: 'loading' });
try {
mod = await LedDriver({
print: log,
printErr: log,
simTx: (u8) => self.postMessage({ t: 'tx', data: u8 }, [u8.buffer]),
simFlash: (json) => self.postMessage({ t: 'flash', data: json }),
simRestart: () => exit('esp_restart()', true),
onAbort: (what) => exit(`abort: ${what}`),
});
} catch (err) {
log(String((err && err.stack) || err));
exit(`load failed: ${err}`);
return;
}
const mac = mod.stringToNewUTF8(String(m.mac));
const nvs = mod.stringToNewUTF8(String(m.nvs || '{}'));
let rc;
try {
rc = mod._sim_boot(mac, nvs);
} catch (err) {
log(String((err && err.stack) || err));
exit(String(err));
return;
} finally {
mod._free(mac);
mod._free(nvs);
}
if (rc !== 0) {
exit(`sim_boot returned ${rc}`);
return;
}
rxPtr = mod._malloc(RX_CAP);
setInterval(() => {
try {
mod._sim_tick();
} catch (err) {
exit(String((err && err.message) || err));
}
}, TICK_MS);
startPixels(m.fps);
self.postMessage({ t: 'state', state: 'running' });
}
self.onmessage = (event) => {
const m = event.data || {};
if (m.t === 'boot' && !mod) {
boot(m);
} else if (m.t === 'rx' && rxPtr && m.data && m.data.length <= RX_CAP) {
mod.HEAPU8.set(m.data, rxPtr);
mod._sim_rx(rxPtr, m.data.length);
}
};