forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw
This commit is contained in:
commit
c5f80a0b00
16 files changed
+737
-151
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Platform-neutral ambient-light types and the CFW report decoder (settings
|
||||
* field 105), shared by ambient-light.ts and ambient-light.ios.ts.
|
||||
*/
|
||||
|
||||
export const ALS_REPORT_VERSION = 1;
|
||||
|
||||
export type AmbientLightReason = "query" | "started" | "poll" | "stopped";
|
||||
|
||||
export type AmbientLightReport = {
|
||||
reason: AmbientLightReason;
|
||||
/** The sensor driver is open (readings are live). */
|
||||
opened: boolean;
|
||||
/** CFW passive polling is installed (stock auto-brightness is not stepping). */
|
||||
passive: boolean;
|
||||
/** The user's stock auto-brightness setting is on. */
|
||||
autoBrightness: boolean;
|
||||
/** The last passive read succeeded (only meaningful while passive). */
|
||||
readOk: boolean;
|
||||
/** Stock driver status: 1 start-read, 2 adjusting, 3 polling. */
|
||||
status: number;
|
||||
/** Ambient light in the stock driver's units (roughly tenths of a lux). */
|
||||
alsValue: number;
|
||||
/** Max of the last five readings (what the stock curve is applied to). */
|
||||
peak: number;
|
||||
/** Brightness level (0..100) the stock curve would choose for `peak`. */
|
||||
stockTarget: number;
|
||||
/** Current brightness setting (0..100). */
|
||||
brightness: number;
|
||||
/** Row of the stock brightness curve (0..5) for `peak`. */
|
||||
gear: number;
|
||||
/** Stock learned scale factor, Q10 (1024 = 1.0). */
|
||||
scaleQ10: number;
|
||||
/** Firmware millisecond tick when the report was built. */
|
||||
tickMs: number;
|
||||
};
|
||||
|
||||
const REASONS: AmbientLightReason[] = ["query", "started", "poll", "stopped"];
|
||||
|
||||
function rd16(b: Uint8Array, i: number): number {
|
||||
return b[i] | (b[i + 1] << 8);
|
||||
}
|
||||
|
||||
function rd32(b: Uint8Array, i: number): number {
|
||||
return (b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)) >>> 0;
|
||||
}
|
||||
|
||||
/** Decode a field-105 record body, or null when it isn't one. */
|
||||
export function decodeAmbientLightReport(body: Uint8Array): AmbientLightReport | null {
|
||||
if (body.length < 24 || body[0] !== 0x41 || body[1] !== 0x4c || body[2] !== ALS_REPORT_VERSION) {
|
||||
return null;
|
||||
}
|
||||
const flags = body[4];
|
||||
return {
|
||||
reason: REASONS[body[3]] ?? "poll",
|
||||
opened: (flags & 0x01) !== 0,
|
||||
passive: (flags & 0x02) !== 0,
|
||||
autoBrightness: (flags & 0x04) !== 0,
|
||||
readOk: (flags & 0x08) !== 0,
|
||||
status: body[5],
|
||||
alsValue: rd32(body, 6),
|
||||
peak: rd32(body, 10),
|
||||
stockTarget: rd16(body, 14),
|
||||
brightness: body[16],
|
||||
gear: body[17],
|
||||
scaleQ10: rd16(body, 18),
|
||||
tickMs: rd32(body, 20),
|
||||
};
|
||||
}
|
||||
|
||||
export type AmbientLightPollingOptions = {
|
||||
/** Poll period in ms; the firmware clamps to 100..5000. Default 500. */
|
||||
intervalMs?: number;
|
||||
/** Report when the reading moved by at least this much. Default 1. */
|
||||
minDelta?: number;
|
||||
/** Also report after this many ms without one (0 = never). Default 5000. */
|
||||
heartbeatMs?: number;
|
||||
/**
|
||||
* Stop automatically when the Faceclaw framebuffer lease is released or
|
||||
* lapses (the phone went away). Default true.
|
||||
*/
|
||||
bindToLease?: boolean;
|
||||
};
|
||||
|
||||
/** The options with their defaults filled in. */
|
||||
export function resolvePollingOptions(options: AmbientLightPollingOptions): Required<AmbientLightPollingOptions> {
|
||||
return {
|
||||
intervalMs: options.intervalMs ?? 500,
|
||||
minDelta: options.minDelta ?? 1,
|
||||
heartbeatMs: options.heartbeatMs ?? 5000,
|
||||
bindToLease: options.bindToLease ?? true,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios";
|
||||
import {
|
||||
decodeAmbientLightReport,
|
||||
resolvePollingOptions,
|
||||
type AmbientLightPollingOptions,
|
||||
type AmbientLightReport,
|
||||
} from "./ambient-light-types";
|
||||
|
||||
export * from "./ambient-light-types";
|
||||
|
||||
/**
|
||||
* Ambient light sensor access on iOS: the same API as ambient-light.ts (see
|
||||
* there for the CFW query and passive-polling semantics) over the running
|
||||
* shared-Kotlin session. No-ops while no session is running.
|
||||
*/
|
||||
|
||||
/** Ask the firmware for one ambient-light report (delivered to listeners). */
|
||||
export function queryAmbientLight(): void {
|
||||
try {
|
||||
FaceclawCommunicatorBridge.getActive()?.queryAmbientLight();
|
||||
} catch (error) {
|
||||
console.warn(`queryAmbientLight failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Start or stop CFW passive light-sensor polling. */
|
||||
export function setAmbientLightPolling(enabled: boolean, options: AmbientLightPollingOptions = {}): void {
|
||||
const { intervalMs, minDelta, heartbeatMs, bindToLease } = resolvePollingOptions(options);
|
||||
try {
|
||||
FaceclawCommunicatorBridge.getActive()?.setAmbientLightPolling(enabled, intervalMs, minDelta, heartbeatMs, bindToLease);
|
||||
} catch (error) {
|
||||
console.warn(`setAmbientLightPolling failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Subscribe to decoded ambient-light reports. Delivered on the main thread. */
|
||||
export function addAmbientLightListener(listener: (report: AmbientLightReport) => void): () => void {
|
||||
const active = FaceclawCommunicatorBridge.getActive();
|
||||
if (!active) return () => {};
|
||||
const unsubscribe = active.addAmbientLightListener((body) => {
|
||||
const report = decodeAmbientLightReport(body);
|
||||
if (report) listener(report);
|
||||
});
|
||||
return () => {
|
||||
try {
|
||||
unsubscribe();
|
||||
} catch {
|
||||
// The session may have been closed during a disconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -16,88 +16,17 @@
|
||||
* Reports are 24-byte ['A','L',version,...] records on settings field 105.
|
||||
*/
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
import {
|
||||
decodeAmbientLightReport,
|
||||
resolvePollingOptions,
|
||||
type AmbientLightPollingOptions,
|
||||
type AmbientLightReport,
|
||||
} from "./ambient-light-types";
|
||||
|
||||
declare const com: any;
|
||||
declare const global: any;
|
||||
|
||||
export const ALS_REPORT_VERSION = 1;
|
||||
|
||||
export type AmbientLightReason = "query" | "started" | "poll" | "stopped";
|
||||
|
||||
export type AmbientLightReport = {
|
||||
reason: AmbientLightReason;
|
||||
/** The sensor driver is open (readings are live). */
|
||||
opened: boolean;
|
||||
/** CFW passive polling is installed (stock auto-brightness is not stepping). */
|
||||
passive: boolean;
|
||||
/** The user's stock auto-brightness setting is on. */
|
||||
autoBrightness: boolean;
|
||||
/** The last passive read succeeded (only meaningful while passive). */
|
||||
readOk: boolean;
|
||||
/** Stock driver status: 1 start-read, 2 adjusting, 3 polling. */
|
||||
status: number;
|
||||
/** Ambient light in the stock driver's units (roughly tenths of a lux). */
|
||||
alsValue: number;
|
||||
/** Max of the last five readings (what the stock curve is applied to). */
|
||||
peak: number;
|
||||
/** Brightness level (0..100) the stock curve would choose for `peak`. */
|
||||
stockTarget: number;
|
||||
/** Current brightness setting (0..100). */
|
||||
brightness: number;
|
||||
/** Row of the stock brightness curve (0..5) for `peak`. */
|
||||
gear: number;
|
||||
/** Stock learned scale factor, Q10 (1024 = 1.0). */
|
||||
scaleQ10: number;
|
||||
/** Firmware millisecond tick when the report was built. */
|
||||
tickMs: number;
|
||||
};
|
||||
|
||||
const REASONS: AmbientLightReason[] = ["query", "started", "poll", "stopped"];
|
||||
|
||||
function rd16(b: Uint8Array, i: number): number {
|
||||
return b[i] | (b[i + 1] << 8);
|
||||
}
|
||||
|
||||
function rd32(b: Uint8Array, i: number): number {
|
||||
return (b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24)) >>> 0;
|
||||
}
|
||||
|
||||
/** Decode a field-105 record body, or null when it isn't one. */
|
||||
export function decodeAmbientLightReport(body: Uint8Array): AmbientLightReport | null {
|
||||
if (body.length < 24 || body[0] !== 0x41 || body[1] !== 0x4c || body[2] !== ALS_REPORT_VERSION) {
|
||||
return null;
|
||||
}
|
||||
const flags = body[4];
|
||||
return {
|
||||
reason: REASONS[body[3]] ?? "poll",
|
||||
opened: (flags & 0x01) !== 0,
|
||||
passive: (flags & 0x02) !== 0,
|
||||
autoBrightness: (flags & 0x04) !== 0,
|
||||
readOk: (flags & 0x08) !== 0,
|
||||
status: body[5],
|
||||
alsValue: rd32(body, 6),
|
||||
peak: rd32(body, 10),
|
||||
stockTarget: rd16(body, 14),
|
||||
brightness: body[16],
|
||||
gear: body[17],
|
||||
scaleQ10: rd16(body, 18),
|
||||
tickMs: rd32(body, 20),
|
||||
};
|
||||
}
|
||||
|
||||
export type AmbientLightPollingOptions = {
|
||||
/** Poll period in ms; the firmware clamps to 100..5000. Default 500. */
|
||||
intervalMs?: number;
|
||||
/** Report when the reading moved by at least this much. Default 1. */
|
||||
minDelta?: number;
|
||||
/** Also report after this many ms without one (0 = never). Default 5000. */
|
||||
heartbeatMs?: number;
|
||||
/**
|
||||
* Stop automatically when the Faceclaw framebuffer lease is released or
|
||||
* lapses (the phone went away). Default true.
|
||||
*/
|
||||
bindToLease?: boolean;
|
||||
};
|
||||
export * from "./ambient-light-types";
|
||||
|
||||
function activeCommunicator(): any {
|
||||
if (!global.isAndroid) return null;
|
||||
@@ -123,14 +52,9 @@ export function queryAmbientLight(): void {
|
||||
* on every session start.
|
||||
*/
|
||||
export function setAmbientLightPolling(enabled: boolean, options: AmbientLightPollingOptions = {}): void {
|
||||
const { intervalMs, minDelta, heartbeatMs, bindToLease } = resolvePollingOptions(options);
|
||||
try {
|
||||
activeCommunicator()?.setAmbientLightPolling(
|
||||
enabled,
|
||||
options.intervalMs ?? 500,
|
||||
options.minDelta ?? 1,
|
||||
options.heartbeatMs ?? 5000,
|
||||
options.bindToLease ?? true,
|
||||
);
|
||||
activeCommunicator()?.setAmbientLightPolling(enabled, intervalMs, minDelta, heartbeatMs, bindToLease);
|
||||
} catch (error) {
|
||||
console.warn(`setAmbientLightPolling failed: ${error}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios";
|
||||
|
||||
/**
|
||||
* Passive view of the glasses' audio traffic on iOS: the same API as
|
||||
* audio-monitor.ts over the running shared-Kotlin session. Nothing here sends
|
||||
* anything to the glasses.
|
||||
*/
|
||||
|
||||
export type AudioArm = "L" | "R" | "?";
|
||||
|
||||
/** The session listeners attach to; changes across reconnects. */
|
||||
export function audioMonitorCommunicator(): FaceclawCommunicatorBridge | null {
|
||||
return FaceclawCommunicatorBridge.getActive();
|
||||
}
|
||||
|
||||
/** True while Faceclaw itself is forwarding glasses audio to one of its apps. */
|
||||
export function isFaceclawAudioCaptureActive(): boolean {
|
||||
try {
|
||||
return Boolean(FaceclawCommunicatorBridge.getActive()?.isAudioCaptureActive());
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe `communicator` to every audio-characteristic packet and every
|
||||
* field-104 mic status record body (undecoded). Delivered on the main thread.
|
||||
* Returns an unsubscribe function.
|
||||
*/
|
||||
export function addAudioMonitorListeners(
|
||||
communicator: FaceclawCommunicatorBridge | null,
|
||||
onPacket: (data: Uint8Array, arm: AudioArm, arrivalMs: number) => void,
|
||||
onStatus: (body: Uint8Array, arm: AudioArm) => void,
|
||||
): () => void {
|
||||
if (!communicator) return () => {};
|
||||
const offPackets = communicator.addAudioMonitorListener((data, arm, arrivalMs) => onPacket(data, toArm(arm), arrivalMs));
|
||||
const offStatus = communicator.addMicStatusListener((body, arm) => onStatus(body, toArm(arm)));
|
||||
return () => {
|
||||
try {
|
||||
offPackets();
|
||||
offStatus();
|
||||
} catch {
|
||||
// The session may have been closed during a disconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function toArm(arm: string): AudioArm {
|
||||
return arm === "L" || arm === "R" ? arm : "?";
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import type { RingInput } from "../g2/ring-input";
|
||||
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
|
||||
import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat";
|
||||
import { type CompassEvent } from "./compass-types";
|
||||
import { type ImuReading } from "./imu-types";
|
||||
import { fromData, toData } from "./kotlin-data";
|
||||
import { previewPixels } from "./ios-graphics";
|
||||
import { iosBluetooth } from "./ios-bluetooth";
|
||||
@@ -14,6 +15,9 @@ declare const FaceclawKitFaceclawBleCommunicatorListener: any;
|
||||
declare const FaceclawKitIosAncsListener: any;
|
||||
declare const FaceclawKitIosAudioPacketListener: any;
|
||||
declare const FaceclawKitFaceclawCompassListener: any;
|
||||
declare const FaceclawKitFaceclawImuListener: any;
|
||||
declare const FaceclawKitIosAmbientLightListener: any;
|
||||
declare const FaceclawKitIosMicStatusListener: any;
|
||||
|
||||
|
||||
export type CommunicatorPhase =
|
||||
@@ -164,6 +168,29 @@ class CompassListener extends NSObject {
|
||||
}
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class ImuListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitFaceclawImuListener];
|
||||
handler!: (reading: ImuReading) => void;
|
||||
onImuDataXYZEventSource(x: number, y: number, z: number, eventSource: number): void {
|
||||
this.handler({ x: Number(x), y: Number(y), z: Number(z), source: Number(eventSource) });
|
||||
}
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class AmbientLightListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitIosAmbientLightListener];
|
||||
handler!: (body: Uint8Array) => void;
|
||||
onNsDataAmbientLightBody(body: NSData): void { this.handler(fromData(body)); }
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class MicStatusListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitIosMicStatusListener];
|
||||
handler!: (body: Uint8Array, arm: string) => void;
|
||||
onNsDataMicStatusBodyArm(body: NSData, arm: string): void { this.handler(fromData(body), String(arm ?? "")); }
|
||||
}
|
||||
|
||||
/**
|
||||
* The iOS twin of the Android communicator bridge: the same public API over
|
||||
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
|
||||
@@ -173,6 +200,8 @@ class CompassListener extends NSObject {
|
||||
* Android so callers never re-enter the bridge from a Kotlin frame.
|
||||
*/
|
||||
export class FaceclawCommunicatorBridge {
|
||||
// The started, not yet closed bridge (see getActive).
|
||||
private static active: FaceclawCommunicatorBridge | null = null;
|
||||
private readonly communicator: any;
|
||||
private readonly listenerProxy: SessionListener;
|
||||
private ancsProxy: AncsListener | null = null;
|
||||
@@ -200,6 +229,17 @@ export class FaceclawCommunicatorBridge {
|
||||
private audioListener: AudioPacketListener | null = null;
|
||||
private microphoneWork: Promise<void> = Promise.resolve();
|
||||
private microphoneToken = 0;
|
||||
private closed = false;
|
||||
|
||||
/**
|
||||
* The bridge whose session is running, for modules that reach the glasses
|
||||
* without the bridge threaded through (imu, ambient-light, audio-monitor):
|
||||
* the iOS stand-in for Android's FaceclawBleCommunicator.getActive().
|
||||
* Set by start(), cleared by close().
|
||||
*/
|
||||
static getActive(): FaceclawCommunicatorBridge | null {
|
||||
return FaceclawCommunicatorBridge.active;
|
||||
}
|
||||
|
||||
constructor(addresses: { right: string; left: string; ring?: string }) {
|
||||
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
|
||||
@@ -450,7 +490,9 @@ export class FaceclawCommunicatorBridge {
|
||||
// ----- session ---------------------------------------------------------------------
|
||||
|
||||
async start(): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.start(); });
|
||||
await this.enqueueNativeCall(() => {
|
||||
if (this.communicator.start() && !this.closed) FaceclawCommunicatorBridge.active = this;
|
||||
});
|
||||
}
|
||||
|
||||
async setG2ScreenOn(screenOn: boolean): Promise<void> {
|
||||
@@ -640,6 +682,8 @@ export class FaceclawCommunicatorBridge {
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
this.closed = true;
|
||||
if (FaceclawCommunicatorBridge.active === this) FaceclawCommunicatorBridge.active = null;
|
||||
this.setAncsListeners(false);
|
||||
this.previewAnimationListeners.clear();
|
||||
await this.enqueueNativeCall(() => {
|
||||
@@ -678,6 +722,70 @@ export class FaceclawCommunicatorBridge {
|
||||
return String(this.communicator.getBandwidthBenchmarkStatus());
|
||||
}
|
||||
|
||||
// ----- sensors and passive audio, for the developer demos -----------------------------
|
||||
// Fire-and-forget, so they skip the native-call queue like the compass controls do.
|
||||
|
||||
/** Enable/disable the accelerometer report stream (an ImuReportPace code, 100..1000). */
|
||||
setImuReportEnabled(enable: boolean, reportFrq: number): void {
|
||||
this.communicator.setImuReportEnabledEnableReportFrq(Boolean(enable), Math.max(0, Math.round(reportFrq)));
|
||||
}
|
||||
|
||||
/** Accelerometer samples, delivered on the main queue. */
|
||||
addImuListener(listener: (reading: ImuReading) => void): () => void {
|
||||
const proxy = ImuListener.new() as ImuListener;
|
||||
proxy.handler = listener;
|
||||
this.communicator.addImuListenerListener(proxy);
|
||||
return () => { this.communicator.removeImuListenerListener(proxy); };
|
||||
}
|
||||
|
||||
/** Ask the CFW for one ambient-light report (delivered to the ambient-light listeners). */
|
||||
queryAmbientLight(): void {
|
||||
this.communicator.queryAmbientLight();
|
||||
}
|
||||
|
||||
setAmbientLightPolling(enable: boolean, intervalMs: number, minDelta: number, heartbeatMs: number, bindToLease: boolean): void {
|
||||
this.communicator.setAmbientLightPollingEnableIntervalMsMinDeltaHeartbeatMsBindToLease(Boolean(enable),
|
||||
Math.round(intervalMs), Math.round(minDelta), Math.round(heartbeatMs), Boolean(bindToLease));
|
||||
}
|
||||
|
||||
/** Raw field-105 ambient-light records, delivered on the main queue. */
|
||||
addAmbientLightListener(listener: (body: Uint8Array) => void): () => void {
|
||||
const proxy = AmbientLightListener.new() as AmbientLightListener;
|
||||
proxy.handler = listener;
|
||||
const handle = this.communicator.addAmbientLightListenerNsDataListener(proxy);
|
||||
return () => { this.communicator.removeAmbientLightListenerListener(handle); };
|
||||
}
|
||||
|
||||
/** Raw field-104 mic_control status records and their temple ("L", "R" or "?"), on the main queue. */
|
||||
addMicStatusListener(listener: (body: Uint8Array, arm: string) => void): () => void {
|
||||
const proxy = MicStatusListener.new() as MicStatusListener;
|
||||
proxy.handler = listener;
|
||||
const handle = this.communicator.addMicStatusListenerNsDataListener(proxy);
|
||||
return () => { this.communicator.removeMicStatusListenerListener(handle); };
|
||||
}
|
||||
|
||||
/** Queue a CFW mic_control record for the chosen temples; false when nothing was queued. */
|
||||
sendFaceclawMicControl(record: Uint8Array, label: string, rightTemple: boolean, leftTemple: boolean): boolean {
|
||||
return Boolean(this.communicator.sendFaceclawMicControlRecordLabelRightTempleLeftTemple(
|
||||
toData(new Uint8Array(record)), label, Boolean(rightTemple), Boolean(leftTemple)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every audio-characteristic packet, whatever enabled the mics, on the
|
||||
* main queue. Passive: sends nothing and doesn't affect capture.
|
||||
*/
|
||||
addAudioMonitorListener(listener: (packet: Uint8Array, arm: string, arrivalMs: number) => void): () => void {
|
||||
const proxy = AudioPacketListener.new() as AudioPacketListener;
|
||||
proxy.handler = listener;
|
||||
const handle = this.communicator.addAudioMonitorListenerNsDataListener(proxy);
|
||||
return () => { this.communicator.removeAudioMonitorListenerListener(handle); };
|
||||
}
|
||||
|
||||
/** True while Faceclaw itself is forwarding glasses audio (speech capture or an app's mic). */
|
||||
isAudioCaptureActive(): boolean {
|
||||
return Boolean(this.communicator.isAudioCaptureActive());
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialized glasses microphone enable/disable (the voice bridge's
|
||||
* IosMicrophoneSession contract). A release while an enable awaits its ACK
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/** Platform-neutral IMU types, shared by imu.ts and imu.ios.ts. */
|
||||
|
||||
export type ImuReading = { x: number; y: number; z: number; source: number };
|
||||
|
||||
/** Map an IMU sample's EventSource to a short label. */
|
||||
export function imuSourceLabel(source: number): string {
|
||||
switch (source) {
|
||||
case 1:
|
||||
return "R arm";
|
||||
case 2:
|
||||
return "ring";
|
||||
case 3:
|
||||
return "L arm";
|
||||
default:
|
||||
return "device";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios";
|
||||
import { type ImuReading } from "./imu-types";
|
||||
|
||||
export * from "./imu-types";
|
||||
|
||||
/**
|
||||
* IMU (accelerometer) access on iOS: the same API as imu.ts over the running
|
||||
* shared-Kotlin session. As there, the stream is experimental, the calls are
|
||||
* no-ops while no session is running, and a listener stays with the session
|
||||
* it subscribed to.
|
||||
*/
|
||||
|
||||
/** Enable/disable the accelerometer report stream. No-op if not connected. */
|
||||
export function setImuReportEnabled(enable: boolean, reportFreq: number): void {
|
||||
try {
|
||||
FaceclawCommunicatorBridge.getActive()?.setImuReportEnabled(enable, reportFreq);
|
||||
} catch (error) {
|
||||
console.warn(`setImuReportEnabled failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Subscribe to IMU readings; returns an unsubscribe function. */
|
||||
export function addImuListener(listener: (reading: ImuReading) => void): () => void {
|
||||
const active = FaceclawCommunicatorBridge.getActive();
|
||||
if (!active) return () => {};
|
||||
const unsubscribe = active.addImuListener(listener);
|
||||
return () => {
|
||||
try {
|
||||
unsubscribe();
|
||||
} catch {
|
||||
// The session may be gone after a disconnect; nothing to clean up.
|
||||
}
|
||||
};
|
||||
}
|
||||
+3
-15
@@ -12,7 +12,9 @@ declare const com: any;
|
||||
* static, so it works from any isolate without threading the bridge through.
|
||||
*/
|
||||
|
||||
export type ImuReading = { x: number; y: number; z: number; source: number };
|
||||
import { type ImuReading } from "./imu-types";
|
||||
|
||||
export * from "./imu-types";
|
||||
|
||||
function activeCommunicator(): any {
|
||||
if (!global.isAndroid) return null;
|
||||
@@ -52,17 +54,3 @@ export function addImuListener(listener: (reading: ImuReading) => void): () => v
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Map an IMU sample's EventSource to a short label. */
|
||||
export function imuSourceLabel(source: number): string {
|
||||
switch (source) {
|
||||
case 1:
|
||||
return "R arm";
|
||||
case 2:
|
||||
return "ring";
|
||||
case 3:
|
||||
return "L arm";
|
||||
default:
|
||||
return "device";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios";
|
||||
|
||||
/** The iOS twin of mic-control-link.ts, over the running shared-Kotlin session. */
|
||||
|
||||
export function micControlSession(): unknown {
|
||||
return FaceclawCommunicatorBridge.getActive();
|
||||
}
|
||||
|
||||
/** Queue a record for the chosen temples; false when the session wasn't ready and nothing was queued. */
|
||||
export function sendMicControlRecord(
|
||||
session: unknown,
|
||||
record: Uint8Array,
|
||||
label: string,
|
||||
rightTemple: boolean,
|
||||
leftTemple: boolean,
|
||||
): boolean {
|
||||
return (session as FaceclawCommunicatorBridge).sendFaceclawMicControl(record, label, rightTemple, leftTemple);
|
||||
}
|
||||
|
||||
/** Status record bodies (undecoded) and their temple: "L", "R" or "?". Returns an unsubscribe function. */
|
||||
export function addMicStatusListener(session: unknown, listener: (body: Uint8Array, arm: string) => void): () => void {
|
||||
const unsubscribe = (session as FaceclawCommunicatorBridge).addMicStatusListener(listener);
|
||||
return () => {
|
||||
try {
|
||||
unsubscribe();
|
||||
} catch {
|
||||
// The session may have been closed during a reconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { toJavaBytes } from "./cloud-stt";
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* Platform plumbing for the CFW mic_control channel (apps/microphones/mic-control.ts):
|
||||
* the running session to send records on and to hear field-104 status replies
|
||||
* from. The session is opaque and is a different object after a reconnect.
|
||||
*/
|
||||
|
||||
export function micControlSession(): unknown {
|
||||
if (!global.isAndroid) return null;
|
||||
try {
|
||||
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Queue a record for the chosen temples; false when the session wasn't ready and nothing was queued. */
|
||||
export function sendMicControlRecord(
|
||||
session: unknown,
|
||||
record: Uint8Array,
|
||||
label: string,
|
||||
rightTemple: boolean,
|
||||
leftTemple: boolean,
|
||||
): boolean {
|
||||
return (session as any).sendFaceclawMicControl(toJavaBytes(record), label, rightTemple, leftTemple) !== false;
|
||||
}
|
||||
|
||||
/** Status record bodies (undecoded) and their temple: "L", "R" or "?". Returns an unsubscribe function. */
|
||||
export function addMicStatusListener(session: unknown, listener: (body: Uint8Array, arm: string) => void): () => void {
|
||||
const communicator = session as any;
|
||||
const proxy = new com.faceclaw.app.FaceclawMicStatusListener({
|
||||
onMicStatus: (body: any, arm: string) => listener(toUint8Array(body), String(arm)),
|
||||
});
|
||||
communicator.addMicStatusListener(proxy);
|
||||
return () => {
|
||||
try {
|
||||
communicator.removeMicStatusListener(proxy);
|
||||
} catch {
|
||||
// The old communicator instance may be gone after a reconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user