forked from Blendman974/faceclaw
Add accelerometer support for EvenHub
This commit is contained in:
1 parent
83b3049045
commit
f87c8e898b
2 files changed
+135
-11
No files matched your search
@@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* Arbitrates the shared G2 accelerometer (cmd-19 IMU report stream) among
|
||||||
|
* running EvenHub apps.
|
||||||
|
*
|
||||||
|
* imuControl(true) registers an app as wanting IMU; only the foreground
|
||||||
|
* (active) window actually receives readings, so a backgrounded app can't keep
|
||||||
|
* the flaky, battery-costly BLE stream running. Unlike the mic this is NOT
|
||||||
|
* gated on screen state — head-gesture apps legitimately want IMU while the
|
||||||
|
* display is blanked. Because only one window is foreground, at most one app is
|
||||||
|
* ever the active reader, which also avoids two apps fighting over the single
|
||||||
|
* global report rate.
|
||||||
|
*
|
||||||
|
* The IMU stream is historically unreliable (the firmware may not populate it;
|
||||||
|
* see the imu-accelerometer memory) — treat it as best-effort.
|
||||||
|
*/
|
||||||
|
import { addImuListener, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||||
|
|
||||||
|
export type EvenHubImuClient = {
|
||||||
|
readonly windowId: string;
|
||||||
|
isForeground(): boolean;
|
||||||
|
deliverImu(reading: ImuReading): void;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EvenHubImuRouter {
|
||||||
|
private readonly requesting = new Map<EvenHubImuClient, number>();
|
||||||
|
private active: EvenHubImuClient | null = null;
|
||||||
|
private unsubscribe: (() => void) | null = null;
|
||||||
|
/** The report rate the stream is currently enabled at; 0 = disabled. */
|
||||||
|
private enabledFreq = 0;
|
||||||
|
|
||||||
|
/** An app enabled IMU (imuControl true) at the given report rate. */
|
||||||
|
requestImu(client: EvenHubImuClient, freq: number): void {
|
||||||
|
this.requesting.set(client, freq);
|
||||||
|
this.evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An app disabled IMU, backgrounded past use, or closed. */
|
||||||
|
releaseImu(client: EvenHubImuClient): void {
|
||||||
|
if (!this.requesting.delete(client)) return;
|
||||||
|
this.evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A requesting app's foreground state changed. */
|
||||||
|
notifyEligibilityChanged(): void {
|
||||||
|
this.evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private eligible(): EvenHubImuClient | null {
|
||||||
|
return Array.from(this.requesting.keys()).find((client) => client.isForeground()) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private evaluate(): void {
|
||||||
|
const next = this.eligible();
|
||||||
|
this.active = next;
|
||||||
|
if (next) {
|
||||||
|
if (!this.unsubscribe) {
|
||||||
|
this.unsubscribe = addImuListener((reading) => this.active?.deliverImu(reading));
|
||||||
|
}
|
||||||
|
const freq = this.requesting.get(next) ?? 100;
|
||||||
|
if (this.enabledFreq !== freq) {
|
||||||
|
setImuReportEnabled(true, freq);
|
||||||
|
this.enabledFreq = freq;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.enabledFreq !== 0) {
|
||||||
|
setImuReportEnabled(false, 0);
|
||||||
|
this.enabledFreq = 0;
|
||||||
|
}
|
||||||
|
this.unsubscribe?.();
|
||||||
|
this.unsubscribe = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const evenHubImuRouter = new EvenHubImuRouter();
|
||||||
+60
-11
@@ -36,6 +36,8 @@ import { compositePage } from "./compositor";
|
|||||||
import { type EvenHubManifest } from "./ehpk";
|
import { type EvenHubManifest } from "./ehpk";
|
||||||
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
|
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
|
||||||
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
|
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
|
||||||
|
import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router";
|
||||||
|
import { type ImuReading } from "../../native/imu";
|
||||||
import { getCurrentLocation } from "../../native/location";
|
import { getCurrentLocation } from "../../native/location";
|
||||||
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
||||||
import { ensureFineLocationPermission } from "../../g2/android-permissions";
|
import { ensureFineLocationPermission } from "../../g2/android-permissions";
|
||||||
@@ -50,6 +52,7 @@ const DOUBLE_CLICK_EVENT = 3;
|
|||||||
const FOREGROUND_ENTER_EVENT = 4;
|
const FOREGROUND_ENTER_EVENT = 4;
|
||||||
const FOREGROUND_EXIT_EVENT = 5;
|
const FOREGROUND_EXIT_EVENT = 5;
|
||||||
const SYSTEM_EXIT_EVENT = 7;
|
const SYSTEM_EXIT_EVENT = 7;
|
||||||
|
const IMU_DATA_REPORT_EVENT = 8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Injected into every served HTML document at document start (before any app
|
* Injected into every served HTML document at document start (before any app
|
||||||
@@ -171,7 +174,7 @@ export type EvenHubWindowHooks = {
|
|||||||
focusSwitcher: () => void;
|
focusSwitcher: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class EvenHubSession implements EvenHubMicClient {
|
export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient {
|
||||||
readonly manifest: EvenHubManifest;
|
readonly manifest: EvenHubManifest;
|
||||||
readonly distDir: string;
|
readonly distDir: string;
|
||||||
|
|
||||||
@@ -188,6 +191,8 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
private screenOn = true;
|
private screenOn = true;
|
||||||
/** The app has enabled its microphone via audioControl(true). */
|
/** The app has enabled its microphone via audioControl(true). */
|
||||||
private micRequested = false;
|
private micRequested = false;
|
||||||
|
/** The app has enabled the accelerometer via imuControl(true). */
|
||||||
|
private imuRequested = false;
|
||||||
/** Non-null while the app has an active location subscription. */
|
/** Non-null while the app has an active location subscription. */
|
||||||
private locationTracker: LocationTracker | null = null;
|
private locationTracker: LocationTracker | null = null;
|
||||||
private log: (message: string) => void;
|
private log: (message: string) => void;
|
||||||
@@ -299,8 +304,10 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
setForeground(foreground: boolean): void {
|
setForeground(foreground: boolean): void {
|
||||||
if (this.foreground === foreground) return;
|
if (this.foreground === foreground) return;
|
||||||
this.foreground = foreground;
|
this.foreground = foreground;
|
||||||
// Foreground gates mic eligibility (an app only hears audio while visible).
|
// Foreground gates mic + IMU eligibility (only the visible app hears audio /
|
||||||
|
// reads the accelerometer).
|
||||||
if (this.micRequested) evenHubMicRouter.notifyEligibilityChanged();
|
if (this.micRequested) evenHubMicRouter.notifyEligibilityChanged();
|
||||||
|
if (this.imuRequested) evenHubImuRouter.notifyEligibilityChanged();
|
||||||
if (!this.pageCreated) return;
|
if (!this.pageCreated) return;
|
||||||
this.emitSysEvent(foreground ? FOREGROUND_ENTER_EVENT : FOREGROUND_EXIT_EVENT, 0);
|
this.emitSysEvent(foreground ? FOREGROUND_ENTER_EVENT : FOREGROUND_EXIT_EVENT, 0);
|
||||||
}
|
}
|
||||||
@@ -343,7 +350,7 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
this.log("evenhub: audioControl denied (app declares no microphone permission)");
|
this.log("evenhub: audioControl denied (app declares no microphone permission)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const enable = readAudioEnable(data);
|
const enable = readEnableFlag(data);
|
||||||
// The 0.0.12 payload shape isn't captured; log it once per toggle so the
|
// The 0.0.12 payload shape isn't captured; log it once per toggle so the
|
||||||
// real field names can be confirmed from hardware.
|
// real field names can be confirmed from hardware.
|
||||||
this.log(`evenhub: audioControl enable=${enable} data=${JSON.stringify(data)}`);
|
this.log(`evenhub: audioControl enable=${enable} data=${JSON.stringify(data)}`);
|
||||||
@@ -415,6 +422,37 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----- accelerometer / IMU (EvenHubImuClient) -----
|
||||||
|
|
||||||
|
/** A shared IMU reading from the router: hand it to the app as a sysEvent. */
|
||||||
|
deliverImu(reading: ImuReading): void {
|
||||||
|
this.pushEvenHubEvent("sysEvent", {
|
||||||
|
eventType: IMU_DATA_REPORT_EVENT,
|
||||||
|
imuData: { x: reading.x, y: reading.y, z: reading.z },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* imuControl(isOpen, reportFrq): enable/disable the accelerometer report
|
||||||
|
* stream. There is no EvenHub IMU permission (nor an Android one), so no
|
||||||
|
* consent gate. Enabling registers intent; the router only actually streams
|
||||||
|
* to the foreground app. reportFrq is one of 100..1000 (step 100).
|
||||||
|
*/
|
||||||
|
private imuControl(data: Record<string, unknown>): boolean {
|
||||||
|
const enable = readEnableFlag(data);
|
||||||
|
const freq = clampImuFreq(readOptionalNumber(data, "reportFrq") ?? readOptionalNumber(data, "reportFreq") ?? 100);
|
||||||
|
// Payload field spelling isn't fully captured; log once per toggle.
|
||||||
|
this.log(`evenhub: imuControl enable=${enable} freq=${freq} data=${JSON.stringify(data)}`);
|
||||||
|
if (enable) {
|
||||||
|
this.imuRequested = true;
|
||||||
|
evenHubImuRouter.requestImu(this, freq);
|
||||||
|
} else if (this.imuRequested) {
|
||||||
|
this.imuRequested = false;
|
||||||
|
evenHubImuRouter.releaseImu(this);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// ----- bridge calls (web -> host) -----
|
// ----- bridge calls (web -> host) -----
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -482,9 +520,9 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
return this.startLocationUpdates(data);
|
return this.startLocationUpdates(data);
|
||||||
case "stopAppLocationUpdates":
|
case "stopAppLocationUpdates":
|
||||||
return this.stopLocationUpdates();
|
return this.stopLocationUpdates();
|
||||||
// Unsupported surface (IMU, pickers): fail politely.
|
|
||||||
case "imuControl":
|
case "imuControl":
|
||||||
return false;
|
return this.imuControl(data);
|
||||||
|
// Unsupported surface (album/camera pickers): fail politely.
|
||||||
case "pickImageFromAlbum":
|
case "pickImageFromAlbum":
|
||||||
case "captureImageFromCamera":
|
case "captureImageFromCamera":
|
||||||
return null;
|
return null;
|
||||||
@@ -628,6 +666,10 @@ export class EvenHubSession implements EvenHubMicClient {
|
|||||||
this.micRequested = false;
|
this.micRequested = false;
|
||||||
evenHubMicRouter.releaseMic(this);
|
evenHubMicRouter.releaseMic(this);
|
||||||
}
|
}
|
||||||
|
if (this.imuRequested) {
|
||||||
|
this.imuRequested = false;
|
||||||
|
evenHubImuRouter.releaseImu(this);
|
||||||
|
}
|
||||||
this.locationTracker?.stop();
|
this.locationTracker?.stop();
|
||||||
this.locationTracker = null;
|
this.locationTracker = null;
|
||||||
if (!this.systemExitSent) {
|
if (!this.systemExitSent) {
|
||||||
@@ -677,13 +719,14 @@ function toAppLocation(location: TrackedLocation): AppLocation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the enable flag from an audioControl payload. The exact wire field name
|
* Read the enable flag from an audioControl / imuControl payload. The exact
|
||||||
* for SDK 0.0.12 isn't captured, so accept the plausible spellings; default to
|
* wire field name for SDK 0.0.12 isn't captured, so accept the plausible
|
||||||
* enabling (the common intent) when the payload is opaque, and log it so the
|
* spellings (the mock bridge reads `isOpen`; IMU's PB field is `iMUReportEn`);
|
||||||
* real shape can be pinned from hardware.
|
* default to enabling (the common intent) when the payload is opaque, and log
|
||||||
|
* it so the real shape can be pinned from hardware.
|
||||||
*/
|
*/
|
||||||
function readAudioEnable(data: Record<string, unknown>): boolean {
|
function readEnableFlag(data: Record<string, unknown>): boolean {
|
||||||
for (const key of ["enable", "open", "isOpen", "on", "status", "value", "start"]) {
|
for (const key of ["enable", "open", "isOpen", "on", "status", "value", "start", "iMUReportEn", "IMU_ReportEn"]) {
|
||||||
const value = data[key];
|
const value = data[key];
|
||||||
if (typeof value === "boolean") return value;
|
if (typeof value === "boolean") return value;
|
||||||
if (typeof value === "number") return value !== 0;
|
if (typeof value === "number") return value !== 0;
|
||||||
@@ -692,6 +735,12 @@ function readAudioEnable(data: Record<string, unknown>): boolean {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Snap a requested IMU report rate to the SDK's 100..1000 (step 100) grid. */
|
||||||
|
function clampImuFreq(freq: number): number {
|
||||||
|
const snapped = Math.round(freq / 100) * 100;
|
||||||
|
return Math.min(1000, Math.max(100, snapped));
|
||||||
|
}
|
||||||
|
|
||||||
/** ring/left-arm/right-arm -> PB EventSourceType. */
|
/** ring/left-arm/right-arm -> PB EventSourceType. */
|
||||||
function gestureSource(source: "ring" | "left-arm" | "right-arm"): number {
|
function gestureSource(source: "ring" | "left-arm" | "right-arm"): number {
|
||||||
switch (source) {
|
switch (source) {
|
||||||
|
|||||||
Reference in new issue
Block a user