Display compass diagnostic information

This commit is contained in:
James Babcock committed 2026-09-08 11:20:43 -07:00
1 parent 1154474754
commit 083628f37a
13 files changed
+807 -10

No files matched your search

+38 -3
View File
@@ -7,6 +7,7 @@ import {
addCompassListener,
setCompassEnabled,
type CompassEvent,
type CompassDiagnostics,
} from "../../native/compass";
import { wrapText } from "../../graphics/textwrap";
import { type InputEvent } from "../../ui/gestures";
@@ -25,6 +26,7 @@ import { isCompassCalibrated, normalizeHeading } from "./calibration";
import { CompassCalibrationLayer } from "./calibration-layer";
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
import { compassDebugLines, isCompassDebugEnabled, setCompassDebugEnabled } from "./debug";
export const COMPASS_WINDOW_ID = "compass";
export const COMPASS_SURFACE_ID = "window:compass";
@@ -95,6 +97,7 @@ const WALL_LIT = 62;
class CompassLayer implements Layer {
private rawHeading: number | null = null;
private diagnostics: CompassDiagnostics | null = null;
/** A firmware calibration message, shown until the next heading arrives. */
private firmwareStatus: string | null = null;
private enabled = false;
@@ -141,6 +144,11 @@ class CompassLayer implements Layer {
this.requestRender();
}
toggleDebugInfo(): void {
setCompassDebugEnabled(!isCompassDebugEnabled());
this.requestRender();
}
stop(): void {
if (this.removed) return;
this.removed = true;
@@ -168,6 +176,7 @@ class CompassLayer implements Layer {
const visible = shell.isWindowVisible(COMPASS_WINDOW_ID);
if (visible === this.enabled) return;
this.enabled = visible;
this.diagnostics = null;
setCompassEnabled(visible);
this.firmwareStatus = null;
// The stored fix may have aged out while the compass was hidden.
@@ -179,6 +188,7 @@ class CompassLayer implements Layer {
if (this.removed) return;
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
this.rawHeading = normalizeHeading(event.headingDegrees);
this.diagnostics = event.diagnostics ?? null;
this.firmwareStatus = null;
} else if (event.command === COMPASS_CALIBRATION_STARTED) {
this.firmwareStatus = "Calibrating — move the glasses";
@@ -204,6 +214,17 @@ class CompassLayer implements Layer {
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
const smallStep = lineStep(small);
const debugLines = isCompassDebugEnabled() ? compassDebugLines(this.diagnostics) : [];
const debugWidth = Math.max(0, ...debugLines.map((line) => small.measureText(line)));
const debugGap = debugLines.length ? 12 : 0;
// Keep diagnostics beside the heading even in narrow windows. The rose
// stays centred on the optical axis; the combined readout fits the viewport.
const headingFont = large.measureText(headingText) + debugGap + debugWidth <= width - EDGE_PAD * 2
? large : small;
const headingWidth = headingFont.measureText(headingText);
const readoutWidth = headingWidth + debugGap + debugWidth;
const debugHeight = debugLines.length ? (debugLines.length - 1) * smallStep + small.lineHeight : 0;
const readoutHeight = Math.max(headingFont.lineHeight, debugHeight);
// One column centred on the display's true centre, so the rose sits where
// the wearer is looking rather than 32px right of it.
@@ -211,7 +232,7 @@ class CompassLayer implements Layer {
// The rose is sized and placed first — it hangs off the bottom edge — and
// the readout then floats in whatever room is left above it.
const textHeight = large.lineHeight + 4 + statusLines.length * smallStep;
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
const radius = Math.max(
24,
Math.min(
@@ -240,8 +261,14 @@ class CompassLayer implements Layer {
}
const image = this.background.image.clone();
drawCompassRose(image, cx, cy, radius, heading);
image.drawText(large, Math.round(cx - large.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
y += large.lineHeight + 4;
const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2)));
image.drawText(headingFont, readoutX, y + Math.floor((readoutHeight - headingFont.lineHeight) / 2),
headingText, heading === null ? 150 : 255);
for (let i = 0; i < debugLines.length; i++) {
image.drawText(small, readoutX + headingWidth + debugGap,
y + Math.floor((readoutHeight - debugHeight) / 2) + i * smallStep, debugLines[i]!, 175);
}
y += readoutHeight + 4;
for (const line of statusLines) {
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
y += smallStep;
@@ -286,6 +313,14 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
layer.toggleNorthReference();
},
},
{
label: `Debug information: ${isCompassDebugEnabled() ? "On" : "Off"}`,
description: "Show magnetic accuracy (0–3), anomaly flags (0–2), and orientation source beside the heading.",
onSelect: (ctx) => {
ctx.stack.pop();
layer.toggleDebugInfo();
},
},
],
baseLayer: new YieldAtRootLayer(layer),
submitFrame: options.submitFrame,
+21
View File
@@ -0,0 +1,21 @@
import type { CompassDiagnostics } from "../../native/compass";
import { getBooleanSetting, setBooleanSetting } from "../../native/settings-store";
const DEBUG_KEY = "compass.debugInfo";
export function isCompassDebugEnabled(): boolean {
return getBooleanSetting(DEBUG_KEY, false);
}
export function setCompassDebugEnabled(enabled: boolean): void {
setBooleanSetting(DEBUG_KEY, enabled);
}
export function compassDebugLines(diagnostics: CompassDiagnostics | null): string[] {
if (!diagnostics) return ["Diagnostics", "unavailable"];
if (!(diagnostics.flags & 0x80)) return ["Sample data", "unavailable"];
const accuracy = diagnostics.magneticAccuracy < 0 ? "--" : `${diagnostics.magneticAccuracy}/3`;
const anomalies = diagnostics.magneticAnomalies < 0 ? "--" : String(diagnostics.magneticAnomalies);
const source = ["--", "GRV", "GMRV", "RV"][diagnostics.orientationSource] ?? "--";
return [`Mag: ${accuracy}`, `Anom: ${anomalies}`, `Src: ${source}`];
}
+23 -2
View File
@@ -5,10 +5,23 @@ export const COMPASS_CHANGED = 15;
export const COMPASS_CALIBRATION_STARTED = 16;
export const COMPASS_CALIBRATION_COMPLETE = 17;
export type CompassDiagnostics = {
/** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
magneticAccuracy: number;
/** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
magneticAnomalies: number;
/** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
orientationSource: number;
flags: number;
sampleTimeMs: number;
};
export type CompassEvent = {
command: number;
/** Magnetic heading in degrees, or -1 for calibration-only events. */
headingDegrees: number;
/** Absent with stock/older CFW or an unrecognized diagnostic extension. */
diagnostics?: CompassDiagnostics;
};
function activeCommunicator(): any {
@@ -41,8 +54,16 @@ export function addCompassListener(listener: (event: CompassEvent) => void): ()
const active = activeCommunicator();
if (!active) return () => {};
const proxy = new com.faceclaw.app.FaceclawCompassListener({
onCompassEvent: (command: number, headingDegrees: number) => {
listener({ command: Number(command), headingDegrees: Number(headingDegrees) });
onCompassEvent: (command: number, headingDegrees: number, magneticAccuracy: number,
magneticAnomalies: number, orientationSource: number, diagnosticFlags: number, sampleTimeMs: number) => {
listener({
command: Number(command), headingDegrees: Number(headingDegrees),
diagnostics: diagnosticFlags >= 0 ? {
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags),
sampleTimeMs: Number(sampleTimeMs),
} : undefined,
});
},
});
active.addCompassListener(proxy);