diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts index 27cb1303..9559ea42 100644 --- a/app/apps/compass/compass-app.ts +++ b/app/apps/compass/compass-app.ts @@ -10,6 +10,7 @@ import { type CompassDiagnostics, } from "../../native/compass"; import { wrapText } from "../../graphics/textwrap"; +import { glassesTypeSetting } from "../../ui/dashboard-settings"; import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; @@ -151,6 +152,7 @@ class CompassLayer implements Layer { } private statusText(): string { + if (noCompassSensor()) return "These glasses have no compass sensor"; if (this.firmwareStatus !== null) return this.firmwareStatus; if (!this.enabled) return "Compass paused"; if (this.rawHeading === null) return "Waiting for compass data…"; @@ -206,8 +208,10 @@ class CompassLayer implements Layer { image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125); y += smallStep; } - const accuracyText = magneticAccuracyText(this.diagnostics); - image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175); + if (!noCompassSensor()) { + const accuracyText = magneticAccuracyText(this.diagnostics); + image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175); + } return image; } @@ -216,10 +220,15 @@ class CompassLayer implements Layer { // "Uncalibrated" prompt advertises, and re-calibrating is a normal thing to // want once the glasses have been taken off and put back on. handleInput(event: InputEvent, ctx: LayerContext): void { - if (event.type === "click") this.openCalibration(ctx); + if (event.type === "click" && !noCompassSensor()) this.openCalibration(ctx); } } +/** A G1 has an accelerometer and a gyroscope, but no magnetometer. */ +function noCompassSensor(): boolean { + return glassesTypeSetting.get() === "g1"; +} + export function createCompassAppWindow(options: InProcessAppOptions): InProcessWindow { let requestRender = () => {}; const layer = new CompassLayer(() => requestRender()); @@ -231,7 +240,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW icon: "compass", closeable: true, actions: options.actions, - menuItems: () => [ + menuItems: () => noCompassSensor() ? [] : [ { label: "Calibrate", onSelect: (ctx) => { diff --git a/app/g2/brightness-curve.ts b/app/g2/brightness-curve.ts index 357c76de..4f215640 100644 --- a/app/g2/brightness-curve.ts +++ b/app/g2/brightness-curve.ts @@ -8,6 +8,12 @@ const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100"; */ export const RAYNEO_IO_BRIGHTNESS_CURVE = "0:0,3:11,10:26,200:26,300:41,600:56,1000:70,4000:93,5000:100"; +/** + * An Even G1's OPT3007 also reads lux (centilux on the wire). These knots are the stock firmware's table: levels 0, 10, 25, + * 41 and 42 of 42 at 0, 50, 200, 1000 and 2000 lux (g1_firmware/docs/VM.md §6.7). + */ +export const G1_BRIGHTNESS_CURVE = "0:0,50:24,200:60,1000:98,2000:100"; + /** Percent of the configured range; defaults (20–100) give 20/26/46/100. */ export function brightnessCurveError(value: string): string | null { const pairs = value.split(",").map(part => part.trim().split(":")); diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index ac40fdf4..212945e8 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1,6 +1,6 @@ import { normalizeNightscoutThreshold, type NightscoutThresholds } from "../apps/nightscout/nightscout-alerts"; import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures"; -import { DEFAULT_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE, normalizeBrightnessCurve, brightnessCurveError } from "../g2/brightness-curve"; +import { DEFAULT_BRIGHTNESS_CURVE, G1_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE, normalizeBrightnessCurve, brightnessCurveError } from "../g2/brightness-curve"; import { getBooleanSetting, getStringSetting, @@ -600,7 +600,10 @@ export const autoBrightnessMaxSetting = new ConfigSettingEnum({ export const autoBrightnessCurveSetting = new ConfigSettingString({ id: "autoBrightnessCurve", label: "Auto light curve", storageKey: "display.autoBrightnessCurve", defaultValue: DEFAULT_BRIGHTNESS_CURVE, normalize: normalizeBrightnessCurve, validate: brightnessCurveError, - dynamicDefault: () => (glassesTypeSetting.get() === "rayneo-io" ? RAYNEO_IO_BRIGHTNESS_CURVE : undefined), + dynamicDefault: () => { + const glasses = glassesTypeSetting.get(); + return glasses === "rayneo-io" ? RAYNEO_IO_BRIGHTNESS_CURVE : glasses === "g1" ? G1_BRIGHTNESS_CURVE : undefined; + }, description: "2–16 lux:percent pairs, separated by commas. Percent is within your minimum–maximum range. Start at 0:0, end at 100%, and increase lux without decreasing percent. Incomplete edits stay in the preview; brightness keeps using the last valid curve.", }); /** The screen on/off fade at Normal speed. */ diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g1/G1Protocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g1/G1Protocol.kt index 1df1a24a..93770653 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g1/G1Protocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g1/G1Protocol.kt @@ -22,9 +22,13 @@ object G1Protocol { const val OP_HELLO = 0x01 const val OP_HEALTH_OK = 0x07 const val OP_PROPS = 0x0A + const val OP_MIC = 0x13 const val OP_EVENTS = 0x14 const val OP_FC_INFO = 0x1D + const val OP_PROP_SET = 0x1E + const val EV_NOTICE = 0x05 + const val EV_MIC_DATA = 0x06 const val EV_INPUT = 0x07 const val EV_FC_ACK = 0x08 private const val EV_REPLY = 0x80 @@ -34,16 +38,33 @@ object G1Protocol { private const val MAX_PACKET = 244 private const val PACKET_HEADER = 2 + /** The light sensor converts only while something uses it, as this bit of `EVENTS` does; the left arm has none. */ + const val TRIGGER_LIGHT = 4 const val TRIGGER_BATTERY = 5 const val TRIGGER_CASE = 6 const val TRIGGER_WEAR = 7 const val TRIGGER_TOUCH = 8 - const val EVENTS_MASK = (1 shl TRIGGER_BATTERY) or (1 shl TRIGGER_CASE) or (1 shl TRIGGER_WEAR) or (1 shl TRIGGER_TOUCH) + const val EVENTS_MASK = (1 shl TRIGGER_LIGHT) or (1 shl TRIGGER_BATTERY) or (1 shl TRIGGER_CASE) or + (1 shl TRIGGER_WEAR) or (1 shl TRIGGER_TOUCH) + + const val NOTICE_MIC_STOPPED = 5 + const val MIC_STOPPED_LEASE = 3 + const val NOTICE_GUARD = 7 const val PROP_BATT_PCT = 0x11 const val PROP_CHARGE = 0x12 const val PROP_IN_CASE = 0x13 const val PROP_WORN = 0x14 + const val PROP_ACC_X = 0x18 + const val PROP_ACC_Y = 0x19 + const val PROP_ACC_Z = 0x1A + /** 0.01 lux, on the right arm, while the panel is on. */ + const val PROP_LIGHT = 0x1C + const val PROP_IMU_RATE = 0x28 + const val PROP_BRIGHTNESS_MAX = 0x29 + const val IMU_RATE_IDLE = 13 + const val IMU_RATE_STREAM = 26 + const val BRIGHTNESS_LEVEL_MAX = 42 /** A property without a value yet (VM_UNKNOWN). */ const val UNKNOWN = Int.MIN_VALUE @@ -78,6 +99,13 @@ object G1Protocol { fun eventsRequest(mask: Int): ByteArray = byteArrayOf(mask.toByte(), (mask ushr 8).toByte()) + /** `PROP_SET` (0.5.63+): what SYS_WRITE does, for a host that runs no instructions. */ + fun propSet(id: Int, value: Int): ByteArray = + byteArrayOf(id.toByte(), value.toByte(), (value ushr 8).toByte(), (value ushr 16).toByte(), (value ushr 24).toByte()) + + /** On, or a renewal of the lease; 32 kbps, the rate [Lc3PacketFramer] decodes. */ + fun micRequest(on: Boolean): ByteArray = byteArrayOf(if (on) 1 else 0, 0) + /** An event's type, or -1 for bytes too short to be one. */ fun eventType(data: ByteArray): Int = if (data.size >= EVENT_HEADER) data[0].toInt() and 255 else -1 @@ -106,6 +134,38 @@ object G1Protocol { return Input(data[EVENT_HEADER + 2].toInt() and 255, u32(data, EVENT_HEADER + 3)) } + class Notice(@JvmField val kind: Int, @JvmField val body: ByteArray) + + fun parseNotice(data: ByteArray): Notice? { + if (eventType(data) != EV_NOTICE || data.size < EVENT_HEADER + 1) return null + return Notice(data[EVENT_HEADER].toInt() and 255, data.copyOfRange(EVENT_HEADER + 1, data.size)) + } + + /** + * What the arm's light reading looks like to [BrightnessPolicy] and the Developer app: a live passive poll in the + * G2 custom firmware's report (settings field 105), whose value counts tenths of a lux. + */ + fun alsReport(centilux: Int, brightness: Int, tickMs: Long): ByteArray { + val report = ByteArray(24) + report[0] = 0x41 + report[1] = 0x4c + report[2] = 1 + report[3] = 2 // a poll + report[4] = 0x0b // opened, passive, read ok + report[5] = 3 // polling + val tenths = centilux / 10 + for (i in 0..3) { + report[6 + i] = (tenths ushr (8 * i)).toByte() + report[10 + i] = (tenths ushr (8 * i)).toByte() + report[20 + i] = (tickMs ushr (8 * i)).toByte() + } + report[14] = brightness.toByte() + report[16] = brightness.toByte() + report[18] = 0 + report[19] = 4 // scale 1.0 in Q10 + return report + } + class Hello( @JvmField val version: String, @JvmField val leftArm: Boolean, @@ -113,13 +173,15 @@ object G1Protocol { @JvmField val panelOk: Boolean, @JvmField val encrypted: Boolean, @JvmField val inCase: Boolean, + /** `MIC` captures here: the right arm. */ + @JvmField val mic: Boolean, ) fun parseHello(body: ByteArray): Hello? { if (body.size < 7 || body[0].toInt() != 5) return null val flags = body[5].toInt() and 255 return Hello("${body[1].toInt() and 255}.${body[2].toInt() and 255}.${body[3].toInt() and 255}", - body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0) + body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0, body[6].toInt() and 4 != 0) } /** The display path's version, or -1 for a body that isn't FC_INFO's. */ @@ -142,7 +204,51 @@ object G1Protocol { else -> null } + /** `MIC_DATA`'s frames: the counter of the first one, how many, and where they start in [data]. */ + fun micFrames(data: ByteArray): Triple? { + if (eventType(data) != EV_MIC_DATA || data.size < EVENT_HEADER + 3) return null + val count = data[EVENT_HEADER + 2].toInt() and 255 + val start = EVENT_HEADER + 3 + return if (data.size - start < count * Lc3PacketFramer.FRAME_BYTES) null else Triple(u16(data, EVENT_HEADER), count, start) + } + private fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8) private fun u32(b: ByteArray, p: Int): Int = u16(b, p) or (u16(b, p + 2) shl 16) } + +/** + * A G1's microphone frames as the packets [Lc3PacketFramer] takes from a G2: five 10 ms LC3 frames, then the two values + * of the G2's stereo capture (a G1 has one mic: zero) and a packet counter. A gap loses the part-filled packet, and the + * counter tells the decoder to conceal it. + */ +class G1MicAssembler { + private val frames = ByteArray(Lc3PacketFramer.LC3_BYTES) + private var have = 0 + private var firstCounter = 0 + private var nextCounter = -1 + + fun reset() { + have = 0 + nextCounter = -1 + } + + fun ingest(first: Int, count: Int, data: ByteArray, start: Int, emit: (ByteArray) -> Unit) { + val size = Lc3PacketFramer.FRAME_BYTES + for (i in 0 until count) { + val counter = (first + i) and 0xffff + if (counter != nextCounter) have = 0 + if (have == 0) firstCounter = counter + data.copyInto(frames, have * size, start + i * size, start + (i + 1) * size) + have++ + nextCounter = (counter + 1) and 0xffff + if (have == Lc3PacketFramer.FRAMES_PER_PACKET) { + val packet = ByteArray(Lc3PacketFramer.PACKET_BYTES) + frames.copyInto(packet) + packet[Lc3PacketFramer.COUNTER_OFFSET] = (firstCounter / Lc3PacketFramer.FRAMES_PER_PACKET).toByte() + emit(packet) + have = 0 + } + } + } +} 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 99edbcee..c39d7aa3 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 @@ -86,13 +86,16 @@ internal fun GlassesSessionCore.enqueueBenchmarkMessageLocked() { kotlin.random.Random.nextBytes(payload) payload[0] = CFW_MSG_DIAGNOSTICS.toByte() // CFW diagnostic-control mode... payload[1] = 0x7f.toByte() // ...with an unused sub-op: acked, no effect + // A G1's arms are two links and every frame goes down both, so the run alternates between them. + val leftArm = if (isG1) benchmarkMessageCounter++ % 2 == 0 else connectionOptions.sendImagesToLeft val message = messageBuilder.imagePayload( "bandwidth", DASHBOARD_TILE, nextMapSessionId(), payload, "bandwidth no-op " + payload.size + "B", - connectionOptions.sendImagesToLeft) + leftArm) + if (isG1) message.cfwRequiredLenses = if (leftArm) G1Timings.LENS_LEFT else G1Timings.LENS_RIGHT val payloadBytes = payload.size // Logical custom-message bytes; excludes length tag and packet framing. val wireBytes = message.message.size 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 d7ee3663..8a39dacc 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 @@ -438,6 +438,7 @@ class GlassesSessionCore( internal var benchmarkLastAckAtMs = 0L internal var benchmarkEndAtMs = 0L // 0 while running; set when the run drains internal var benchmarkMessagesSent = 0 + internal var benchmarkMessageCounter = 0 internal var benchmarkMessagesAcked = 0 internal var benchmarkTimeouts = 0 internal var benchmarkPayloadBytesAcked = 0L @@ -693,10 +694,7 @@ class GlassesSessionCore( } // The iO's packets are Opus from its remote audio stream (see ioStartAudioCapture). if (isIo) return ioStartAudioCapture(listener) - if (isG1) { - logLine("skip G1 mic: not in this version") - return false - } + if (isG1) return g1StartAudioCapture(listener) var magic = 0 monitor.withLock { if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) { @@ -715,7 +713,7 @@ class GlassesSessionCore( fun stopG2AudioCapture() { if (isIo) return ioStopAudioCapture() - if (isG1) return + if (isG1) return g1StopAudioCapture() var magic = 0 monitor.withLock { audioPacketListener = null @@ -918,6 +916,7 @@ class GlassesSessionCore( * (ignored on disable). */ fun setImuReportEnabled(enable: Boolean, reportFrq: Int) { + if (isG1) return g1SetImuReport(enable) monitor.withLock { if (!running || !sessionReady) { logLine("skip IMU " + (if (enable) "enable" else "disable") + "; session not ready") @@ -1275,6 +1274,11 @@ class GlassesSessionCore( /** Request one CFW ambient-light report (image-handler mode 16 op 0). */ fun queryAmbientLight() { monitor.withLock { + if (isG1) { + g1.lastLightAtMs = 0 + interruptibleSleep.interrupt() + return + } enqueueAmbientLightControlLocked(byteArrayOf(CFW_MSG_AMBIENT_LIGHT.toByte(), 0.toByte()), "als query", false) } interruptibleSleep.interrupt() @@ -1357,7 +1361,7 @@ class GlassesSessionCore( * queued (no session, or a malformed record). */ fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean { - if (record == null || record.size < 4) { + if (!isG2 || record == null || record.size < 4) { return false } monitor.withLock { @@ -1825,7 +1829,7 @@ class GlassesSessionCore( val sleepMs = driveSession() if (sleepMs > 0) { - interruptibleSleep.sleep(sleepMs) + interruptibleSleep.sleep(if (isG1) minOf(sleepMs, g1MaxSleepMs()) else sleepMs) } } catch (t: Throwable) { if (!running) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt index 3f692742..01f10c07 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionG1.kt @@ -41,6 +41,20 @@ internal class G1SessionState { /** The panel may be blank: the next update ends in PRESENT even with no pixels to send. */ var presentPending = false + /** What a PROPS request asked for, by arm and tag: a reply doesn't repeat it. */ + val propRequests = HashMap() + /** `PROP_SET`s for the worker thread to write: arm to body. */ + val pendingPropSets = ArrayList>() + var lastLightAtMs = 0L + /** The last reading that made it to the log, in 0.01 lux. */ + var loggedLight = -1 + /** IMU samples are polled this often while someone wants them; 0: nobody does. */ + var imuPeriodMs = 0L + var lastImuAtMs = 0L + var micOn = false + var lastMicRenewAtMs = 0L + val mic = G1MicAssembler() + fun nextTag(): Int { tag = tag % 255 + 1 return tag @@ -51,6 +65,13 @@ internal class G1SessionState { replies.clear() lastPropsAtMs = 0 presentPending = true + propRequests.clear() + pendingPropSets.clear() + lastLightAtMs = 0 + lastImuAtMs = 0 + micOn = false + lastMicRenewAtMs = 0 + mic.reset() } } @@ -60,11 +81,22 @@ internal object G1Timings { /** The OS may be showing its pairing dialog. */ const val PAIRING_MS = 30_000 const val PROPS_MS = 60_000L + /** Well inside the arm's 2 s lease. */ + const val MIC_RENEW_MS = 1_000L + /** The sensor converts in 800 ms. */ + const val LIGHT_DEMO_MS = 800L + const val LIGHT_AUTO_MS = 1_000L + const val LIGHT_MANUAL_MS = 5_000L + /** The G2's IMU rate is a pace code with no meaning here, and a poll is one round trip. */ + const val IMU_POLL_MS = 50L + const val IDLE_WAKE_MS = 500L const val LENS_LEFT = 1 const val LENS_RIGHT = 2 } private val G1_PROPS = intArrayOf(G1Protocol.PROP_BATT_PCT, G1Protocol.PROP_CHARGE, G1Protocol.PROP_IN_CASE, G1Protocol.PROP_WORN) +private val G1_LIGHT = intArrayOf(G1Protocol.PROP_LIGHT) +private val G1_IMU = intArrayOf(G1Protocol.PROP_ACC_X, G1Protocol.PROP_ACC_Y, G1Protocol.PROP_ACC_Z) internal val GlassesSessionCore.isG1: Boolean get() = glassesType == GlassesType.G1 @@ -75,6 +107,8 @@ internal fun g1SupportsMessage(message: OutboundMessage): Boolean { if (message.sid != CfwTransport.SID || message.message.isEmpty()) return false return when (message.message[0].toInt() and CFW_MSG_TYPE_MASK) { CFW_MSG_DRAW_CALLS, CFW_MSG_PRESENT, CFW_MSG_BRIGHTNESS -> true + // The bandwidth benchmark's no-op: the firmware acknowledges what it doesn't know. + CFW_MSG_DIAGNOSTICS -> message.kind == "bandwidth" else -> false } } @@ -123,23 +157,121 @@ private fun GlassesSessionCore.g1Control(arm: Int, op: Int, body: ByteArray = By throw IllegalStateException("no reply to control $op from the " + (if (arm == 0) "left" else "right") + " arm") } -private fun GlassesSessionCore.g1RequestProps(arm: Int) { - val tag = monitor.withLock { g1.nextTag() } - g1WriteControl(g1ArmAddress(arm), G1Protocol.OP_PROPS, tag, ByteArray(G1_PROPS.size) { G1_PROPS[it].toByte() }) +private fun GlassesSessionCore.g1RequestProps(arm: Int, ids: IntArray) { + val tag = monitor.withLock { + val tag = g1.nextTag() + g1.propRequests[arm * 256 + tag] = ids + tag + } + g1WriteControl(g1ArmAddress(arm), G1Protocol.OP_PROPS, tag, ByteArray(ids.size) { ids[it].toByte() }) } -/** Nothing here keeps a lease; this only refreshes the battery. Worker thread, outside the monitor. */ +/** + * The firmware keeps no session of its own for most of this: the worker polls what it wants to know, keeps the + * microphone's lease and writes the properties that were asked for. Worker thread, outside the monitor. + */ internal fun GlassesSessionCore.g1Maintain() { - val due = monitor.withLock { + var status = false + var mic: Boolean? = null + var light = false + var imu = false + var sets: List> = emptyList() + monitor.withLock { + if (!sessionReady) return val now = now() - val due = sessionReady && now - g1.lastPropsAtMs >= G1Timings.PROPS_MS - if (due) g1.lastPropsAtMs = now - due + if (now - g1.lastPropsAtMs >= G1Timings.PROPS_MS) { + status = true + g1.lastPropsAtMs = now + } + if (audioCaptureActive) { + if (now - g1.lastMicRenewAtMs >= G1Timings.MIC_RENEW_MS) { + mic = true + g1.micOn = true + g1.lastMicRenewAtMs = now + } + } else if (g1.micOn) { + mic = false + g1.micOn = false + } + // The sensor only reads while the panel is on; a dark panel keeps the last filtered value. + val interval = if (brightnessDemoPolling) G1Timings.LIGHT_DEMO_MS + else if (brightnessPolicy.automatic) G1Timings.LIGHT_AUTO_MS else G1Timings.LIGHT_MANUAL_MS + if (brightnessSentVisible == true && !chargingMode && !shutdownRequested && now - g1.lastLightAtMs >= interval) { + light = true + g1.lastLightAtMs = now + } + if (g1.imuPeriodMs > 0 && now - g1.lastImuAtMs >= g1.imuPeriodMs) { + imu = true + g1.lastImuAtMs = now + } + if (g1.pendingPropSets.isNotEmpty()) { + sets = g1.pendingPropSets.toList() + g1.pendingPropSets.clear() + } } - if (due) { - g1RequestProps(0) - g1RequestProps(1) + if (status) { + g1RequestProps(0, G1_PROPS) + g1RequestProps(1, G1_PROPS) } + mic?.let { on -> + val tag = monitor.withLock { g1.nextTag() } + g1WriteControl(rightAddress, G1Protocol.OP_MIC, tag, G1Protocol.micRequest(on)) + } + if (light) g1RequestProps(1, G1_LIGHT) + if (imu) g1RequestProps(1, G1_IMU) + for ((arm, body) in sets) { + val tag = monitor.withLock { g1.nextTag() } + g1WriteControl(g1ArmAddress(arm), G1Protocol.OP_PROP_SET, tag, body) + } +} + +/** The longest the session worker may sleep and keep up with [g1Maintain]. */ +internal fun GlassesSessionCore.g1MaxSleepMs(): Long = + monitor.withLock { if (g1.imuPeriodMs > 0) g1.imuPeriodMs else G1Timings.IDLE_WAKE_MS } + +/** + * The right arm's microphone: 16 kHz mono LC3, in the packets the G2's decoder takes. The arm stops it 2 s after the last + * renewal and at a disconnection; meanwhile it shows its indicator in a corner of the panel (VM.md §6.5). + */ +internal fun GlassesSessionCore.g1StartAudioCapture(listener: FaceclawAudioPacketListener): Boolean { + monitor.withLock { + if (!running || !sessionReady || shutdownRequested) { + logLine("skip G1 mic: session not ready") + return false + } + if (g1.arms[1].hello?.mic != true) { + logLine("skip G1 mic: this arm has no capture") + return false + } + g1.mic.reset() + audioPacketListener = listener + audioCaptureActive = true + g1.lastMicRenewAtMs = 0 + } + interruptibleSleep.interrupt() + return true +} + +internal fun GlassesSessionCore.g1StopAudioCapture() { + monitor.withLock { + audioPacketListener = null + audioCaptureActive = false + } + interruptibleSleep.interrupt() +} + +/** + * There is no magnetometer on a G1, only the accelerometer and gyroscope: x, y, z in g, head frame, from the right arm. + * [enable] raises the sample rate and polls it; [g1MaxSleepMs] follows. + */ +internal fun GlassesSessionCore.g1SetImuReport(enable: Boolean) { + monitor.withLock { + g1.imuPeriodMs = if (enable) G1Timings.IMU_POLL_MS else 0 + g1.lastImuAtMs = 0 + g1.pendingPropSets.add(1 to G1Protocol.propSet(G1Protocol.PROP_IMU_RATE, + if (enable) G1Protocol.IMU_RATE_STREAM else G1Protocol.IMU_RATE_IDLE)) + } + interruptibleSleep.interrupt() } // --------------------------------------------------------------------------------------------- @@ -378,6 +510,8 @@ internal fun GlassesSessionCore.g1ConnectLoopOnce() { if (events.status != 0) throw IllegalStateException("EVENTS refused: " + events.status) // The arm counts a boot healthy only after a host has said so (VM.md §6.4, safe mode). g1Control(arm, G1Protocol.OP_HEALTH_OK) + // A limit the last session left, when the other eye's panel guard had capped it. + g1Control(arm, G1Protocol.OP_PROP_SET, G1Protocol.propSet(G1Protocol.PROP_BRIGHTNESS_MAX, G1Protocol.BRIGHTNESS_LEVEL_MAX)) } monitor.withLock { sessionReady = true @@ -439,12 +573,23 @@ internal fun GlassesSessionCore.g1OnNotification(address: String, characteristic interruptibleSleep.interrupt() } G1Protocol.EV_INPUT -> g1HandleInput(arm, G1Protocol.parseInput(data) ?: return) + G1Protocol.EV_MIC_DATA -> g1HandleMicData(address, data) + G1Protocol.EV_NOTICE -> g1HandleNotice(arm, G1Protocol.parseNotice(data) ?: return) else -> if (type >= 0x80) { val reply = G1Protocol.parseReply(data) ?: return if (reply.op == G1Protocol.OP_PROPS) { - if (reply.status == 0) G1Protocol.parseProps(reply.body, G1_PROPS.size)?.let { g1HandleProps(arm, it) } + val ids = monitor.withLock { g1.propRequests.remove(arm * 256 + reply.tag) } ?: return + val values = if (reply.status == 0) G1Protocol.parseProps(reply.body, ids.size) else null + if (values != null) g1HandlePropsReply(arm, ids, values) return } + if (reply.op == G1Protocol.OP_MIC) { + if (reply.status != 0) g1MicRefused(reply.status) + return + } + if (reply.op == G1Protocol.OP_PROP_SET && reply.status != 0) { + logLine("G1 PROP_SET refused: " + reply.status + " (firmware before 0.5.63?)") + } monitor.withLock { lastIncomingAtMs = now() g1.replies[arm * 256 + reply.tag] = reply @@ -454,6 +599,73 @@ internal fun GlassesSessionCore.g1OnNotification(address: String, characteristic } } +private fun GlassesSessionCore.g1HandleMicData(address: String, data: ByteArray) { + val (first, count, start) = G1Protocol.micFrames(data) ?: return + val packets = ArrayList(1) + monitor.withLock { g1.mic.ingest(first, count, data, start) { packets.add(it) } } + for (packet in packets) handleRenderNotification(address, packet) +} + +private fun GlassesSessionCore.g1MicRefused(status: Int) { + logWarn("G1 mic refused: status $status (12: no capture, or the link isn't encrypted)") + monitor.withLock { + audioCaptureActive = false + audioPacketListener = null + g1.micOn = false + } +} + +private fun GlassesSessionCore.g1HandleNotice(arm: Int, notice: G1Protocol.Notice) { + when (notice.kind) { + G1Protocol.NOTICE_MIC_STOPPED -> { + val reason = if (notice.body.isNotEmpty()) notice.body[0].toInt() and 255 else 0 + logLine("G1 mic stopped by the arm: reason $reason") + if (reason == G1Protocol.MIC_STOPPED_LEASE) { + // A renewal came late: ask again, the capture still wants it. + monitor.withLock { g1.lastMicRenewAtMs = 0 } + interruptibleSleep.interrupt() + } else { + g1MicRefused(reason) + } + } + G1Protocol.NOTICE_GUARD -> { + if (notice.body.isEmpty()) return + // This arm's panel guard capped its level (a mostly white screen): keep the eyes matched. + val cap = notice.body[0].toInt() and 255 + logLine("G1 panel guard on the " + (if (arm == 0) "left" else "right") + " arm: cap $cap") + monitor.withLock { g1.pendingPropSets.add((1 - arm) to G1Protocol.propSet(G1Protocol.PROP_BRIGHTNESS_MAX, cap)) } + interruptibleSleep.interrupt() + } + } +} + +private fun GlassesSessionCore.g1HandlePropsReply(arm: Int, ids: IntArray, values: IntArray) { + when { + ids === G1_PROPS -> g1HandleProps(arm, values) + ids === G1_LIGHT -> if (values[0] != G1Protocol.UNKNOWN) g1HandleLight(values[0]) + ids === G1_IMU -> if (values.none { it == G1Protocol.UNKNOWN }) { + emitImuData(values[0] / 1000.0, values[1] / 1000.0, values[2] / 1000.0, BleProtocol.EVENT_SOURCE_GLASSES_R) + } + } +} + +/** A reading in the shape of the G2's report, so the brightness policy and the Developer app need nothing new. */ +private fun GlassesSessionCore.g1HandleLight(centilux: Int) { + val report = monitor.withLock { + val logged = g1.loggedLight + if (logged < 0 || kotlin.math.abs(centilux - logged) > maxOf(100, logged / 2)) { + g1.loggedLight = centilux + logLine("G1 light: " + centilux / 100 + " lux") + } + val report = G1Protocol.alsReport(centilux, brightnessPolicy.target, now()) + lastIncomingAtMs = now() + brightnessPolicy.sample(report, lastIncomingAtMs) + report + } + emitAmbientLight(report) + interruptibleSleep.interrupt() +} + private fun GlassesSessionCore.g1HandleInput(arm: Int, input: G1Protocol.Input) { when (input.trigger) { G1Protocol.TRIGGER_TOUCH -> g1HandleGesture(arm, input.value) @@ -498,7 +710,10 @@ private fun GlassesSessionCore.g1HandleProps(arm: Int, values: IntArray, battery if (values[0] != G1Protocol.UNKNOWN) state.battery = values[0] if (values[1] != G1Protocol.UNKNOWN) state.charge = values[1] if (values[2] != G1Protocol.UNKNOWN) state.inCase = values[2] - if (values[3] != G1Protocol.UNKNOWN) state.worn = values[3] + if (values[3] != G1Protocol.UNKNOWN) { + if (values[3] != state.worn) logLine("G1 " + (if (arm == 0) "left" else "right") + " arm: worn " + values[3]) + state.worn = values[3] + } val batteries = g1.arms.map { it.battery }.filter { it >= 0 } val battery = if (batteries.isEmpty()) -1 else batteries.min() diff --git a/tests/brightness-curve.test.cjs b/tests/brightness-curve.test.cjs index 1136e068..3427275a 100644 --- a/tests/brightness-curve.test.cjs +++ b/tests/brightness-curve.test.cjs @@ -1,6 +1,6 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE } = require('../.test-build/app/g2/brightness-curve.js'); +const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_BRIGHTNESS_CURVE, G1_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE } = require('../.test-build/app/g2/brightness-curve.js'); test('brightness curve accepts monotone knots and canonicalizes spacing', () => { assert.equal(normalizeBrightnessCurve(' 0:0, 2.5:20,1000:100 '), '0:0,2.5:20,1000:100'); }); @@ -50,6 +50,9 @@ test('an unset curve follows the paired glasses; a chosen one stays', () => { store.set('deviceType', 'rayneo-io'); assert.equal(api.getBrightnessPreferences().curve, RAYNEO_IO_BRIGHTNESS_CURVE); assert.equal(brightnessCurveError(RAYNEO_IO_BRIGHTNESS_CURVE), null); + store.set('deviceType', 'g1'); + assert.equal(api.getBrightnessPreferences().curve, G1_BRIGHTNESS_CURVE); + assert.equal(brightnessCurveError(G1_BRIGHTNESS_CURVE), null); api.autoBrightnessCurveSetting.set('0:0,2:50,8:100'); assert.equal(api.getBrightnessPreferences().curve, '0:0,2:50,8:100'); store.set('deviceType', 'g2'); diff --git a/tests/compass-readout.test.cjs b/tests/compass-readout.test.cjs index 7a801134..05acf06f 100644 --- a/tests/compass-readout.test.cjs +++ b/tests/compass-readout.test.cjs @@ -23,7 +23,7 @@ const font = (size) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../ap const small = font(18), large = font(32); const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 }; -function compass(width = 576, height = 260) { +function compass(width = 576, height = 260, glasses = 'g2') { let listener, window; class RecordingImage extends graphics.GrayImage { texts = []; @@ -55,6 +55,7 @@ function compass(width = 576, height = 260) { }, '../../ui/shell/shell': { shell: { isWindowVisible: () => true } }, '../../native/location-permissions': { hasLocationPermission: () => false }, + '../../ui/dashboard-settings': { glassesTypeSetting: { get: () => glasses } }, './calibration': calibration, './calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} }, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) }, @@ -63,6 +64,7 @@ function compass(width = 576, height = 260) { app.createCompassAppWindow({ onClosed() {} }); return { menu: () => window.menuItems(), paint: () => window.baseLayer.paint(ctx), + tap: () => window.baseLayer.handleInput({ type: 'click' }, ctx), heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) }; } @@ -110,3 +112,13 @@ test('calibration readout fits the viewport without overlapping the heading or s } } }); + +test('glasses without a magnetometer say so and offer no calibration', () => { + const app = compass(576, 260, 'g1'); + const image = app.paint(); + assert.ok(image.texts.some((t) => t.text === 'These glasses have no compass sensor')); + assert.equal(calibrationText(image), undefined); + assert.equal(app.menu().length, 0); + // ctx.stack has no push or topMatches here, so opening calibration would throw. + app.tap(); +}); diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index 740a313e..4326f5ba 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -44,6 +44,7 @@ const compass = load('app/apps/compass/compass-app.ts', { '../../ui/shell/in-process-window': {}, '../../ui/shell/shell': {}, '../../native/location-permissions': {}, + '../../ui/dashboard-settings': { glassesTypeSetting: { get: () => 'g2' } }, './calibration': calibration, './calibration-layer': {}, './compass-rose': rose, diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt index 42032373..ee0af11f 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/G1SessionTest.kt @@ -119,6 +119,11 @@ private class FakeG1Arm(val left: Boolean) { var bonded = false var inCase = 0 var battery = 80 + val props = HashMap() + val propSets = ArrayList>() + val micRequests = ArrayList() + var micStatus = 0 + var benchmarkMessages = 0 } private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink { @@ -140,6 +145,18 @@ private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink { core.onNotification(address, G1Ble.EVT_CHAR_UUID, data) } + /** `MIC_DATA` from the right arm: [count] frames, each filled with its counter's low byte. */ + fun micData(first: Int, count: Int) { + val body = ByteArray(3 + count * 40) + body[0] = first.toByte() + body[1] = (first ushr 8).toByte() + body[2] = count.toByte() + for (i in 0 until count) body.fill((first + i).toByte(), 3 + i * 40, 3 + (i + 1) * 40) + event(RIGHT, G1Protocol.EV_MIC_DATA, body) + } + + fun notice(address: String, kind: Int, vararg body: Byte) = event(address, G1Protocol.EV_NOTICE, byteArrayOf(kind.toByte()) + body) + fun input(address: String, trigger: Int, value: Int) { val body = ByteArray(7) body[2] = trigger.toByte() @@ -203,6 +220,7 @@ private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink { body[3] = 62 body[4] = (if (a.left) 1 else 0).toByte() body[5] = (2 or 64).toByte() + body[6] = (if (a.left) 0 else 4).toByte() reply(address, op, tag, 0, body) } G1Protocol.OP_FC_INFO -> if (hasDisplayPath) { @@ -219,11 +237,35 @@ private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink { reply(address, op, tag, 0, ByteArray(1)) } G1Protocol.OP_PROPS -> { - val values = lock.withLock { intArrayOf(a.battery, 0, a.inCase, 1) } + val ids = f.drop(2).map { it.toInt() and 255 } + val values = lock.withLock { + ids.map { + when (it) { + G1Protocol.PROP_BATT_PCT -> a.battery + G1Protocol.PROP_CHARGE -> 0 + G1Protocol.PROP_IN_CASE -> a.inCase + G1Protocol.PROP_WORN -> 1 + else -> a.props[it] ?: G1Protocol.UNKNOWN + } + } + } val body = ByteArray(4 * values.size) for (i in values.indices) for (b in 0..3) body[4 * i + b] = (values[i] ushr (8 * b)).toByte() reply(address, op, tag, 0, body) } + G1Protocol.OP_PROP_SET -> { + val id = f[2].toInt() and 255 + val value = (f[3].toInt() and 255) or ((f[4].toInt() and 255) shl 8) + lock.withLock { + a.propSets.add(id to value) + a.props[id] = value + } + reply(address, op, tag, 0) + } + G1Protocol.OP_MIC -> { + lock.withLock { a.micRequests.add(f[2].toInt()) } + reply(address, op, tag, a.micStatus) + } else -> reply(address, op, tag, 2) } } @@ -276,6 +318,7 @@ private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink { if (a.visible && m[3].toInt() == 0) a.panelOn = false a.visible = m[3].toInt() != 0 } + CFW_MSG_DIAGNOSTICS -> a.benchmarkMessages++ else -> throw AssertionError("message type $type must not reach a G1") } } @@ -620,7 +663,203 @@ class G1SessionTest { } } +private class G1AudioSink(platform: ProtocolPlatform) : FaceclawAudioPacketListener { + private val lock = platform.createLock() + private val list = ArrayList>() + val packets get() = lock.withLock { list.toList() } + + override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) { + lock.withLock { list.add(data!! to arm!!) } + } +} + +class G1FeaturesTest { + private fun ready(): G1Session { + val s = G1Session() + assertTrue(s.core.start()) + assertTrue(awaitTrue(5_000) { s.ready() }, s.tail()) + return s + } + + private fun requests(s: G1Session) = s.glasses.lock.withLock { s.glasses.rightArm.micRequests.toList() } + + @Test + fun theMicIsRenewedAndItsFramesBecomeG2Packets() { + val s = ready() + try { + val sink = G1AudioSink(s.platform) + assertTrue(s.core.startG2AudioCapture(sink)) + assertTrue(s.core.isAudioCaptureActive()) + assertTrue(awaitTrue(2_000) { requests(s).contains(1) }, s.tail()) + assertTrue(awaitTrue(3_000) { requests(s).count { it == 1 } >= 2 }, "the lease must be renewed:\n" + s.tail()) + assertEquals(emptyList(), s.glasses.lock.withLock { s.glasses.leftArm.micRequests.toList() }, "the left arm has no mic") + + s.glasses.micData(100, 5) + s.glasses.micData(105, 5) + val packets = sink.packets + assertEquals(2, packets.size) + for ((packet, arm) in packets) { + assertEquals("R", arm) + assertEquals(Lc3PacketFramer.PACKET_BYTES, packet.size) + } + assertEquals(20, packets[0].first[Lc3PacketFramer.COUNTER_OFFSET].toInt()) + assertEquals(21, packets[1].first[Lc3PacketFramer.COUNTER_OFFSET].toInt()) + assertEquals(104.toByte(), packets[0].first[199], "the frames in order") + assertEquals(0.toByte(), packets[0].first[Lc3PacketFramer.SSR_OFFSET]) + + s.core.stopG2AudioCapture() + assertFalse(s.core.isAudioCaptureActive()) + assertTrue(awaitTrue(2_000) { requests(s).last() == 0 }, requests(s).toString()) + s.glasses.micData(110, 5) + assertEquals(2, sink.packets.size, "nothing reaches a stopped capture") + } finally { + s.core.disconnect() + } + } + + @Test + fun aRefusedOrFaultedMicEndsTheCaptureAndALateRenewalDoesNot() { + val s = ready() + try { + val sink = G1AudioSink(s.platform) + assertTrue(s.core.startG2AudioCapture(sink)) + assertTrue(awaitTrue(2_000) { requests(s).isNotEmpty() }, s.tail()) + val before = requests(s).size + s.glasses.notice(RIGHT, G1Protocol.NOTICE_MIC_STOPPED, G1Protocol.MIC_STOPPED_LEASE.toByte()) + assertTrue(s.core.isAudioCaptureActive()) + assertTrue(awaitTrue(900) { requests(s).size > before }, "asked again at once") + s.glasses.notice(RIGHT, G1Protocol.NOTICE_MIC_STOPPED, 2) + assertFalse(s.core.isAudioCaptureActive(), "a capture fault ends it") + + s.glasses.lock.withLock { s.glasses.rightArm.micStatus = 12 } + assertTrue(s.core.startG2AudioCapture(sink)) + assertTrue(awaitTrue(2_000) { !s.core.isAudioCaptureActive() }, s.tail()) + } finally { + s.core.disconnect() + } + } + + @Test + fun lightReadingsDriveTheBrightnessPolicy() { + val s = ready() + try { + s.core.configureBrightness(true, 50, 20, 100, "0:0,50:24,200:60,1000:98,2000:100", 280) + s.submit(1, 1) + assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail()) + // 1000 lux on the right arm's sensor, reported in 0.01 lux; the policy filters it towards the curve's top. + s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_LIGHT] = 100_000 } + assertTrue(awaitTrue(6_000) { s.glasses.lock.withLock { s.glasses.rightArm.brightness > 60 && s.glasses.leftArm.brightness > 60 } }, + "both eyes follow: " + s.glasses.lock.withLock { s.glasses.rightArm.brightness } + "\n" + s.tail()) + // In the dark, a lower level, again on both. + s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_LIGHT] = 500 } + assertTrue(awaitTrue(12_000) { s.glasses.lock.withLock { s.glasses.rightArm.brightness < 50 && s.glasses.leftArm.brightness < 50 } }, + "brightness " + s.glasses.lock.withLock { s.glasses.rightArm.brightness } + "\n" + s.tail()) + } finally { + s.core.disconnect() + } + } + + @Test + fun onePanelGuardsCapHoldsBothEyes() { + val s = ready() + try { + s.glasses.notice(RIGHT, G1Protocol.NOTICE_GUARD, 12, 12) + assertTrue(awaitTrue(2_000) { s.glasses.lock.withLock { s.glasses.leftArm.props[G1Protocol.PROP_BRIGHTNESS_MAX] == 12 } }, s.tail()) + assertEquals(G1Protocol.BRIGHTNESS_LEVEL_MAX, s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_BRIGHTNESS_MAX] }, + "only the other eye: the arm's own cap already holds it") + s.glasses.notice(RIGHT, G1Protocol.NOTICE_GUARD, 42, 21) + assertTrue(awaitTrue(2_000) { s.glasses.lock.withLock { s.glasses.leftArm.props[G1Protocol.PROP_BRIGHTNESS_MAX] == 42 } }, s.tail()) + } finally { + s.core.disconnect() + } + } + + @Test + fun imuSamplesArePolledWhileWanted() { + val s = ready() + try { + val lock = s.platform.createLock() + val readings = ArrayList>() + s.core.addImuListener(object : FaceclawImuListener { + override fun onImuData(x: Double, y: Double, z: Double, eventSource: Int) { + lock.withLock { readings.add(listOf(x, y, z, eventSource.toDouble())) } + } + }) + s.glasses.lock.withLock { + s.glasses.rightArm.props[G1Protocol.PROP_ACC_X] = 100 + s.glasses.rightArm.props[G1Protocol.PROP_ACC_Y] = -1000 + s.glasses.rightArm.props[G1Protocol.PROP_ACC_Z] = 250 + } + s.core.setImuReportEnabled(true, 200) + assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail()) + assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() }) + assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } }) + s.core.setImuReportEnabled(false, 0) + assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } }) + sleepMs(200) + val count = lock.withLock { readings.size } + sleepMs(300) + assertEquals(count, lock.withLock { readings.size }, "the polling stops") + } finally { + s.core.disconnect() + } + } + + @Test + fun theBandwidthBenchmarkRunsOverBothArms() { + val s = ready() + try { + s.submit(1, 1) + assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail()) + assertTrue(s.core.startBandwidthBenchmarkWithLinkMode(1000, 3, 1_000, 0)) + assertTrue(awaitTrue(8_000) { !s.core.monitor.withLock { s.core.benchmarkActive } }, s.tail()) + assertTrue(s.core.monitor.withLock { !s.core.benchmarkAborted && s.core.benchmarkMessagesAcked > 2 && s.core.benchmarkTimeouts == 0 }, + s.core.getBandwidthBenchmarkStatus() + "\n" + s.tail()) + val left = s.glasses.lock.withLock { s.glasses.leftArm.benchmarkMessages } + val right = s.glasses.lock.withLock { s.glasses.rightArm.benchmarkMessages } + assertTrue(left > 0 && kotlin.math.abs(left - right) <= 1, "left $left right $right") + } finally { + s.core.disconnect() + } + } +} + class G1ProtocolTest { + @Test + fun micFramesFillPacketsAcrossEventsAndAGapDropsTheRemainder() { + fun frames(first: Int, count: Int) = ByteArray(count * 40) { (first + it / 40).toByte() } + val out = ArrayList() + val a = G1MicAssembler() + // MTU too small for five frames an event: two, two, then one completes a packet. + a.ingest(10, 2, frames(10, 2), 0) { out.add(it) } + a.ingest(12, 2, frames(12, 2), 0) { out.add(it) } + assertTrue(out.isEmpty()) + a.ingest(14, 3, frames(14, 3), 0) { out.add(it) } + assertEquals(1, out.size) + assertEquals(2, out[0][Lc3PacketFramer.COUNTER_OFFSET].toInt(), "frame 10's packet") + assertEquals(14.toByte(), out[0][160]) + // 15 and 16 are waiting; 20 comes next, so they are gone and a new packet starts there. + a.ingest(20, 5, frames(20, 5), 0) { out.add(it) } + assertEquals(2, out.size) + assertEquals(4, out[1][Lc3PacketFramer.COUNTER_OFFSET].toInt()) + assertEquals(20.toByte(), out[1][0]) + } + + @Test + fun aLightReadingBecomesAReportThePolicyTakes() { + val report = G1Protocol.alsReport(123_400, 40, 5000) + assertEquals(24, report.size) + val policy = BrightnessPolicy() + policy.configure(true, 50, 2, 100, "0:0,1000:100", 280) + policy.sample(report, 1000) + assertTrue(policy.target > 90, "1234 lux on this curve: ${policy.target}") + } + + @Test + fun aPropertySetIsIdAndLittleEndianValue() { + assertTrue(byteArrayOf(0x29, 42, 0, 0, 0).contentEquals(G1Protocol.propSet(G1Protocol.PROP_BRIGHTNESS_MAX, 42))) + } + @Test fun packetsCarryARecordWithinTheMtu() { val record = ByteArray(1000) { it.toByte() } @@ -678,8 +917,10 @@ class G1ProtocolTest { body[3] = 62 body[4] = 1 body[5] = (2 or 64 or 128).toByte() + body[6] = 4 val hello = assertNotNull(G1Protocol.parseHello(body)) assertEquals("0.5.62", hello.version) + assertTrue(hello.mic) assertTrue(hello.leftArm && hello.panelOk && hello.encrypted && hello.inCase) assertFalse(hello.safeMode) body[0] = 4