feat: DDP frame sync over UDP broadcast, DDP acks, DDP enable setting

- Driver acks each completed DDP frame; the test waits for the acks before broadcasting the sync.
- Sync listener on UDP 4049: a frame is held until the next sync, then shown.
- DDP pixels are shown at full brightness, ignoring the device brightness settings.
- "DDP and sync receive on" setting (NVS, network API, web UI); off ignores DDP and sync packets.
- ddp_test.py: finds devices on the subnet, drives them in lockstep at a fixed rate, runs animations.
- /api/ddp reports sync counters for diagnosis.
- Includes in-progress WebSocket, multi-driver web UI and Wi-Fi work from the same tree.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-11 14:10:55 +13:00
1 parent 47f4eba0d6
commit 4cd3bc7973
294 files changed
+48954 -301

No files matched your search

+102 -22
View File
@@ -17,31 +17,88 @@
return direct(path, opts);
}
// This device's own API
async function direct(path, opts) {
const r = await fetch(path, opts);
const j = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(j.error || r.statusText);
return j;
// The page talks to this device over one WebSocket (/ws). Replies come back in the order the
// requests were sent, so each reply belongs to the oldest request still waiting.
const WS_TIMEOUT_MS = 15000;
let sock = null;
const waiting = []; // {text, resolve, reject}, oldest first
let watchdog = null;
function kick() {
clearTimeout(watchdog);
watchdog = waiting.length ? setTimeout(() => closeSock(new Error('The device did not answer.')), WS_TIMEOUT_MS) : null;
}
// Another device: this driver makes the call (POST /api/relay), so the browser only talks to this one.
async function relayTo(host, path, opts) {
const method = (opts && opts.method) || 'GET';
const body = opts && typeof opts.body === 'string' ? opts.body : '';
const r = await fetch('/api/relay', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ host, method, path, body }),
function closeSock(err) {
const s = sock;
sock = null;
clearTimeout(watchdog);
watchdog = null;
if (s) {
s.onopen = s.onmessage = s.onclose = s.onerror = null;
try { s.close(); } catch (_) { /* already closed */ }
}
while (waiting.length) waiting.shift().reject(err);
}
function openSock() {
const s = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/ws');
sock = s;
s.onopen = () => {
if (sock === s) for (const w of waiting) s.send(w.text);
};
s.onmessage = (ev) => {
const w = waiting.shift();
kick();
if (!w) return;
try { w.resolve(JSON.parse(ev.data)); } catch (e) { w.reject(e); }
};
s.onclose = () => { if (sock === s) closeSock(new Error('Lost the connection to the device.')); };
s.onerror = () => {}; // onclose follows
}
// One request over the WebSocket; resolves to the reply envelope {status, body}.
function wsCall(msg) {
return new Promise((resolve, reject) => {
const text = JSON.stringify(msg);
waiting.push({ text, resolve, reject });
if (!sock) openSock();
else if (sock.readyState === WebSocket.OPEN) sock.send(text);
kick();
});
const env = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(env.error || r.statusText);
}
// An API call. host is another driver's address; it is left out for this device.
async function wsApi(method, path, body, host) {
const msg = { method, path, body };
if (host) msg.host = host;
const env = await wsCall(msg);
let j = {};
try { j = JSON.parse(env.body || '{}'); } catch (_) { j = {}; }
if (env.status < 200 || env.status >= 300) throw new Error(j.error || 'HTTP ' + env.status);
return j;
}
// This device's own API, over the WebSocket. Firmware upload and info stay on plain HTTP (/ota).
async function direct(path, opts) {
if (path.startsWith('/ota')) {
const r = await fetch(path, opts);
const j = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(j.error || r.statusText);
return j;
}
const method = (opts && opts.method) || 'GET';
const body = opts && typeof opts.body === 'string' ? opts.body : '';
return wsApi(method, path, body);
}
// Another device: this driver makes the call, so the browser only talks to this one.
async function relayTo(host, path, opts) {
const method = (opts && opts.method) || 'GET';
const body = opts && typeof opts.body === 'string' ? opts.body : '';
return wsApi(method, path, body, host);
}
// All devices: the same change goes to every device at once. Reads come from this device.
async function fanOut(path, opts) {
const method = (opts && opts.method) || 'GET';
@@ -513,22 +570,30 @@
status.patterns.forEach((name) => $('pattern').add(new Option(name, name)));
}
// One brightness slider. With one device selected it sets that device's own level.
// With All devices selected it is a master scale: each device's level is multiplied by it, so
// the devices keep their own levels relative to each other.
const brightKey = () => (base === ALL ? 'master' : 'brightness');
let globalTimer;
$('globalBright').addEventListener('input', () => {
$('globalOut').textContent = $('globalBright').value;
clearTimeout(globalTimer);
globalTimer = setTimeout(() => {
post('/api/brightness', { brightness: Number($('globalBright').value) }).catch((e) => {
post('/api/brightness', { [brightKey()]: Number($('globalBright').value) }).catch((e) => {
$('status').textContent = e.message;
});
}, 150);
});
async function loadStatus() {
const host = base;
const s = await api('/api/status');
// Another device may have been selected while this answer was on its way: drop it
if (host !== base) return;
if (document.activeElement !== $('globalBright')) {
$('globalBright').value = s.global_brightness;
$('globalOut').textContent = s.global_brightness;
const level = base === ALL ? s.master_brightness : s.global_brightness;
$('globalBright').value = level;
$('globalOut').textContent = level;
}
$('apName').textContent = s.ap_ssid;
$('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected';
@@ -822,6 +887,8 @@
$('sMask').value = n.netmask;
$('sGw').value = n.gateway;
$('sDns').value = n.dns;
$('apOn').checked = n.ap_enabled !== false;
$('ddpOn').checked = n.ddp_enabled !== false;
updateStaticFields();
}
@@ -873,6 +940,8 @@
netmask: $('sMask').value.trim(),
gateway: $('sGw').value.trim(),
dns: $('sDns').value.trim(),
ap_enabled: $('apOn').checked,
ddp_enabled: $('ddpOn').checked,
};
$('netMsg').textContent = 'Saving…';
try {
@@ -933,9 +1002,7 @@
async function discoverDevices() {
try {
// Both asked of this device, which is the one the page came from
const [dr, sr] = await Promise.all([fetch('/api/devices'), fetch('/api/status')]);
const j = await dr.json();
const st = await sr.json();
const [j, st] = await Promise.all([direct('/api/devices'), direct('/api/status')]);
const next = Array.isArray(j.devices) ? j.devices.filter((d) => d && d.host) : [];
const name = typeof st.hostname === 'string' ? st.hostname : '';
if (JSON.stringify(next) !== JSON.stringify(found) || name !== localName) {
@@ -1096,6 +1163,19 @@
setMenu(false);
});
});
// Settings is split into sections: one visible at a time
function showSub(id) {
document.querySelectorAll('.subtab').forEach((b) => {
const on = b.dataset.sub === id;
b.classList.toggle('active', on);
b.setAttribute('aria-selected', String(on));
});
document.querySelectorAll('.subpage').forEach((p) => { p.hidden = p.dataset.sub !== id; });
}
document.querySelectorAll('.subtab').forEach((btn) => {
btn.addEventListener('click', () => showSub(btn.dataset.sub));
});
$('modeToggle').addEventListener('click', () => setMode(isEditMode() ? 'run' : 'edit'));
$('newPreset').addEventListener('click', () => setMenu(false));
applyMode();
+26 -1
View File
@@ -35,6 +35,15 @@
</section>
<section id="settings" hidden>
<div class="subnav" role="tablist" aria-label="Settings sections">
<button type="button" class="subtab active" data-sub="devices" role="tab">Devices</button>
<button type="button" class="subtab" data-sub="presets" role="tab">Presets</button>
<button type="button" class="subtab" data-sub="led" role="tab">LED strip</button>
<button type="button" class="subtab" data-sub="network" role="tab">Network</button>
<button type="button" class="subtab" data-sub="firmware" role="tab">Firmware</button>
</div>
<div class="subpage" data-sub="devices">
<h2>Devices</h2>
<p class="muted">Control other LED drivers from this page. Add each one by its address, such as 10.1.1.50 or led-desk.local. The list is kept in this browser.</p>
<ul id="devList" class="list"></ul>
@@ -46,6 +55,9 @@
</form>
<p id="devMsg" class="muted" role="status"></p>
</div>
<div class="subpage" data-sub="presets" hidden>
<h2>Presets</h2>
<p class="muted">Adds the default presets (Rainbow, Chase, Aurora, Candle, Twinkle, Flame, Sweep, Off) that are not in the list yet. Your own presets are kept.</p>
<button id="addDefaults" type="button">Add default presets</button>
@@ -56,6 +68,9 @@
<button id="syncPresets" type="button">Sync presets to other devices</button>
<p id="syncMsg" class="muted" role="status"></p>
</div>
<div class="subpage" data-sub="led" hidden>
<h2>LED strip</h2>
<form id="hwForm">
<label>Number of LEDs
@@ -82,6 +97,9 @@
</form>
<p id="hwMsg" class="muted" role="status"></p>
</div>
<div class="subpage" data-sub="firmware" hidden>
<h2>Firmware</h2>
<p>Running <span id="fwInfo">…</span></p>
<form id="fwForm">
@@ -95,6 +113,9 @@
</div>
<p id="fwMsg" class="muted" role="status"></p>
</div>
<div class="subpage" data-sub="network" hidden>
<h2>Access point</h2>
<p>Network <strong id="apName">…</strong> (open). Address <strong>192.168.4.1</strong>.</p>
@@ -139,10 +160,14 @@
<input id="sDns" inputmode="decimal" autocomplete="off">
</label>
</div>
<label class="check"><input type="checkbox" id="apOn"> Access point on</label>
<label class="check"><input type="checkbox" id="ddpOn"> DDP and sync receive on</label>
<p class="muted">Turn it off to use only your Wi-Fi network. Turning it off needs a connected Wi-Fi network. If the network stays down for about a minute, the access point comes back on by itself.</p>
<button type="submit">Save network settings</button>
</form>
<p id="netMsg" class="muted" role="status"></p>
<p class="muted">The access point always stays at 192.168.4.1, so you can get back in if a setting breaks the client connection.</p>
<p class="muted">While the access point is on it stays at 192.168.4.1, so you can get back in if a setting breaks the client connection.</p>
</div>
</section>
</main>
+23
View File
@@ -304,6 +304,28 @@ button.tab-action {
button.tab-action:hover { background-color: #7b6bdb; }
/* Settings sections: a second row of tabs inside Settings */
.subnav {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
margin: 1rem 0 0.5rem;
}
button.subtab {
padding: 0.35rem 0.75rem;
border: none;
border-radius: 4px;
background-color: #3a3a3a;
color: white;
font-size: 0.85rem;
font-weight: 500;
cursor: pointer;
}
button.subtab:hover { background-color: #4a4a4a; }
button.subtab.active { background-color: #6a5acd; }
.preset-tile-name {
font-weight: 600;
font-size: 0.9rem;
@@ -517,6 +539,7 @@ button.menu-toggle {
/* Bigger touch targets */
button,
button.tab,
button.subtab,
button.tab-action,
button.preset-edit {
min-height: 2.75rem;