Brightness control

This commit is contained in:
James Babcock committed 2026-11-05 11:18:17 -08:00
1 parent 739e81c8a6
commit f803f4212c
9 files changed
+233 -23

No files matched your search

+7
View File
@@ -1,5 +1,12 @@
export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100";
const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100";
/**
* The RayNeo iO's sensor reads real lux, far above the G2's behind-the-lens values, so the
* G2's curve would sit at maximum in any lit room. These knots put the default 20–100 range on
* the stock firmware's own scene levels: 29, 41, 41, 53, 65, 76, 94 and 100 percent at 3, 10,
* 200, 300, 600, 1000, 4000 and 5000 lux.
*/
export const RAYNEO_IO_BRIGHTNESS_CURVE = "0:0,3:11,10:26,200:26,300:41,600:56,1000:70,4000:93,5000:100";
/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */
export function brightnessCurveError(value: string): string | null {
+13 -4
View File
@@ -1,6 +1,6 @@
import { normalizeNightscoutThreshold, type NightscoutThresholds } from "../apps/nightscout/nightscout-alerts";
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
import { DEFAULT_BRIGHTNESS_CURVE, normalizeBrightnessCurve, brightnessCurveError } from "../g2/brightness-curve";
import { DEFAULT_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE, normalizeBrightnessCurve, brightnessCurveError } from "../g2/brightness-curve";
import {
getBooleanSetting,
getStringSetting,
@@ -215,6 +215,8 @@ type ConfigSettingStringOptions<TId extends string> = ConfigSettingOptions<strin
inputKind?: "text" | "email" | "password";
normalize?: (value: string | null | undefined) => string;
validate?: (value: string) => string | null;
/** The default while nothing is stored, when it depends on context (else `defaultValue`). */
dynamicDefault?: () => string | undefined;
};
// Every string setting by id, so isolates that can only pass an id over a
@@ -232,6 +234,7 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
readonly inputKind: "text" | "email" | "password";
private readonly normalizer: (value: string | null | undefined) => string;
private readonly validator?: (value: string) => string | null;
private readonly dynamicDefault: (() => string | undefined) | undefined;
constructor(options: ConfigSettingStringOptions<TId>) {
super(options);
@@ -240,11 +243,16 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
this.inputKind = options.inputKind ?? "text";
this.normalizer = options.normalize ?? ((value) => value ?? "");
this.validator = options.validate;
this.dynamicDefault = options.dynamicDefault;
stringSettingsById.set(this.id, this);
}
private currentDefault(): string {
return this.dynamicDefault?.() ?? this.defaultValue;
}
get(): string {
return this.normalizer(getStringSetting(this.storageKey, this.defaultValue));
return this.normalizer(getStringSetting(this.storageKey, this.currentDefault()));
}
set(value: string): string {
@@ -263,8 +271,8 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
getValidValue(): string {
const value = this.get();
if (!this.validationError(value)) return value;
const saved = this.normalizer(getStringSetting(this.storageKey + ".valid", this.defaultValue));
return this.validationError(saved) ? this.defaultValue : saved;
const saved = this.normalizer(getStringSetting(this.storageKey + ".valid", this.currentDefault()));
return this.validationError(saved) ? this.currentDefault() : saved;
}
}
@@ -592,6 +600,7 @@ 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),
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. */
@@ -5,9 +5,9 @@ package com.faceclaw.app
* frames, so wake starts on the intended PRESENT, never on an older black frame. */
internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? = null) {
if (isIo) {
// The iO has no CFW brightness; ioMaintain drives the launcher's (and the screen) instead.
ioNoteFrameVisibleLocked(frameVisible)
return
// Older iO firmware has no message 30; ioMaintain drives the launcher's (and the screen) instead.
if (!ioFirmwareOwnsBrightness) return
}
if (!customFirmwareDetected || !sessionReady || !fixedLayoutCreated || shutdownRequested) return
val visible = frameVisible ?: brightnessSentVisible ?: return
@@ -30,18 +30,22 @@ internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? =
pendingMessages.addLast(message)
brightnessSentLevel = target
brightnessSentVisible = visible
if (isIo) io.lastBrightnessAtMs = now()
}
internal fun GlassesSessionCore.maintainBrightnessLocked(now: Long) {
if (!customFirmwareDetected || !sessionReady || !fixedLayoutCreated || shutdownRequested) return
// Passive polling owns stock ALS even in manual mode. Slower manual polling
// keeps stock from writing the panel without needlessly streaming samples.
if (now - brightnessAlsStartedAt >= 2000 && !hasPendingOrInflightKindLocked("als-control")) {
val interval = if (brightnessDemoPolling) 250 else if (brightnessPolicy.automatic) 500 else 5000
val interval = if (brightnessDemoPolling) 250 else if (brightnessPolicy.automatic) 500 else 5000
// The G2's stock can close the sensor, so it hears this again; the iO's polling lasts as long as the page.
val alsDue = if (isIo) interval != io.alsIntervalSent else now - brightnessAlsStartedAt >= 2000
if (alsDue && !hasPendingOrInflightKindLocked("als-control")) {
val payload = byteArrayOf(CFW_MSG_AMBIENT_LIGHT.toByte(), 1, 1,
interval.toByte(), (interval shr 8).toByte(), 0, 0, 0xe8.toByte(), 3)
enqueueAmbientLightControlLocked(payload, "brightness passive ALS", false)
brightnessAlsStartedAt = now
if (isIo) io.alsIntervalSent = interval
}
enqueueBrightnessLocked()
}
@@ -49,6 +49,8 @@ internal class IoSessionState {
var displayVisible = false
var brightnessSentLevel = -1
var lastBrightnessAtMs = 0L
/** The light sensor poll interval this page was last asked for ([ioFirmwareOwnsBrightness]), -1 none. */
var alsIntervalSent = -1
/** Last `screen_status` push: 1 on, 0 off, -1 unknown. */
var screenStatus = -1
@@ -81,6 +83,7 @@ internal class IoSessionState {
lastStatusAtMs = 0
brightnessSentLevel = -1
lastBrightnessAtMs = 0
alsIntervalSent = -1
screenStatus = -1
voiceWakeSent = -1
voiceAbortPending = false
@@ -122,9 +125,29 @@ internal fun ioSupportsCfwType(type: Int): Boolean =
internal val GlassesSessionCore.isIo: Boolean get() = glassesType == GlassesType.RAYNEO_IO
/** Messages the iO cannot take (stock EvenHub/settings traffic, the G2's hardware CFW modes). */
internal fun GlassesSessionCore.ioSupportsMessage(message: OutboundMessage): Boolean =
message.sid == CfwTransport.SID && message.message.isNotEmpty() && ioSupportsCfwType(message.message[0].toInt() and 255)
/**
* The firmware runs message 30 and the light sensor (faceclaw-rayneo-cfw bright.c), with the G2's
* wire formats, so the shared brightness code drives it. Older firmware gets the launcher's
* brightness command, which is all it has (see [ioMaintain]).
*/
internal val GlassesSessionCore.ioFirmwareOwnsBrightness: Boolean get() = io.ident?.brightnessControl == true
/** Messages the iO cannot take (stock EvenHub/settings traffic, the G2's other hardware CFW modes). */
internal fun GlassesSessionCore.ioSupportsMessage(message: OutboundMessage): Boolean {
if (message.sid != CfwTransport.SID || message.message.isEmpty()) return false
val type = message.message[0].toInt() and 255
return ioSupportsCfwType(type) ||
(ioFirmwareOwnsBrightness && (type == CFW_MSG_BRIGHTNESS || type == CFW_MSG_AMBIENT_LIGHT))
}
/** A page opened (or reopened): the firmware gave brightness back when the last one closed. */
private fun GlassesSessionCore.ioResetBrightnessLocked() {
brightnessSentVisible = null
brightnessSentLevel = -1
brightnessAlsStartedAt = -2000
brightnessPolicy.resetSamples()
io.alsIntervalSent = -1
}
// ---------------------------------------------------------------------------------------------
// Writes
@@ -333,6 +356,7 @@ internal fun GlassesSessionCore.ioConnectLoopOnce() {
for (transport in cfwTransports) transport.reset()
io.lastKeepaliveAtMs = lastAckAtMs
io.lastStatusAtMs = lastAckAtMs
ioResetBrightnessLocked()
}
setStateDisplay("connected", "Connected.")
ConnectionCounters.increment("io.session-ready")
@@ -415,12 +439,21 @@ internal fun GlassesSessionCore.ioMaintain() {
}
// Faceclaw decides when the display is dark (blanked frames); while it shows
// content, keep the iO's screen on and at Faceclaw's brightness.
val level = RayNeoIoProtocol.brightnessLevel(brightnessPolicy.target)
if (fixedLayoutCreated && io.pageWanted && io.displayVisible && !shutdownRequested
&& (level != io.brightnessSentLevel || now - io.lastBrightnessAtMs >= IoTimings.SCREEN_REFRESH_MS)) {
brightness = level
io.brightnessSentLevel = level
io.lastBrightnessAtMs = now
val showing = fixedLayoutCreated && io.pageWanted && io.displayVisible && !shutdownRequested
if (ioFirmwareOwnsBrightness) {
// Repeating message 30 starts no fade; visible=1 is what holds the screen on.
if (showing && now - io.lastBrightnessAtMs >= IoTimings.SCREEN_REFRESH_MS) {
io.lastBrightnessAtMs = now
brightnessSentLevel = -1
enqueueBrightnessLocked()
}
} else {
val level = RayNeoIoProtocol.brightnessLevel(brightnessPolicy.target)
if (showing && (level != io.brightnessSentLevel || now - io.lastBrightnessAtMs >= IoTimings.SCREEN_REFRESH_MS)) {
brightness = level
io.brightnessSentLevel = level
io.lastBrightnessAtMs = now
}
}
}
if (ctrl >= 0) {
@@ -660,6 +693,15 @@ private fun GlassesSessionCore.ioHandleCfw(address: String, frame: RayNeoIoProto
io.ident = RayNeoIoProtocol.parseIdent(payload)
monitor.signalAll()
}
RayNeoIoProtocol.KIND_ALS -> {
val body = RayNeoIoProtocol.parseAlsReport(payload) ?: return
monitor.withLock {
lastIncomingAtMs = now()
brightnessPolicy.sample(body, lastIncomingAtMs)
}
emitAmbientLight(body)
interruptibleSleep.interrupt()
}
RayNeoIoProtocol.KIND_PAGE -> ioHandlePage(payload)
RayNeoIoProtocol.KIND_GESTURE -> ioHandleGesture(RayNeoIoProtocol.parseGesture(payload) ?: return)
else -> logDebug("RayNeo iO: CFW reply kind " + (payload[0].toInt() and 255) + " " + hex(payload.copyOf(minOf(payload.size, 24))))
@@ -683,6 +725,7 @@ private fun GlassesSessionCore.ioHandlePage(payload: ByteArray) {
imageRetryAfterMs = 0
io.lastKeepaliveAtMs = now()
io.lastBrightnessAtMs = 0
ioResetBrightnessLocked()
}
} else if (state == 0 && fixedLayoutCreated) {
// The page closed under us (lease, the wearer's long press): the firmware freed
@@ -196,7 +196,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
if (benchmarkActive) {
maintainBenchmarkLocked(now)
}
if (!benchmarkActive && !isIo) maintainBrightnessLocked(now)
if (!benchmarkActive && (!isIo || ioFirmwareOwnsBrightness)) maintainBrightnessLocked(now)
// Up to WINDOW_SIZE messages may be in flight at once (full
// pipelining); a slot frees when an ack arrives. An active
@@ -1363,6 +1363,7 @@ internal fun GlassesSessionCore.clearAllMessagesLocked(reason: String) {
brightnessSentVisible = null
brightnessSentLevel = -1
brightnessAlsStartedAt = -2000
io.alsIntervalSent = -1
clearPendingMessagesLocked(reason)
clearInFlightMessagesLocked(reason)
// The image pipeline is gone: drop the pipelined delta base so the next
@@ -1384,6 +1385,7 @@ internal fun GlassesSessionCore.clearAllMessagesPreservingWakeLeaseLocked(reason
brightnessSentVisible = null
brightnessSentLevel = -1
brightnessAlsStartedAt = -2000
io.alsIntervalSent = -1
val pendingIterator = pendingMessages.iterator()
while (pendingIterator.hasNext()) {
val message = pendingIterator.next()
@@ -362,6 +362,8 @@ object RayNeoIoProtocol {
* Set the launcher's brightness (1..8). The stock handler also turns the screen on
* (BrightnessMonitor::setBrightness -> turnOnScreenForSetting), which is how the phone
* wakes the iO's display and keeps the stock auto-off from blanking a static page.
* Only for firmware without [Ident.brightnessControl]: stock auto-brightness soon
* replaces the level, and each one writes the glasses' preferences.
*/
@JvmStatic
fun launcherBrightness(level: Int): String =
@@ -440,6 +442,7 @@ object RayNeoIoProtocol {
const val KIND_IDENT = 0x11
const val KIND_PAGE = 0x13
const val KIND_DIAG = 0x15
const val KIND_ALS = 0x1f
/** `[01|03][stream][ordinal u16][lens][size u16][crc u16]`: the G2 ACK body, one per lens. */
@JvmStatic
@@ -457,6 +460,10 @@ object RayNeoIoProtocol {
/** The assistant's remote audio stream (type 58/59 below) is allowed (CFW revision 10). */
const val IDENT_FLAG_MIC = 0x20
const val IDENT_FLAG_FACECLAW = 0x80
/** Message 30: the firmware sets the panel level and runs fades, with stock auto-brightness off. */
const val FEATURE_BRIGHTNESS = "bright30"
/** Message 16: the firmware reads the light sensor and sends [KIND_ALS] reports. */
const val FEATURE_ALS = "als16"
class Ident(
@JvmField val width: Int,
@@ -467,13 +474,17 @@ object RayNeoIoProtocol {
@JvmField val name: String,
/** "Faceclaw/<n>" -> n when the firmware runs the Faceclaw display lists, else -1. */
@JvmField val faceclawRevision: Int,
/** What the firmware adds to the display lists (a third string in IDENT). */
@JvmField val features: List<String> = emptyList(),
) {
val acceptsCompressed: Boolean get() = flags and IDENT_FLAG_ZLIB != 0
val micStream: Boolean get() = flags and IDENT_FLAG_MIC != 0
val faceclaw: Boolean get() = flags and IDENT_FLAG_FACECLAW != 0 && faceclawRevision > 0
/** Faceclaw owns brightness and the light sensor, as on the G2 custom firmware. */
val brightnessControl: Boolean get() = faceclaw && FEATURE_BRIGHTNESS in features && FEATURE_ALS in features
}
/** `[11][ver][w u16][h u16][maxChunk u16][storeKiB u16][rev u16][flags]name\0[lineHeight]["Faceclaw/n"\0]`. */
/** `[11][ver][w u16][h u16][maxChunk u16][storeKiB u16][rev u16][flags]name\0[lineHeight]["Faceclaw/n"\0["features"\0]]`. */
@JvmStatic
fun parseIdent(payload: ByteArray): Ident? {
if (payload.size < 13 || payload[0].toInt() != KIND_IDENT) return null
@@ -482,14 +493,32 @@ object RayNeoIoProtocol {
while (end < payload.size && payload[end].toInt() != 0) end++
val name = payload.copyOfRange(13, end).decodeToString()
var faceclaw = -1
var features = emptyList<String>()
val extra = end + 2 // NUL, line height
if (extra < payload.size) {
var e = extra
while (e < payload.size && payload[e].toInt() != 0) e++
val text = payload.copyOfRange(extra, e).decodeToString()
if (text.startsWith("Faceclaw/")) faceclaw = text.removePrefix("Faceclaw/").toIntOrNull() ?: -1
val more = e + 1
if (more < payload.size) {
var f = more
while (f < payload.size && payload[f].toInt() != 0) f++
features = payload.copyOfRange(more, f).decodeToString().split(' ').filter { it.isNotEmpty() }
}
}
return Ident(u16(2), u16(4), u16(6), u16(10), payload[12].toInt() and 255, name, faceclaw)
return Ident(u16(2), u16(4), u16(6), u16(10), payload[12].toInt() and 255, name, faceclaw, features)
}
/**
* `[1f]` + the 24-byte `['A','L',1,reason,...]` body the G2 custom firmware sends as settings
* field 105 (value in tenths of a lux), or null. The body goes to the same consumers.
*/
@JvmStatic
fun parseAlsReport(payload: ByteArray): ByteArray? {
if (payload.size < 25 || (payload[0].toInt() and 255) != KIND_ALS) return null
val body = payload.copyOfRange(1, 25)
return if (body[0].toInt() == 65 && body[1].toInt() == 76 && body[2].toInt() == 1) body else null
}
/** PAGE `[13][state][reason]`: state 1 open. */
+13 -1
View File
@@ -1,6 +1,6 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_BRIGHTNESS_CURVE } = require('../.test-build/app/g2/brightness-curve.js');
const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_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');
});
@@ -43,6 +43,18 @@ test('the screen fade speed scales the 280 ms on/off fade, or makes it instant',
const fadeAt = (speed) => { api.screenFadeSetting.set(speed); return api.getBrightnessPreferences().fadeMs; };
assert.deepEqual(['very-fast', 'fast', 'normal', 'slow', 'disabled'].map(fadeAt), [56, 140, 280, 560, 0]);
});
test('an unset curve follows the paired glasses; a chosen one stays', () => {
const store = new Map();
const api = settingsFixture(store);
assert.equal(api.getBrightnessPreferences().curve, DEFAULT_BRIGHTNESS_CURVE);
store.set('deviceType', 'rayneo-io');
assert.equal(api.getBrightnessPreferences().curve, RAYNEO_IO_BRIGHTNESS_CURVE);
assert.equal(brightnessCurveError(RAYNEO_IO_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');
assert.equal(api.getBrightnessPreferences().curve, '0:0,2:50,8:100');
});
test('curve drafts preview exactly while brightness keeps the previous valid saved curve', () => {
const store = new Map([['display.autoBrightnessCurve', '0:0,2:50,8:100']]);
let api = settingsFixture(store), curve = api.autoBrightnessCurveSetting;
@@ -112,6 +112,21 @@ class RayNeoIoProtocolTest {
val fc = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d00ff474c4153534c594346572d494f2f3133001946616365636c61772f333800"))!!
assertTrue(fc.faceclaw)
assertEquals(38, fc.faceclawRevision)
assertFalse(fc.brightnessControl)
val bright = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d00ff474c4153534c594346572d494f2f3133001946616365636c61772f33380062726967687433302061" + "6c73313600"))!!
assertEquals(listOf("bright30", "als16"), bright.features)
assertEquals(38, bright.faceclawRevision)
assertTrue(bright.brightnessControl)
// a Glassly build with no display lists has no brightness control, whatever else it says
assertFalse(RayNeoIoProtocol.Ident(540, 220, 240, 13, 0x7f, "x", -1, listOf("bright30", "als16")).brightnessControl)
val als = ByteArray(25)
als[0] = 0x1f; als[1] = 65; als[2] = 76; als[3] = 1
assertEquals(24, RayNeoIoProtocol.parseAlsReport(als)!!.size)
assertEquals(65, RayNeoIoProtocol.parseAlsReport(als)!![0].toInt())
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf(24)))
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf().also { it[3] = 2 }))
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf().also { it[0] = 0x15 }))
assertEquals(1, RayNeoIoProtocol.parsePageState(hex("130100")))
val g = RayNeoIoProtocol.parseGesture(hex("0703010a000000"))!!
@@ -38,6 +38,8 @@ private class FakeIoGlasses(platform: ProtocolPlatform) : SessionLink {
/** The phone id the glasses accept; null = pairing mode (they bind to the next one). */
@Volatile var boundTo: ByteArray? = null
@Volatile var faceclaw = true
/** Firmware with message 30 and the light sensor (IDENT's features string). */
@Volatile var brightnessFirmware = false
@Volatile var pageOpen = false
@Volatile var hangUps = 0
private val controlsList = ArrayList<Int>()
@@ -169,7 +171,9 @@ private class FakeIoGlasses(platform: ProtocolPlatform) : SessionLink {
} ?: return
val message = lock.withLock { inflateRecords(records).last() }
val crc = (complete[3].toInt() and 255) or ((complete[4].toInt() and 255) shl 8)
val ok = pageOpen && CfwTransport.crc(message) == crc && ioSupportsCfwType(message[0].toInt() and 255)
val type = message[0].toInt() and 255
val ok = pageOpen && CfwTransport.crc(message) == crc &&
(ioSupportsCfwType(type) || (brightnessFirmware && (type == CFW_MSG_BRIGHTNESS || type == CFW_MSG_AMBIENT_LIGHT)))
if (ok) lock.withLock { messagesList.add(message) }
for (lens in 1..2) {
notify(0xf0, byteArrayOf(if (ok) 1 else 3, stream.toByte(), 0, 0, lens.toByte(),
@@ -180,7 +184,22 @@ private class FakeIoGlasses(platform: ProtocolPlatform) : SessionLink {
private fun ident(): ByteArray {
val name = "GLASSLYCFW-IO/13".encodeToByteArray()
return byteArrayOf(0x11, 13, 0x1c, 0x02, 0xdc.toByte(), 0x00, 0xf0.toByte(), 0x00, 0x00, 0x01, 0x0d, 0x00, 0xff.toByte()) +
name + byteArrayOf(0, 25) + "Faceclaw/38".encodeToByteArray() + byteArrayOf(0)
name + byteArrayOf(0, 25) + "Faceclaw/38".encodeToByteArray() + byteArrayOf(0) +
(if (brightnessFirmware) "bright30 als16".encodeToByteArray() + byteArrayOf(0) else ByteArray(0))
}
/** RPL_ALS as the firmware sends it: a poll of [tenths] of a lux from a healthy sensor. */
fun alsPoll(tenths: Int) {
val report = ByteArray(25)
report[0] = 0x1f
report[1] = 65
report[2] = 76
report[3] = 1
report[4] = 2 // poll
report[5] = 0x0b // opened, passive, read ok
report[6] = 3
for (i in 0..3) report[7 + i] = (tenths ushr (8 * i)).toByte()
notify(0xf0, report)
}
override fun disconnect(address: String) {}
@@ -222,6 +241,8 @@ private class IoListener : FaceclawBleCommunicatorListener {
override fun onPhoneLockState(locked: Boolean) {}
override fun onCaseState(caseBattery: Int, caseCharging: Int, caseDocked: Boolean) {}
override fun onEvenAppConflict(message: String?) {}
override fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int) {}
@@ -335,6 +356,74 @@ class RayNeoIoSessionTest {
assertEquals(RayNeoIoProtocol.CTRL_PAGE_CLOSE, s.glasses.controls.last())
}
@Test
fun firmwareWithBrightnessControlGetsTheG2Messages() {
val s = IoSession()
s.glasses.brightnessFirmware = true
s.core.configureBrightness(false, 70, 2, 100, "0:0,1000:100", 280)
s.core.configureCompositorScreen(540, 220)
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
val reportLock = s.platform.createLock()
val reports = ArrayList<ByteArray>()
s.core.addAmbientLightListener(object : FaceclawAmbientLightListener {
override fun onAmbientLight(body: ByteArray?) { reportLock.withLock { reports.add(body!!) } }
})
fun ofType(type: Int) = s.glasses.messages.filter { (it[0].toInt() and 255) == type }
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, "page never opened:\n" + s.tail())
s.submitFrame(0x7f, 1)
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, "frame never displayed:\n" + s.tail())
// visible at 70%, fading over the configured 280 ms, ahead of the frame it fades in
val wake = byteArrayOf(30, 1, 70, 1, 0x18, 0x01)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).isNotEmpty() }, s.tail())
assertTrue(ofType(CFW_MSG_BRIGHTNESS).first().contentEquals(wake))
val order = s.glasses.messages.map { it[0].toInt() and 255 }
assertTrue(order.indexOf(CFW_MSG_BRIGHTNESS) < order.indexOf(CFW_MSG_PRESENT), order.toString())
// the launcher's command writes preferences and loses to stock auto: not used
assertTrue(s.glasses.launcher.none { "brightness_change" in it }, s.glasses.launcher.toString())
// the light sensor, at the manual rate; the iO needs telling only once
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).isNotEmpty() }, s.tail())
val poll = ofType(CFW_MSG_AMBIENT_LIGHT).first()
assertEquals(listOf(16, 1), poll.take(2).map { it.toInt() })
assertEquals(5000, (poll[3].toInt() and 255) or ((poll[4].toInt() and 255) shl 8))
// each 5 s the same command, as the screen keepalive
val sent = ofType(CFW_MSG_BRIGHTNESS).size
s.platform.offsetMs += IoTimings.SCREEN_REFRESH_MS + 1
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).size > sent }, s.tail())
assertTrue(ofType(CFW_MSG_BRIGHTNESS).last().let { it[2].toInt() == 70 && it[3].toInt() == 1 })
assertEquals(1, ofType(CFW_MSG_AMBIENT_LIGHT).size)
// Auto: reports reach the policy and listeners, the sample rate follows, a target goes out
s.core.configureBrightness(true, 70, 20, 100, "0:0,100:100", 280)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size == 2 }, s.tail())
assertEquals(500, ofType(CFW_MSG_AMBIENT_LIGHT).last().let { (it[3].toInt() and 255) or ((it[4].toInt() and 255) shl 8) })
s.glasses.alsPoll(1000) // 100 lux, the curve's top
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).last()[2].toInt() == 100 }, s.tail())
assertTrue(awaitTrue(1_000) { reportLock.withLock { reports.isNotEmpty() } })
assertEquals(65, reportLock.withLock { reports.first()[0].toInt() })
// A request dropped with the queue before it was written goes out again
s.core.monitor.withLock { s.core.clearAllMessagesLocked("test") }
val queued = ofType(CFW_MSG_AMBIENT_LIGHT).size
s.glasses.alsPoll(1000)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size > queued }, s.tail())
// A reopened page starts over: the firmware gave brightness back when it closed
val before = ofType(CFW_MSG_AMBIENT_LIGHT).size
s.glasses.pageOpen = false
s.glasses.page(0, 3)
assertTrue(awaitTrue(5_000) { s.glasses.pageOpen && s.pageUp() }, s.tail())
s.submitFrame(0x30, 2)
assertTrue(awaitTrue(5_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size > before }, s.tail())
} finally {
s.core.close()
}
}
@Test
fun refusalAsksForPairingModeAndConnectsOnceItIsOn() {
val s = IoSession()