Add calibration options to the compass app

This commit is contained in:
James Babcock committed 2026-08-20 09:57:33 -07:00
1 parent 9e447b70e2
commit 12bb76ba3c
4 files changed
+252 -35

No files matched your search

+73 -35
View File
@@ -8,7 +8,10 @@ import {
setCompassEnabled,
type CompassEvent,
} from "../../native/compass";
import { wrapText } from "../../graphics/textwrap";
import { type DashboardInputEvent, type Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import { screenCenterInViewportX } from "../../ui/shell/geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
@@ -16,14 +19,21 @@ import {
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { calibrateHeading, isCompassCalibrated, normalizeHeading } from "./calibration";
import { CompassCalibrationLayer } from "./calibration-layer";
export const COMPASS_WINDOW_ID = "compass";
export const COMPASS_SURFACE_ID = "window:compass";
const RECONCILE_INTERVAL_MS = 400;
/** Vertical breathing room between the rose, the readout and the status line. */
const STACK_GAP = 8;
/** Inset of the cardinal-label ring from the rose's outer circle. */
const LABEL_INSET = 14;
class CompassLayer implements Layer {
private heading: number | null = null;
private status = "Waiting for compass data…";
private rawHeading: number | null = null;
/** A firmware calibration message, shown until the next heading arrives. */
private firmwareStatus: string | null = null;
private enabled = false;
private removed = false;
private timer: ReturnType<typeof setInterval> | null = null;
@@ -52,56 +62,76 @@ class CompassLayer implements Layer {
this.stop();
}
openCalibration(ctx: LayerContext): void {
if (ctx.stack.topMatches((layer) => layer instanceof CompassCalibrationLayer)) return;
ctx.stack.push(new CompassCalibrationLayer(() => this.rawHeading));
}
private reconcile(): void {
if (this.removed) return;
const visible = shell.isWindowVisible(COMPASS_WINDOW_ID);
if (visible === this.enabled) return;
this.enabled = visible;
setCompassEnabled(visible);
this.status = visible ? "Waiting for compass data…" : "Compass paused";
this.firmwareStatus = null;
this.requestRender();
}
private onCompassEvent(event: CompassEvent): void {
if (this.removed) return;
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
this.heading = normalizeHeading(event.headingDegrees);
this.status = "Magnetic heading";
this.rawHeading = normalizeHeading(event.headingDegrees);
this.firmwareStatus = null;
} else if (event.command === COMPASS_CALIBRATION_STARTED) {
this.status = "Calibrating — move the glasses";
this.firmwareStatus = "Calibrating — move the glasses";
} else if (event.command === COMPASS_CALIBRATION_COMPLETE) {
this.status = "Calibration complete";
this.firmwareStatus = "Calibration complete";
}
this.requestRender();
}
private statusText(): string {
if (this.firmwareStatus !== null) return this.firmwareStatus;
if (!this.enabled) return "Compass paused";
if (this.rawHeading === null) return "Waiting for compass data…";
return isCompassCalibrated() ? "Magnetic heading" : "Uncalibrated - Tap to calibrate";
}
paint(ctx: LayerContext): GrayImage {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const large = getDefaultLargeFont();
const cx = Math.round(width * 0.28);
const cy = Math.round(height * 0.51);
const radius = Math.min(94, Math.round(height * 0.34));
const heading = this.rawHeading === null ? null : calibrateHeading(this.rawHeading);
const radius = Math.min(63, Math.round(height * 0.227));
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
const smallStep = lineStep(small);
drawCompassRose(image, cx, cy, radius, this.heading);
// One column centred on the display's true centre, so the rose's centre
// dot sits where the wearer is looking rather than 32px right of it.
const cx = screenCenterInViewportX();
const stackHeight = radius * 2 + STACK_GAP + large.lineHeight + STACK_GAP + statusLines.length * smallStep;
const cy = Math.max(0, Math.round((height - stackHeight) / 2)) + radius;
if (this.heading !== null) {
const headingText = `${Math.round(this.heading)}°`;
const direction = cardinalDirection(this.heading);
const textX = Math.round(width * 0.57);
image.drawText(large, textX, 72, headingText, 255);
image.drawText(large, textX, 108, direction, 210);
} else {
image.drawText(medium, Math.round(width * 0.56), 84, "--°", 150);
image.drawText(small, Math.round(width * 0.56), 116, "No heading yet", 130);
drawCompassRose(image, cx, cy, radius, heading);
let y = cy + radius + STACK_GAP;
image.drawText(large, Math.round(cx - large.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
y += large.lineHeight + STACK_GAP;
for (const line of statusLines) {
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
y += smallStep;
}
image.drawText(small, Math.round(width * 0.56), 158, this.status, 125);
return image;
}
handleInput(_event: DashboardInputEvent, _ctx: LayerContext): void {}
// A tap opens calibration from either state: it is the affordance the
// "Uncalibrated" prompt advertises, and re-calibrating is a normal thing to
// want once the glasses have been taken off and put back on.
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "click") this.openCalibration(ctx);
}
}
export function createCompassAppWindow(options: InProcessAppOptions): InProcessWindow {
@@ -116,6 +146,15 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
icon: "compass",
closeable: true,
actions: options.actions,
menuItems: () => [
{
label: "Calibrate",
onSelect: (ctx) => {
ctx.stack.pop();
layer.openCalibration(ctx);
},
},
],
baseLayer: new YieldAtRootLayer(layer),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
@@ -130,10 +169,6 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
return app;
}
function normalizeHeading(value: number): number {
return ((value % 360) + 360) % 360;
}
function cardinalDirection(heading: number): string {
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
@@ -147,24 +182,27 @@ function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: numbe
const cardinals: Array<[string, number]> = [["N", -90], ["E", 0], ["S", 90], ["W", 180]];
for (const [label, degrees] of cardinals) {
const angle = (degrees - (heading ?? 0)) * Math.PI / 180;
const x = cx + Math.cos(angle) * (radius - 14) - medium.measureText(label) / 2;
const y = cy + Math.sin(angle) * (radius - 14) - medium.lineHeight / 2;
const x = cx + Math.cos(angle) * (radius - LABEL_INSET) - medium.measureText(label) / 2;
const y = cy + Math.sin(angle) * (radius - LABEL_INSET) - medium.lineHeight / 2;
image.drawText(medium, Math.round(x), Math.round(y), label, label === "N" ? 230 : 105);
}
const intercardinals: Array<[string, number]> = [["NE", -45], ["SE", 45], ["SW", 135], ["NW", -135]];
for (const [label, degrees] of intercardinals) {
const angle = (degrees - (heading ?? 0)) * Math.PI / 180;
const x = cx + Math.cos(angle) * (radius - 14) - small.measureText(label) / 2;
const y = cy + Math.sin(angle) * (radius - 14) - small.lineHeight / 2;
const x = cx + Math.cos(angle) * (radius - LABEL_INSET) - small.measureText(label) / 2;
const y = cy + Math.sin(angle) * (radius - LABEL_INSET) - small.lineHeight / 2;
image.drawText(small, Math.round(x), Math.round(y), label, 85);
}
image.fillRect(cx - 2, cy - 2, 5, 5, 180);
if (heading === null) return;
const northAngle = (-90 - heading) * Math.PI / 180;
const nx = cx + Math.cos(northAngle) * (radius - 30);
const ny = cy + Math.sin(northAngle) * (radius - 30);
const sx = cx - Math.cos(northAngle) * (radius - 38);
const sy = cy - Math.sin(northAngle) * (radius - 38);
// Stop the needle just inside the label ring. The labels keep their font
// size as the rose shrinks, so a fixed inset would run the needle into them.
const needle = Math.max(8, radius - LABEL_INSET - medium.lineHeight / 2 - 4);
const nx = cx + Math.cos(northAngle) * needle;
const ny = cy + Math.sin(northAngle) * needle;
const sx = cx - Math.cos(northAngle) * (needle - 8);
const sy = cy - Math.sin(northAngle) * (needle - 8);
drawThickLine(image, cx, cy, nx, ny, 255);
image.drawLine(cx, cy, sx, sy, 75);
}