// LED strip web UI. Talks to the JSON API in main/web.cpp. (() => { const $ = (id) => document.getElementById(id); async function api(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; } const post = (path, body) => api(path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); const toHex = (rgb) => '#' + rgb.map((v) => v.toString(16).padStart(2, '0')).join(''); const fromHex = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16)); function updateOutputs() { $('speedOut').textContent = $('speed').value + ' ms'; $('brightOut').textContent = $('bright').value; } // Coalesce slider drags into one request let sendTimer; function sendPattern() { updateOutputs(); clearTimeout(sendTimer); sendTimer = setTimeout(async () => { try { await post('/api/pattern', { mode: $('mode').value, c1: fromHex($('c1').value), c2: fromHex($('c2').value), speed_ms: Number($('speed').value), brightness: Number($('bright').value), }); $('status').textContent = 'Applied'; } catch (e) { $('status').textContent = e.message; } }, 150); } async function loadStatus() { const s = await api('/api/status'); $('apName').textContent = s.ap_ssid; $('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected'; if (!$('mode').options.length) { s.modes.forEach((m) => $('mode').add(new Option(m, m))); } $('mode').value = s.mode; $('c1').value = toHex(s.c1); $('c2').value = toHex(s.c2); $('speed').value = s.speed_ms; $('bright').value = s.brightness; updateOutputs(); } async function loadWifi() { const w = await api('/api/wifi'); if (w.ssid && !$('ssid').value) $('ssid').value = w.ssid; } async function scan() { const list = $('networks'); list.textContent = 'Scanning…'; try { const { networks } = await api('/api/wifi/scan'); list.textContent = ''; networks.sort((a, b) => b.rssi - a.rssi).forEach((n) => { const li = document.createElement('li'); const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'net'; btn.textContent = `${n.ssid || '(hidden)'} ${n.rssi} dBm${n.open ? '' : ' 🔒'}`; btn.addEventListener('click', () => { $('ssid').value = n.ssid; $('pass').focus(); }); li.appendChild(btn); list.appendChild(li); }); if (!networks.length) list.textContent = 'No networks found.'; } catch (e) { list.textContent = e.message; } } // Tabs document.querySelectorAll('.tab').forEach((btn) => { btn.addEventListener('click', () => { document.querySelectorAll('.tab').forEach((b) => b.classList.toggle('active', b === btn)); $('patterns').hidden = btn.dataset.tab !== 'patterns'; $('settings').hidden = btn.dataset.tab !== 'settings'; }); }); ['mode', 'c1', 'c2', 'speed', 'bright'].forEach((id) => { $(id).addEventListener('input', sendPattern); $(id).addEventListener('change', sendPattern); }); $('scan').addEventListener('click', scan); $('wifiForm').addEventListener('submit', async (ev) => { ev.preventDefault(); $('wifiMsg').textContent = 'Saving…'; try { const j = await post('/api/wifi', { ssid: $('ssid').value, password: $('pass').value }); $('wifiMsg').textContent = j.message || 'Saved'; $('pass').value = ''; setTimeout(() => loadStatus().catch(() => {}), 4000); } catch (e) { $('wifiMsg').textContent = e.message; } }); loadStatus().catch((e) => { $('status').textContent = e.message; }); loadWifi().catch(() => {}); setInterval(() => loadStatus().catch(() => {}), 5000); })();