forked from jimrandomh/faceclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimu.ts
More file actions
68 lines (62 loc) · 2.11 KB
/
Copy pathimu.ts
File metadata and controls
68 lines (62 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
declare const com: any;
/**
* IMU (accelerometer) access. The G2 gates its accelerometer stream behind an
* ImuCtrl control message; once enabled, the firmware reports x/y/z samples as
* sys-events, which the Java communicator decodes and forwards here.
*
* Note: this stream has historically been unreliable — the firmware may not
* populate IMU data at all. Treat it as experimental.
*
* Talks to the active communicator via the FaceclawBleCommunicator.getActive()
* static, so it works from any isolate without threading the bridge through.
*/
export type ImuReading = { x: number; y: number; z: number; source: number };
function activeCommunicator(): any {
if (!global.isAndroid) return null;
try {
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
} catch {
return null;
}
}
/** Enable/disable the accelerometer report stream. No-op if not connected. */
export function setImuReportEnabled(enable: boolean, reportFreq: number): void {
const active = activeCommunicator();
if (!active) return;
try {
active.setImuReportEnabled(enable, Math.max(0, Math.round(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 = activeCommunicator();
if (!active) return () => {};
const proxy = new com.faceclaw.app.FaceclawImuListener({
onImuData: (x: number, y: number, z: number, source: number) => {
listener({ x: Number(x), y: Number(y), z: Number(z), source: Number(source) });
},
});
active.addImuListener(proxy);
return () => {
try {
activeCommunicator()?.removeImuListener(proxy);
} catch {
// The instance may be gone after a disconnect; nothing to clean up.
}
};
}
/** 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";
}
}