chore: move led-simulator in from its submodule

Merges led-simulator (sim-bridge, 4d8aeb5) under led-simulator/ with its history,
in place of the submodule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-16 15:08:49 +13:00
commit 698c1463c0
41 files changed
+4369 -4

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
/* One simulated led-driver: Pyodide running led-driver/src/main.py.
*
* 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().
*
* Messages to the 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())
*/
/* global importScripts, loadPyodide, SimRing */
importScripts('/web/ring.js');
let ring = null;
let sleepView = null;
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);
}
};
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));
}
}
self.onmessage = async (event) => {
const m = event.data || {};
if (m.t !== 'boot') {
return;
}
ring = new SimRing(m.ringSab);
sleepView = new Int32Array(m.sleepSab);
self.postMessage({ t: 'state', state: 'loading' });
try {
importScripts(`${m.pyodideUrl}pyodide.js`);
const py = await loadPyodide({
indexURL: m.pyodideUrl,
stdout: log,
stderr: log,
});
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);
}
self.postMessage({ t: 'exit', error: reset ? 'machine.reset()' : msg, reset });
}
};