Compass has higher sample rate, and distinguishes map north from true north

This commit is contained in:
James Babcock committed 2026-09-01 14:37:09 -07:00
1 parent eb8a863222
commit a72e29a04f
13 files changed
+429 -55

No files matched your search

+36 -5
View File
@@ -3,18 +3,46 @@
* running EvenHub apps, mirroring the IMU router. Only the foreground app
* receives headings, so a backgrounded app can't keep the sensor running.
*
* The compass is NOT pre-calibrated — the magnetometer is in the right arm,
* which rests on the head with a wearer-dependent curl (see compass-app memory);
* headings are relative, not true north.
* The raw reading is NOT pre-calibrated — the magnetometer is in the right
* arm, which rests on the head with a wearer-dependent curl (see compass-app
* memory). Each delivery also carries the Compass app's wearer-calibrated
* magnetic heading and, when the phone has a location, declination and the
* true heading, so apps needn't redo that work.
*/
import { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from "../../native/compass";
import { isCompassCalibrated } from "../compass/calibration";
import { refreshDeclination } from "../compass/declination";
import { resolveHeading } from "../compass/heading";
/** Wire shape of the `compass` extension event (faceclaw-extensions `CompassReading`). */
export type EvenHubCompassReading = {
headingDegrees: number;
magneticHeadingDegrees: number;
wearerCalibrated: boolean;
declinationDegrees?: number;
trueHeadingDegrees?: number;
};
export type EvenHubCompassClient = {
readonly windowId: string;
isForeground(): boolean;
deliverCompass(headingDegrees: number): void;
deliverCompass(reading: EvenHubCompassReading): void;
};
export function buildCompassReading(rawDegrees: number): EvenHubCompassReading {
const resolved = resolveHeading(rawDegrees);
const reading: EvenHubCompassReading = {
headingDegrees: rawDegrees,
magneticHeadingDegrees: resolved.magneticDegrees,
wearerCalibrated: isCompassCalibrated(),
};
if (resolved.declinationDegrees !== null && resolved.trueDegrees !== null) {
reading.declinationDegrees = resolved.declinationDegrees;
reading.trueHeadingDegrees = resolved.trueDegrees;
}
return reading;
}
class EvenHubCompassRouter {
private readonly requesting = new Set<EvenHubCompassClient>();
private active: EvenHubCompassClient | null = null;
@@ -47,13 +75,16 @@ class EvenHubCompassRouter {
this.unsubscribe = addCompassListener((event: CompassEvent) => {
// Only heading updates carry a real heading; calibration events use -1.
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
this.active?.deliverCompass(event.headingDegrees);
this.active?.deliverCompass(buildCompassReading(event.headingDegrees));
}
});
}
if (!this.enabled) {
setCompassEnabled(true);
this.enabled = true;
// Uses location only if the wearer already granted it; an app turning
// on the compass is not a reason to raise a system permission prompt.
refreshDeclination();
}
} else {
if (this.enabled) {
+3 -3
View File
@@ -56,7 +56,7 @@ import { type EvenHubManifest } from "./ehpk";
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router";
import { evenHubCompassRouter, type EvenHubCompassClient } from "./compass-router";
import { evenHubCompassRouter, type EvenHubCompassClient, type EvenHubCompassReading } from "./compass-router";
import { type ImuReading } from "../../native/imu";
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
import { getCurrentLocation } from "../../native/location";
@@ -1010,8 +1010,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
// ----- compass (EvenHubCompassClient) -----
deliverCompass(headingDegrees: number): void {
this.pushExtEvent("compass", { headingDegrees });
deliverCompass(reading: EvenHubCompassReading): void {
this.pushExtEvent("compass", reading);
}
private setCompass(enable: boolean): void {