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:
1 parent
47f4eba0d6
commit
4cd3bc7973
294 files changed
+48954
-301
No files matched your search
+102
-22
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user