Add a light-sensor demo to the Developer app

This commit is contained in:
James Babcock committed 2026-09-03 22:23:54 -07:00
1 parent aef6194687
commit c37a9eec45
9 files changed
+525 -20

No files matched your search

@@ -0,0 +1,10 @@
package com.faceclaw.app;
public interface FaceclawAmbientLightListener {
/**
* A CFW ambient-light report (settings-channel field 105) from the master
* temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is
* decoded on the TS side (app/native/ambient-light.ts).
*/
void onAmbientLight(byte[] body);
}
@@ -73,6 +73,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final java.util.List<CompassSubscription> compassSubscriptions =
new java.util.concurrent.CopyOnWriteArrayList<>();
private final java.util.List<FaceclawAmbientLightListener> ambientLightListeners =
new java.util.concurrent.CopyOnWriteArrayList<>();
private final java.util.List<FaceclawMicStatusListener> micStatusListeners =
new java.util.concurrent.CopyOnWriteArrayList<>();
private volatile String lastFirmwareCapabilities = "";
@@ -595,6 +597,91 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return lastFirmwareCapabilities;
}
public void addAmbientLightListener(FaceclawAmbientLightListener listener) {
if (listener != null) {
ambientLightListeners.add(listener);
}
}
public void removeAmbientLightListener(FaceclawAmbientLightListener listener) {
if (listener != null) {
ambientLightListeners.remove(listener);
}
}
private void emitAmbientLight(byte[] body) {
if (ambientLightListeners.isEmpty()) {
return;
}
byte[] copy = java.util.Arrays.copyOf(body, body.length);
mainHandler.post(() -> {
for (FaceclawAmbientLightListener alsListener : ambientLightListeners) {
try {
alsListener.onAmbientLight(copy);
} catch (Throwable t) {
Log.w(TAG, "ambient light listener failed", t);
}
}
});
}
/** Request one CFW ambient-light report (image-handler mode 16 op 0). */
public void queryAmbientLight() {
synchronized (lock) {
enqueueAmbientLightControlLocked(new byte[] { (byte) 16, (byte) 0 }, "als query", false);
}
interruptibleSleep.interrupt();
}
/**
* Start or stop CFW passive light-sensor polling (image-handler mode 16
* ops 1/2). While polling, the firmware reads its OPT3001 every intervalMs
* (clamped 100..5000 by the firmware), never steps the panel brightness
* itself, and pushes a field-105 report when the reading moved by at least
* minDelta or heartbeatMs elapsed. bindToLease stops polling automatically
* when the Faceclaw framebuffer lease is released or lapses. Starting is
* idempotent and re-opens the sensor if the stock firmware closed it.
*/
public void setAmbientLightPolling(boolean enable, int intervalMs, int minDelta,
int heartbeatMs, boolean bindToLease) {
byte[] payload = enable
? new byte[] {
(byte) 16,
(byte) 1,
(byte) (bindToLease ? 1 : 0),
(byte) (intervalMs & 0xff),
(byte) ((intervalMs >> 8) & 0xff),
(byte) (minDelta & 0xff),
(byte) ((minDelta >> 8) & 0xff),
(byte) (heartbeatMs & 0xff),
(byte) ((heartbeatMs >> 8) & 0xff),
}
: new byte[] { (byte) 16, (byte) 2 };
synchronized (lock) {
clearMessagesOfKindLocked("als-control");
enqueueAmbientLightControlLocked(payload, "als polling " + (enable ? "start" : "stop"), true);
}
interruptibleSleep.interrupt();
}
private void enqueueAmbientLightControlLocked(byte[] payload, String label, boolean priority) {
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) {
logLine("skip " + label + "; display path not ready");
return;
}
OutboundMessage message = messageBuilder.imagePayload(
"als-control",
DASHBOARD_TILE,
nextMapSessionId(),
payload,
label,
connectionOptions.sendImagesToLeft);
message.onTimeout = () -> logLine(label + " ack timeout");
if (priority) pendingMessages.addFirst(message);
else pendingMessages.addLast(message);
logLine("queue " + label);
}
public void addMicStatusListener(FaceclawMicStatusListener listener) {
if (listener != null) {
micStatusListeners.add(listener);
@@ -1410,6 +1497,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
emitMicStatus(micStatus, address);
}
}
if (!faceclawWakeNotification
&& frame.ok
&& frame.sid == BleProtocol.SID_UI_SETTING) {
// CFW ambient-light report (field 105) from the master temple.
byte[] alsReport = BleProtocol.parseFaceclawAlsReport(frame.pb);
if (alsReport != null) {
emitAmbientLight(alsReport);
}
}
if (!faceclawWakeNotification
&& decodedWearState < 0
&& frame.ok
@@ -57,6 +57,12 @@ public class BleProtocol {
*/
public static final int FACECLAW_MIC_CONTROL_FIELD = 103;
public static final int FACECLAW_MIC_STATUS_FIELD = 104;
/**
* CFW als_sensor (EVENCFW/18, caps token als16): ambient-light reports on
* settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from
* the master temple (decoded in app/native/ambient-light.ts).
*/
public static final int FACECLAW_ALS_REPORT_FIELD = 105;
public static final int FACECLAW_WAKE_OP_ACQUIRE = 1;
public static final int FACECLAW_WAKE_OP_RELEASE = 2;
public static final int FACECLAW_WAKE_OP_CLAIM = 3;
@@ -449,6 +455,26 @@ public class BleProtocol {
return body;
}
/**
* Return the raw CFW ambient-light report from a sid-0x09 frame, or null
* when this is an ordinary settings frame. Only the 'A','L',version prelude
* is validated here; the fields are decoded on the TS side.
*/
public static byte[] parseFaceclawAlsReport(byte[] pb) {
if (pb == null) {
return null;
}
byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_ALS_REPORT_FIELD);
if (body == null
|| body.length < 24
|| body[0] != 'A'
|| body[1] != 'L'
|| (body[2] & 0xff) != 1) {
return null;
}
return body;
}
/**
* Return the uint16 wake nonce from a CFW field-102 notification, or -1
* when this is an ordinary settings frame.