Glanceboard: address review of head-tilt angle and glance down

- A verification read that still disagrees no longer requeues the writes at
  once (which looped while the firmware refused the value); the next regular
  poll retries.
- "Glasses default" now writes back the glasses' own switch and angle, saved
  before Faceclaw first changed them (persisted across app restarts), and
  drops writes still queued. Disabling the Glanceboard does the same.
- "Set to current head angle" takes two readings, head level then tilted up,
  and uses the angle between them, so it no longer depends on the sensor's
  axes or folds past 45°. A head that did not move is reported, not stored
  as 5°. The math is in head-tilt-math.ts, with tests.
- A head-tilt that woke a suspended session, or a trigger under 25° (where a
  level gaze stays above the head-down threshold), hides after the tap
  duration instead of the 60 s cap. The setting's description explains it.
- The angle rows are hidden on iOS, which cannot change the setting.
- The capture row repaints while measuring, ignores repeat selects, and its
  status clears when the menu reopens or the angle changes; the angle is
  sent once, by the controller's settings listener.
- Comments describing a head-tilt as a timed press are updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BrendenandClaude Opus 5.5 committed 2026-11-06 12:20:19 -08:00
1 parent cf74f34767
commit b1ca8b8ca1
15 files changed
+476 -128

No files matched your search

+18 -8
View File
@@ -20,8 +20,9 @@ import { shouldShowNotificationOnGlasses } from "../native/notification-sources"
import { watchNotificationListenerBinding } from "../native/notification-access";
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
import { defaultRingName, setGlassesRingLink } from "../native/ring-status";
import { applyGlanceAngle } from "./head-angle";
import { glanceAngleDegrees } from "../apps/glanceboard/glanceboard-settings";
import { applyGlanceAngle, glassesHeadUpTrigger, syncGlanceAngleOriginal } from "./head-angle";
import { glanceAngleTriggerDegrees, glanceTapTimeoutMs } from "../apps/glanceboard/glanceboard-settings";
import { glanceTiltMaxMs } from "./glance-state";
import { getBooleanSetting, setBooleanSetting } from "../native/settings-store";
import { grayImageToPreviewSource } from "../native/gray-image-preview";
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
@@ -335,7 +336,7 @@ class DashboardController {
private evenHubResumePromise: Promise<boolean> | null = null;
/** Faceclaw's firmware at the required revision reported in; its extensions can be used. */
private customFirmwareConfirmed = false;
private lastGlanceAngleDegrees = glanceAngleDegrees();
private lastGlanceAngleDegrees = glanceAngleTriggerDegrees();
private faceclawWakeLeaseState: boolean | null = null;
/** Ring connection mode the current session was started with (it applies per connection). */
private ringModeAtConnect: RingConnectionMode = "glasses";
@@ -1758,6 +1759,7 @@ class DashboardController {
this.requestShellRender();
this.updateCompositePreview();
this.updateConnectedForegroundNotification();
syncGlanceAngleOriginal();
}, SHELL_REFRESH_INTERVAL_MS);
this.previewTimer = setInterval(() => this.updateCompositePreview(), PREVIEW_INTERVAL_MS);
this.screenTimeoutTimer = setInterval(() => {
@@ -2520,9 +2522,9 @@ class DashboardController {
}
}
/** Re-send the head-tilt angle when the setting changed (from either settings UI). */
/** Re-send the head-tilt angle when it changed (the setting, from either settings UI, or the Glanceboard toggle). */
private applyGlanceAngleIfChanged(): void {
const degrees = glanceAngleDegrees();
const degrees = glanceAngleTriggerDegrees();
if (degrees === this.lastGlanceAngleDegrees) return;
this.lastGlanceAngleDegrees = degrees;
applyGlanceAngle();
@@ -2531,12 +2533,20 @@ class DashboardController {
/**
* The Glanceboard event a sleep-time input means, or null when the shell
* should see the input as usual. A display-wake carries the head-tilt
* gesture (HEAD_UP_EVENT) as a press; the double-tap display-wake stays the
* regular UI's wake.
* gesture (HEAD_UP_EVENT) as a tilt, which lasts until the head-down; the
* double-tap display-wake stays the regular UI's wake.
*/
private glanceEventFor(inputEvent: InputEvent, event: RawInputEvent): GlanceEvent | null {
if (inputEvent.type === "display-wake") {
return event.eventType === OsEventTypeList.HEAD_UP_EVENT ? this.glance.eventForGesture("head-tilt") : null;
if (event.eventType !== OsEventTypeList.HEAD_UP_EVENT) return null;
const glanceEvent = this.glance.eventForGesture("head-tilt");
if (glanceEvent?.type !== "tilt") return glanceEvent;
const maxMs = glanceTiltMaxMs({
resumedSession: this.evenHubSessionSuspended || this.evenHubResumePromise !== null,
triggerDegrees: glassesHeadUpTrigger() ?? (glanceAngleTriggerDegrees() || null),
tapTimeoutMs: glanceTapTimeoutMs(),
});
return { type: "tilt", maxMs };
}
return this.glance.eventForGesture(inputEvent.type);
}
+1 -1
View File
@@ -82,7 +82,7 @@ export class GlanceHost {
* The glance event a sleep-time gesture means under the board's settings,
* or null when the shell should see the input as usual: nothing while the
* board is disabled, no press from a tap when "Show on tap" is disabled,
* no hold when "Show on long press" is off, no press from a head-tilt when
* no hold when "Show on long press" is off, no tilt from a head-tilt when
* "Show on head tilt" is off (it then wakes the regular UI). A release
* always passes so a hold in progress can end.
*/
+41 -13
View File
@@ -1,9 +1,10 @@
/**
* The Glanceboard's visibility rules, as a pure reducer so they can be tested
* without the display plumbing. The board is an alternate sleep-time display:
* while the shell is asleep a press (single tap, or a head-tilt wake) shows it
* for a short while, a hold (long-press or short-then-long-press) shows it until release,
* and a double-tap dismisses it in favour of the regular UI. GlanceHost feeds
* while the shell is asleep a press (single tap) shows it for a short while, a
* head-tilt wake shows it until the head comes back down, a hold (long-press or
* short-then-long-press) shows it until release, and a double-tap dismisses it
* in favour of the regular UI. GlanceHost feeds
* these events in and applies the resulting visibility to the compositor.
*
* No NativeScript imports: this module is compiled and run under plain node
@@ -20,15 +21,38 @@ export const GLANCE_TIMEOUT_MS = 3000;
*/
export const GLANCE_TILT_MAX_MS = 60_000;
/**
* The firmware reports head-down once the pitch falls 20° below the head-up
* trigger (a fixed hysteresis in DRV_IMUCheckHeadUpEvent). Under this trigger,
* looking level again does not get below that, so the head-down may never come.
*/
export const GLANCE_LOW_TILT_TRIGGER_DEGREES = 25;
/**
* How long a head-tilt showing may last without a head-down. The full cap,
* except when the head-down is unlikely to arrive, where the tap duration is
* used instead: a tilt that woke a suspended EvenHub session (the head-down
* is only forwarded once the page is back, so a quick glance can end before
* that), or a trigger too low for a level gaze to count as looking down.
* triggerDegrees is the glasses' trigger, or null when not known.
*/
export function glanceTiltMaxMs(options: { resumedSession: boolean; triggerDegrees: number | null; tapTimeoutMs: number }): number {
const lowTrigger =
options.triggerDegrees !== null && options.triggerDegrees > 0 && options.triggerDegrees < GLANCE_LOW_TILT_TRIGGER_DEGREES;
return options.resumedSession || lowTrigger ? options.tapTimeoutMs : GLANCE_TILT_MAX_MS;
}
export type GlanceState = {
visible: boolean;
/** A long-press is holding the board up; the timer does not run meanwhile. */
holding: boolean;
/**
* A head-tilt is holding the board up: it stays until the firmware reports
* the head back down (or GLANCE_TILT_MAX_MS passes).
* the head back down (or tiltMaxMs passes).
*/
tilted: boolean;
/** The current head-tilt's cap (0 when not tilted); a hold ending mid-tilt re-arms it. */
tiltMaxMs: number;
/** When the board hides on its own (epoch ms), or null while held / hidden. */
hideAtMs: number | null;
};
@@ -36,8 +60,8 @@ export type GlanceState = {
export type GlanceEvent =
/** Single tap: show, and (re)start the auto-hide timer. */
| { type: "press" }
/** Head-tilt (IMU head-up): show until the head comes back down. */
| { type: "tilt" }
/** Head-tilt (IMU head-up): show until the head comes back down, or maxMs (default GLANCE_TILT_MAX_MS). */
| { type: "tilt"; maxMs?: number }
/** The IMU reported the head back down: ends a head-tilt showing. */
| { type: "head-down" }
/** Long-press: show and keep showing until the release. */
@@ -49,7 +73,7 @@ export type GlanceEvent =
/** Double-tap (or any other wake of the regular UI): hide now. */
| { type: "dismiss" };
export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, tilted: false, hideAtMs: null });
export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: null });
export function reduceGlance(
state: GlanceState,
@@ -61,20 +85,24 @@ export function reduceGlance(
case "press":
// A tap during a hold or a head-tilt changes nothing: those decide when it ends.
if (state.holding || state.tilted) return state;
return { visible: true, holding: false, tilted: false, hideAtMs: nowMs + timeoutMs };
case "tilt":
return { visible: true, holding: state.holding, tilted: true, hideAtMs: state.holding ? null : nowMs + GLANCE_TILT_MAX_MS };
return { visible: true, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: nowMs + timeoutMs };
case "tilt": {
const maxMs = event.maxMs ?? GLANCE_TILT_MAX_MS;
return { visible: true, holding: state.holding, tilted: true, tiltMaxMs: maxMs, hideAtMs: state.holding ? null : nowMs + maxMs };
}
case "head-down":
// Only a head-tilt showing ends with the head; a tap's timer or a hold keeps running.
if (!state.tilted) return state;
return state.holding ? { ...state, tilted: false } : GLANCE_HIDDEN;
return state.holding ? { ...state, tilted: false, tiltMaxMs: 0 } : GLANCE_HIDDEN;
case "hold":
return { visible: true, holding: true, tilted: state.tilted, hideAtMs: null };
return { visible: true, holding: true, tilted: state.tilted, tiltMaxMs: state.tiltMaxMs, hideAtMs: null };
case "release":
// Releases delivered late or after a disabled hold gesture mean nothing here.
if (!state.holding) return state;
// Head still up: the board stays, as if the tilt had just shown it.
return state.tilted ? { visible: true, holding: false, tilted: true, hideAtMs: nowMs + GLANCE_TILT_MAX_MS } : GLANCE_HIDDEN;
return state.tilted
? { visible: true, holding: false, tilted: true, tiltMaxMs: state.tiltMaxMs, hideAtMs: nowMs + state.tiltMaxMs }
: GLANCE_HIDDEN;
case "timeout":
if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state;
return GLANCE_HIDDEN;
+95 -48
View File
@@ -1,6 +1,8 @@
import { addImuListener, setImuReportEnabled, type ImuReading } from "../native/imu";
import { getHeadUpStatus, setHeadUpTrigger, type HeadUpStatus } from "../native/glasses-controls";
import { glanceAngleDegrees, glanceAngleSetting, type GlanceAngle } from "../apps/glanceboard/glanceboard-settings";
import { getStringSetting, setStringSetting } from "../native/settings-store";
import { glanceAngleSetting, glanceAngleTriggerDegrees, type GlanceAngle } from "../apps/glanceboard/glanceboard-settings";
import { angleBetweenDegrees, glanceAngleForTilt, meanVector, type Vec3 } from "./head-tilt-math";
// Same ImuReportPace code the accelerometer demo uses (not literal Hz).
const IMU_REPORT_PACE = 200;
@@ -9,27 +11,14 @@ const SAMPLE_WINDOW_MS = 1_200;
/** Give up when the glasses send nothing for this long. */
const FIRST_SAMPLE_TIMEOUT_MS = 3_000;
const MIN_SAMPLES = 3;
/** The setting's range; the firmware itself accepts 1..90. */
export const MIN_GLANCE_ANGLE = 5;
export const MAX_GLANCE_ANGLE = 60;
/** degrees/rounded are set on success; reason on failure. */
export type HeadAngleResult = { ok: boolean; degrees?: number; rounded?: number; reason?: string };
/** The level reading is used for a tilt reading taken within this long. */
const LEVEL_READING_VALID_MS = 60_000;
/**
* Measure how far the head is tilted up right now, from the glasses'
* accelerometer stream (the same IMU whose pitch the firmware compares with
* its head-up threshold).
*
* The temple's sensor axes are not documented, so this does not assume which
* axis points forward. When worn, gravity lies mostly along the sensor's
* vertical axis, and tilting the head up turns it toward the forward axis; the
* angle between gravity and the dominant axis is therefore the tilt, whichever
* axis that is and whichever sign it has. Head roll adds to it, so the wearer
* should keep the head level side to side. Past 45° the dominant axis swaps,
* which is why the setting stops at 60° and readings are clamped there.
* Average the glasses' accelerometer (gravity) over about a second, with the
* report stream on only for that long.
*/
export async function measureHeadTilt(): Promise<HeadAngleResult> {
async function measureGravity(): Promise<{ ok: true; gravity: Vec3 } | { ok: false; reason: string }> {
const samples: ImuReading[] = [];
let firstAt = 0;
const unsubscribe = addImuListener((reading) => {
@@ -55,46 +44,104 @@ export async function measureHeadTilt(): Promise<HeadAngleResult> {
if (samples.length < MIN_SAMPLES) {
return { ok: false, reason: samples.length === 0 ? "no sensor data" : "too few samples" };
}
let x = 0, y = 0, z = 0;
for (const s of samples) {
x += s.x;
y += s.y;
z += s.z;
}
x /= samples.length;
y /= samples.length;
z /= samples.length;
const magnitude = Math.hypot(x, y, z);
if (!(magnitude > 1e-6)) return { ok: false, reason: "sensor reads zero" };
const dominant = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
const degrees = (Math.acos(Math.min(1, dominant / magnitude)) * 180) / Math.PI;
return { ok: true, degrees, rounded: roundGlanceAngle(degrees) };
const gravity = meanVector(samples);
return gravity ? { ok: true, gravity } : { ok: false, reason: "sensor reads zero" };
}
/** Nearest multiple of 5, kept within the setting's range. */
export function roundGlanceAngle(degrees: number): number {
const rounded = Math.round(degrees / 5) * 5;
return Math.min(MAX_GLANCE_ANGLE, Math.max(MIN_GLANCE_ANGLE, rounded));
let levelReading: { gravity: Vec3; atMs: number } | null = null;
let measuring = false;
/** Which reading the next capture takes: the level one, or the tilted one after it. */
export function headAngleCaptureStep(): "level" | "tilt" {
return levelReading && Date.now() - levelReading.atMs < LEVEL_READING_VALID_MS ? "tilt" : "level";
}
export function headAngleCaptureRunning(): boolean {
return measuring;
}
/**
* Measure, store the rounded angle in the Head tilt angle setting, and send
* it to the glasses. Returns a short status for the menu row.
* One step of "Set to current head angle". The first call reads the head
* level; the next (within a minute) reads it tilted up, stores the angle
* between the two, rounded to 5°, as the Head tilt angle, and the
* controller's settings listener sends it to the glasses. Returns a short
* status for the menu row, or null when a capture is already running.
*/
export async function setGlanceAngleFromHead(): Promise<string> {
const result = await measureHeadTilt();
if (!result.ok) return `Failed: ${result.reason}`;
glanceAngleSetting.set(String(result.rounded) as GlanceAngle);
const sent = applyGlanceAngle();
return sent ? `Set to ${result.rounded}°` : `Saved ${result.rounded}° (glasses offline)`;
export async function captureHeadAngleStep(): Promise<string | null> {
if (measuring) return null;
measuring = true;
try {
const step = headAngleCaptureStep();
const result = await measureGravity();
if (result.ok === false) return `Failed: ${result.reason}`;
if (step === "level") {
levelReading = { gravity: result.gravity, atMs: Date.now() };
return "Now tilt up, select";
}
const level = levelReading!.gravity;
levelReading = null;
const angle = glanceAngleForTilt(angleBetweenDegrees(level, result.gravity));
if (angle.ok === false) return `Failed: ${angle.reason}`;
glanceAngleSetting.set(String(angle.rounded) as GlanceAngle);
return getHeadUpStatus() ? `Set to ${angle.rounded}°` : `Saved ${angle.rounded}° (offline)`;
} finally {
measuring = false;
}
}
/** Push the Head tilt angle setting to the session (on change and on each connect). */
/** Forget a level reading (the settings menu was reopened). */
export function resetHeadAngleCapture(): void {
if (!measuring) levelReading = null;
}
// The glasses' own head-up switch and angle from before Faceclaw first changed
// them, as "switch,angle", or "" when none is held. Kept here (not only in the
// session) so "Glasses default" can still restore them after an app restart.
const ORIGINAL_KEY = "glanceboard.headTiltOriginal";
function savedOriginal(): { switch: number; angle: number } | null {
const [sw, angle] = getStringSetting(ORIGINAL_KEY, "").split(",").map(Number);
return (sw === 0 || sw === 1) && Number.isInteger(angle) ? { switch: sw, angle } : null;
}
/**
* Push the wanted trigger to the session (on change and on each connect):
* the Head tilt angle while the Glanceboard is enabled, else none, which
* writes back the glasses' own values if Faceclaw changed them.
*/
export function applyGlanceAngle(): boolean {
return setHeadUpTrigger(glanceAngleDegrees());
const sent = setHeadUpTrigger(glanceAngleTriggerDegrees(), savedOriginal());
syncGlanceAngleOriginal();
return sent;
}
/** The glasses' current head-up trigger (angle + calibration), or null when not read yet. */
/**
* Keep the persisted original in step with the session: save the values the
* session captured before its first write, and drop them once a read shows
* them restored. Called on apply, from the controller's periodic refresh, and
* when the settings row draws.
*/
export function syncGlanceAngleOriginal(status: HeadUpStatus | null = getHeadUpStatus()): void {
if (!status) return;
const saved = savedOriginal();
if (!saved) {
if (status.originalSwitch === 0 || status.originalSwitch === 1) {
setStringSetting(ORIGINAL_KEY, `${status.originalSwitch},${status.originalAngle}`);
}
return;
}
if (
glanceAngleTriggerDegrees() === 0 &&
status.desired < 0 &&
!status.restorePending &&
status.switch === saved.switch &&
status.angle === saved.angle
) {
setStringSetting(ORIGINAL_KEY, "");
}
}
/** The glasses' current head-up trigger (angle + calibration), 0 when off, or null when not read yet. */
export function glassesHeadUpTrigger(status: HeadUpStatus | null = getHeadUpStatus()): number | null {
if (!status || status.switch < 0) return null;
return status.switch === 1 ? status.angle + status.calibration : 0;
+58
View File
@@ -0,0 +1,58 @@
/**
* Math for "Set to current head angle", kept free of NativeScript imports so
* tests/head-tilt-math.test.cjs can run it under plain node.
*
* The temple's accelerometer axes are not documented (nor whether the report
* stream applies the firmware's mount matrix), so nothing here assumes which
* axis points forward. Instead the capture takes two readings, one with the
* head level and one tilted up, and the tilt is the angle between the two
* gravity vectors. That works for any mounting and any angle; what it cannot
* tell is direction (tilting down by 30° reads the same as up by 30°), so the
* row's instructions say to tilt up, and head roll between the two readings
* adds to the result.
*/
export type Vec3 = { x: number; y: number; z: number };
/** The setting's range; the firmware itself accepts 1..90. */
export const MIN_GLANCE_ANGLE = 5;
export const MAX_GLANCE_ANGLE = 60;
/** Mean of the samples, or null when there are none or they sum to zero. */
export function meanVector(samples: readonly Vec3[]): Vec3 | null {
if (samples.length === 0) return null;
let x = 0, y = 0, z = 0;
for (const s of samples) {
x += s.x;
y += s.y;
z += s.z;
}
const mean = { x: x / samples.length, y: y / samples.length, z: z / samples.length };
return Math.hypot(mean.x, mean.y, mean.z) > 1e-6 ? mean : null;
}
/** Angle between two gravity readings, in degrees (0..180). */
export function angleBetweenDegrees(a: Vec3, b: Vec3): number {
const dot = a.x * b.x + a.y * b.y + a.z * b.z;
const cross = Math.hypot(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
// atan2 stays accurate near 0° and 180°, where acos of the normalized dot does not.
return (Math.atan2(cross, dot) * 180) / Math.PI;
}
/** Nearest multiple of 5, kept within the setting's range. */
export function roundGlanceAngle(degrees: number): number {
const rounded = Math.round(degrees / 5) * 5;
return Math.min(MAX_GLANCE_ANGLE, Math.max(MIN_GLANCE_ANGLE, rounded));
}
/**
* The setting value for a measured tilt, or why it cannot be used: a tilt that
* would round below the minimum is reported (a head that did not move, not a
* 5° trigger), and so is one past the maximum.
*/
export function glanceAngleForTilt(degrees: number): { ok: true; rounded: number } | { ok: false; reason: string } {
if (!Number.isFinite(degrees)) return { ok: false, reason: "no reading" };
if (degrees < MIN_GLANCE_ANGLE - 2.5) return { ok: false, reason: "head not tilted" };
if (degrees >= MAX_GLANCE_ANGLE + 2.5) return { ok: false, reason: `over ${MAX_GLANCE_ANGLE}°` };
return { ok: true, rounded: roundGlanceAngle(degrees) };
}