Keypads as channels and bindings, the PDMs' way (lib/maptool/keypad.rb): can.json's keypads (a Blink Marine PKP by its protocol, or any signals as inputs) give channels pad.K1, pad.E3, pad.E3 press; the drive profile's bindings give each a screen action, a knob action (zoom, pan, rows), another in an open menu and its LED colour, sent back to the PKP. Outputs (lib/maptool/outputs.rb): relay modules (simulated, no current measured), a binding's output with toggle/on/off/hold/pulse, hold off when the keypad goes silent, the state re-sent every 100 ms; /sim.html switches them by hand. CANopen TIME from maptool while no other node sends it (lib/maptool/can_time.rb), shown in /can.html
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-04 16:14:39 +01:00
1 parent bf90f4cf23
commit 38fb06853e
14 files changed
+785 -24

No files matched your search

+102 -3
View File
@@ -41,6 +41,9 @@ const KEYS = {
palette: ["", "", "Day, dusk, night"],
};
const NOT_YET = new Set();
// A knob's function → the screen keys it stands for (their labels: ⟳ ⟲)
const KNOB_KEYS = { zoom: ["zoomIn", "zoomOut"] };
const KEYPAD_ABSENT_MS = 5000; // no keypad on the bus: asked again after this
// The Terrain view switches itself on off-road (docs/design.adoc, Screen
// profiles): an unpaved surface (/api/road, off a route) or roll / pitch past
// TILT_DEG for TILT_S; back to the road view after PAVED_BACK_M on paved road.
@@ -421,7 +424,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
el.disabled = NOT_YET.has(cell.action) || !keyAction[cell.action];
el.addEventListener("click", () => keyAction[cell.action]?.());
if (cell.action === "zoomIn" || cell.action === "zoomOut") el.classList.add("drive-key--big");
if (cell.key) bindKey(el, cell.key); // "keypad" too, for when the keypad is on the bus
if (cell.key) bindKey(el, cell.key);
} else {
el = document.createElement("div");
}
@@ -596,6 +599,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
else if (cell.widget === "compass") renderCompass(el);
else if (cell.widget === "key") renderKey(el, cell);
}
sendLeds();
}
map.on("pitchend", render);
setInterval(render, RENDER_MS);
@@ -627,6 +631,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
app.append(el);
cells.push({ cell, el });
}
labelKeys(); // a keypad heard before the profile came
render();
map.resize();
});
@@ -651,8 +656,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
// (the keypad's encoder will send the same arrows), Enter its first button,
// Space its eye, + adds it, ⌫ up a folder, Esc closes. The labels in the
// corners show from the first key pressed on, so a touch-only screen stays clean.
// ponytail: keyboard only; the keypad's keys and its heartbeat switching the
// labels to "keypad" wait for the keypad on the bus (and the diagnostics' mapping)
// (The keypads' channels: the profile's bindings, further down.)
const KEYS_SEEN = "maptool.driveKeys";
let row = 0;
try { if (localStorage.getItem(KEYS_SEEN)) document.documentElement.classList.add("drive-keys"); } catch { /* nothing kept */ }
@@ -704,6 +708,101 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
if (!done) done = press(bound(openPanel) || bound(app));
if (done) e.preventDefault(); // F1, F5, / are the browser's otherwise
});
// Keypads (can.json's "keypads", GET /api/keypad, a long poll) and the
// profile's "bindings" (docs/design.adoc, Switches and the keypad): a
// channel ("pad.K1", "pad.E3" — a key, a knob, its "press") → an action,
// another one while a menu is open ("menu"). A screen key's action clicks
// it; knobs turn: zoom, panX / panY, rows (the menu's ↑ ↓); enter, close.
// A keypad's heartbeat on the bus labels the screen keys with its
// channels (none = touch only) and the line under them in the binding's
// LED colour; its key LEDs follow them, "led_on" (green) while on.
// ponytail: screen functions only — outputs (relays), the modes (hold,
// pulse, positions…) and the mapping UI (the canvas' GridBinding) to come.
const bindings = () => profile?.bindings || [];
let present = [];
let ledsSent = {};
const keyEl = (action) => cells.find((c) => c.cell.widget === "key" && c.cell.action === action)?.el;
const shortName = (input) => input.slice(input.indexOf(".") + 1).replace(/ press$/, " ⏎");
function labelKeys() {
for (const { cell, el } of cells) {
if (cell.widget !== "key") continue;
delete el.dataset.keypad;
delete el.dataset.led;
}
for (const b of bindings()) {
if (!present.some((d) => b.input.startsWith(`${d.name}.`)) || !b.action) continue;
const knob = KNOB_KEYS[b.action];
for (const [i, action] of (knob || [b.action]).entries()) {
const el = keyEl(action);
if (!el) continue;
el.dataset.keypad = shortName(b.input) + (knob ? (i ? " ⟲" : " ⟳") : "");
if (b.led) el.dataset.led = b.led;
}
}
}
function sendLeds() {
for (const d of present.filter((p) => p.leds)) {
const on = {};
for (const b of bindings()) {
const k = b.input.startsWith(`${d.name}.`) && /^K\d+$/.exec(b.input.slice(d.name.length + 1))?.[0];
const el = k && b.led && keyEl(b.action);
if (el) on[k] = el.disabled ? null : el.getAttribute("aria-pressed") === "true" ? b.led_on || "green" : b.led;
}
const body = JSON.stringify({ keypad: d.name, on });
if (body === ledsSent[d.name]) continue;
ledsSent[d.name] = body;
fetch("/api/keypad/leds", { method: "PUT", headers: { "Content-Type": "application/json" }, body }).catch(() => { ledsSent[d.name] = ""; });
}
}
const navKey = (key) => document.dispatchEvent(new KeyboardEvent("keydown", { key }));
const PAN_PX = 80;
const knobAction = {
zoom: (dir) => keyEl(dir > 0 ? "zoomIn" : "zoomOut")?.click(),
panX: (dir) => map.panBy([dir * PAN_PX, 0], { duration: 100 }),
panY: (dir) => map.panBy([0, dir * PAN_PX], { duration: 100 }),
rows: (dir) => navKey(dir > 0 ? "ArrowDown" : "ArrowUp"),
enter: () => navKey("Enter"),
close: () => navKey("Escape"),
};
const onInput = (event) => {
if (!document.documentElement.classList.contains("driving")) return;
const [, channel, turn] = /^(.*?)(?: (⟳|⟲))?$/.exec(event);
const b = bindings().find((x) => x.input === channel);
const action = (openPanel && b?.menu) || b?.action;
if (!action) return;
if (knobAction[action]) return knobAction[action](turn === "⟲" ? -1 : 1);
const el = keyEl(action);
if (el && visible(el)) el.click();
};
(async function keypadLoop() {
let since = null;
for (;;) {
let answer = null;
try {
const response = await fetch(`/api/keypad${since == null ? "" : `?since=${since}`}`, { cache: "no-store" });
if (response.status === 404) return; // no keypads configured on this server
if (response.ok) answer = await response.json();
} catch { /* the server restarting, offline for a moment */ }
const now = answer?.present || [];
if (JSON.stringify(now) !== JSON.stringify(present)) {
present = now;
document.documentElement.classList.toggle("drive-keypad", present.length > 0);
if (present.length) document.documentElement.classList.add("drive-keys");
ledsSent = {};
labelKeys();
sendLeds();
}
if (!present.length) {
since = null;
await new Promise((r) => setTimeout(r, KEYPAD_ABSENT_MS));
continue;
}
if (since != null) for (const e of answer.events) onInput(e.input);
since = answer.seq;
}
})();
let saved = null;
try { saved = localStorage.getItem(KEY); } catch { /* nothing kept */ }
set(saved === "on");