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
This commit is contained in:
1 parent
bf90f4cf23
commit
38fb06853e
14 files changed
+785
-24
No files matched your search
@@ -4947,6 +4947,15 @@ html.driving #app,
|
||||
border-radius: var(--drive-radius);
|
||||
}
|
||||
html.driving .app-body { border-radius: calc(var(--drive-radius) + 2px); overflow: hidden; }
|
||||
|
||||
/* The CAN keypad on the bus (html.drive-keypad, driving.js): the corner
|
||||
labels its keys (none = touch only), a line under the key in its LED colour */
|
||||
html.driving.drive-keypad [data-key]::after { content: attr(data-keypad); }
|
||||
html.driving.drive-keypad [data-key]:not([data-keypad])::after { display: none; }
|
||||
html.driving.drive-keypad [data-led] { box-shadow: inset 0 calc(-4px * var(--s, 1)) 0 var(--led); }
|
||||
[data-led="cyan"] { --led: #22c3d6; }
|
||||
[data-led="white"] { --led: #e8e6df; }
|
||||
[data-led="yellow"] { --led: #f0a020; }
|
||||
.drive-rec__cutrow { flex: none; height: calc(var(--cell, 80px) / 2); display: flex; align-items: center; gap: 10px; padding-left: 28px; background: var(--band); color: var(--drive-muted); font-size: calc(14px * var(--s, 1)); }
|
||||
.drive-rec__cutrow span:nth-child(2) { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.drive-panel .drive-rec__cutrow button { height: 100%; padding: 0 16px; background: none; border-left: 1px solid var(--drive-line); font-size: calc(14px * var(--s, 1)); font-weight: 700; }
|
||||
|
||||
+27
-11
@@ -21,16 +21,32 @@
|
||||
{ "area": "5 / 14 / 6 / 17", "widget": "field", "value": "toEnd" },
|
||||
{ "area": "6 / 14 / 9 / 17", "widget": "elevation", "km": 10 },
|
||||
{ "area": "9 / 14 / 10 / 17", "widget": "compass" },
|
||||
{ "area": "10 / 1 / 11 / 3", "widget": "key", "action": "trip", "key": "F1", "keypad": "K1" },
|
||||
{ "area": "10 / 3 / 11 / 5", "widget": "key", "action": "points", "key": "F2", "keypad": "K2" },
|
||||
{ "area": "10 / 5 / 11 / 7", "widget": "key", "action": "layers", "key": "F3", "keypad": "K3" },
|
||||
{ "area": "10 / 7 / 11 / 9", "widget": "key", "action": "search", "key": "/", "keypad": "K4" },
|
||||
{ "area": "10 / 9 / 11 / 11", "widget": "key", "action": "menu", "key": "F5", "keypad": "K5" },
|
||||
{ "area": "10 / 11 / 11 / 12", "widget": "key", "action": "zoomOut", "key": "-", "keypad": "E3 ⟲" },
|
||||
{ "area": "10 / 12 / 11 / 13", "widget": "key", "action": "zoomIn", "key": "+", "keypad": "E3 ⟳" },
|
||||
{ "area": "10 / 13 / 11 / 14", "widget": "key", "action": "pitch", "key": "3", "keypad": "K6" },
|
||||
{ "area": "10 / 14 / 11 / 15", "widget": "key", "action": "follow", "key": "f", "keypad": "E3 ⏎" },
|
||||
{ "area": "10 / 15 / 11 / 16", "widget": "key", "action": "terrain", "key": "t", "keypad": "K7" },
|
||||
{ "area": "10 / 16 / 11 / 17", "widget": "key", "action": "palette", "key": "n", "keypad": "K8" }
|
||||
{ "area": "10 / 1 / 11 / 3", "widget": "key", "action": "trip", "key": "F1" },
|
||||
{ "area": "10 / 3 / 11 / 5", "widget": "key", "action": "points", "key": "F2" },
|
||||
{ "area": "10 / 5 / 11 / 7", "widget": "key", "action": "layers", "key": "F3" },
|
||||
{ "area": "10 / 7 / 11 / 9", "widget": "key", "action": "search", "key": "/" },
|
||||
{ "area": "10 / 9 / 11 / 11", "widget": "key", "action": "menu", "key": "F5" },
|
||||
{ "area": "10 / 11 / 11 / 12", "widget": "key", "action": "zoomOut", "key": "-" },
|
||||
{ "area": "10 / 12 / 11 / 13", "widget": "key", "action": "zoomIn", "key": "+" },
|
||||
{ "area": "10 / 13 / 11 / 14", "widget": "key", "action": "pitch", "key": "3" },
|
||||
{ "area": "10 / 14 / 11 / 15", "widget": "key", "action": "follow", "key": "f" },
|
||||
{ "area": "10 / 15 / 11 / 16", "widget": "key", "action": "terrain", "key": "t" },
|
||||
{ "area": "10 / 16 / 11 / 17", "widget": "key", "action": "palette", "key": "n" }
|
||||
],
|
||||
"bindings": [
|
||||
{ "input": "pad.K1", "action": "trip", "led": "cyan" },
|
||||
{ "input": "pad.K2", "action": "points", "led": "cyan" },
|
||||
{ "input": "pad.K3", "action": "layers", "led": "cyan" },
|
||||
{ "input": "pad.K4", "action": "search", "led": "cyan" },
|
||||
{ "input": "pad.K5", "action": "menu", "menu": "close", "led": "cyan" },
|
||||
{ "input": "pad.K6", "action": "pitch", "led": "white" },
|
||||
{ "input": "pad.K7", "action": "terrain", "led": "yellow" },
|
||||
{ "input": "pad.K8", "action": "palette", "led": "yellow" },
|
||||
{ "input": "pad.E1", "action": "panX", "menu": "rows" },
|
||||
{ "input": "pad.E1 press", "menu": "enter" },
|
||||
{ "input": "pad.E2", "action": "panY" },
|
||||
{ "input": "pad.E2 press", "output": "relays.1" },
|
||||
{ "input": "pad.E3", "action": "zoom" },
|
||||
{ "input": "pad.E3 press", "action": "follow" }
|
||||
]
|
||||
}
|
||||
+102
-3
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user