forked from Blendman974/faceclaw
Custom auto-brightness, and fade in/out effects
This commit is contained in:
1 parent
f66e0b025f
commit
4f3ad37c20
29 files changed
+589
-93
No files matched your search
+7
-8
@@ -422,18 +422,17 @@ class BleProtocol {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lens brightness: G2SettingPackage{commandId=1 (DeviceReceiveInfo),
|
||||
* deviceReceiveInfoFromApp(3){deviceReceiveBrightness(1){autoAdjust(1),
|
||||
* brightnessLevel(2)}}} on sid 0x09. brightnessLevel is 0-100 (nonlinear; 0 is
|
||||
* dim-but-visible, not off). When autoAdjust is set the ambient-light sensor drives
|
||||
* brightness, so the level field is omitted; otherwise the level is always encoded
|
||||
* (explicit zero included) so brightnessLevel=0 reaches the wire.
|
||||
* Stock settings helper (Faceclaw uses CFW mode 30). autoAdjust and
|
||||
* brightnessLevel are oneof alternatives: never encode both. A level
|
||||
* alone does NOT disable stock auto adjustment; it is a temporary
|
||||
* manual override when auto remains enabled. Stock clamps to 2..100.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun buildSetBrightness(magic: Int, autoAdjust: Boolean, brightnessLevel: Int): ByteArray {
|
||||
var brightness: MutableList<ByteArray> = ArrayList()
|
||||
brightness.add(encodeVarintField(1, (if (autoAdjust) 1 else 0)))
|
||||
if (!autoAdjust) {
|
||||
if (autoAdjust) {
|
||||
brightness.add(encodeVarintField(1, 1))
|
||||
} else {
|
||||
brightness.add(encodeVarintField(2, brightnessLevel))
|
||||
}
|
||||
return concat(
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.ln
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/** Phone-owned policy. Firmware receives only a transient target and fade time. */
|
||||
class BrightnessPolicy {
|
||||
var automatic = true
|
||||
private set
|
||||
var fadeMs = 280
|
||||
private set
|
||||
var target = 40
|
||||
private set
|
||||
private var manual = 50
|
||||
private var minimum = 20
|
||||
private var maximum = 100
|
||||
private var curve = parseCurve(DEFAULT_CURVE)
|
||||
private var filteredLux: Double? = null
|
||||
private var sampleAt = 0L
|
||||
|
||||
fun setMode(auto: Boolean, level: Int) {
|
||||
automatic = auto
|
||||
manual = level.coerceIn(2, 100)
|
||||
target = if (auto) filteredLux?.let { evaluate(it) } ?: target.coerceIn(minimum, maximum) else manual
|
||||
}
|
||||
|
||||
fun configure(auto: Boolean, level: Int, min: Int, max: Int, points: String, fade: Int) {
|
||||
val parsed = parseCurve(points) // Validate before mutating the live policy.
|
||||
automatic = auto
|
||||
manual = level.coerceIn(2, 100)
|
||||
minimum = min.coerceIn(2, 100)
|
||||
maximum = max.coerceIn(minimum, 100)
|
||||
curve = parsed
|
||||
fadeMs = fade.coerceIn(0, 1500)
|
||||
target = if (auto) filteredLux?.let { evaluate(it) } ?: target.coerceIn(minimum, maximum) else manual
|
||||
}
|
||||
|
||||
/** Only live successful passive POLL reports count, never cached query/start reports. */
|
||||
fun sample(report: ByteArray, now: Long): Boolean {
|
||||
if (report.size < 24 || report[0] != 65.toByte() || report[1] != 76.toByte() ||
|
||||
report[2] != 1.toByte() || report[3] != 2.toByte() || (report[4].toInt() and 11) != 11) return false
|
||||
var reading = 0L
|
||||
for (i in 0..3) reading = reading or ((report[6+i].toLong() and 255) shl (8*i))
|
||||
val lux = reading / 10.0
|
||||
val old = filteredLux
|
||||
val elapsed = (now - sampleAt).coerceAtLeast(0)
|
||||
filteredLux = if (old == null || elapsed > 10_000) lux else {
|
||||
val seconds = if (lux > old) 0.8 else 3.0
|
||||
old + (lux-old) * (1-exp(-elapsed/1000.0/seconds))
|
||||
}
|
||||
sampleAt = now
|
||||
if (!automatic) return false
|
||||
val next = evaluate(filteredLux!!)
|
||||
// Compare with the last accepted target, not the previous sample, so
|
||||
// gradual drift accumulates. Bounds obey the same deadband to avoid
|
||||
// extra panel writes when light hovers near the minimum or maximum.
|
||||
if (abs(next-target) < AUTO_CHANGE_THRESHOLD) return false
|
||||
val changed = target != next
|
||||
target = next
|
||||
return changed
|
||||
}
|
||||
|
||||
fun resetSamples() { filteredLux = null; sampleAt = 0 }
|
||||
|
||||
private fun evaluate(lux: Double): Int {
|
||||
val x = ln(1+lux.coerceAtLeast(0.0))
|
||||
var fraction = curve.last().second / 100
|
||||
if (x <= curve.first().first) fraction = curve.first().second / 100
|
||||
else for (i in 1 until curve.size) {
|
||||
val (right, high) = curve[i]
|
||||
if (x <= right) {
|
||||
val (left, low) = curve[i-1]
|
||||
fraction = (low + (high-low)*(x-left)/(right-left))/100
|
||||
break
|
||||
}
|
||||
}
|
||||
return (minimum+(maximum-minimum)*fraction).roundToInt().coerceIn(minimum, maximum)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val AUTO_CHANGE_THRESHOLD = 5
|
||||
const val DEFAULT_CURVE = "0:0,0.2:7,1.5:33,5:100"
|
||||
/** Lux:percent-of-configured-range knots; interpolate in log(1+lux). */
|
||||
fun parseCurve(text: String): List<Pair<Double, Double>> {
|
||||
val entries = text.split(',')
|
||||
require(entries.size in 2..16) { "Use 2–16 lux:percent pairs" }
|
||||
val points = entries.map {
|
||||
val pair = it.trim().split(':')
|
||||
require(pair.size == 2) { "Expected lux:percent" }
|
||||
val lux = pair[0].trim().toDouble()
|
||||
val percent = pair[1].trim().toDouble()
|
||||
require(lux.isFinite() && lux in 0.0..1_000_000.0 && percent.isFinite() && percent in 0.0..100.0)
|
||||
ln(1+lux) to percent
|
||||
}
|
||||
require(points.first() == (0.0 to 0.0) && points.last().second == 100.0) { "Start at 0:0 and end at 100%" }
|
||||
require(points.zipWithNext().all { (a,b) -> b.first > a.first && b.second >= a.second }) { "Lux must increase and brightness must not decrease" }
|
||||
return points
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -34,5 +34,6 @@ const val CFW_MSG_DRAW_CALLS = 26
|
||||
const val CFW_MSG_SET_ROOT_DISPLAY_LIST = 27
|
||||
const val CFW_MSG_PRESENT = 28
|
||||
const val CFW_MSG_CREATE_SURFACE = 29
|
||||
const val CFW_MSG_BRIGHTNESS = 30
|
||||
const val CFW_MSG_TYPE_MASK = 127
|
||||
const val CFW_MSG_FLAG_LENSES_DIFFER = 128
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** These functions run under the session monitor. Config survives reconnection;
|
||||
* queued state and sensor freshness do not. Private commands are ordered with
|
||||
* frames, so wake starts on the intended PRESENT, never on an older black frame. */
|
||||
internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? = null) {
|
||||
if (!customFirmwareDetected || !sessionReady || !fixedLayoutCreated || shutdownRequested) return
|
||||
val visible = frameVisible ?: brightnessSentVisible ?: return
|
||||
val target = brightnessPolicy.target
|
||||
if (brightnessSentLevel == target && brightnessSentVisible == visible) return
|
||||
if (frameVisible == null && hasPendingImageLocked()) return
|
||||
if (frameVisible == null && hasPendingOrInflightKindLocked("brightness-output")) return
|
||||
val duration = if (brightnessSentVisible != visible) brightnessPolicy.fadeMs
|
||||
else if (brightnessPolicy.automatic) 1200 else 120
|
||||
val payload = byteArrayOf(CFW_MSG_BRIGHTNESS.toByte(), 1, target.toByte(),
|
||||
(if (visible) 1 else 0).toByte(), duration.toByte(), (duration shr 8).toByte())
|
||||
val message = messageBuilder.customMessage("brightness-output", payload,
|
||||
"brightness target=$target visible=$visible fade=$duration", -1, connectionOptions.sendImagesToLeft)
|
||||
message.onTimeout = MessageCallback {
|
||||
brightnessSentLevel = -1
|
||||
brightnessSentVisible = null
|
||||
// Retry with a PRESENT even if the scene did not change during recovery.
|
||||
lastEnqueuedFingerprint = ""
|
||||
}
|
||||
pendingMessages.addLast(message)
|
||||
brightnessSentLevel = target
|
||||
brightnessSentVisible = visible
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
enqueueBrightnessLocked()
|
||||
}
|
||||
+20
-16
@@ -183,6 +183,11 @@ class GlassesSessionCore(
|
||||
* needed here.
|
||||
*/
|
||||
internal var customFirmwareDetected = false
|
||||
internal val brightnessPolicy = BrightnessPolicy()
|
||||
internal var brightnessSentVisible: Boolean? = null
|
||||
internal var brightnessSentLevel = -1
|
||||
internal var brightnessAlsStartedAt = -2000L
|
||||
internal var brightnessDemoPolling = false
|
||||
internal var cfwCleanupDelivered = false
|
||||
internal var lastCfwCleanupAckMagic = 0
|
||||
|
||||
@@ -612,28 +617,19 @@ class GlassesSessionCore(
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lens brightness. Fire-and-forget, like the IMU control: the
|
||||
* message is queued ahead of other traffic and any not-yet-sent brightness
|
||||
* message is superseded. When autoAdjust is true the ambient-light sensor
|
||||
* drives brightness and brightnessLevel is ignored; otherwise
|
||||
* brightnessLevel (0-100) is applied directly.
|
||||
*/
|
||||
/** Update desired brightness; retained before readiness and across reconnect. */
|
||||
fun setBrightness(autoAdjust: Boolean, brightnessLevel: Int) {
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady) {
|
||||
logLine("skip brightness set; session not ready")
|
||||
return
|
||||
}
|
||||
clearMessagesOfKindLocked("brightness-control")
|
||||
val message = messageBuilder.setBrightness(autoAdjust, brightnessLevel)
|
||||
message.onTimeout = MessageCallback { logLine("brightness control ack timeout") }
|
||||
pendingMessages.addFirst(message)
|
||||
logLine("queue brightness " + (if (autoAdjust) "auto" else "level=$brightnessLevel"))
|
||||
brightnessPolicy.setMode(autoAdjust, brightnessLevel)
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
fun configureBrightness(auto: Boolean, level: Int, minimum: Int, maximum: Int, curve: String, fadeMs: Int) {
|
||||
monitor.withLock { brightnessPolicy.configure(auto, level, minimum, maximum, curve, fadeMs) }
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the stock wear detector, then ask CFW to emit its current cached
|
||||
* state. The queue order matters: the query must run after the setting is
|
||||
@@ -798,6 +794,13 @@ class GlassesSessionCore(
|
||||
*/
|
||||
fun setAmbientLightPolling(enable: Boolean, intervalMs: Int, minDelta: Int,
|
||||
heartbeatMs: Int, bindToLease: Boolean) {
|
||||
// The brightness controller owns passive mode throughout a CFW session.
|
||||
// The demo can request faster samples without releasing that ownership.
|
||||
if (customFirmwareDetected) {
|
||||
monitor.withLock { brightnessDemoPolling = enable; brightnessAlsStartedAt = -2000 }
|
||||
interruptibleSleep.interrupt()
|
||||
return
|
||||
}
|
||||
val payload = if (enable)
|
||||
byteArrayOf(
|
||||
CFW_MSG_AMBIENT_LIGHT.toByte(),
|
||||
@@ -1467,6 +1470,7 @@ class GlassesSessionCore(
|
||||
// CFW ambient-light report (field 105) from the master temple.
|
||||
val alsReport = BleProtocol.parseFaceclawAlsReport(frame.pb)
|
||||
if (alsReport != null) {
|
||||
if (address.equals(rightAddress, ignoreCase = true)) brightnessPolicy.sample(alsReport, now())
|
||||
emitAmbientLight(alsReport)
|
||||
}
|
||||
}
|
||||
|
||||
+15
-1
@@ -169,6 +169,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
if (benchmarkActive) {
|
||||
maintainBenchmarkLocked(now)
|
||||
}
|
||||
if (!benchmarkActive) maintainBrightnessLocked(now)
|
||||
|
||||
// Up to WINDOW_SIZE messages may be in flight at once (full
|
||||
// pipelining); a slot frees when an ack arrives. An active
|
||||
@@ -582,6 +583,10 @@ internal fun GlassesSessionCore.logImageUpdateLandmarkLocked(event: String, mess
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.enqueueCreateLayoutLocked() {
|
||||
brightnessSentVisible = null
|
||||
brightnessSentLevel = -1
|
||||
brightnessAlsStartedAt = -2000
|
||||
brightnessPolicy.resetSamples()
|
||||
// New session: re-assert the firmware-debug-flags overlay once
|
||||
// the layout is ready (the mode-7 send is gated on this having reset).
|
||||
firmwareDebugFlagsLastSent = -1
|
||||
@@ -705,7 +710,7 @@ internal fun GlassesSessionCore.enqueueAmbientLightControlLocked(payload: ByteAr
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
val fingerprint = desiredFingerprintSnapshot()
|
||||
val fingerprint: String
|
||||
val packedSnapshot: ByteArray?
|
||||
val width: Int
|
||||
val height: Int
|
||||
@@ -714,6 +719,7 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
val draws: Array<SurfaceCompositor.ScreenDraw>?
|
||||
val scene: ShellScene
|
||||
desiredTilesLock.locked {
|
||||
fingerprint = desiredFingerprint
|
||||
packedSnapshot = desiredPacked
|
||||
width = desiredWidth
|
||||
height = desiredHeight
|
||||
@@ -724,6 +730,8 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
desiredFrameId = 0
|
||||
}
|
||||
val packedFrame: ByteArray = packedSnapshot ?: ByteArray(0)
|
||||
// Visibility belongs to this immutable composite, not the latest UI request.
|
||||
enqueueBrightnessLocked(!fingerprint.startsWith("blanked:"))
|
||||
if (customFirmwareDetected && packedFrame.size > 0) {
|
||||
val rendered = scenePlanner.plan(packedFrame, width, height, draws, scene, nextImageFrameId,
|
||||
connectionOptions.TEXTURE_CACHE_FRAMES, connectionOptions.INCREMENTAL_FRAMES,
|
||||
@@ -1195,6 +1203,9 @@ internal fun GlassesSessionCore.clearMessagesOfKindLocked(kind: String) {
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.clearAllMessagesLocked(reason: String) {
|
||||
brightnessSentVisible = null
|
||||
brightnessSentLevel = -1
|
||||
brightnessAlsStartedAt = -2000
|
||||
clearPendingMessagesLocked(reason)
|
||||
clearInFlightMessagesLocked(reason)
|
||||
// The image pipeline is gone: drop the pipelined delta base so the next
|
||||
@@ -1213,6 +1224,9 @@ internal fun GlassesSessionCore.clearAllMessagesLocked(reason: String) {
|
||||
* direct prelude write.
|
||||
*/
|
||||
internal fun GlassesSessionCore.clearAllMessagesPreservingWakeLeaseLocked(reason: String) {
|
||||
brightnessSentVisible = null
|
||||
brightnessSentLevel = -1
|
||||
brightnessAlsStartedAt = -2000
|
||||
val pendingIterator = pendingMessages.iterator()
|
||||
while (pendingIterator.hasNext()) {
|
||||
val message = pendingIterator.next()
|
||||
|
||||
@@ -147,6 +147,8 @@ class IosGlassesSession internal constructor(
|
||||
fun setCompassEnabledForOwner(owner: String?, enable: Boolean) = core.setCompassEnabled(owner, enable)
|
||||
|
||||
fun setBrightness(autoAdjust: Boolean, brightnessLevel: Int) = core.setBrightness(autoAdjust, brightnessLevel)
|
||||
fun configureBrightness(auto: Boolean, level: Int, minimum: Int, maximum: Int, curve: String, fadeMs: Int) =
|
||||
core.configureBrightness(auto, level, minimum, maximum, curve, fadeMs)
|
||||
|
||||
fun enableWearDetectionAndRequestState() = core.enableWearDetectionAndRequestState()
|
||||
|
||||
|
||||
Reference in new issue
Block a user