Outputs: positions (a key steps off → a → b → off, a knob up and down, the last off before the next on), strobe (a period) and dimmer (a knob's level by a step; a dimmable module gets a byte per channel, a relay just switches); in /keypad.html and /sim.html, a key's LED on while a position is chosen or a strobe runs
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-04 18:50:37 +01:00
1 parent e82b2236b5
commit 82a728e024
7 files changed
+239 -55

No files matched your search

+51 -11
View File
@@ -54,7 +54,12 @@
<label><span>Drives</span><select id="drives"></select></label>
<fieldset id="mode-box"><legend>Mode</legend><div class="modes" id="modes"></div>
<span class="hint" id="mode-hint"></span></fieldset>
<fieldset id="positions-box"><legend>Positions — a press steps off → 1 → 2 → … → off, a knob turns up and down</legend>
<ol id="positions" style="margin: 0; padding-left: 1.5em; display: flex; flex-direction: column; gap: 6px"></ol>
<div class="actions" style="margin-top: 6px"><button type="button" id="add-position">+ Position</button></div></fieldset>
<label id="pulse-box"><span>Pulse, s</span><input id="pulse" type="number" min="0.1" step="0.1" value="1"></label>
<label id="period-box"><span>Strobe period (on + off), s</span><input id="period" type="number" min="0.2" step="0.1" value="1"></label>
<label id="step-box"><span>Dimmer step per knob click, %</span><input id="step" type="number" min="1" max="100" step="1" value="10"></label>
<label><span>In an open menu</span><select id="menu"></select></label>
<div class="row" id="led-box">
<label><span>LED</span><select id="led"></select></label>
@@ -81,7 +86,10 @@
const KNOB_ACTIONS = ["zoom", "panX", "panY", "rows"];
const MENU = ["rows", "enter", "close"];
const MODE_HINT = { toggle: "each press switches it", on: "a press switches it on", off: "a press switches it off",
hold: "on while held — off when let go or the keypad goes silent", pulse: "on for the time below" };
hold: "on while held — off when let go or the keypad goes silent", pulse: "on for the time below",
positions: "one output per position, the last off before the next on", strobe: "a press starts or stops it blinking",
dimmer: "a knob sets the level, a press switches it on or off; only a dimmable module dims, a relay just switches" };
const KNOB_MODES = ["positions", "dimmer"]; // what a turn does; the other modes take presses
let info = null, bindings = [], screenActions = [], selected = null, dirty = false;
const json = async (url, options) => {
@@ -92,7 +100,8 @@
};
const binding = (input) => bindings.find((b) => b.input === input);
const isKnob = (input) => /\.E\d+$/.test(input);
const what = (b) => (b ? b.output ? `${b.output}${outputLabel(b.output) ? ` ${outputLabel(b.output)}` : ""}` : b.action || (b.menu ? `menu: ${b.menu}` : "") : "");
const outs = (b) => b?.outputs || (b?.output ? [b.output] : []);
const what = (b) => (!b ? "" : b.outputs ? `${b.mode}: ${b.outputs.join(", ")}` : b.output ? `${b.output}${outputLabel(b.output) ? ` ${outputLabel(b.output)}` : ""}${b.mode && b.mode !== "toggle" ? ` · ${b.mode}` : ""}` : b.action || (b.menu ? `menu: ${b.menu}` : ""));
const outputLabel = (o) => info.outputs.find((x) => x.output === o)?.label || "";
function key(input, label, cls = "") {
@@ -121,22 +130,31 @@
const pkpKey = /\.K\d+$/.test(selected) && info.keypads.find((d) => selected.startsWith(`${d.name}.`))?.protocol === "pkp";
$("title").textContent = `${selected}${knob ? " · turning" : selected.endsWith(" press") ? " · pressing the knob" : ""}`;
const actions = [...(knob ? KNOB_ACTIONS : []), ...screenActions];
const drives = b.output ? `output:${b.output}` : b.action ? `action:${b.action}` : "";
const first = outs(b)[0];
const drives = first ? `output:${first}` : b.action ? `action:${b.action}` : "";
$("drives").innerHTML = `<option value="">— nothing —</option>` +
`<optgroup label="Screen">${actions.map((a) => `<option value="action:${esc(a)}"${`action:${a}` === drives ? " selected" : ""}>${esc(a)}</option>`).join("")}</optgroup>` +
(knob ? "" : `<optgroup label="Output">${info.outputs.map((o) => `<option value="output:${esc(o.output)}"${`output:${o.output}` === drives ? " selected" : ""}>${esc(o.output)}${o.label ? ` · ${esc(o.label)}` : ""}</option>`).join("")}</optgroup>`);
const mode = b.mode || "toggle";
$("mode-box").hidden = !b.output;
$("modes").innerHTML = info.modes.map((m) => `<button type="button" data-mode="${m}" aria-pressed="${m === mode}">${m}</button>`).join("");
`<optgroup label="Output">${outputOptions(drives.slice(7))}</optgroup>`;
const isOut = !!first;
const mode = b.mode || (knob ? "dimmer" : "toggle");
$("mode-box").hidden = !isOut;
$("modes").innerHTML = info.modes.filter((m) => !knob || KNOB_MODES.includes(m)).map((m) => `<button type="button" data-mode="${m}" aria-pressed="${m === mode}">${m}</button>`).join("");
$("mode-hint").textContent = MODE_HINT[mode] || "";
$("pulse-box").hidden = !b.output || mode !== "pulse";
$("positions-box").hidden = !isOut || mode !== "positions";
$("positions").innerHTML = (b.outputs || []).map((o, i) => `<li><span class="actions"><select data-position="${i}">${outputOptions(o)}</select>${i ? `<button type="button" data-drop="${i}" aria-label="Remove position ${i + 1}">✕</button>` : ""}</span></li>`).join("");
$("pulse-box").hidden = !isOut || mode !== "pulse";
$("pulse").value = b.pulse_s ?? 1;
$("period-box").hidden = !isOut || mode !== "strobe";
$("period").value = b.period_s ?? 1;
$("step-box").hidden = !isOut || mode !== "dimmer";
$("step").value = b.step ?? 10;
$("menu").innerHTML = options([...(knob ? ["rows"] : MENU.slice(1)).map((m) => [m, m]), ...screenActions.map((a) => [a, a])], b.menu, "— the same —");
$("led-box").hidden = !pkpKey;
const colors = info.colors.map((c) => [c, c]);
$("led").innerHTML = options(colors, b.led, "off");
$("led_on").innerHTML = options(colors, b.led_on, b.output ? "red" : "green");
}
const outputOptions = (value) => info.outputs.map((o) => `<option value="${esc(o.output)}"${o.output === value ? " selected" : ""}>${esc(o.output)}${o.label ? ` · ${esc(o.label)}` : ""}${o.dimmable ? " · dimmable" : ""}</option>`).join("");
function render() {
renderPads();
renderEditor();
@@ -150,7 +168,7 @@
if (!b) bindings.push((b = { input: selected }));
Object.assign(b, fields);
for (const k of Object.keys(b)) if (b[k] === "" || b[k] == null) delete b[k];
if (!b.action && !b.output && !b.menu) bindings = bindings.filter((x) => x !== b);
if (!b.action && !b.output && !b.outputs && !b.menu) bindings = bindings.filter((x) => x !== b);
dirty = true;
render();
}
@@ -160,12 +178,34 @@
});
$("drives").addEventListener("change", (e) => {
const [kind, value] = [e.target.value.slice(0, e.target.value.indexOf(":")), e.target.value.slice(e.target.value.indexOf(":") + 1)];
change(kind === "output" ? { output: value, action: null } : { action: kind === "action" ? value : null, output: null, mode: null, pulse_s: null });
const b = binding(selected);
if (kind === "output" && b?.outputs) change({ outputs: [value, ...b.outputs.slice(1)] });
else if (kind === "output") change({ output: value, action: null, mode: isKnob(selected) ? "dimmer" : b?.mode });
else change({ action: kind === "action" ? value : null, output: null, outputs: null, mode: null, pulse_s: null, period_s: null, step: null });
});
$("modes").addEventListener("click", (e) => {
const m = e.target.closest("button[data-mode]")?.dataset.mode;
if (m) change({ mode: m === "toggle" ? null : m, pulse_s: m === "pulse" ? binding(selected)?.pulse_s ?? 1 : null });
if (!m) return;
const list = outs(binding(selected));
change({ mode: m === "toggle" ? null : m, pulse_s: m === "pulse" ? binding(selected)?.pulse_s ?? 1 : null,
period_s: m === "strobe" ? binding(selected)?.period_s ?? 1 : null, step: m === "dimmer" ? binding(selected)?.step ?? 10 : null,
outputs: m === "positions" ? list : null, output: m === "positions" ? null : list[0] });
});
$("positions").addEventListener("change", (e) => {
const i = Number(e.target.dataset.position);
change({ outputs: binding(selected).outputs.map((o, k) => (k === i ? e.target.value : o)) });
});
$("positions").addEventListener("click", (e) => {
const i = e.target.dataset.drop;
if (i != null) change({ outputs: binding(selected).outputs.filter((_, k) => k !== Number(i)) });
});
$("add-position").addEventListener("click", () => {
const list = binding(selected).outputs;
const next = info.outputs.find((o) => !list.includes(o.output))?.output || list[list.length - 1];
change({ outputs: [...list, next] });
});
$("period").addEventListener("change", (e) => change({ period_s: Number(e.target.value) || 1 }));
$("step").addEventListener("change", (e) => change({ step: Number(e.target.value) || 10 }));
$("pulse").addEventListener("change", (e) => change({ pulse_s: Number(e.target.value) || 1 }));
$("menu").addEventListener("change", (e) => change({ menu: e.target.value }));
$("led").addEventListener("change", (e) => change({ led: e.target.value }));
+4 -4
View File
@@ -55,9 +55,9 @@
<dl id="trip"></dl>
</section>
<section id="outputs" hidden>
<h2>Relays</h2>
<h2>Outputs</h2>
<div id="relays"></div>
<p style="color: GrayText; font-size: 0.85rem">can.json's "outputs" (simulated: no frames sent). A click switches one; the keypad's bindings with an "output" do too.</p>
<p style="color: GrayText; font-size: 0.85rem">can.json's "outputs" (simulated: no frames sent). A click switches one (and stops its strobe); the keypad's bindings with an output do too.</p>
</section>
</div>
@@ -118,9 +118,9 @@
$("outputs").hidden = !data;
if (!data) return;
const esc = (s) => String(s).replace(/[&<>"]/g, (ch) => `&#${ch.charCodeAt(0)};`);
const by = (o) => data.bindings.filter((b) => b.output === o).map((b) => `${b.input} (${b.mode || "toggle"})`).join(", ");
const by = (o) => data.bindings.filter((b) => (b.outputs || [b.output]).includes(o)).map((b) => `${b.input} (${b.mode || "toggle"})`).join(", ");
$("relays").innerHTML = data.modules.map((m) => `<p>${esc(m.name)} · node ${m.node}${m.simulated ? " · simulated" : ""}</p><div class="actions">` +
m.channels.map((c) => `<button type="button" data-output="${esc(m.name)}/${c.n}" class="${c.on ? "on" : ""}" title="${esc(by(`${m.name}.${c.n}`))}">${c.on ? "●" : "○"} ${c.n}${c.label ? ` ${esc(c.label)}` : ""}</button>`).join("") + "</div>").join("");
m.channels.map((c) => `<button type="button" data-output="${esc(m.name)}/${c.n}" class="${c.on ? "on" : ""}" title="${esc(by(`${m.name}.${c.n}`))}">${c.on ? "●" : "○"} ${c.n}${c.label ? ` ${esc(c.label)}` : ""}${c.level != null ? ` ${Math.round(c.level)} %` : ""}${c.strobe ? " · strobe" : ""}</button>`).join("") + "</div>").join("");
}
$("relays").addEventListener("click", (e) => {
const b = e.target.closest("button[data-output]");