Simplify glanceboard-trigger angle setting; fix incorrectly-rotated accelerometer readings on G1
The glanceboard head-tilt option was more complicated than necessary, because it wanted the user to take two angle measurements, a baseline angle and a tilt angle. That was a workaround to not knowing the accelerometer's orientation/calibration, but actually, we do know those. (Except, as it turns out, on the G1, the accelerometer orientation is different than it is on the G2, and there was a bug where EvenHub apps got their accelerometer readings in the wrong orientation. So for example the EvenHub Level app didn't work correctly.)
This commit is contained in:
1 parent
37d81d0308
commit
e09f8eae96
17 files changed
+255
-281
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||
import { addImuListener, imuPitchDegrees, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
@@ -23,7 +23,8 @@ const BAR_RIGHT_INSET = 4;
|
||||
/**
|
||||
* Accelerometer demo: turns the IMU stream on while this page is visible (the
|
||||
* Developer window is foregrounded and the screen is on), off otherwise, and
|
||||
* shows the latest x/y/z reading with a simple per-axis bar.
|
||||
* shows the latest x/y/z reading (Faceclaw's frame, imu-types.ts) with a
|
||||
* simple per-axis bar, and the head's pitch.
|
||||
*
|
||||
* The IMU stream is experimental — the firmware may never populate it — so the
|
||||
* page also reports how many samples have arrived.
|
||||
@@ -92,7 +93,8 @@ export class AccelerometerDemoLayer implements Layer {
|
||||
const bodyX = padding.left + BODY_INDENT;
|
||||
|
||||
image.drawText(small, padding.left, top, "Accelerometer", 220);
|
||||
const status = this.enabled ? "IMU on" : "IMU off";
|
||||
const pitch = this.reading ? `pitch ${Math.round(imuPitchDegrees(this.reading))}° ` : "";
|
||||
const status = pitch + (this.enabled ? "IMU on" : "IMU off");
|
||||
image.drawText(small, right - small.measureText(status), top, status, 150);
|
||||
|
||||
if (this.reading) {
|
||||
|
||||
@@ -5,16 +5,9 @@ import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from
|
||||
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawRightValueMenuItem, drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import {
|
||||
captureHeadAngleStep,
|
||||
glassesHeadUpTrigger,
|
||||
headAngleCaptureRunning,
|
||||
headAngleCaptureStep,
|
||||
resetHeadAngleCapture,
|
||||
syncGlanceAngleOriginal,
|
||||
} from "../../g2/head-angle";
|
||||
import { getHeadUpStatus, headUpControlsAvailable } from "../../native/glasses-controls";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import { liveHeadPitchFooter } from "../../g2/head-angle";
|
||||
import { headUpControlsAvailable } from "../../native/glasses-controls";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
@@ -342,8 +335,13 @@ function openGlanceSettings(ctx: LayerContext): void {
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
enumSettingMenuItem(glanceTiltTimeoutSetting),
|
||||
// Only Android can change the glasses' head-up setting (glasses-controls.ts).
|
||||
// The controller's settings listener sends a new angle to the glasses.
|
||||
...(headUpControlsAvailable() ? [enumSettingMenuItem(glanceAngleSetting), headAngleCaptureMenuItem()] : []),
|
||||
// The controller's settings listener sends a new angle to the glasses; the
|
||||
// picker shows the head's current angle to choose by.
|
||||
...(headUpControlsAvailable()
|
||||
? [enumSettingMenuItem(glanceAngleSetting, {
|
||||
pickerFooter: liveHeadPitchFooter(() => shell.isWindowVisible(GLANCEBOARD_WINDOW_ID)),
|
||||
})]
|
||||
: []),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
uiFontOverridePickerMenuItem(glanceFont, {
|
||||
title: "Glanceboard font",
|
||||
@@ -353,56 +351,10 @@ function openGlanceSettings(ctx: LayerContext): void {
|
||||
enumSettingMenuItem(glanceDepthSetting),
|
||||
...widgetSettingsMenuItems(),
|
||||
];
|
||||
resetHeadAngleCapture();
|
||||
headAngleCapture = null;
|
||||
const layer = new MenuLayer("Glanceboard settings", items(), PAGE_MENU_LAYOUT);
|
||||
ctx.stack.push(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* "Set to current head angle", in two selects: one with the head level, one
|
||||
* tilted up to where the display should wake (head-angle.ts). The row shows
|
||||
* the step or the last result; otherwise the glasses' current trigger. A
|
||||
* result is shown only while the Head tilt angle still has the value it was
|
||||
* shown with, and only until the menu is reopened.
|
||||
*/
|
||||
let headAngleCapture: { status: string; angleSetting: string } | null = null;
|
||||
|
||||
function headAngleCaptureMenuItem(): MenuItem {
|
||||
const label = "Set to current head angle";
|
||||
const setStatus = (status: string) => {
|
||||
headAngleCapture = { status, angleSetting: glanceAngleSetting.get() };
|
||||
};
|
||||
return {
|
||||
label,
|
||||
description:
|
||||
"Two steps. Look straight ahead, keep still and select; then tilt your head up to where the display should wake, keep still and select again. " +
|
||||
"Faceclaw reads the glasses' angle for about a second each time and saves the difference, rounded to 5°, as the Head tilt angle. Keep your head level side to side.",
|
||||
onSelect: async (ctx) => {
|
||||
// Selects during a measurement are ignored; it would share the IMU stream.
|
||||
if (headAngleCaptureRunning()) return;
|
||||
setStatus("Measuring...");
|
||||
ctx.actions.requestRender();
|
||||
const status = await captureHeadAngleStep();
|
||||
if (status !== null) setStatus(status);
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
const status = getHeadUpStatus();
|
||||
syncGlanceAngleOriginal(status);
|
||||
let value: string;
|
||||
if (headAngleCapture && headAngleCapture.angleSetting === glanceAngleSetting.get() &&
|
||||
(headAngleCaptureRunning() || headAngleCaptureStep() === "tilt" || !headAngleCapture.status.startsWith("Now"))) {
|
||||
value = headAngleCapture.status;
|
||||
} else {
|
||||
const current = glassesHeadUpTrigger(status);
|
||||
value = current === null ? "" : current === 0 ? "glasses: off" : `glasses: ${current}°`;
|
||||
}
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, value);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* One row per widget on the board, in slot order: a submenu of the widget's
|
||||
* settings, or a disabled row for a widget that has none.
|
||||
|
||||
@@ -173,7 +173,7 @@ export const glanceAngleSetting = new ConfigSettingEnum<GlanceAngle>({
|
||||
values: GLANCE_ANGLE_VALUES,
|
||||
formatValue: (value) => (value === "glasses" ? "Glasses default" : `${value}°`),
|
||||
description:
|
||||
"How far you tilt your head up to wake the display. Glasses default uses the glasses' own setting from the Even app. " +
|
||||
"How far you tilt your head up to wake the display; the list shows your head's angle now. Glasses default uses the glasses' own setting from the Even app. " +
|
||||
"The board hides when your head drops 20° below this angle; under 25°, looking straight ahead may not count, so it hides after the tap duration instead.",
|
||||
});
|
||||
|
||||
|
||||
+81
-82
@@ -1,97 +1,100 @@
|
||||
import { addImuListener, setImuReportEnabled, type ImuReading } from "../native/imu";
|
||||
import { addImuListener, imuPitchDegrees, setImuReportEnabled, type ImuReading } from "../native/imu";
|
||||
import { getHeadUpStatus, setHeadUpTrigger, type HeadUpStatus } from "../native/glasses-controls";
|
||||
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";
|
||||
import { glanceAngleTriggerDegrees } from "../apps/glanceboard/glanceboard-settings";
|
||||
|
||||
// Same ImuReportPace code the accelerometer demo uses (not literal Hz).
|
||||
const IMU_REPORT_PACE = 200;
|
||||
/** How long to average once samples start arriving. */
|
||||
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 level reading is used for a tilt reading taken within this long. */
|
||||
const LEVEL_READING_VALID_MS = 60_000;
|
||||
/** How often the live reading checks visibility and repaints a changed value. */
|
||||
const LIVE_TICK_MS = 250;
|
||||
/** Weight of each new sample in the smoothed pitch. */
|
||||
const LIVE_SMOOTHING = 0.3;
|
||||
/** A reading older than this (or none this long after the stream started) is shown as missing. */
|
||||
const LIVE_STALE_MS = 3_000;
|
||||
|
||||
/**
|
||||
* Average the glasses' accelerometer (gravity) over about a second, with the
|
||||
* report stream on only for that long.
|
||||
* The head's pitch, live, for the Head tilt angle picker. The IMU report
|
||||
* stream runs while `visible()` holds (checked every tick, so it stops while
|
||||
* the screen is off or the window is in the background) and until stop().
|
||||
* text() is the footer line; a changed value repaints at most once a tick.
|
||||
*/
|
||||
async function measureGravity(): Promise<{ ok: true; gravity: Vec3 } | { ok: false; reason: string }> {
|
||||
const samples: ImuReading[] = [];
|
||||
let firstAt = 0;
|
||||
const unsubscribe = addImuListener((reading) => {
|
||||
class LiveHeadPitch {
|
||||
private pitch: number | null = null;
|
||||
private lastSampleAt = 0;
|
||||
private streaming = false;
|
||||
private streamingSince = 0;
|
||||
private shown = "";
|
||||
private readonly unsubscribe: () => void;
|
||||
private readonly timer: ReturnType<typeof setInterval>;
|
||||
|
||||
constructor(
|
||||
private readonly visible: () => boolean,
|
||||
private readonly repaint: () => void,
|
||||
) {
|
||||
this.unsubscribe = addImuListener((reading) => this.onReading(reading));
|
||||
this.timer = setInterval(() => this.tick(), LIVE_TICK_MS);
|
||||
this.tick();
|
||||
}
|
||||
|
||||
text(): string {
|
||||
return this.shown;
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
clearInterval(this.timer);
|
||||
this.unsubscribe();
|
||||
if (this.streaming) setImuReportEnabled(false, IMU_REPORT_PACE);
|
||||
this.streaming = false;
|
||||
}
|
||||
|
||||
private onReading(reading: ImuReading): void {
|
||||
// Glasses temples only (1 right, 3 left, 0 unspecified); the ring is 2.
|
||||
if (reading.source === 2) return;
|
||||
if (!Number.isFinite(reading.x) || !Number.isFinite(reading.y) || !Number.isFinite(reading.z)) return;
|
||||
if (firstAt === 0) firstAt = Date.now();
|
||||
samples.push(reading);
|
||||
});
|
||||
setImuReportEnabled(true, IMU_REPORT_PACE);
|
||||
try {
|
||||
const start = Date.now();
|
||||
while (true) {
|
||||
await delay(100);
|
||||
const now = Date.now();
|
||||
if (firstAt === 0 && now - start >= FIRST_SAMPLE_TIMEOUT_MS) break;
|
||||
if (firstAt !== 0 && now - firstAt >= SAMPLE_WINDOW_MS) break;
|
||||
if (!this.streaming || reading.source === 2) return;
|
||||
if (!(Math.hypot(reading.x, reading.y, reading.z) > 1e-6)) return;
|
||||
const pitch = imuPitchDegrees(reading);
|
||||
this.pitch = this.pitch === null ? pitch : this.pitch + LIVE_SMOOTHING * (pitch - this.pitch);
|
||||
this.lastSampleAt = Date.now();
|
||||
}
|
||||
|
||||
private tick(): void {
|
||||
const now = Date.now();
|
||||
const visible = this.visible();
|
||||
if (visible !== this.streaming) {
|
||||
setImuReportEnabled(visible, IMU_REPORT_PACE);
|
||||
this.streaming = visible;
|
||||
this.streamingSince = now;
|
||||
this.pitch = null;
|
||||
}
|
||||
const text = this.format(now);
|
||||
if (text !== this.shown) {
|
||||
this.shown = text;
|
||||
this.repaint();
|
||||
}
|
||||
} finally {
|
||||
setImuReportEnabled(false, IMU_REPORT_PACE);
|
||||
unsubscribe();
|
||||
}
|
||||
if (samples.length < MIN_SAMPLES) {
|
||||
return { ok: false, reason: samples.length === 0 ? "no sensor data" : "too few samples" };
|
||||
|
||||
private format(now: number): string {
|
||||
if (this.pitch !== null && now - this.lastSampleAt < LIVE_STALE_MS) return `Head now: ${Math.round(this.pitch)}°`;
|
||||
return now - Math.max(this.streamingSince, this.lastSampleAt) < LIVE_STALE_MS ? "Head now: …" : "Head now: no data";
|
||||
}
|
||||
const gravity = meanVector(samples);
|
||||
return gravity ? { ok: true, gravity } : { ok: false, reason: "sensor reads zero" };
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* The Head tilt angle picker's footer: the head's current pitch, read while
|
||||
* the picker is open and `visible()` holds (enumSettingMenuItem pickerFooter).
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/** Forget a level reading (the settings menu was reopened). */
|
||||
export function resetHeadAngleCapture(): void {
|
||||
if (!measuring) levelReading = null;
|
||||
export function liveHeadPitchFooter(visible: () => boolean): { text: () => string; open: (repaint: () => void) => () => void } {
|
||||
let reader: LiveHeadPitch | null = null;
|
||||
return {
|
||||
text: () => reader?.text() ?? "",
|
||||
open: (repaint) => {
|
||||
const opened = new LiveHeadPitch(visible, repaint);
|
||||
reader = opened;
|
||||
return () => {
|
||||
opened.stop();
|
||||
if (reader === opened) reader = null;
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// The glasses' own head-up switch and angle from before Faceclaw first changed
|
||||
@@ -146,7 +149,3 @@ export function glassesHeadUpTrigger(status: HeadUpStatus | null = getHeadUpStat
|
||||
if (!status || status.switch < 0) return null;
|
||||
return status.switch === 1 ? status.angle + status.calibration : 0;
|
||||
}
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* 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) };
|
||||
}
|
||||
@@ -1,7 +1,23 @@
|
||||
/** Platform-neutral IMU types, shared by imu.ts and imu.ios.ts. */
|
||||
|
||||
/**
|
||||
* An accelerometer sample in g, in Faceclaw's frame for every glasses model
|
||||
* (the G2's own; the session rotates other models' samples into it, see
|
||||
* ImuOrientation.kt): +x forward, +y to the wearer's left, +z up through the
|
||||
* top of the head. At rest it is the "up" direction: head level reads
|
||||
* (0, 0, 1), looking 45° up (0.71, 0, 0.71), the head tilted 45° to the right
|
||||
* (0, 0.71, 0.71).
|
||||
*/
|
||||
export type ImuReading = { x: number; y: number; z: number; source: number };
|
||||
|
||||
/**
|
||||
* How far the head is tilted up from level, in degrees (negative: down), from
|
||||
* a reading taken at rest. Rolling the head to the side does not change it.
|
||||
*/
|
||||
export function imuPitchDegrees(reading: { x: number; y: number; z: number }): number {
|
||||
return (Math.atan2(reading.x, Math.hypot(reading.y, reading.z)) * 180) / Math.PI;
|
||||
}
|
||||
|
||||
/** Map an IMU sample's EventSource to a short label. */
|
||||
export function imuSourceLabel(source: number): string {
|
||||
switch (source) {
|
||||
|
||||
@@ -1724,9 +1724,18 @@ type SettingsMenuOptions<T> = {
|
||||
label?: string
|
||||
}
|
||||
|
||||
type EnumSettingMenuOptions<T> = SettingsMenuOptions<T> & {
|
||||
/**
|
||||
* A live line under the picker's values, such as a sensor reading to pick
|
||||
* by. open() runs as the picker opens, with a function that repaints it,
|
||||
* and returns what to run when the picker closes.
|
||||
*/
|
||||
pickerFooter?: { text: () => string; open: (repaint: () => void) => () => void }
|
||||
}
|
||||
|
||||
export function enumSettingMenuItem<TValue extends string, TId extends string = string>(
|
||||
setting: ConfigSettingEnum<TValue, TId>,
|
||||
opts?: SettingsMenuOptions<TValue>
|
||||
opts?: EnumSettingMenuOptions<TValue>
|
||||
): MenuItem {
|
||||
const label = opts?.label ?? setting.label;
|
||||
return {
|
||||
@@ -1754,7 +1763,11 @@ export function enumSettingMenuItem<TValue extends string, TId extends string =
|
||||
);
|
||||
},
|
||||
}));
|
||||
openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
const footer = opts?.pickerFooter;
|
||||
openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current)), {
|
||||
footer: footer?.text,
|
||||
onClosed: footer?.open(() => ctx.actions.requestRender()),
|
||||
});
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, setting.displayValue(setting.get()));
|
||||
|
||||
+33
-6
@@ -57,7 +57,7 @@ export type MenuLayout = {
|
||||
* Dim hint line pinned to the bottom edge inside the box, below the items
|
||||
* (gesture help such as "●— system menu"). Takes a line off the item area.
|
||||
*/
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
/** Brightness factor for everything beneath the menu (see Layer.dimUnderneath). Default: none. */
|
||||
dimUnderneath?: number;
|
||||
/**
|
||||
@@ -83,9 +83,16 @@ export type MenuPageLayout = {
|
||||
/** Width of the rows. Default (and at most): the window's, inside its side padding. */
|
||||
width?: number;
|
||||
/** Dim hint line pinned above the window's bottom padding, below the rows. */
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
};
|
||||
|
||||
/** A menu's footer line: fixed text, or read on every paint (a live value). */
|
||||
export type MenuFooter = string | (() => string);
|
||||
|
||||
function footerText(footer: MenuFooter): string {
|
||||
return typeof footer === "function" ? footer() : footer;
|
||||
}
|
||||
|
||||
export type MenuItemRenderArgs = {
|
||||
image: GrayImage;
|
||||
x: number;
|
||||
@@ -360,7 +367,7 @@ export class MenuLayer implements Layer {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
}
|
||||
if (layout.footer) {
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, layout.footer, 110);
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, footerText(layout.footer), 110);
|
||||
}
|
||||
|
||||
const bodyY = y + chromeTop;
|
||||
@@ -397,7 +404,7 @@ export class MenuLayer implements Layer {
|
||||
let fixedBottom = height - padding.bottom;
|
||||
if (layout.footer) {
|
||||
const footerY = height - padding.bottom - font.lineHeight;
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, layout.footer, 110);
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, footerText(layout.footer), 110);
|
||||
bottom = fixedBottom = footerY - MENU_FOOTER_GAP;
|
||||
}
|
||||
if (bottom <= top) return image;
|
||||
@@ -431,17 +438,26 @@ export class MenuLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
export type ModalMenuOptions = {
|
||||
/** A dim line under the items (see MenuLayout.footer). */
|
||||
footer?: MenuFooter;
|
||||
/** Runs when the menu leaves the stack, however it closes. */
|
||||
onClosed?: () => void;
|
||||
};
|
||||
|
||||
/** Open a centered, bordered menu over the current layer. */
|
||||
export function openModalMenu(
|
||||
ctx: LayerContext,
|
||||
title: string,
|
||||
items: MenuItem[],
|
||||
initialSelectedIndex = 0,
|
||||
options: ModalMenuOptions = {},
|
||||
): void {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const font = getDefaultSmallFont();
|
||||
const menuWidth = Math.min(320, width - 40);
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font);
|
||||
const footerHeight = options.footer ? MENU_FOOTER_GAP + lineStep(font) : 0;
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font) + footerHeight;
|
||||
// 20px clear above and below, or 8 in a short window (the RayNeo iO's),
|
||||
// where those rows are worth more than the margin.
|
||||
const marginY = height < SHORT_WINDOW_HEIGHT ? 8 : 20;
|
||||
@@ -454,8 +470,19 @@ export function openModalMenu(
|
||||
maxHeight: menuHeight,
|
||||
// Floats nearer than the content it opens over, like the window and system menus.
|
||||
depth: 4,
|
||||
footer: options.footer,
|
||||
};
|
||||
ctx.stack.push(new MenuLayer(title, items, layout).selectItem(initialSelectedIndex));
|
||||
ctx.stack.push(new ModalMenuLayer(title, items, layout, options.onClosed).selectItem(initialSelectedIndex));
|
||||
}
|
||||
|
||||
class ModalMenuLayer extends MenuLayer {
|
||||
constructor(title: string, items: MenuItem[], layout: MenuLayout, private readonly onClosed?: () => void) {
|
||||
super(title, items, layout);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.onClosed?.();
|
||||
}
|
||||
}
|
||||
|
||||
export function isMenuItemDisabled(item: MenuItem): boolean {
|
||||
|
||||
+2
@@ -3,6 +3,8 @@ package com.faceclaw.app
|
||||
/**
|
||||
* Receives IMU (accelerometer) readings decoded from the glasses' sys-event stream. Registered on
|
||||
* FaceclawBleCommunicator via addImuListener; callbacks are delivered on the Android main thread.
|
||||
* x, y, z are in g, in the canonical frame of every glasses model (+x forward, +y left, +z up;
|
||||
* see ImuOrientation).
|
||||
*/
|
||||
interface FaceclawImuListener {
|
||||
fun onImuData(x: Double, y: Double, z: Double, eventSource: Int): Unit
|
||||
|
||||
+3
-1
@@ -653,14 +653,16 @@ internal fun GlassesSessionCore.finishFrame(frameId: Int, outcome: String) {
|
||||
}
|
||||
}
|
||||
|
||||
/** [x], [y], [z] as the hardware reports them; listeners get them in [ImuOrientation]'s frame. */
|
||||
internal fun GlassesSessionCore.emitImuData(x: Double, y: Double, z: Double, eventSource: Int) {
|
||||
if (imuListeners.isEmpty()) {
|
||||
return
|
||||
}
|
||||
val (cx, cy, cz) = ImuOrientation.toCanonical(glassesType, x, y, z)
|
||||
host.postToMain {
|
||||
for (imuListener in imuListeners) {
|
||||
try {
|
||||
imuListener.onImuData(x, y, z, eventSource)
|
||||
imuListener.onImuData(cx, cy, cz, eventSource)
|
||||
} catch (t: Throwable) {
|
||||
logWarn("listener onImuData failed", t)
|
||||
}
|
||||
|
||||
+2
-1
@@ -299,7 +299,8 @@ internal fun GlassesSessionCore.g1StopAudioCapture() {
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no magnetometer on a G1, only the accelerometer and gyroscope: x, y, z in g, head frame, from the right arm.
|
||||
* There is no magnetometer on a G1, only the accelerometer and gyroscope: x, y, z in g from the right arm, in the
|
||||
* G1's own axes, which [emitImuData] rotates into the G2's ([ImuOrientation]).
|
||||
* [enable] raises the sample rate and polls it; [g1MaxSleepMs] follows.
|
||||
*/
|
||||
internal fun GlassesSessionCore.g1SetImuReport(enable: Boolean) {
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* Faceclaw's accelerometer frame. Every model's samples are rotated into it before they reach a
|
||||
* FaceclawImuListener ([GlassesSessionCore.emitImuData]), so Faceclaw apps and EvenHub pages see
|
||||
* the same frame whichever glasses are worn. It is the G2's own frame, in g, glasses worn:
|
||||
*
|
||||
* +x forward (the way the wearer faces), +y to the wearer's left, +z up through the top of the head.
|
||||
*
|
||||
* At rest a reading is the "up" direction (the reaction to gravity): head level reads (0, 0, 1),
|
||||
* looking 45° up (0.71, 0, 0.71), and the head tilted 45° to the right (right ear down)
|
||||
* (0, 0.71, 0.71). Pitch above level is atan2(x, hypot(y, z)).
|
||||
*/
|
||||
object ImuOrientation {
|
||||
/**
|
||||
* A sample as [type]'s hardware reports it, in the canonical frame. Axes measured on hardware
|
||||
* with the Developer app's accelerometer page:
|
||||
* - G2: already canonical.
|
||||
* - G1 (g1v0): +x right, +y up, +z back. Level reads (0, 1, 0), 45° up (0, 0.71, -0.71),
|
||||
* 45° to the right (-0.71, 0.71, 0).
|
||||
* - RayNeo iO: no accelerometer stream yet, so nothing to rotate; measure its axes when one
|
||||
* is added.
|
||||
*/
|
||||
fun toCanonical(type: GlassesType, x: Double, y: Double, z: Double): DoubleArray = when (type) {
|
||||
GlassesType.G2 -> doubleArrayOf(x, y, z)
|
||||
GlassesType.G1 -> doubleArrayOf(-z, -x, y)
|
||||
GlassesType.RAYNEO_IO -> doubleArrayOf(x, y, z)
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const {
|
||||
MAX_GLANCE_ANGLE,
|
||||
MIN_GLANCE_ANGLE,
|
||||
angleBetweenDegrees,
|
||||
glanceAngleForTilt,
|
||||
meanVector,
|
||||
roundGlanceAngle,
|
||||
} = require("../.test-build/app/g2/head-tilt-math.js");
|
||||
|
||||
const rad = (deg) => (deg * Math.PI) / 180;
|
||||
|
||||
/**
|
||||
* Gravity (m/s^2) after pitching the head by pitchDeg, in a sensor frame
|
||||
* mounted so that no axis lines up with the head: the level reading points
|
||||
* along an arbitrary direction, and the pitch rotates it about an arbitrary
|
||||
* axis perpendicular to that (Rodrigues' formula).
|
||||
*/
|
||||
function gravityPitched(pitchDeg) {
|
||||
const v = normalize({ x: 0.3, y: -0.9, z: 0.2 });
|
||||
const k0 = { x: 0.9, y: 0.4, z: 0.45 };
|
||||
const dot = k0.x * v.x + k0.y * v.y + k0.z * v.z;
|
||||
const k = normalize({ x: k0.x - dot * v.x, y: k0.y - dot * v.y, z: k0.z - dot * v.z });
|
||||
const c = Math.cos(rad(pitchDeg));
|
||||
const s = Math.sin(rad(pitchDeg));
|
||||
const cross = { x: k.y * v.z - k.z * v.y, y: k.z * v.x - k.x * v.z, z: k.x * v.y - k.y * v.x };
|
||||
return { x: 9.8 * (v.x * c + cross.x * s), y: 9.8 * (v.y * c + cross.y * s), z: 9.8 * (v.z * c + cross.z * s) };
|
||||
}
|
||||
|
||||
function normalize(v) {
|
||||
const m = Math.hypot(v.x, v.y, v.z);
|
||||
return { x: v.x / m, y: v.y / m, z: v.z / m };
|
||||
}
|
||||
|
||||
test("the tilt between two readings does not depend on the sensor's axes, past 45° too", () => {
|
||||
const level = gravityPitched(0);
|
||||
for (const pitch of [5, 20, 30, 45, 50, 60, 75]) {
|
||||
assert.ok(Math.abs(angleBetweenDegrees(level, gravityPitched(pitch)) - pitch) < 1e-6, `pitch ${pitch}`);
|
||||
}
|
||||
// From a non-zero level reading the difference is what counts.
|
||||
assert.ok(Math.abs(angleBetweenDegrees(gravityPitched(-10), gravityPitched(40)) - 50) < 1e-6);
|
||||
assert.ok(Math.abs(angleBetweenDegrees(level, level)) < 1e-6);
|
||||
});
|
||||
|
||||
test("meanVector averages and rejects empty or zero input", () => {
|
||||
assert.deepEqual(meanVector([{ x: 1, y: 2, z: 3 }, { x: 3, y: 2, z: 1 }]), { x: 2, y: 2, z: 2 });
|
||||
assert.equal(meanVector([]), null);
|
||||
assert.equal(meanVector([{ x: 0, y: 0, z: 0 }]), null);
|
||||
});
|
||||
|
||||
test("roundGlanceAngle rounds to 5° within the setting's range", () => {
|
||||
assert.equal(roundGlanceAngle(27.4), 25);
|
||||
assert.equal(roundGlanceAngle(27.5), 30);
|
||||
assert.equal(roundGlanceAngle(1), MIN_GLANCE_ANGLE);
|
||||
assert.equal(roundGlanceAngle(80), MAX_GLANCE_ANGLE);
|
||||
});
|
||||
|
||||
test("glanceAngleForTilt reports a head that did not move or went too far instead of clamping", () => {
|
||||
assert.deepEqual(glanceAngleForTilt(31), { ok: true, rounded: 30 });
|
||||
assert.deepEqual(glanceAngleForTilt(52), { ok: true, rounded: 50 });
|
||||
assert.deepEqual(glanceAngleForTilt(3), { ok: true, rounded: 5 });
|
||||
assert.equal(glanceAngleForTilt(0.5).ok, false);
|
||||
assert.equal(glanceAngleForTilt(65).ok, false);
|
||||
assert.equal(glanceAngleForTilt(NaN).ok, false);
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const { imuPitchDegrees } = require("../.test-build/app/native/imu-types.js");
|
||||
|
||||
const rad = (deg) => (deg * Math.PI) / 180;
|
||||
|
||||
/**
|
||||
* A reading at rest in the canonical frame (+x forward, +y left, +z up), in
|
||||
* m/s^2 to show the scale does not matter: the head pitched up by pitchDeg,
|
||||
* then rolled by rollDeg.
|
||||
*/
|
||||
function reading(pitchDeg, rollDeg = 0) {
|
||||
const p = rad(pitchDeg);
|
||||
const r = rad(rollDeg);
|
||||
return { x: 9.8 * Math.sin(p), y: 9.8 * Math.cos(p) * Math.sin(r), z: 9.8 * Math.cos(p) * Math.cos(r) };
|
||||
}
|
||||
|
||||
test("imuPitchDegrees reads the head's pitch from one reading, past 45° and with the head rolled", () => {
|
||||
for (const pitch of [-30, 0, 5, 20, 45, 50, 60, 75]) {
|
||||
for (const roll of [0, 30, -40]) {
|
||||
assert.ok(Math.abs(imuPitchDegrees(reading(pitch, roll)) - pitch) < 1e-6, `pitch ${pitch} roll ${roll}`);
|
||||
}
|
||||
}
|
||||
// The poses measured on a G2 (in g).
|
||||
const s = Math.SQRT1_2;
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: 0, z: 1 })) < 1e-9);
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: s, y: 0, z: s }) - 45) < 1e-9);
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: s, z: s })) < 1e-9);
|
||||
});
|
||||
@@ -1130,7 +1130,8 @@ class G1FeaturesTest {
|
||||
}
|
||||
s.core.setImuReportEnabled(true, 200)
|
||||
assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail())
|
||||
assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
// The G1's (x, y, z) arrive in the G2's frame as (-z, -x, y) (ImuOrientation).
|
||||
assertEquals(listOf(-0.25, -0.1, -1.0, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } })
|
||||
s.core.setImuReportEnabled(false, 0)
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } })
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
import kotlin.test.*
|
||||
|
||||
class ImuOrientationTest {
|
||||
private val s = sqrt(0.5)
|
||||
|
||||
private fun assertVec(expected: DoubleArray, actual: DoubleArray, what: String) {
|
||||
for (i in 0..2) assertTrue(abs(expected[i] - actual[i]) < 1e-9, "$what: expected ${expected.toList()}, got ${actual.toList()}")
|
||||
}
|
||||
|
||||
/** The poses measured on hardware (level, 45° up, 45° to the right) read the G2's values on every model. */
|
||||
@Test fun measuredPosesMatchTheG2Frame() {
|
||||
val level = doubleArrayOf(0.0, 0.0, 1.0)
|
||||
val up = doubleArrayOf(s, 0.0, s)
|
||||
val right = doubleArrayOf(0.0, s, s)
|
||||
val g1 = listOf(doubleArrayOf(0.0, 1.0, 0.0), doubleArrayOf(0.0, s, -s), doubleArrayOf(-s, s, 0.0))
|
||||
for ((name, canonical, raw) in listOf(Triple("level", level, g1[0]), Triple("up", up, g1[1]), Triple("right", right, g1[2]))) {
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G1, raw[0], raw[1], raw[2]), "G1 $name")
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G2, canonical[0], canonical[1], canonical[2]), "G2 $name")
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -46,7 +46,7 @@
|
||||
"../app/g2/glasses-artwork.ts",
|
||||
"../app/g2/glasses-display-state.ts",
|
||||
"../app/g2/glance-state.ts",
|
||||
"../app/g2/head-tilt-math.ts",
|
||||
"../app/native/imu-types.ts",
|
||||
"../app/ui/shell/notification-modal-queue.ts",
|
||||
"../app/ui/shell/notification-popup-delay.ts",
|
||||
"../app/ui/shell/notification-popup-timeout.ts",
|
||||
|
||||
Reference in new issue
Block a user