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>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-16 19:32:46 +13:00
1 parent 698c1463c0
commit 581b751904
506 files changed
+42336 -3599

No files matched your search

+83 -62
View File
@@ -1,84 +1,105 @@
/* One simulated led-driver: Pyodide running led-driver/src/main.py.
/* One simulated led-driver: the C++ firmware (led-driver/main) built to WebAssembly (sim-driver/ in led-controller).
*
* The page posts {t: 'boot', ...} once. The worker loads Pyodide, writes the
* strip's flash (driver source, patterns, settings.json, presets.json) and the
* stubs into the Pyodide filesystem, then runs web/driver_boot.py, which never
* returns while the driver is powered. Power-off is Worker.terminate().
* 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().
*
* Messages to the page:
* 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
* {t:'flash', path, data} settings.json / presets.json changed
* {t:'exit', error, reset} main.py stopped (reset: machine.reset())
* {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())
*/
/* global importScripts, loadPyodide, SimRing */
importScripts('/web/ring.js');
import LedDriver from './wasm/led_driver.mjs';
let ring = null;
let sleepView = null;
const FRAME_CAP = 512 * 3; // FX_LEDS_MAX
const TICK_MS = 4;
const RX_CAP = 6 + 250;
self.simRecv = () => (ring ? ring.pop() : undefined);
self.simPending = () => (ring ? ring.count() : 0);
self.simTx = (u8) => self.postMessage({ t: 'tx', data: u8 }, [u8.buffer]);
self.simPixels = (u8) => self.postMessage({ t: 'px', data: u8 }, [u8.buffer]);
self.simFlash = (path, text) =>
self.postMessage({ t: 'flash', path: String(path), data: String(text) });
self.simSleep = (ms) => {
if (ms > 0 && sleepView) {
// Nobody notifies this slot: it is a plain blocking sleep.
Atomics.wait(sleepView, 0, 0, ms);
}
};
let mod = null;
let rxPtr = 0;
function log(text) {
self.postMessage({ t: 'log', text: String(text) });
}
function writeTree(FS, root, files) {
for (const [rel, body] of Object.entries(files)) {
const path = `${root}/${String(rel).replace(/^\/+/, '')}`;
const dir = path.slice(0, path.lastIndexOf('/'));
if (dir) {
FS.mkdirTree(dir);
}
FS.writeFile(path, String(body));
}
function exit(error, reset = false) {
self.postMessage({ t: 'exit', error: String(error), reset });
}
self.onmessage = async (event) => {
const m = event.data || {};
if (m.t !== 'boot') {
return;
}
ring = new SimRing(m.ringSab);
sleepView = new Int32Array(m.sleepSab);
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 {
importScripts(`${m.pyodideUrl}pyodide.js`);
const py = await loadPyodide({
indexURL: m.pyodideUrl,
stdout: log,
stderr: log,
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}`),
});
const FS = py.FS;
writeTree(FS, '/flash', m.files.driver);
writeTree(FS, '/flash', m.flash);
writeTree(FS, '/stubs', m.files.stubs);
FS.writeFile('/boot.py', m.bootPy);
py.globals.set('__sim_env', py.toPy(m.env));
py.runPython('import os\nos.environ.update({str(k): str(v) for k, v in __sim_env.items()})\ndel __sim_env');
self.postMessage({ t: 'state', state: 'running' });
// Blocks until main.py exits (it normally never does).
py.runPython('import runpy\nrunpy.run_path("/boot.py", run_name="__boot__")');
self.postMessage({ t: 'exit', error: 'main.py returned', reset: false });
} catch (err) {
const msg = String((err && err.message) || err);
const reset = /SystemExit:\s*75\b/.test(msg);
if (!reset) {
log(msg);
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));
}
self.postMessage({ t: 'exit', error: reset ? 'machine.reset()' : msg, reset });
}, 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);
}
};