Switches in drive mode, after the Design canvas' GridSwitches: Menu → Switches (only when /api/outputs answers), 3×3 tiles per page with the output's name, On / Off / Flashing / level, the key and mode driving it and its LED colour line, a group per module, All off, More ▸, a tap toggles. The status pill above the car (pypilot's note in SidePanel): ● REC while the trip is recorded, estimated / no fix, hidden when all is well
This commit is contained in:
1 parent
82a728e024
commit
88fa2d27a3
5 files changed
+162
-5
No files matched your search
@@ -924,3 +924,99 @@ export function todayMenu(ctx) {
|
||||
ctx.routePanel?.onStopsChanged(() => render());
|
||||
return { el, render, opened: () => { page = 0; render(); } };
|
||||
}
|
||||
|
||||
// Switches (the Design canvas' GridSwitches): can.json's outputs as tiles —
|
||||
// state, mode, the key that drives it and a line in its LED colour; a tap
|
||||
// switches it (POST /api/outputs/<module>/<n>, a strobe stops). Groups are
|
||||
// the modules; nine tiles a page. Polled while open (GET /api/outputs).
|
||||
const LED = { red: "#e8453a", green: "#34b545", blue: "#3b82f6", yellow: "#f0a020", cyan: "#22c3d6", magenta: "#b45bd0", white: "#e8e6df" };
|
||||
const TILES = 9;
|
||||
export function switchesMenu(ctx) {
|
||||
const el = panel("drive-switches", "Switches", `
|
||||
<div class="drive-switches__tiles"></div>
|
||||
<div class="drive-switches__groups"></div>
|
||||
<button type="button" class="drive-switches__off" data-action="off">All off</button>
|
||||
<button type="button" class="drive-switches__page" data-action="page"></button>`);
|
||||
let data = null;
|
||||
let group = null; // a module's name, null = all
|
||||
let page = 0;
|
||||
let timer = null;
|
||||
const outputsOf = (b) => b.outputs || (b.output ? [b.output] : []);
|
||||
const short = (input) => input.slice(input.indexOf(".") + 1).replace(/ press$/, " ⏎");
|
||||
|
||||
function tiles() {
|
||||
return data.modules.filter((m) => !group || m.name === group).flatMap((m) => m.channels.map((c) => {
|
||||
const output = `${m.name}.${c.n}`;
|
||||
const bound = data.bindings.filter((b) => outputsOf(b).includes(output));
|
||||
return { m, c, output, bound };
|
||||
}));
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (el.hidden || !data) return;
|
||||
const all = tiles();
|
||||
const pages = Math.max(1, Math.ceil(all.length / TILES));
|
||||
page = Math.min(page, pages - 1);
|
||||
const on = data.modules.reduce((n, m) => n + m.channels.filter((c) => c.on).length, 0);
|
||||
el.querySelector(".drive-panel__note").textContent = `${on} on${data.modules.every((m) => m.simulated) ? " · simulated" : ""}`;
|
||||
el.querySelector(".drive-switches__tiles").replaceChildren(...all.slice(page * TILES, page * TILES + TILES).map(({ m, c, output, bound }) => {
|
||||
const b = bound[0];
|
||||
const tile = document.createElement("button");
|
||||
tile.type = "button";
|
||||
tile.className = "drive-switches__tile";
|
||||
tile.setAttribute("aria-pressed", String(!!c.on));
|
||||
tile.style.setProperty("--led", LED[b?.led] || (b ? LED.red : "transparent"));
|
||||
const state = c.strobe ? "Flashing" : c.on ? `On${c.level != null ? ` · ${Math.round(c.level)} %` : ""}` : `Off${c.level != null ? ` · ${Math.round(c.level)} %` : ""}`;
|
||||
const mode = bound.map((x) => x.mode || "toggle").filter((x, i, a) => a.indexOf(x) === i).join(" · ");
|
||||
tile.innerHTML = `<span class="drive-switches__top"><span class="drive-switches__icon" aria-hidden="true">⏻</span><span class="drive-switches__key"></span></span>
|
||||
<span class="drive-switches__name"></span><span class="drive-switches__state"></span><span class="drive-switches__mode"></span>`;
|
||||
tile.querySelector(".drive-switches__key").textContent = bound.map((x) => short(x.input)).join(" · ");
|
||||
tile.querySelector(".drive-switches__name").textContent = c.label || output;
|
||||
tile.querySelector(".drive-switches__state").textContent = state;
|
||||
tile.querySelector(".drive-switches__mode").textContent = mode || (c.label ? output : "");
|
||||
tile.title = `${output}${c.label ? ` · ${c.label}` : ""}`;
|
||||
tile.addEventListener("click", () => send(`/api/outputs/${encodeURIComponent(m.name)}/${c.n}`));
|
||||
return tile;
|
||||
}));
|
||||
const groups = data.modules.length > 1 ? [null, ...data.modules.map((m) => m.name)] : [];
|
||||
el.querySelector(".drive-switches__groups").replaceChildren(...groups.map((name) => {
|
||||
const g = document.createElement("button");
|
||||
g.type = "button";
|
||||
g.textContent = name ?? "All";
|
||||
g.setAttribute("aria-pressed", String(group === name));
|
||||
g.addEventListener("click", () => { group = name; page = 0; render(); });
|
||||
return g;
|
||||
}));
|
||||
const pageButton = el.querySelector('[data-action="page"]');
|
||||
pageButton.hidden = pages < 2;
|
||||
pageButton.textContent = `More ▸ ${page + 1}/${pages}`;
|
||||
el.querySelector('[data-action="off"]').disabled = !on;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const r = await fetch("/api/outputs", { cache: "no-store" });
|
||||
data = r.ok ? await r.json() : null;
|
||||
} catch { /* offline for a moment: the last state stays */ }
|
||||
render();
|
||||
}
|
||||
async function send(url, body) {
|
||||
await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: body && JSON.stringify(body) }).catch(() => {});
|
||||
load();
|
||||
}
|
||||
|
||||
el.querySelector(".drive-panel__close").addEventListener("click", ctx.close);
|
||||
el.querySelector('[data-action="page"]').addEventListener("click", () => { page++; if (page * TILES >= tiles().length) page = 0; render(); });
|
||||
el.querySelector('[data-action="off"]').addEventListener("click", async () => {
|
||||
const on = data.modules.flatMap((m) => m.channels.filter((c) => c.on).map((c) => `/api/outputs/${encodeURIComponent(m.name)}/${c.n}`));
|
||||
await Promise.all(on.map((url) => fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: '{"on":false}' }).catch(() => {})));
|
||||
load();
|
||||
});
|
||||
return {
|
||||
el, render,
|
||||
// whether this server has outputs at all (the Menu's row shows then)
|
||||
available: () => fetch("/api/outputs", { cache: "no-store" }).then((r) => r.ok).catch(() => false),
|
||||
opened: () => { page = 0; load(); clearInterval(timer); timer = setInterval(load, 1000); },
|
||||
closed: () => clearInterval(timer),
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import { watchDashboard, maneuverSvg, laneSvg, distance, VOICE_KEY } from "/js/d
|
||||
import { SPEED_LIMITS, COUNTRY_NAMES, isHeavy, motorwayPayment } from "/js/speed_limits.js";
|
||||
import { currentVehicle, onVehicle } from "/js/panels.js";
|
||||
import { formatSunInfo } from "/js/sun.js";
|
||||
import { pointsMenu, layersMenu, terrainMenu, levelMenu, recordingMenu, todayMenu, elevationHtml, cellButton, bindKey, emptyRow as empty, escapeHtml } from "/js/drive_menus.js";
|
||||
import { pointsMenu, layersMenu, terrainMenu, levelMenu, recordingMenu, todayMenu, switchesMenu, elevationHtml, cellButton, bindKey, emptyRow as empty, escapeHtml } from "/js/drive_menus.js";
|
||||
|
||||
// Driving mode: the grid of docs/design.adoc ("Layout: a grid, not floating
|
||||
// panels"; the Design canvas' Grid) — the page becomes cells, the map one
|
||||
@@ -109,6 +109,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
|
||||
menuPanel.innerHTML = `<div class="drive-panel__head">Menu</div>
|
||||
<button type="button" class="drive-panel__close" aria-label="Close">✕</button>
|
||||
<button type="button" class="drive-menu__row" data-action="today">Today ▸ <small>the stops left, tonight, the weather, the day's notes</small></button>
|
||||
<button type="button" class="drive-menu__row" data-action="switches" hidden>Switches ▸ <small>the relays and lights, what drives them</small></button>
|
||||
<button type="button" class="drive-menu__row" data-action="overview">The whole route <small>the follow pauses</small></button>
|
||||
<button type="button" class="drive-menu__row" data-action="terrain-auto"></button>
|
||||
<button type="button" class="drive-menu__row" data-action="voice"></button>
|
||||
@@ -150,6 +151,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
|
||||
renderProfile();
|
||||
menuPanel.querySelector('[data-action="overview"]').addEventListener("click", overview);
|
||||
menuPanel.querySelector('[data-action="today"]').addEventListener("click", () => open(today.el));
|
||||
menuPanel.querySelector('[data-action="switches"]').addEventListener("click", () => open(switches.el));
|
||||
menuPanel.querySelector('[data-action="leave"]').addEventListener("click", () => set(false));
|
||||
const crosshair = document.createElement("div");
|
||||
crosshair.className = "drive-crosshair";
|
||||
@@ -260,7 +262,9 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
|
||||
endTrip: () => endTrip(),
|
||||
});
|
||||
const today = todayMenu(menuCtx);
|
||||
const menus = [points, layers, terrain, level, recording, today];
|
||||
const switches = switchesMenu(menuCtx);
|
||||
switches.available().then((yes) => { menuPanel.querySelector('[data-action="switches"]').hidden = !yes; });
|
||||
const menus = [points, layers, terrain, level, recording, today, switches];
|
||||
let offRoad = false;
|
||||
let tiltSince = null;
|
||||
let pavedM = 0;
|
||||
@@ -356,6 +360,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
|
||||
logButton.classList.toggle("drive-trip__log--on", !!log?.active);
|
||||
logState.textContent = log?.active ? `● ${log.folder.replace(/^Trips\//, "")} · ${(log.distance_m / 1000).toFixed(1)} km` : "Not recording";
|
||||
logState.classList.toggle("drive-trip__log-text--on", !!log?.active);
|
||||
document.documentElement.classList.toggle("trip-rec", !!log?.active);
|
||||
};
|
||||
const endTrip = async () => {
|
||||
if (!log?.active || !window.confirm(`End the trip "${log.folder}"? Its track stays in the library.`)) return;
|
||||
@@ -381,7 +386,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) {
|
||||
logState.textContent = "Log in to record";
|
||||
}
|
||||
map.getContainer().append(crosshair);
|
||||
app.append(tripPanel, menuPanel, points.el, layers.el, terrain.el, level.el, recording.el, today.el);
|
||||
app.append(tripPanel, menuPanel, points.el, layers.el, terrain.el, level.el, recording.el, today.el, switches.el);
|
||||
|
||||
// --- the grid's cells ---------------------------------------------------
|
||||
|
||||
|
||||
@@ -245,9 +245,27 @@ export class GnssControl {
|
||||
this._marker.setRotation(data.track);
|
||||
}
|
||||
this._marker.getElement().classList.toggle("gnss-marker--estimated", !!data.estimated);
|
||||
this._showTag(lngLat, data);
|
||||
}
|
||||
|
||||
// The status pill above the car (pypilot's note in the Design canvas' SidePanel): REC while
|
||||
// drive mode records the trip (it sets .trip-rec on <html>), estimated / no fix; none when all is well
|
||||
_showTag(lngLat, data) {
|
||||
const rec = document.documentElement.classList.contains("trip-rec");
|
||||
const fix = data.estimated ? "estimated" : data.mode >= 2 ? "" : "no fix";
|
||||
if (!rec && !fix) return this._tag?.getElement().replaceChildren();
|
||||
if (!this._tag) {
|
||||
const el = document.createElement("div");
|
||||
el.className = "gnss-tag";
|
||||
this._tag = new Marker({ element: el, anchor: "bottom", offset: [0, -16] }).setLngLat(lngLat).addTo(this._map);
|
||||
}
|
||||
glideMarker(this._tag, lngLat, POLL_INTERVAL_MS);
|
||||
this._tag.getElement().innerHTML = (rec ? `<span class="gnss-tag__rec">● REC</span>` : "") + (fix ? `<span>${fix}</span>` : "");
|
||||
}
|
||||
|
||||
_removeMarker() {
|
||||
this._tag?.remove();
|
||||
this._tag = null;
|
||||
if (this._marker) {
|
||||
this._marker.remove();
|
||||
this._marker = null;
|
||||
|
||||
Reference in new issue
Block a user