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

+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()