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:
commit
698c1463c0
41 files changed
+4369
-4
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/* SharedArrayBuffer message ring: the page pushes received ESP-NOW frames,
|
||||
* the driver worker pops them while main.py runs (it never yields to the JS
|
||||
* event loop, so postMessage cannot reach it).
|
||||
*
|
||||
* Layout: Int32[4] header {head, tail, dropped, count} then `capacity` bytes.
|
||||
* Each message is a little-endian u16 length followed by the bytes.
|
||||
* One producer (page) and one consumer (worker).
|
||||
*
|
||||
* Loaded as a classic script by both the page and the worker.
|
||||
*/
|
||||
(function (g) {
|
||||
'use strict';
|
||||
const HEADER = 16;
|
||||
const HEAD = 0;
|
||||
const TAIL = 1;
|
||||
const DROPPED = 2;
|
||||
const COUNT = 3;
|
||||
|
||||
class SimRing {
|
||||
constructor(sab) {
|
||||
this.sab = sab;
|
||||
this.idx = new Int32Array(sab, 0, 4);
|
||||
this.cap = sab.byteLength - HEADER;
|
||||
this.data = new Uint8Array(sab, HEADER, this.cap);
|
||||
}
|
||||
|
||||
static create(capacity = 65536) {
|
||||
return new SimRing(new SharedArrayBuffer(HEADER + capacity));
|
||||
}
|
||||
|
||||
used() {
|
||||
const h = Atomics.load(this.idx, HEAD);
|
||||
const t = Atomics.load(this.idx, TAIL);
|
||||
return (h - t + this.cap) % this.cap;
|
||||
}
|
||||
|
||||
count() {
|
||||
return Atomics.load(this.idx, COUNT);
|
||||
}
|
||||
|
||||
dropped() {
|
||||
return Atomics.load(this.idx, DROPPED);
|
||||
}
|
||||
|
||||
/** Producer: false (and counted as dropped) when the ring is full. */
|
||||
push(bytes) {
|
||||
const n = bytes.length;
|
||||
if (n > 0xffff || n + 2 >= this.cap - this.used()) {
|
||||
Atomics.add(this.idx, DROPPED, 1);
|
||||
return false;
|
||||
}
|
||||
const cap = this.cap;
|
||||
const data = this.data;
|
||||
let h = Atomics.load(this.idx, HEAD);
|
||||
data[h] = n & 0xff;
|
||||
h = (h + 1) % cap;
|
||||
data[h] = n >> 8;
|
||||
h = (h + 1) % cap;
|
||||
for (let i = 0; i < n; i += 1) {
|
||||
data[h] = bytes[i];
|
||||
h = (h + 1) % cap;
|
||||
}
|
||||
Atomics.store(this.idx, HEAD, h);
|
||||
Atomics.add(this.idx, COUNT, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Consumer: next message as a Uint8Array, or undefined when empty. */
|
||||
pop() {
|
||||
let t = Atomics.load(this.idx, TAIL);
|
||||
if (t === Atomics.load(this.idx, HEAD)) {
|
||||
return undefined;
|
||||
}
|
||||
const cap = this.cap;
|
||||
const data = this.data;
|
||||
const lo = data[t];
|
||||
t = (t + 1) % cap;
|
||||
const hi = data[t];
|
||||
t = (t + 1) % cap;
|
||||
const n = lo | (hi << 8);
|
||||
const out = new Uint8Array(n);
|
||||
for (let i = 0; i < n; i += 1) {
|
||||
out[i] = data[t];
|
||||
t = (t + 1) % cap;
|
||||
}
|
||||
Atomics.store(this.idx, TAIL, t);
|
||||
Atomics.sub(this.idx, COUNT, 1);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
g.SimRing = SimRing;
|
||||
})(typeof self !== 'undefined' ? self : globalThis);
|
||||
Reference in new issue
Block a user