/* Pattern gallery: every led-driver pattern side by side, from the C++ engine built to WebAssembly * (wasm/led_fx.mjs: led-driver/main/patterns.cpp and the effects, no threads). * * One module instance per pattern, so each has its own engine state; the wasm is compiled once and shared. * Each instance has its own clock and runs patterns_frame() every 10 ms of it (FRAME_MS on the board). */ import LedFx from './wasm/led_fx.mjs'; const FRAME_MS = 10; const MAX_STEPS = 25; // per animation frame, after the tab was in the background const LED_CAP = 512; const $ = (id) => document.getElementById(id); const state = { leds: 119, speed: 1, paused: false, cards: [] }; function banner(text) { const el = $('banner'); el.textContent = text; el.hidden = !text; } function hex(c) { return `#${c.map((v) => Number(v).toString(16).padStart(2, '0')).join('')}`; } function rgb(h) { const v = parseInt(h.slice(1), 16); return [(v >> 16) & 255, (v >> 8) & 255, v & 255]; } async function instantiate(wasmModule) { return LedFx({ print: () => {}, printErr: (t) => console.warn('[fx]', t), instantiateWasm(imports, receive) { WebAssembly.instantiate(wasmModule, imports).then((inst) => receive(inst, wasmModule)); return {}; }, }); } function presetJson(card) { return JSON.stringify(card.preset); } function apply(card, restart) { const p = card.mod.stringToNewUTF8(presetJson(card)); const ok = card.mod._fx_select_json(p, restart ? 1 : 0); card.mod._free(p); if (restart) { card.clock = 0; card.next = 0; } return ok; } function numField(card, key, label, opts = {}) { const l = document.createElement('label'); if (opts.cls) l.className = opts.cls; l.textContent = label; const i = document.createElement('input'); i.type = 'number'; i.step = opts.step ?? 1; if (opts.min !== undefined) i.min = opts.min; if (opts.max !== undefined) i.max = opts.max; i.value = card.preset[key] ?? 0; i.onchange = () => { const v = Number(i.value); if (Number.isFinite(v)) { card.preset[key] = Math.round(v); apply(card, opts.restart); } }; l.append(i); return l; } function checkField(card, key, label, restart) { const l = document.createElement('label'); l.className = 'check'; const i = document.createElement('input'); i.type = 'checkbox'; i.checked = Boolean(card.preset[key]); i.onchange = () => { card.preset[key] = i.checked; apply(card, restart); }; l.append(i, label); return l; } function colourList(card) { const l = document.createElement('label'); l.className = 'full'; l.textContent = 'Colours'; const box = document.createElement('div'); box.className = 'colours'; const render = () => { const items = card.preset.colors.map((c, k) => { const i = document.createElement('input'); i.type = 'color'; i.value = hex(c); i.title = 'Click to change; right-click to remove'; i.oninput = () => { card.preset.colors[k] = rgb(i.value); apply(card, false); }; i.oncontextmenu = (ev) => { ev.preventDefault(); card.preset.colors.splice(k, 1); apply(card, false); render(); }; return i; }); const add = document.createElement('button'); add.type = 'button'; add.className = 'secondary'; add.textContent = '+'; add.title = 'Add a colour'; add.disabled = card.preset.colors.length >= 8; add.onclick = () => { const last = card.preset.colors[card.preset.colors.length - 1]; card.preset.colors.push(last ? [last[2], last[0], last[1]] : [255, 0, 0]); apply(card, false); render(); }; const clear = document.createElement('button'); clear.type = 'button'; clear.className = 'link'; clear.textContent = card.preset.colors.length ? 'use defaults' : 'pattern defaults'; clear.disabled = !card.preset.colors.length; clear.onclick = () => { card.preset.colors = []; apply(card, false); render(); }; box.replaceChildren(...items, add, clear); }; render(); l.append(box); return l; } function bgField(card) { const l = document.createElement('label'); l.textContent = 'Background'; const i = document.createElement('input'); i.type = 'color'; i.value = hex(card.preset.background || [0, 0, 0]); i.oninput = () => { card.preset.background = rgb(i.value); apply(card, false); }; l.append(i); return l; } function buildCard(mod, index) { mod._fx_init(state.leds); const name = mod.UTF8ToString(mod._fx_name(index)); const ptr = mod._fx_defaults_json(index); const preset = JSON.parse(mod.UTF8ToString(ptr)); mod._free(ptr); preset.n5 = preset.n5 ?? 0; const el = document.createElement('section'); el.className = 'card'; el.innerHTML = '