diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt index b9837e79..d4ec955b 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -250,6 +250,9 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre fun enableWearDetectionAndRequestState() = core.enableWearDetectionAndRequestState() fun configureHeadUpWake(enabled: Int, angle: Int) = core.configureHeadUpWake(enabled, angle) + fun beginHeadAngleMeasurement(): Boolean = core.beginHeadAngleMeasurement() + fun endHeadAngleMeasurement() = core.endHeadAngleMeasurement() + fun getHeadUpCalibration(): Double = core.getHeadUpCalibration() fun startBandwidthBenchmark(messageSize: Int, windowSize: Int, durationMs: Int): Boolean = core.startBandwidthBenchmark(messageSize, windowSize, durationMs) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index ac7ba7de..1c4e2180 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -78,7 +78,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; import { type ContinuousCaptureOptions, type LayerActions, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; -import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, getHeadUpPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; +import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, getHeadUpPreferences, headDownHideSetting, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; import { getInstalledEvenHubAppById, @@ -2403,6 +2403,13 @@ class DashboardController { frameTimings.logFrame(frameId, `TS input handler start: ${event.kind} ${eventLabel(event.kind, event.eventType)}`); let frameOwned = false; try { + if (event.kind === "sys-event" && event.eventType === OsEventTypeList.HEAD_DOWN_EVENT) { + if (headDownHideSetting.get()) { + this.glance.dismiss(); + if (shell.isScreenOn()) shell.sleep(); + } + return; + } const inputEvent = rawInputEventToInputEvent(event); if (!acceptInput(inputEvent)) return; // The gesture, plus which app is on screen and whether input goes to it, diff --git a/app/g2/events.ts b/app/g2/events.ts index 1b49436c..bc2404ba 100644 --- a/app/g2/events.ts +++ b/app/g2/events.ts @@ -77,6 +77,7 @@ export const OsEventTypeList = { * this eventType; the glance host treats it as a press. */ HEAD_UP_EVENT: 12, + HEAD_DOWN_EVENT: 15, /** Faceclaw/18: R1 touch-down, before click/hold/slide recognition. */ RING_PRESS_EVENT: 14, } as const; @@ -95,5 +96,6 @@ export const OsEventTypeName: Record = { 10: "RING_LONG_PRESS_RELEASE_EVENT", 11: "SHORT_THEN_LONG_PRESS_EVENT", 12: "HEAD_UP_EVENT", + 15: "HEAD_DOWN_EVENT", 14: "RING_PRESS_EVENT", }; diff --git a/app/g2/glance-host.ts b/app/g2/glance-host.ts index cf5d05ff..5d6645da 100644 --- a/app/g2/glance-host.ts +++ b/app/g2/glance-host.ts @@ -1,3 +1,4 @@ +import { headDownHideSetting } from "../ui/dashboard-settings"; /** * Shows the Glanceboard while the shell sleeps. The board is not a window: * it lives on its own full-screen opaque compositor surface above the shell @@ -92,7 +93,7 @@ export class GlanceHost { if (gesture === "click" && !provider.showOnTap()) return null; if ((gesture === "long-press" || gesture === "short-then-long-press") && !provider.showOnLongPress()) return null; if (gesture === "head-tilt" && !provider.showOnHeadTilt()) return null; - return glanceEventForGesture(gesture, this.isVisible()); + return glanceEventForGesture(gesture, this.isVisible(), headDownHideSetting.get()); } /** diff --git a/app/g2/glance-state.ts b/app/g2/glance-state.ts index 9848d284..774a5669 100644 --- a/app/g2/glance-state.ts +++ b/app/g2/glance-state.ts @@ -12,11 +12,13 @@ /** Fallback press timeout when the host supplies none; each further press restarts it. */ export const GLANCE_TIMEOUT_MS = 3000; +export const GLANCE_TILT_MAX_MS = 60_000; export type GlanceState = { visible: boolean; /** A long-press is holding the board up; the timer does not run meanwhile. */ holding: boolean; + tilted?: boolean; /** When the board hides on its own (epoch ms), or null while held / hidden. */ hideAtMs: number | null; }; @@ -24,6 +26,7 @@ export type GlanceState = { export type GlanceEvent = /** Single tap or head-tilt wake: show, and (re)start the auto-hide timer. */ | { type: "press" } + | { type: "tilt" } /** Long-press: show and keep showing until the release. */ | { type: "hold" } /** The long-press ended. */ @@ -44,12 +47,15 @@ export function reduceGlance( switch (event.type) { case "press": // A tap during a hold changes nothing: the hold decides when it ends. - if (state.holding) return state; + if (state.holding || state.tilted) return state; return { visible: true, holding: false, hideAtMs: nowMs + timeoutMs }; + case "tilt": + return { visible: true, holding: state.holding, tilted: true, hideAtMs: state.holding ? null : nowMs + GLANCE_TILT_MAX_MS }; case "hold": - return { visible: true, holding: true, hideAtMs: null }; + return { visible: true, holding: true, ...(state.tilted ? { tilted: true } : {}), hideAtMs: null }; case "release": // Releases delivered late or after a disabled hold gesture mean nothing here. + if (state.holding && state.tilted) return { visible: true, holding: false, tilted: true, hideAtMs: nowMs + GLANCE_TILT_MAX_MS }; return state.holding ? GLANCE_HIDDEN : state; case "timeout": if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state; @@ -68,11 +74,13 @@ export function reduceGlance( export function glanceEventForGesture( gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | (string & {}), boardVisible: boolean, + tiltDownToHide = false, ): GlanceEvent | null { switch (gesture) { case "click": - case "head-tilt": return { type: "press" }; + case "head-tilt": + return { type: tiltDownToHide ? "tilt" : "press" }; case "long-press": case "short-then-long-press": return { type: "hold" }; diff --git a/app/g2/head-angle.ts b/app/g2/head-angle.ts new file mode 100644 index 00000000..4f40cc25 --- /dev/null +++ b/app/g2/head-angle.ts @@ -0,0 +1,86 @@ +import { headUpAngleSetting } from "../ui/dashboard-settings"; +import type { ImuReading } from "../native/imu-types"; +declare const com: any; + +/** Experimental inclination estimate inspired by https://github.com/jimrandomh/faceclaw/pull/64; not a calibrated sensor-axis pitch. */ +export function estimateHeadInclination(samples: ImuReading[]): number | null { + if (samples.length < 3) return null; + const mean = samples.reduce((v, s) => [v[0]! + s.x, v[1]! + s.y, v[2]! + s.z], [0, 0, 0]).map(v => v / samples.length); + const magnitude = Math.hypot(...mean); + if (!Number.isFinite(magnitude) || magnitude < 1e-6) return null; + // Reject motion instead of persisting a passing acceleration as an angle. + for (const s of samples) { + const length = Math.hypot(s.x, s.y, s.z); + const dot = (s.x * mean[0]! + s.y * mean[1]! + s.z * mean[2]!) / (length * magnitude); + if (!Number.isFinite(dot) || Math.acos(Math.max(-1, Math.min(1, dot))) * 180 / Math.PI > 5) return null; + } + return Math.acos(Math.min(1, Math.max(...mean.map(Math.abs)) / magnitude)) * 180 / Math.PI; +} + +export type HeadAngleDevice = { + beginHeadAngleMeasurement(): boolean; + endHeadAngleMeasurement(): void; + getHeadUpCalibration(): number; + subscribe(listener: (reading: ImuReading) => void): () => void; +}; + +let measuring = false; +export async function setAngleFromCurrentHead(device: HeadAngleDevice | null = androidDevice()): Promise { + if (measuring) return "Measurement already running"; + if (!device) return "Connect the glasses first"; + measuring = true; + let release = () => {}; + let lease = false; + try { + if (!device.beginHeadAngleMeasurement()) return "Glasses unavailable or measurement busy"; + lease = true; + const samples: ImuReading[] = []; + await new Promise((resolve, reject) => { + let source: number | null = null; + let sampleTimer: ReturnType | null = null; + const timeout = setTimeout(resolve, 4200); + const finish = () => { clearTimeout(timeout); if (sampleTimer !== null) clearTimeout(sampleTimer); resolve(); }; + try { + const unsubscribe = device.subscribe(reading => { + if (![0, 1, 3].includes(reading.source) || ![reading.x, reading.y, reading.z].every(Number.isFinite)) return; + if (source === null) { source = reading.source; sampleTimer = setTimeout(finish, 1200); } + if (source === reading.source && samples.length < 200) samples.push(reading); + }); + release = () => { clearTimeout(timeout); if (sampleTimer !== null) clearTimeout(sampleTimer); unsubscribe(); }; + } catch (error) { clearTimeout(timeout); if (sampleTimer !== null) clearTimeout(sampleTimer); reject(error); } + }); + const degrees = estimateHeadInclination(samples); + if (degrees === null) return "No stable sensor data; keep still and retry"; + const calibration = device.getHeadUpCalibration(); + if (!Number.isFinite(calibration)) return "Glasses calibration not received; retry"; + // Preserve the existing raw-angle preference and firmware calibration, rather + // than changing the meaning of saved angles for existing installations. + const offset = Math.round((degrees - calibration) / 5) * 5; + if (offset < 0 || offset > 60) return "Measured angle is outside the supported range"; + headUpAngleSetting.set(String(offset)); + return `Saved estimated head-up angle: ${offset}°`; + } catch { return "Measurement failed; try again"; } + finally { + try { release(); } catch { /* Disconnected communicator. */ } + try { if (lease) device.endHeadAngleMeasurement(); } catch { /* Disconnected communicator. */ } + measuring = false; + } +} + +function androidDevice(): HeadAngleDevice | null { + if (!global.isAndroid) return null; + let active: any; + try { active = com.faceclaw.app.FaceclawBleCommunicator.getActive(); } catch { return null; } + if (!active) return null; + return { + beginHeadAngleMeasurement: () => Boolean(active.beginHeadAngleMeasurement()), + endHeadAngleMeasurement: () => active.endHeadAngleMeasurement(), + getHeadUpCalibration: () => Number(active.getHeadUpCalibration()), + subscribe(listener) { + const proxy = new com.faceclaw.app.FaceclawImuListener({ onImuData: (x: number, y: number, z: number, source: number) => + listener({ x: Number(x), y: Number(y), z: Number(z), source: Number(source) }) }); + active.addImuListener(proxy); + return () => active.removeImuListener(proxy); + }, + }; +} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 247d8f1d..c1ae0b3b 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -53,7 +53,7 @@ import { onAnySettingChanged, previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, voiceProviderSetting, elevenLabsApiKeySetting, openAiApiKeySetting, sonioxApiKeySetting, whisperServerApiKeySetting, whisperServerLanguageSetting, - whisperServerModelSetting, whisperServerUrlSetting, ringConnectionModeSetting, saveTranscribeAudioSetting } from '../ui/dashboard-settings' + headDownHideSetting, whisperServerModelSetting, whisperServerUrlSetting, ringConnectionModeSetting, saveTranscribeAudioSetting } from '../ui/dashboard-settings' import { transcriptionKey, type VoiceProviderSelection } from '../native/cloud-stt-provider' import type { PhoneGesture } from '../phone-ui/phone-gestures' import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from '../apps/onboarding/onboarding-progress' @@ -1007,6 +1007,13 @@ export class IosPreviewController { private handleRingEvent(input: RawInputEvent): void { this.inputQueue = this.inputQueue.then(async () => { if (this.state.phase !== 'connected') return + if (input.kind === 'sys-event' && input.eventType === OsEventTypeList.HEAD_DOWN_EVENT) { + if (headDownHideSetting.get()) { + this.glance.dismiss() + if (shell.isScreenOn()) shell.sleep() + } + return + } this.onActivity() if (input.ringInput) this.state = { ...this.state, ring: true } await this.receiveInput(rawInputEventToInputEvent(input), diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 17ff529a..bdba6fa5 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -566,6 +566,11 @@ export const headUpWakeSetting = new HeadUpWakeSetting({ description: "Enable waking the glasses when you look up. Until changed, the glasses keep their current setting. Glanceboard's Show on head tilt chooses what appears after waking.", }); +export const headDownHideSetting = new ConfigSettingBoolean({ + id: "head-down-hide", label: "Tilt down to hide", storageKey: "display.headDownHide", defaultValue: false, + description: "Lowering your head hides the display, including Glanceboard. A board shown by head tilt stays until you lower your head, with a 60-second safety timeout. Requires firmware forwarding HEAD_DOWN (Faceclaw/38).", +}); + export function normalizeHeadUpAngle(value: string | null | undefined): string { if (value == null || value.trim() === "" || !Number.isFinite(Number(value))) return "30"; return String(Math.max(0, Math.min(60, Math.round(Number(value))))); @@ -576,7 +581,7 @@ export const headUpAngleSetting = new ConfigSettingEnum({ values: Array.from({ length: 61 }, (_, angle) => String(angle)), normalize: normalizeHeadUpAngle, formatValue: angle => `${angle}°`, - description: "Look-up trigger angle, 0–60 degrees. Until changed, the glasses keep their current angle. Requires validation on your Faceclaw firmware.", + description: "Firmware head-up angle, 0–60 degrees, added to the glasses’ stored calibration. Until changed, the glasses keep their current angle. Requires validation on your Faceclaw firmware.", }); /** -1 means leave that firmware field unchanged; no writes on an untouched installation. */ diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index a7e5e255..b8d63a68 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -1,3 +1,4 @@ +import { setAngleFromCurrentHead } from "../../g2/head-angle"; import { remoteInputMenuItem } from "./remote-input-menu"; import { knownFolders } from "@nativescript/core"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; @@ -51,6 +52,7 @@ import { brightnessSetting, headUpWakeSetting, headUpAngleSetting, + headDownHideSetting, autoBrightnessMinSetting, autoBrightnessMaxSetting, autoBrightnessCurveSetting, @@ -120,7 +122,16 @@ function settingsSections(): SettingsSection[] { // the dashboard controller when changed and on each connect. enumSettingMenuItem(brightnessSetting), autoBrightnessMenuItem(), - ...(!global.isIOS ? [toggleSettingMenuItem(headUpWakeSetting), enumSettingMenuItem(headUpAngleSetting)] : []), + ...(!global.isIOS ? [toggleSettingMenuItem(headUpWakeSetting), enumSettingMenuItem(headUpAngleSetting), { + label: "Set to current head angle", + description: "Experimental estimate inspired by PR #64. Keep still, without tilting sideways, at a moderate head-up angle (under 45°). Adjust manually if needed.", + onSelect: async (ctx: LayerContext) => { + shell.showAlert("Measuring head angle — keep still"); + const result = await setAngleFromCurrentHead(); + shell.showAlert(result); + ctx.actions.requestRender(); + }, + }, toggleSettingMenuItem(headDownHideSetting)] : []), enumSettingMenuItem(screenTimeoutSetting, { onChange: () => { shell.noteUserActivity(); diff --git a/docs/validation-head-up.md b/docs/validation-head-up.md index 5dce40d4..279ba255 100644 --- a/docs/validation-head-up.md +++ b/docs/validation-head-up.md @@ -51,3 +51,17 @@ On the target glasses, record both lens firmware versions and test: 6. Check both lenses remain synchronized, including reconnecting only one arm. Do not change firmware to run these checks without explicit permission. + +## Shared tilt-down hide and current-angle estimate + +Inspired by [Faceclaw PR #64](https://github.com/jimrandomh/faceclaw/pull/64), Display settings now include one Tilt down to hide toggle (off by default), used by both the regular glasses UI and Glanceboard. HEAD_DOWN is forwarded by the existing firmware (event 15, Faceclaw/38), consumed before input filtering/activity tracking, and never wakes the screen. With the option enabled it dismisses Glanceboard and sleeps the regular UI, including after a manual wake. The action is an explicit sleep gesture, like manually turning the display off. With the option disabled, it is ignored, preserving prior behavior. + +A Glanceboard shown by head-up uses a 60-second safety limit when Tilt down to hide is enabled; otherwise the existing tap timeout is retained. Show on head tilt still selects what appears on wake. There are no separate Glanceboard angle/hide preferences. iOS consumes the shared HEAD_DOWN event so it cannot become spurious input; current-angle measurement remains Android-only. + +Set to current head angle performs a short experimental IMU measurement, then stores the result in the existing shared Head-up angle preference. It reads the glasses' restored calibration (settings request field 9) and subtracts it before saving the raw firmware angle. Existing raw-angle preferences and untouched fields keep their original meaning. A temporary native IMU lease restores the latest application request, including changes made during measurement. Sampling uses one temple only; ring readings, non-finite/zero vectors, unstable samples and unsupported offsets are rejected. Failures leave the saved angle unchanged. Existing synchronization sends the result to both lenses and restores it on reconnect. + +Important measurement limitation: PR #64's dominant-axis inclination estimate is approximate, includes side tilt, and cannot disambiguate a dominant-axis swap beyond 45 degrees. The menu explicitly marks it experimental and asks for a moderate angle below 45 degrees with the head level sideways. It does not claim hardware-calibrated pitch; the manual 0–60 degree control remains available. Firmware calibration is not rewritten. Do not rely on software tests alone for physical angle accuracy. + +Additional software coverage: shared preference persistence; controller routing with the display awake/asleep and the option on/off; Glanceboard safety timeout; measurement success/failure, concurrent calls, missing/moving sensors, ring exclusion and cleanup; calibration protocol zero/signed/missing fields; native IMU lease restoration; native HEAD_DOWN forwarding without user activity. + +Extended independent validation: 1070 JavaScript tests passed, 2 skipped; 272 Kotlin Android host tests passed; TypeScript and changed-source lint passed. Physical wake, hide and angle accuracy remain unverified on glasses. diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt index 431fb653..12d23c50 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt @@ -514,6 +514,13 @@ class BleProtocol { ) ) + /** Settings read: restored calibration is signed, proto3 omits a zero value. */ + @JvmStatic + fun parseHeadUpCalibration(pb: ByteArray): Int? { + val request = readFieldBytes(stripTrailingCrc(pb), 4) ?: return null + return readVarintFieldValue(request, 9, 0) + } + /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ @JvmStatic fun buildSetWearDetection(magic: Int, enabled: Boolean): ByteArray { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt index 7a61c259..9d836e56 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt @@ -306,6 +306,10 @@ class GlassesSessionCore( internal var brightnessDemoPolling = false internal var headUpPreferences: Pair? = null internal var headUpSent: Pair? = null + internal var headAngleMeasuring = false + internal var imuRequestedEnabled = false + internal var imuRequestedPace = 200 + internal var headUpCalibration: Int? = null internal var cfwCleanupDelivered = false internal var lastCfwCleanupAckMagic = 0 @@ -857,19 +861,50 @@ class GlassesSessionCore( */ fun setImuReportEnabled(enable: Boolean, reportFrq: Int) { monitor.withLock { - if (!running || !sessionReady) { - logLine("skip IMU " + (if (enable) "enable" else "disable") + "; session not ready") - return - } - clearMessagesOfKindLocked("imu-control") - val message = messageBuilder.enableOrDisableImu(enable, reportFrq) - message.onTimeout = MessageCallback { logLine("IMU control ack timeout") } - pendingMessages.addFirst(message) - logLine("queue IMU " + (if (enable) "enable freq=$reportFrq" else "disable")) + imuRequestedEnabled = enable + imuRequestedPace = reportFrq + queueSharedImuLocked() } interruptibleSleep.interrupt() } + private fun queueSharedImuLocked() { + if (!running || !sessionReady) return + val enabled = imuRequestedEnabled || headAngleMeasuring + val pace = if (headAngleMeasuring) { + if (imuRequestedEnabled) minOf(imuRequestedPace, 200) else 200 + } else imuRequestedPace + clearMessagesOfKindLocked("imu-control") + val message = messageBuilder.enableOrDisableImu(enabled, pace) + message.onTimeout = MessageCallback { logLine("IMU control ack timeout") } + pendingMessages.addFirst(message) + } + + /** Temporary IMU lease; application requests made meanwhile are restored on release. */ + fun beginHeadAngleMeasurement(): Boolean { + val started = monitor.withLock { + if (!running || !sessionReady || shutdownRequested || chargingMode || headAngleMeasuring) return@withLock false + headAngleMeasuring = true + headUpCalibration = null + queueSharedImuLocked() + pendingMessages.addFirst(createBatteryQueryMessageLocked()) + true + } + if (started) interruptibleSleep.interrupt() + return started + } + + fun endHeadAngleMeasurement() { + monitor.withLock { + if (!headAngleMeasuring) return + headAngleMeasuring = false + queueSharedImuLocked() + } + interruptibleSleep.interrupt() + } + + fun getHeadUpCalibration(): Double = monitor.withLock { headUpCalibration?.toDouble() ?: Double.NaN } + /** * Enable/disable the stock compass through CFW image-handler mode 10. The * desired state is retained across reconnects; headings arrive through @@ -1992,18 +2027,16 @@ class GlassesSessionCore( } else if (decoded != null && "sys-event" == decoded.kind && decoded.eventType == BleProtocol.EVENT_HEAD_DOWN) { - // Faceclaw/38 also forwards the head coming back down. Nothing - // uses it yet; as an unknown input it would count as activity - // and keep the screen awake, so drop it here. - logLine("head-down forwarded by CFW (ignored)") - decoded = null + // A lifecycle gesture, never ordinary user activity. TS consumes + // it before the shell's activity/wake handling. + logLine("head-down forwarded by CFW") } event = decoded if (decoded != null) { // Pure IMU samples arrive continuously; don't let them count // as user input (which would starve battery polling). val pureImuSample = "sys-event" == decoded.kind - && decoded.eventType == BleProtocol.EVENT_IMU_DATA_REPORT + && (decoded.eventType == BleProtocol.EVENT_IMU_DATA_REPORT || decoded.eventType == BleProtocol.EVENT_HEAD_DOWN) if (!pureImuSample) { lastConnectionOrInputAtMs = lastIncomingAtMs if (decoded.eventSource == BleProtocol.EVENT_SOURCE_RING) { @@ -2244,6 +2277,8 @@ class GlassesSessionCore( fixedLayoutCreated = false clearAllMessagesLocked("session ready") headUpSent = null + headUpCalibration = null + headAngleMeasuring = false displayedFingerprint = "" lastAckAtMs = now() lastIncomingAtMs = lastAckAtMs diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt index a2c693a1..c6d2c063 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt @@ -1054,6 +1054,7 @@ internal fun GlassesSessionCore.createBatteryQueryMessageLocked(): OutboundMessa ringBattery = if (ring == null) -1 else ring.battery ringCharging = if (ring == null) -1 else ring.charging if (ringBattery >= 0) releaseRingFromGlassesLocked("glasses report the ring connected") + headUpCalibration = BleProtocol.parseHeadUpCalibration(message.ackPayload) val snapshot = BleProtocol.parseSettingsBattery(message.ackPayload) if (snapshot != null) { headsetBattery = snapshot.battery diff --git a/tests/assistant-settings-visibility.test.cjs b/tests/assistant-settings-visibility.test.cjs index 43e96713..125efbf6 100644 --- a/tests/assistant-settings-visibility.test.cjs +++ b/tests/assistant-settings-visibility.test.cjs @@ -40,6 +40,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') { }, }); const load = loader({ global: { isIOS: false, isAndroid: true } }, { + '../../g2/head-angle': { setAngleFromCurrentHead: async () => 'test' }, './remote-input-menu': { remoteInputMenuItem: () => ({ label: 'Remote input' }) }, '@nativescript/core': { knownFolders: {} }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) }, diff --git a/tests/glance-state.test.cjs b/tests/glance-state.test.cjs index 0bc7d415..5f872718 100644 --- a/tests/glance-state.test.cjs +++ b/tests/glance-state.test.cjs @@ -65,3 +65,14 @@ test("the press timeout is the host's when supplied", () => { assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), shown); assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + 10_000), GLANCE_HIDDEN); }); + +test('shared tilt-down option holds a tilted board up to the safety cap; legacy defaults keep the timer', () => { + const { GLANCE_TILT_MAX_MS } = require('../.test-build/app/g2/glance-state.js'); + assert.deepEqual(glanceEventForGesture('head-tilt', false), { type: 'press' }); + assert.deepEqual(glanceEventForGesture('head-tilt', false, true), { type: 'tilt' }); + const state = reduceGlance(GLANCE_HIDDEN, { type: 'tilt' }, 1000); + assert.equal(state.hideAtMs, 1000 + GLANCE_TILT_MAX_MS); + assert.equal(reduceGlance(state, { type: 'timeout' }, 2000), state); + assert.deepEqual(reduceGlance(state, { type: 'dismiss' }, 2000), GLANCE_HIDDEN); + assert.deepEqual(reduceGlance(state, { type: 'timeout' }, state.hideAtMs), GLANCE_HIDDEN); +}); diff --git a/tests/head-angle.test.cjs b/tests/head-angle.test.cjs new file mode 100644 index 00000000..56c75460 --- /dev/null +++ b/tests/head-angle.test.cjs @@ -0,0 +1,52 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const vm = require('node:vm'); +const ts = require('typescript'); +function fixture() { + const timers = new Map(), saved = []; let id = 0, listener, ended = 0, released = 0; + const context = { exports: {}, global: { isAndroid: false }, + require: () => ({ headUpAngleSetting: { set: value => saved.push(value) } }), + setTimeout(fn, ms) { timers.set(++id, { fn, ms }); return id; }, clearTimeout: id => timers.delete(id) }; + vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/g2/head-angle.ts', 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + const device = { beginHeadAngleMeasurement: () => true, endHeadAngleMeasurement: () => ended++, getHeadUpCalibration: () => 10, + subscribe(fn) { listener = fn; return () => released++; } }; + const sample = (degrees, source = 1) => { + const r = degrees * Math.PI / 180; + listener({ x: Math.sin(r), y: 0, z: Math.cos(r), source }); + }; + return { api: context.exports, device, saved, sample, ended: () => ended, released: () => released, + tick(ms) { for (const [key, timer] of [...timers]) if (timer.ms === ms) { timers.delete(key); timer.fn(); } } }; +} +test('short measurement compensates firmware calibration, persists one shared angle and releases resources', async () => { + const h = fixture(); const pending = h.api.setAngleFromCurrentHead(h.device); + h.sample(30); h.sample(30); h.sample(30); h.tick(1200); + assert.match(await pending, /Saved.*20°/); + assert.deepEqual(h.saved, ['20']); assert.equal(h.ended(), 1); assert.equal(h.released(), 1); +}); +test('no data, motion, unknown calibration and unsupported offsets leave the old angle unchanged', async () => { + for (const kind of ['none', 'motion', 'unknown', 'range']) { + const h = fixture(); + if (kind === 'unknown') h.device.getHeadUpCalibration = () => NaN; + if (kind === 'range') h.device.getHeadUpCalibration = () => 70; + const pending = h.api.setAngleFromCurrentHead(h.device); + if (kind !== 'none') { h.sample(30); h.sample(kind === 'motion' ? 0 : 30); h.sample(30); h.tick(1200); } + else h.tick(4200); + assert.doesNotMatch(await pending, /Saved/); assert.equal(h.saved.length, 0); + assert.equal(h.ended(), 1); assert.equal(h.released(), 1); + } +}); +test('ring readings never calibrate the glasses; concurrent measurements are rejected', async () => { + const h = fixture(); const pending = h.api.setAngleFromCurrentHead(h.device); + assert.match(await h.api.setAngleFromCurrentHead(h.device), /already running/); + h.sample(30, 2); h.sample(30, 2); h.sample(30, 2); h.tick(4200); + assert.doesNotMatch(await pending, /Saved/); assert.equal(h.saved.length, 0); +}); +test('failure to subscribe releases the IMU lease and allows a later attempt', async () => { + const h = fixture(); h.device.subscribe = () => { throw new Error('offline'); }; + assert.match(await h.api.setAngleFromCurrentHead(h.device), /failed/); + assert.equal(h.ended(), 1); + assert.match(await h.api.setAngleFromCurrentHead(null), /Connect/); +}); diff --git a/tests/head-down-hide.test.cjs b/tests/head-down-hide.test.cjs new file mode 100644 index 00000000..cdd17fcb --- /dev/null +++ b/tests/head-down-hide.test.cjs @@ -0,0 +1,23 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const vm = require('node:vm'); +const ts = require('typescript'); +test('head-down hides both displays under one option and is never treated as input or wake', async () => { + const source = ts.createSourceFile('controller.ts', fs.readFileSync('app/g2/dashboard-controller.ts', 'utf8'), ts.ScriptTarget.Latest, true); + const cls = source.statements.find(n => ts.isClassDeclaration(n) && n.name?.text === 'DashboardController'); + const method = cls.members.find(n => n.name?.getText(source) === 'handleInputEvent').getText(source); + for (const enabled of [false, true]) for (const awake of [false, true]) { + let dismissed = 0, slept = 0, finished = 0; + const context = { exports: {}, OsEventTypeList: { HEAD_DOWN_EVENT: 15 }, headDownHideSetting: { get: () => enabled }, + shell: { isScreenOn: () => awake, sleep: () => slept++ }, eventLabel: () => '', + frameTimings: { logFrame() {}, finishFrame() { finished++; } }, + rawInputEventToInputEvent() { throw new Error('head-down entered the regular input path'); } }; + vm.runInNewContext(ts.transpileModule(`export class Controller { ${method} }`, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + const c = new context.exports.Controller(); c.glance = { dismiss: () => dismissed++ }; + await c.handleInputEvent({ kind: 'sys-event', eventType: 15, frameId: 42 }); + assert.equal(dismissed, enabled ? 1 : 0); assert.equal(slept, enabled && awake ? 1 : 0); assert.equal(finished, 1); + } +}); diff --git a/tests/head-up-wake.test.cjs b/tests/head-up-wake.test.cjs index 8a4c6e84..3f04c673 100644 --- a/tests/head-up-wake.test.cjs +++ b/tests/head-up-wake.test.cjs @@ -15,12 +15,14 @@ function evaluate(code, globals) { function settings(values = new Map()) { const file = ts.createSourceFile('settings.ts', source('app/ui/dashboard-settings.ts'), ts.ScriptTarget.Latest, true); const names = new Set(['ConfigSetting', 'ConfigSettingBoolean', 'ConfigSettingEnum', 'HeadUpWakeSetting', - 'headUpWakeSetting', 'normalizeHeadUpAngle', 'headUpAngleSetting', 'getHeadUpPreferences']); + 'headUpWakeSetting', 'headDownHideSetting', 'normalizeHeadUpAngle', 'headUpAngleSetting', 'getHeadUpPreferences']); const code = file.statements.filter(node => names.has(node.name?.text) || (ts.isVariableStatement(node) && node.declarationList.declarations.some(d => names.has(d.name.getText(file))))) .map(node => node.getText(file)).join('\n'); return evaluate(code, { fixedChoiceSettings: new Map(), + getBooleanSetting: (key, fallback) => values.get(key) ?? fallback, + setBooleanSetting: (key, value) => values.set(key, value), getStringSetting: (key, fallback) => values.get(key) ?? fallback, setStringSetting: (key, value) => values.set(key, value), }); @@ -74,3 +76,14 @@ test('controller skips unset preferences, deduplicates changes and forces reconn const connect = controller.members.find(node => node.name?.getText(file) === 'connect').getText(file); assert.match(connect, /pushHeadUpPreferences\(true\)/); }); + + +test('one shared tilt-down preference defaults off and survives restart independently of head-up', () => { + const values = new Map(); let prefs = settings(values); + assert.equal(prefs.headDownHideSetting.get(), false); + prefs.headDownHideSetting.set(true); prefs = settings(values); + assert.equal(prefs.headDownHideSetting.get(), true); + assert.equal(prefs.getHeadUpPreferences().enabled, -1); + assert.equal(prefs.getHeadUpPreferences().angle, -1); + assert.equal(values.has('glanceboard.headDownHide'), false); +}); diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs index 073ebf05..37f1e6f1 100644 --- a/tests/ios-glanceboard.test.cjs +++ b/tests/ios-glanceboard.test.cjs @@ -34,6 +34,7 @@ function fixture() { setTimeout: (fn, ms) => { tasks.set(++nextTask, { fn, at: now + ms }); return nextTask; }, clearTimeout: id => tasks.delete(id), setInterval: () => ++nextTask, clearInterval() {} }; const { GlanceHost } = load('app/g2/glance-host.ts', { + '../ui/dashboard-settings': { headDownHideSetting: { get: () => false } }, '../graphics/image': images, '../graphics/plane': planes, '../graphics/glyph-wire': { prepareFrameDraws: () => null }, '../native/frame-timings': timings, '../ui/shell/geometry': { minWindowTop: () => 48 }, diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt index 266a560b..fd13f864 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt @@ -316,6 +316,41 @@ private class Session { } class GlassesSessionCoreTest { + @Test fun headDownIsForwardedWithoutCountingAsUserActivity() { + val s = Session() + try { + s.startAndAwaitLayout() + s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs = 1L } + val pb = hex("6a061a04080f1001") // deviceEvent{sysEvent{type=HEAD_DOWN, source=right arm}} + s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, + BleProtocol.framePb(pb, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_NOTIFY, 77)[0]) + assertTrue(waitUntil(1_000) { s.listener.events.contains("sys-event:15") }) + assertEquals(1L, s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs }) + } finally { s.core.close() } + } + + + @Test fun angleMeasurementRestoresLatestApplicationImuRequest() { + val s = Session() + val core = s.core + core.running = true + core.sessionReady = true + try { + core.setImuReportEnabled(true, 100) + assertTrue(core.beginHeadAngleMeasurement()) + assertFalse(core.beginHeadAngleMeasurement()) + core.setImuReportEnabled(false, 200) + assertEquals("IMU enable", core.pendingMessages.first { it.kind == "imu-control" }.label) + core.endHeadAngleMeasurement() + assertEquals("IMU disable", core.pendingMessages.first { it.kind == "imu-control" }.label) + assertTrue(core.beginHeadAngleMeasurement()) + core.setImuReportEnabled(true, 100) + core.endHeadAngleMeasurement() + assertEquals("IMU enable", core.pendingMessages.first { it.kind == "imu-control" }.label) + assertEquals(100, core.imuRequestedPace) + } finally { core.running = false } + } + @Test fun headUpWritesOnlyExplicitFieldsToBothArmsAndResendsAfterReconnect() { val s = Session() try { diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/HeadUpProtocolTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/HeadUpProtocolTest.kt index b00ede21..0dceae93 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/HeadUpProtocolTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/HeadUpProtocolTest.kt @@ -1,9 +1,18 @@ package com.faceclaw.app import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull import kotlin.test.assertContentEquals class HeadUpProtocolTest { + @Test fun calibrationReadHandlesOmittedZeroSignedValuesAndMissingSettings() { + assertEquals(0, BleProtocol.parseHeadUpCalibration(bytes("22000000"))) + assertEquals(12, BleProtocol.parseHeadUpCalibration(bytes("2202480c0000"))) + assertEquals(-12, BleProtocol.parseHeadUpCalibration(bytes("220648f4ffffff0f0000"))) + assertNull(BleProtocol.parseHeadUpCalibration(bytes("08010000"))) + } + private fun bytes(hex: String) = hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray() @Test fun separateSwitchAndAngleMessagesMatchReferenceWireFormat() {