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

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

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

No files matched your search

@@ -358,6 +358,14 @@ class GlassesSessionCore(
// 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
// The glasses' own switch and angle from before Faceclaw first changed them
// (switch -1 = unknown): captured before the first write, or handed back by TS,
// which persists them across app restarts. Written back while
// headUpRestorePending (TS stopped managing the trigger), then forgotten once a
// read confirms them.
internal var headUpOriginalSwitch = -1
internal var headUpOriginalAngle = 0
internal var headUpRestorePending = false
// 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.
@@ -962,15 +970,32 @@ class GlassesSessionCore(
* 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.
*
* [originalSwitch] (0/1, or -1 when unknown) and [originalAngle] are the glasses' own
* values from before Faceclaw changed them, as TS saved them from getHeadUpStatus; they
* take precedence over what this object captured. With degrees 0 they are written back.
*/
fun setHeadUpTrigger(degrees: Int) {
fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) {
monitor.withLock {
if (originalSwitch in 0..1) {
headUpOriginalSwitch = originalSwitch
headUpOriginalAngle = originalAngle
}
// A change of mind drops writes still queued for the previous value.
clearMessagesOfKindLocked("head-up-control")
headUpVerifyPending = false
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
headUpRestorePending = desiredHeadUpTrigger < 0 && headUpOriginalSwitch >= 0
logLine(when {
desiredHeadUpTrigger > 0 -> "head-up trigger requested: ${desiredHeadUpTrigger}°"
headUpRestorePending -> "head-up trigger: restore the glasses' own " +
"(switch=$headUpOriginalSwitch angle=$headUpOriginalAngle)"
else -> "head-up trigger: left to the glasses"
})
if (!running || !sessionReady) return
if (desiredHeadUpTrigger <= 0 && !headUpRestorePending) return
if (headUpSwitch >= 0) {
queueHeadUpWritesLocked(desiredHeadUpTrigger - headUpCalibration)
applyHeadUpLocked(verifying = false)
} else {
// Calibration unknown: read the settings first; the ack applies the trigger.
lastBatteryRefreshAtMs = 0L
@@ -987,6 +1012,9 @@ class GlassesSessionCore(
status["angle"] = headUpAngle
status["calibration"] = headUpCalibration
status["desired"] = desiredHeadUpTrigger
status["originalSwitch"] = headUpOriginalSwitch
status["originalAngle"] = headUpOriginalAngle
status["restorePending"] = headUpRestorePending
status["readAgeMs"] = if (headUpReadAtMs == 0L) -1L else maxOf(0L, now() - headUpReadAtMs)
return try { Json.write(status) } catch (e: Exception) { "{}" }
}
@@ -1059,30 +1059,69 @@ internal fun GlassesSessionCore.reconcileHeadUpLocked(ackPayload: ByteArray?) {
headUpCalibration = headUp[2]
headUpReadAtMs = now()
}
if (headUpSwitch < 0 || !sessionReady) return
applyHeadUpLocked(verifying)
}
/**
* Compare the last read with what is wanted (the trigger, or the glasses' original values
* while a restore is pending) and queue writes when they differ. After a verifying read
* that still disagrees nothing is queued: the next regular poll retries, so a firmware
* that clamps or refuses the value costs one attempt per poll interval, not a write loop.
*/
internal fun GlassesSessionCore.applyHeadUpLocked(verifying: Boolean) {
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)")
val wantSwitch: Int
val wantAngle: Int
if (desired > 0) {
wantSwitch = 1
wantAngle = desired - headUpCalibration
} else if (headUpRestorePending) {
wantSwitch = headUpOriginalSwitch
wantAngle = headUpOriginalAngle
} else {
return
}
if (headUpSwitch == wantSwitch && headUpAngle == wantAngle) {
if (desired > 0) {
if (verifying) logLine("head-up trigger confirmed: ${desired}° (angle $wantAngle + calibration $headUpCalibration)")
} else {
logLine("head-up setting restored (switch=$wantSwitch angle=$wantAngle)")
headUpRestorePending = false
headUpOriginalSwitch = -1
headUpOriginalAngle = 0
}
return
}
if (verifying) {
logLine("head-up trigger not applied yet (switch=$headUpSwitch angle=$headUpAngle); retrying on next poll")
logLine("head-up setting not applied yet (switch=$headUpSwitch angle=$headUpAngle); retrying on next poll")
return
}
queueHeadUpWritesLocked(wantAngle)
if (desired > 0 && headUpOriginalSwitch < 0) {
headUpOriginalSwitch = headUpSwitch
headUpOriginalAngle = headUpAngle
logLine("head-up original saved (switch=$headUpSwitch angle=$headUpAngle)")
}
queueHeadUpWritesLocked(wantSwitch, wantAngle)
}
internal fun GlassesSessionCore.queueHeadUpWritesLocked(angle: Int) {
internal fun GlassesSessionCore.queueHeadUpWritesLocked(switch: Int, 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 sw = messageBuilder.setHeadUp(BleProtocol.HEAD_UP_FIELD_SWITCH, switch)
sw.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)
// Switch on before the angle; when switching off, the angle goes first.
if (switch == 1) {
pendingMessages.addLast(sw)
pendingMessages.addLast(set)
} else {
pendingMessages.addLast(set)
pendingMessages.addLast(sw)
}
headUpVerifyPending = true
lastBatteryRefreshAtMs = 0L
logLine("queue head-up switch on + angle $angle (trigger ${desiredHeadUpTrigger}°, calibration $headUpCalibration)")
logLine("queue head-up switch $switch + angle $angle (trigger ${desiredHeadUpTrigger}°, calibration $headUpCalibration)")
}
internal fun GlassesSessionCore.createBatteryQueryMessageLocked(): OutboundMessage {