forked from Blendman974/faceclaw
Brightness control
This commit is contained in:
1 parent
739e81c8a6
commit
f803f4212c
9 files changed
+233
-23
No files matched your search
+8
-4
@@ -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()
|
||||
}
|
||||
+52
-9
@@ -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
|
||||
|
||||
+3
-1
@@ -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()
|
||||
|
||||
+31
-2
@@ -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. */
|
||||
|
||||
Reference in new issue
Block a user