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 e384d0da..29813c9b 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 @@ -265,6 +265,10 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre /** See [GlassesSessionCore.getRingStatus]. */ fun getRingStatus(): String = core.getRingStatus() + fun setHeadUpTrigger(degrees: Int) = core.setHeadUpTrigger(degrees) + + fun getHeadUpStatus(): String = core.getHeadUpStatus() + /** See [GlassesSessionCore.setGlassesRingLink]. */ fun setGlassesRingLink(action: String, fallbackAddress: String?, fallbackName: String?): Boolean = core.setGlassesRingLink(action, fallbackAddress, fallbackName) diff --git a/app/apps/glanceboard/glanceboard-app.ts b/app/apps/glanceboard/glanceboard-app.ts index 8afaf474..1346b724 100644 --- a/app/apps/glanceboard/glanceboard-app.ts +++ b/app/apps/glanceboard/glanceboard-app.ts @@ -5,7 +5,8 @@ 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 { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu"; +import { drawRightValueMenuItem, drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu"; +import { applyGlanceAngle, glassesHeadUpTrigger, setGlanceAngleFromHead } from "../../g2/head-angle"; 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"; @@ -19,6 +20,7 @@ import { glanceLayout, glanceLayoutSetting, glanceShowLinesSetting, + glanceAngleSetting, glanceShowOnHeadTiltSetting, glanceShowOnLongPressSetting, glanceSlotChoiceLabel, @@ -312,6 +314,8 @@ function openGlanceSettings(ctx: LayerContext): void { enumSettingMenuItem(glanceTapDurationSetting), toggleSettingMenuItem(glanceShowOnLongPressSetting), toggleSettingMenuItem(glanceShowOnHeadTiltSetting), + enumSettingMenuItem(glanceAngleSetting, { onChange: () => { applyGlanceAngle(); } }), + headAngleCaptureMenuItem(), toggleSettingMenuItem(glanceShowLinesSetting), uiFontOverridePickerMenuItem(glanceFont, { title: "Glanceboard font", @@ -325,6 +329,33 @@ function openGlanceSettings(ctx: LayerContext): void { ctx.stack.push(layer); } +/** + * "Set to current head angle": measure the head's tilt from the glasses' + * accelerometer, round it to the nearest 5°, store it as the Head tilt angle + * and send it to the glasses. The row's value shows the result, or the + * glasses' current trigger before the first use. + */ +let headAngleCaptureStatus = ""; + +function headAngleCaptureMenuItem(): MenuItem { + const label = "Set to current head angle"; + return { + label, + description: + "Tilt your head to where the display should wake, keep still, and select. " + + "Faceclaw reads the glasses' angle for about a second, rounds it to the nearest 5°, and saves it as the Head tilt angle.", + onSelect: async () => { + headAngleCaptureStatus = "Measuring..."; + headAngleCaptureStatus = await setGlanceAngleFromHead(); + }, + render: ({ image, x, y, width }) => { + const current = glassesHeadUpTrigger(); + const value = headAngleCaptureStatus || (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. diff --git a/app/apps/glanceboard/glanceboard-settings.ts b/app/apps/glanceboard/glanceboard-settings.ts index 4d729490..9b2827a7 100644 --- a/app/apps/glanceboard/glanceboard-settings.ts +++ b/app/apps/glanceboard/glanceboard-settings.ts @@ -73,7 +73,7 @@ export const glanceTapDurationSetting = new ConfigSettingEnum values: ["off", "3s", "5s", "7s", "10s"], formatValue: (value) => (value === "off" ? "Disabled" : value.replace("s", " s")), description: - "How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.", + "How long a single tap keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.", }); /** Whether a single tap while asleep shows the board. */ @@ -102,9 +102,35 @@ export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({ label: "Show on head tilt", storageKey: "glanceboard.showOnHeadTilt", defaultValue: true, - description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.", + description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI. It stays up until you look back down.", }); +const GLANCE_ANGLE_VALUES = ["glasses", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60"] as const; +export type GlanceAngle = (typeof GLANCE_ANGLE_VALUES)[number]; + +/** + * How far the head must tilt up for a head-tilt wake, as the glasses' own + * pitch reading (degrees). "glasses" leaves whatever the glasses already have + * (set by the Even app or its calibration). A number is written to the + * glasses' head-up setting on every connection (glasses-controls.ts). + */ +export const glanceAngleSetting = new ConfigSettingEnum({ + id: "glanceboard-head-tilt-angle", + label: "Head tilt angle", + storageKey: "glanceboard.headTiltAngle", + defaultValue: "glasses", + 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 keeps the angle already stored on the glasses.", +}); + +/** The trigger in degrees, or 0 when the glasses keep their own. */ +export function glanceAngleDegrees(): number { + const value = glanceAngleSetting.get(); + return value === "glasses" ? 0 : Number(value); +} + const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const; export type GlanceDepth = (typeof DEPTH_VALUES)[number]; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 3666f299..80dcc041 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -20,6 +20,8 @@ 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 { getBooleanSetting, setBooleanSetting } from "../native/settings-store"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat"; @@ -333,6 +335,7 @@ class DashboardController { private evenHubResumePromise: Promise | null = null; /** Faceclaw's firmware at the required revision reported in; its extensions can be used. */ private customFirmwareConfirmed = false; + private lastGlanceAngleDegrees = glanceAngleDegrees(); private faceclawWakeLeaseState: boolean | null = null; /** Ring connection mode the current session was started with (it applies per connection). */ private ringModeAtConnect: RingConnectionMode = "glasses"; @@ -484,6 +487,7 @@ class DashboardController { this.applyAppLayoutsIfChanged(); this.syncAssistantBridgeIfChanged(); this.syncLockScreenSettingIfChanged(); + this.applyGlanceAngleIfChanged(); // A Preview color or scaling change should show on the mirror at once, // not at the next composited frame (cheap: floor-limited, dedup'd on // the Java side). @@ -1591,6 +1595,8 @@ class DashboardController { if (mappedPhase === "connected") { this.syncEvenHubScreenOffSetting(); this.ensureWearStateTracking(); + // The session reapplies it on reconnects; restate it for a new session. + applyGlanceAngle(); this.maybeWakeAfterChargingReconnect(); } else if (mappedPhase !== "charging") { this.cancelEvenHubSuspendTimer(); @@ -2386,6 +2392,16 @@ 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) { + // Only the Glanceboard cares (it ends a head-tilt showing); never + // shell input, so it can't count as activity or wake anything. + const glanceEvent = this.glance.eventForGesture("head-down"); + if (glanceEvent) { + await this.glance.handleEvent(glanceEvent, frameId); + frameOwned = true; + } + return; + } const inputEvent = rawInputEventToInputEvent(event); if (!acceptInput(inputEvent)) return; // The gesture, plus which app is on screen and whether input goes to it, @@ -2504,6 +2520,14 @@ class DashboardController { } } + /** Re-send the head-tilt angle when the setting changed (from either settings UI). */ + private applyGlanceAngleIfChanged(): void { + const degrees = glanceAngleDegrees(); + if (degrees === this.lastGlanceAngleDegrees) return; + this.lastGlanceAngleDegrees = degrees; + applyGlanceAngle(); + } + /** * 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 diff --git a/app/g2/events.ts b/app/g2/events.ts index 1b49436c..a8f7b5ed 100644 --- a/app/g2/events.ts +++ b/app/g2/events.ts @@ -79,6 +79,12 @@ export const OsEventTypeList = { HEAD_UP_EVENT: 12, /** Faceclaw/18: R1 touch-down, before click/hold/slide recognition. */ RING_PRESS_EVENT: 14, + /** + * Faceclaw/38: the IMU head-down (the head back below the head-up + * threshold minus 20 degrees), forwarded while a page is up. Ends a + * head-tilt Glanceboard; nothing else uses it. + */ + HEAD_DOWN_EVENT: 15, } as const; export const OsEventTypeName: Record = { @@ -96,4 +102,5 @@ export const OsEventTypeName: Record = { 11: "SHORT_THEN_LONG_PRESS_EVENT", 12: "HEAD_UP_EVENT", 14: "RING_PRESS_EVENT", + 15: "HEAD_DOWN_EVENT", }; diff --git a/app/g2/glance-state.ts b/app/g2/glance-state.ts index 9848d284..bd0bdeb8 100644 --- a/app/g2/glance-state.ts +++ b/app/g2/glance-state.ts @@ -13,17 +13,33 @@ /** Fallback press timeout when the host supplies none; each further press restarts it. */ export const GLANCE_TIMEOUT_MS = 3000; +/** + * Safety cap for a head-tilt showing: the board normally stays up until the + * head comes back down (as the stock dashboard does), but a lost head-down + * event must not leave it on forever. + */ +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; + /** + * A head-tilt is holding the board up: it stays until the firmware reports + * the head back down (or GLANCE_TILT_MAX_MS passes). + */ + tilted: boolean; /** When the board hides on its own (epoch ms), or null while held / hidden. */ hideAtMs: number | null; }; export type GlanceEvent = - /** Single tap or head-tilt wake: show, and (re)start the auto-hide timer. */ + /** 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" } + /** The IMU reported the head back down: ends a head-tilt showing. */ + | { type: "head-down" } /** Long-press: show and keep showing until the release. */ | { type: "hold" } /** The long-press ended. */ @@ -33,7 +49,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, hideAtMs: null }); +export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, tilted: false, hideAtMs: null }); export function reduceGlance( state: GlanceState, @@ -43,14 +59,22 @@ export function reduceGlance( ): GlanceState { switch (event.type) { case "press": - // A tap during a hold changes nothing: the hold decides when it ends. - if (state.holding) return state; - return { visible: true, holding: false, hideAtMs: nowMs + timeoutMs }; + // 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 }; + 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; case "hold": - return { visible: true, holding: true, hideAtMs: null }; + return { visible: true, holding: true, tilted: state.tilted, hideAtMs: null }; case "release": // Releases delivered late or after a disabled hold gesture mean nothing here. - return state.holding ? GLANCE_HIDDEN : state; + 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; case "timeout": if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state; return GLANCE_HIDDEN; @@ -66,13 +90,16 @@ export function reduceGlance( * already handles or ignores while asleep. */ export function glanceEventForGesture( - gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | (string & {}), + gesture: "click" | "double-click" | "long-press" | "short-then-long-press" | "long-press-release" | "head-tilt" | "head-down" | (string & {}), boardVisible: boolean, ): GlanceEvent | null { switch (gesture) { case "click": - case "head-tilt": return { type: "press" }; + case "head-tilt": + return { type: "tilt" }; + case "head-down": + return boardVisible ? { type: "head-down" } : null; 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..cd5c12ab --- /dev/null +++ b/app/g2/head-angle.ts @@ -0,0 +1,105 @@ +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"; + +// 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 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 }; + +/** + * 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. + */ +export async function measureHeadTilt(): Promise { + const samples: ImuReading[] = []; + let firstAt = 0; + const unsubscribe = addImuListener((reading) => { + // 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; + } + } 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" }; + } + 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) }; +} + +/** 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)); +} + +/** + * 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. + */ +export async function setGlanceAngleFromHead(): Promise { + 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)`; +} + +/** Push the Head tilt angle setting to the session (on change and on each connect). */ +export function applyGlanceAngle(): boolean { + return setHeadUpTrigger(glanceAngleDegrees()); +} + +/** The glasses' current head-up trigger (angle + calibration), 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; +} + +function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 2442b87c..51b66869 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -961,6 +961,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) { + // As on Android: only the Glanceboard uses it (it ends a head-tilt + // showing); it never reaches the shell or counts as activity. + const glanceEvent = this.glance.eventForGesture('head-down') + if (glanceEvent) await this.glance.handleEvent(glanceEvent, 0) + return + } this.onActivity() if (input.ringInput) this.state = { ...this.state, ring: true } await this.receiveInput(rawInputEventToInputEvent(input), diff --git a/app/native/glasses-controls.ts b/app/native/glasses-controls.ts new file mode 100644 index 00000000..d77bb658 --- /dev/null +++ b/app/native/glasses-controls.ts @@ -0,0 +1,51 @@ +declare const com: any; + +/** + * Head-up (look-up wake) trigger on the active glasses session + * (GlassesSessionCore.setHeadUpTrigger / getHeadUpStatus). Android only for + * now: on iOS, or with no session, the setter does nothing and the getter + * returns null. + */ + +/** GlassesSessionCore.getHeadUpStatus. switch -1 = not read on this connection. */ +export type HeadUpStatus = { + switch: number; + angle: number; + calibration: number; + /** The trigger Faceclaw is maintaining, or -1 when it leaves the glasses' own setting. */ + desired: number; + readAgeMs: number; +}; + +function activeCommunicator(): any { + if (!global.isAndroid) return null; + try { + return com.faceclaw.app.FaceclawBleCommunicator.getActive(); + } catch { + return null; + } +} + +/** Ask the session to keep the head-up trigger at `degrees` (1..90); 0 stops managing it. */ +export function setHeadUpTrigger(degrees: number): boolean { + const active = activeCommunicator(); + if (!active) return false; + try { + active.setHeadUpTrigger(Math.round(degrees)); + return true; + } catch (error) { + console.warn(`setHeadUpTrigger failed: ${error}`); + return false; + } +} + +export function getHeadUpStatus(): HeadUpStatus | null { + const active = activeCommunicator(); + if (!active) return null; + try { + return JSON.parse(String(active.getHeadUpStatus())) as HeadUpStatus; + } catch (error) { + console.warn(`getHeadUpStatus failed: ${error}`); + return null; + } +} 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 16fa3610..2e69e0ba 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 @@ -497,6 +497,70 @@ class BleProtocol { ) } + // Field numbers from g2_setting.proto (package g2.g2_setting; see g2-kit-unofficial + // ble/gen/g2_setting_pb.ts), named after the schema fields. + + /** G2SettingPackage.commandId */ + private const val G2_SETTING_PACKAGE_COMMAND_ID: Int = 1 + + /** G2SettingPackage.magicRandom */ + private const val G2_SETTING_PACKAGE_MAGIC_RANDOM: Int = 2 + + /** G2SettingPackage.deviceReceiveInfoFromApp */ + private const val G2_SETTING_PACKAGE_DEVICE_RECEIVE_INFO_FROM_APP: Int = 3 + + /** G2SettingPackage.deviceReceiveRequestFromApp (the settings-read reply) */ + private const val G2_SETTING_PACKAGE_DEVICE_RECEIVE_REQUEST_FROM_APP: Int = 4 + + /** g2_settingCommandId.DeviceReceiveInfo */ + private const val G2_SETTING_COMMAND_DEVICE_RECEIVE_INFO: Int = 1 + + /** DeviceReceiveInfoFromAPP.deviceReceiveHeadUpSetting */ + private const val DEVICE_RECEIVE_INFO_HEAD_UP_SETTING: Int = 4 + + /** DeviceReceiveRequestFromAPP.headUpSwitchRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_SWITCH_RESTORED: Int = 7 + + /** DeviceReceiveRequestFromAPP.headUpAngleRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_ANGLE_RESTORED: Int = 8 + + /** DeviceReceiveRequestFromAPP.headUpAngleCalibrationRestored */ + private const val DEVICE_RECEIVE_REQUEST_HEAD_UP_CALIBRATION_RESTORED: Int = 9 + + /** + * One head-up (look-up wake) setting: DeviceReceiveInfoFromAPP.deviceReceiveHeadUpSetting, + * a DeviceReceive_Head_UP_Setting carrying one of headUpSwitch, headUpAngle, + * headUpCalibrationSwitch or headUpCalibration ([field] is one of the HEAD_UP_FIELD_*). + * The firmware treats that submessage as a oneof: setting_handle_head_up_setting + * (2.3.0.24, 0x46c39a) receives a single {field, value} and applies it, so the switch and + * the angle go out as separate packages. headUpCalibration is not stored from the + * phone; it is the pitch the glasses measured on their own calibration screen. The + * effective trigger pitch is angle + calibration, clamped to -90..90 and accepted only in + * 1..90 (SVC_Settings_HeadUpConfig 0x470580, DRV_IMUHUDParameterConfig). The angle is a + * uint32 on the wire but used as a signed int, so a negative offset is sent as its + * 32-bit two's complement. + */ + @JvmStatic + fun buildSetHeadUp(magic: Int, field: Int, value: Int): ByteArray { + var headUp: ByteArray = concat(CollectionUtils.listOf(encodeKey(field, 0), encodeUint32(value))) + return concat( + CollectionUtils.listOf( + encodeVarintField(G2_SETTING_PACKAGE_COMMAND_ID, G2_SETTING_COMMAND_DEVICE_RECEIVE_INFO), + encodeVarintField(G2_SETTING_PACKAGE_MAGIC_RANDOM, magic), + encodeMessageField( + G2_SETTING_PACKAGE_DEVICE_RECEIVE_INFO_FROM_APP, + encodeMessageField(DEVICE_RECEIVE_INFO_HEAD_UP_SETTING, headUp), + ), + ) + ) + } + + /** DeviceReceive_Head_UP_Setting.headUpSwitch */ + const val HEAD_UP_FIELD_SWITCH: Int = 1 + + /** DeviceReceive_Head_UP_Setting.headUpAngle */ + const val HEAD_UP_FIELD_ANGLE: Int = 2 + /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ @JvmStatic fun buildSetWearDetection(magic: Int, enabled: Boolean): ByteArray { @@ -905,6 +969,26 @@ class BleProtocol { return BatterySnapshot(battery, charging, silentMode) } + /** + * The head-up settings in a settings READ ack: deviceReceiveRequestFromApp (field 4) + * headUpSwitchRestored=7, headUpAngleRestored=8, headUpAngleCalibrationRestored=9. + * Returns null when the ack has no request submessage. Absent fields (proto3 drops + * zeros) read as 0; angle and calibration are signed ints carried as uint32. + */ + @JvmStatic + fun parseSettingsHeadUp(pb: ByteArray): IntArray? { + var root: ByteArray = stripTrailingCrc(pb) + var request: ByteArray? = readFieldBytes(root, G2_SETTING_PACKAGE_DEVICE_RECEIVE_REQUEST_FROM_APP) + if ((request == null)) { + return null + } + return intArrayOf( + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_SWITCH_RESTORED, 0), + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_ANGLE_RESTORED, 0), + readVarintFieldValue(request, DEVICE_RECEIVE_REQUEST_HEAD_UP_CALIBRATION_RESTORED, 0), + ) + } + /** GlassesCaseDataPackage{commandId=CASE_INFO, magicRandom, caseInfo={}}, as the Even app sends it. */ @JvmStatic fun buildCaseInfoQuery(magic: Int): ByteArray { @@ -1089,6 +1173,18 @@ class BleProtocol { return out.toByteArray() } + /** A varint of the value's 32-bit two's complement (encodeVarint stops short on negatives). */ + private fun encodeUint32(value: Int): ByteArray { + var out: ByteSink = ByteSink() + var v: Long = (value.toLong() and 0xffffffffL) + while ((v >= 0x80L)) { + out.write((((v and 0x7fL).toInt()) or 0x80)) + v = (v ushr 7) + } + out.write(v.toInt()) + return out.toByteArray() + } + private fun crcBytesLe(data: ByteArray): ByteArray { var crc: Int = 0xffff for (b in data) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt index 485565c9..f4501e89 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt @@ -188,6 +188,22 @@ class MessageBuilder { ) } + /** One head-up setting field (see BleProtocol.buildSetHeadUp). */ + fun setHeadUp(field: Int, value: Int): OutboundMessage { + var magic: Int = magicPool.allocate() + return OutboundMessage( + "head-up-control", + ("head-up field " + field + "=" + value), + BleProtocol.SID_UI_SETTING, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildSetHeadUp(magic, field, value), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + fun createLayout(): OutboundMessage { var magic: Int = magicPool.allocate() return OutboundMessage( diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt index 2e7ad976..bbef414b 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt @@ -501,6 +501,9 @@ internal fun GlassesSessionCore.scheduleReconnectLocked(sessionWasReady: Boolean internal fun GlassesSessionCore.resetSessionStateLocked() { ringBattery = -1 ringCharging = -1 + headUpSwitch = -1 + headUpReadAtMs = 0L + headUpVerifyPending = false directRingBattery = -1 directRingCharging = -1 sessionReady = false 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 71b4e074..f9a0413b 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 @@ -350,6 +350,18 @@ class GlassesSessionCore( internal var lastGlassesRingInputAtMs = 0L internal var lastGlassesRingInputLabel = "" internal var lastGlassesRingBatteryAtMs = 0L + // Head-up (look-up wake) state from settings reads: -1 switch = not read yet. + internal var headUpSwitch = -1 + internal var headUpAngle = 0 + internal var headUpCalibration = 0 + internal var headUpReadAtMs = 0L + // The trigger pitch TS asked for (degrees, 1..90), or -1 to leave the glasses' + // own setting alone. Reapplied after every read that disagrees with it. + internal var desiredHeadUpTrigger = -1 + // Set after queueing a correction so the next settings read comes right away to + // confirm it; cleared by that read, so a firmware that refuses the value is + // retried on the normal poll cadence, not in a loop. + internal var headUpVerifyPending = false internal var ringConnectedChangedAtMs = 0L internal var ringConnectCount = 0 internal var lastDirectRingError = "" @@ -945,6 +957,41 @@ class GlassesSessionCore( interruptibleSleep.interrupt() } + /** + * Set the pitch (degrees, 1..90) at which looking up wakes the display, or 0 to stop + * managing it. Kept for the life of this object and reapplied on every connection: the + * next settings read supplies the glasses' calibration, then the switch and angle are + * written (see reconcileHeadUpLocked) and a follow-up read confirms them. + */ + fun setHeadUpTrigger(degrees: Int) { + monitor.withLock { + desiredHeadUpTrigger = if (degrees in 1..90) degrees else -1 + logLine(if (desiredHeadUpTrigger > 0) "head-up trigger requested: ${desiredHeadUpTrigger}°" + else "head-up trigger: left to the glasses") + if (!running || !sessionReady || desiredHeadUpTrigger <= 0) return + if (headUpSwitch >= 0) { + queueHeadUpWritesLocked(desiredHeadUpTrigger - headUpCalibration) + } else { + // Calibration unknown: read the settings first; the ack applies the trigger. + lastBatteryRefreshAtMs = 0L + } + } + interruptibleSleep.interrupt() + } + + /** Head-up settings as last read, as JSON (switch -1 = not read on this connection). */ + fun getHeadUpStatus(): String { + monitor.withLock { + val status = linkedMapOf() + status["switch"] = headUpSwitch + status["angle"] = headUpAngle + status["calibration"] = headUpCalibration + status["desired"] = desiredHeadUpTrigger + status["readAgeMs"] = if (headUpReadAtMs == 0L) -1L else maxOf(0L, now() - headUpReadAtMs) + return try { Json.write(status) } catch (e: Exception) { "{}" } + } + } + // --------------------------------------------------------------------- // Bandwidth benchmark @@ -1981,11 +2028,10 @@ 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 + // Faceclaw/38 also forwards the head coming back down. TS uses + // it to end a head-tilt Glanceboard and otherwise drops it + // before the shell, so it never counts as activity. + logLine("head-down forwarded by CFW") } event = decoded if (decoded != null) { 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 fbfac3d2..bd78b09b 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 @@ -1045,6 +1045,46 @@ internal fun GlassesSessionCore.shouldPollBatteryLocked(now: Long): Boolean { && (lastBatteryRefreshAtMs == 0L || now - lastBatteryRefreshAtMs >= ConnectionOptions.BATTERY_REFRESH_INTERVAL_MS) } +/** + * Bring the glasses' head-up trigger to [GlassesSessionCore.desiredHeadUpTrigger] after a settings + * read: switch on, then angle = trigger - calibration (the firmware adds its stored calibration). + */ +internal fun GlassesSessionCore.reconcileHeadUpLocked(ackPayload: ByteArray?) { + val verifying = headUpVerifyPending + headUpVerifyPending = false + val headUp = if (ackPayload == null) null else BleProtocol.parseSettingsHeadUp(ackPayload) + if (headUp != null) { + headUpSwitch = headUp[0] + headUpAngle = headUp[1] + headUpCalibration = headUp[2] + headUpReadAtMs = now() + } + val desired = desiredHeadUpTrigger + if (desired <= 0 || headUpSwitch < 0 || !sessionReady) return + val wantAngle = desired - headUpCalibration + if (headUpSwitch == 1 && headUpAngle == wantAngle) { + if (verifying) logLine("head-up trigger confirmed: ${desired}° (angle $wantAngle + calibration $headUpCalibration)") + return + } + if (verifying) { + logLine("head-up trigger not applied yet (switch=$headUpSwitch angle=$headUpAngle); retrying on next poll") + } + queueHeadUpWritesLocked(wantAngle) +} + +internal fun GlassesSessionCore.queueHeadUpWritesLocked(angle: Int) { + clearMessagesOfKindLocked("head-up-control") + val on = messageBuilder.setHeadUp(BleProtocol.HEAD_UP_FIELD_SWITCH, 1) + on.onTimeout = MessageCallback { logLine("head-up switch ack timeout") } + val set = messageBuilder.setHeadUp(BleProtocol.HEAD_UP_FIELD_ANGLE, angle) + set.onTimeout = MessageCallback { logLine("head-up angle ack timeout") } + pendingMessages.addLast(on) + pendingMessages.addLast(set) + headUpVerifyPending = true + lastBatteryRefreshAtMs = 0L + logLine("queue head-up switch on + angle $angle (trigger ${desiredHeadUpTrigger}°, calibration $headUpCalibration)") +} + internal fun GlassesSessionCore.createBatteryQueryMessageLocked(): OutboundMessage { val message = messageBuilder.batteryQuery() message.onAck = MessageCallback { @@ -1053,6 +1093,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") + reconcileHeadUpLocked(message.ackPayload) val snapshot = BleProtocol.parseSettingsBattery(message.ackPayload) if (snapshot != null) { headsetBattery = snapshot.battery diff --git a/tests/glance-state.test.cjs b/tests/glance-state.test.cjs index 0bc7d415..73eb9f0d 100644 --- a/tests/glance-state.test.cjs +++ b/tests/glance-state.test.cjs @@ -4,6 +4,7 @@ const assert = require("node:assert/strict"); const { GLANCE_HIDDEN, GLANCE_TIMEOUT_MS, + GLANCE_TILT_MAX_MS, glanceEventForGesture, reduceGlance, } = require("../.test-build/app/g2/glance-state.js"); @@ -12,7 +13,7 @@ const T0 = 1_000_000; test("a press shows the board and arms the auto-hide timer", () => { const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0); - assert.deepEqual(shown, { visible: true, holding: false, hideAtMs: T0 + GLANCE_TIMEOUT_MS }); + assert.deepEqual(shown, { visible: true, holding: false, tilted: false, hideAtMs: T0 + GLANCE_TIMEOUT_MS }); // A timer firing early is ignored; on time it hides. assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS - 1), shown); assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), GLANCE_HIDDEN); @@ -27,7 +28,7 @@ test("a further press renews the timer from the new press", () => { test("a hold shows the board without a timer and the release hides it", () => { const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0); - assert.deepEqual(held, { visible: true, holding: true, hideAtMs: null }); + assert.deepEqual(held, { visible: true, holding: true, tilted: false, hideAtMs: null }); assert.equal(reduceGlance(held, { type: "timeout" }, T0 + 60_000), held); // A tap mid-hold neither hides nor starts a timer. assert.equal(reduceGlance(held, { type: "press" }, T0 + 500), held); @@ -47,9 +48,11 @@ test("dismiss hides from any state", () => { assert.deepEqual(reduceGlance(shown, { type: "dismiss" }, T0), GLANCE_HIDDEN); }); -test("gesture mapping: taps and head-tilt press, both long-press gestures hold, double-tap dismisses only a visible board", () => { +test("gesture mapping: taps press, head-tilt tilts, both long-press gestures hold, double-tap dismisses only a visible board", () => { assert.deepEqual(glanceEventForGesture("click", false), { type: "press" }); - assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "press" }); + assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "tilt" }); + assert.deepEqual(glanceEventForGesture("head-down", true), { type: "head-down" }); + assert.equal(glanceEventForGesture("head-down", false), null); assert.deepEqual(glanceEventForGesture("long-press", false), { type: "hold" }); assert.deepEqual(glanceEventForGesture("short-then-long-press", false), { type: "hold" }); assert.deepEqual(glanceEventForGesture("short-then-long-press", true), { type: "hold" }); @@ -65,3 +68,31 @@ 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("a head-tilt shows the board until the head comes back down", () => { + const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0); + assert.deepEqual(up, { visible: true, holding: false, tilted: true, hideAtMs: T0 + GLANCE_TILT_MAX_MS }); + // The tap duration no longer applies, and taps change nothing. + assert.equal(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), up); + assert.equal(reduceGlance(up, { type: "press" }, T0 + 500), up); + assert.deepEqual(reduceGlance(up, { type: "head-down" }, T0 + 9000), GLANCE_HIDDEN); + // The safety cap still hides it if the head-down never arrives. + assert.deepEqual(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TILT_MAX_MS), GLANCE_HIDDEN); +}); + +test("head-down leaves a tap's or a hold's showing alone", () => { + const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0); + assert.equal(reduceGlance(shown, { type: "head-down" }, T0 + 100), shown); + const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0); + assert.equal(reduceGlance(held, { type: "head-down" }, T0 + 100), held); +}); + +test("a hold and a head-tilt together: the board stays until both end", () => { + const both = reduceGlance(reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0), { type: "hold" }, T0 + 100); + assert.deepEqual(both, { visible: true, holding: true, tilted: true, hideAtMs: null }); + const released = reduceGlance(both, { type: "release" }, T0 + 200); + assert.deepEqual(released, { visible: true, holding: false, tilted: true, hideAtMs: T0 + 200 + GLANCE_TILT_MAX_MS }); + const downWhileHeld = reduceGlance(both, { type: "head-down" }, T0 + 200); + assert.deepEqual(downWhileHeld, { visible: true, holding: true, tilted: false, hideAtMs: null }); + assert.deepEqual(reduceGlance(downWhileHeld, { type: "release" }, T0 + 300), GLANCE_HIDDEN); +});