Glanceboard: configurable head-tilt angle, glance down to hide

- Head tilt angle setting (Glanceboard): sets the glasses' own head-up
  trigger through the stock settings message (g2_setting.proto
  DeviceReceiveInfoFromAPP.deviceReceiveHeadUpSetting), so detection stays
  on the glasses and nothing streams while the screen is off. The stored
  calibration is read back and compensated for, and the value is
  re-applied on every connect. "glasses" (the default) leaves the
  glasses' setting untouched.
- "Set to current head angle" measures the tilt from a ~1.2 s IMU burst
  and rounds it to the nearest 5 degrees.
- Glance down to hide: a head-tilt showing now stays up until the head comes back down
  (HEAD_DOWN, forwarded by Faceclaw/38) instead of a fixed timeout, with
  a 60 s safety cap. HEAD_DOWN goes only to the Glanceboard on Android
  and iOS, so it never counts as shell activity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BrendenandClaude Opus 5.5 committed 2026-11-06 08:58:33 -08:00
1 parent c896ce9ac8
commit cf74f34767
15 files changed
+536 -21

No files matched your search

@@ -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) {
@@ -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(
@@ -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
@@ -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<String, Any?>()
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) {
@@ -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