brilliantsole 0.0.27 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/assets/3d/anchor.glb +0 -0
  2. package/assets/3d/coin.glb +0 -0
  3. package/assets/3d/glasses.glb +0 -0
  4. package/assets/audio/bounceMedium.wav +0 -0
  5. package/assets/audio/bounceStrong.wav +0 -0
  6. package/assets/audio/bounceWeak.wav +0 -0
  7. package/assets/audio/coin.wav +0 -0
  8. package/assets/audio/getUp.wav +0 -0
  9. package/assets/audio/grab.wav +0 -0
  10. package/assets/audio/kick.wav +0 -0
  11. package/assets/audio/platterFadeIn old.wav +0 -0
  12. package/assets/audio/platterFadeIn.wav +0 -0
  13. package/assets/audio/platterFadeOut.wav +0 -0
  14. package/assets/audio/punch.wav +0 -0
  15. package/assets/audio/punchSqueak.wav +0 -0
  16. package/assets/audio/purr.wav +0 -0
  17. package/assets/audio/purrFadeOut.wav +0 -0
  18. package/assets/audio/release.wav +0 -0
  19. package/assets/audio/splat.wav +0 -0
  20. package/assets/audio/stomp.wav +0 -0
  21. package/build/brilliantsole.cjs +2627 -775
  22. package/build/brilliantsole.cjs.map +1 -1
  23. package/build/brilliantsole.js +2273 -721
  24. package/build/brilliantsole.js.map +1 -1
  25. package/build/brilliantsole.ls.js +2096 -535
  26. package/build/brilliantsole.ls.js.map +1 -1
  27. package/build/brilliantsole.min.js +1 -1
  28. package/build/brilliantsole.min.js.map +1 -1
  29. package/build/brilliantsole.module.d.ts +235 -65
  30. package/build/brilliantsole.module.js +2266 -722
  31. package/build/brilliantsole.module.js.map +1 -1
  32. package/build/brilliantsole.module.min.d.ts +235 -65
  33. package/build/brilliantsole.module.min.js +1 -1
  34. package/build/brilliantsole.module.min.js.map +1 -1
  35. package/build/brilliantsole.node.module.d.ts +229 -62
  36. package/build/brilliantsole.node.module.js +2619 -776
  37. package/build/brilliantsole.node.module.js.map +1 -1
  38. package/build/dts/BS-output.d.ts +10 -0
  39. package/build/dts/BS.d.ts +20 -8
  40. package/build/dts/CameraManager.d.ts +72 -0
  41. package/build/dts/Device.d.ts +46 -13
  42. package/build/dts/DeviceInformationManager.d.ts +4 -4
  43. package/build/dts/DeviceManager.d.ts +2 -0
  44. package/build/dts/FileTransferManager.d.ts +18 -8
  45. package/build/dts/InformationManager.d.ts +2 -0
  46. package/build/dts/TfliteManager.d.ts +22 -2
  47. package/build/dts/WifiManager.d.ts +61 -0
  48. package/build/dts/connection/BaseConnectionManager.d.ts +35 -3
  49. package/build/dts/connection/ClientConnectionManager.d.ts +7 -2
  50. package/build/dts/connection/bluetooth/NobleConnectionManager.d.ts +2 -1
  51. package/build/dts/connection/bluetooth/WebBluetoothConnectionManager.d.ts +1 -0
  52. package/build/dts/connection/bluetooth/bluetoothUUIDs.d.ts +2 -2
  53. package/build/dts/connection/udp/UDPConnectionManager.d.ts +28 -0
  54. package/build/dts/connection/webSocket/WebSocketConnectionManager.d.ts +25 -0
  55. package/build/dts/devicePair/DevicePair.d.ts +5 -5
  56. package/build/dts/scanner/BaseScanner.d.ts +4 -1
  57. package/build/dts/scanner/NobleScanner.d.ts +2 -1
  58. package/build/dts/sensor/MotionSensorDataManager.d.ts +5 -2
  59. package/build/dts/sensor/SensorDataManager.d.ts +5 -4
  60. package/build/dts/server/BaseClient.d.ts +5 -3
  61. package/build/dts/server/ServerUtils.d.ts +1 -1
  62. package/build/dts/server/websocket/WebSocketUtils.d.ts +1 -1
  63. package/build/dts/utils/Console.d.ts +2 -0
  64. package/build/dts/utils/ThrottleUtils.d.ts +2 -0
  65. package/build/dts/vibration/VibrationManager.d.ts +19 -2
  66. package/build/index.d.ts +232 -62
  67. package/build/index.node.d.ts +226 -59
  68. package/examples/3d/scene.html +19 -5
  69. package/examples/3d-generic/index.html +144 -0
  70. package/examples/3d-generic/script.js +266 -0
  71. package/examples/basic/index.html +211 -16
  72. package/examples/basic/script.js +731 -105
  73. package/examples/edge-impulse/script.js +135 -44
  74. package/examples/edge-impulse-test/README.md +11 -0
  75. package/examples/edge-impulse-test/edge-impulse-standalone.js +7228 -0
  76. package/examples/edge-impulse-test/edge-impulse-standalone.wasm +0 -0
  77. package/examples/edge-impulse-test/index.html +75 -0
  78. package/examples/edge-impulse-test/run-impulse.js +135 -0
  79. package/examples/edge-impulse-test/script.js +200 -0
  80. package/examples/gloves/edge-impulse-standalone.js +7228 -0
  81. package/examples/gloves/edge-impulse-standalone.wasm +0 -0
  82. package/examples/gloves/index.html +4 -1
  83. package/examples/gloves/run-impulse.js +135 -0
  84. package/examples/gloves/script.js +367 -51
  85. package/examples/graph/script.js +94 -37
  86. package/examples/punch/index.html +135 -0
  87. package/examples/punch/punch.tflite +0 -0
  88. package/examples/punch/script.js +169 -0
  89. package/examples/server/index.html +98 -22
  90. package/examples/server/script.js +317 -109
  91. package/examples/ukaton-firmware-update/merged-firmware.bin +0 -0
  92. package/examples/utils/aframe/aframe-master.min.js +2 -0
  93. package/examples/utils/aframe/bs-vibration.js +150 -0
  94. package/examples/utils/aframe/force-pushable.js +80 -0
  95. package/examples/utils/aframe/grabbable-anchor.js +46 -0
  96. package/examples/utils/aframe/grabbable-listener.js +31 -0
  97. package/examples/utils/aframe/grabbable-physics-body.js +190 -0
  98. package/examples/utils/aframe/grow-shrink.js +25 -0
  99. package/examples/utils/aframe/hand-punch.js +119 -0
  100. package/examples/utils/aframe/my-obb-collider.js +293 -0
  101. package/examples/utils/aframe/occlude-hand-tracking-controls.js +47 -0
  102. package/examples/utils/aframe/occlude-mesh.js +42 -0
  103. package/examples/utils/aframe/palm-up-detector.js +47 -0
  104. package/examples/utils/aframe/shadow-material.js +20 -0
  105. package/examples/utils/aframe/soft-shadow-light.js +9 -0
  106. package/examples/webxr-2/assets/3d/soccerBall.glb +0 -0
  107. package/examples/webxr-2/assets/audio/shellBounce.wav +0 -0
  108. package/examples/webxr-2/assets/audio/shellHit.wav +0 -0
  109. package/examples/webxr-2/assets/audio/shellKick.wav +0 -0
  110. package/examples/webxr-2/assets/audio/soccerBounce.wav +0 -0
  111. package/examples/webxr-2/assets/audio/soccerKick.mp3 +0 -0
  112. package/examples/webxr-2/assets/images/shellTexture.png +0 -0
  113. package/examples/webxr-2/components/bs-ankle.js +337 -0
  114. package/examples/webxr-2/components/coin.js +84 -0
  115. package/examples/webxr-2/components/custom-wrap.js +17 -0
  116. package/examples/webxr-2/components/goomba.js +3250 -0
  117. package/examples/webxr-2/components/init-shell-material.js +215 -0
  118. package/examples/webxr-2/components/platter.js +172 -0
  119. package/examples/webxr-2/components/shell.js +374 -0
  120. package/examples/webxr-2/components/soccer-ball.js +250 -0
  121. package/examples/webxr-2/components/squashed-goomba.js +249 -0
  122. package/examples/webxr-2/edge-impulse-standalone.js +7228 -0
  123. package/examples/webxr-2/edge-impulse-standalone.wasm +0 -0
  124. package/examples/webxr-2/index.html +996 -0
  125. package/examples/webxr-2/kick.tflite +0 -0
  126. package/examples/webxr-2/kick2.tflite +0 -0
  127. package/examples/webxr-2/run-impulse.js +135 -0
  128. package/examples/webxr-2/script.js +384 -0
  129. package/package.json +2 -1
  130. package/src/.prettierrc +4 -0
  131. package/src/BS.ts +70 -8
  132. package/src/CameraManager.ts +499 -0
  133. package/src/Device.ts +581 -86
  134. package/src/DeviceInformationManager.ts +19 -10
  135. package/src/DeviceManager.ts +85 -25
  136. package/src/FileTransferManager.ts +145 -20
  137. package/src/InformationManager.ts +40 -15
  138. package/src/TfliteManager.ts +171 -25
  139. package/src/WifiManager.ts +323 -0
  140. package/src/connection/BaseConnectionManager.ts +128 -30
  141. package/src/connection/ClientConnectionManager.ts +34 -10
  142. package/src/connection/bluetooth/BluetoothConnectionManager.ts +8 -2
  143. package/src/connection/bluetooth/NobleConnectionManager.ts +147 -41
  144. package/src/connection/bluetooth/WebBluetoothConnectionManager.ts +99 -34
  145. package/src/connection/bluetooth/bluetoothUUIDs.ts +40 -13
  146. package/src/connection/udp/UDPConnectionManager.ts +356 -0
  147. package/src/connection/websocket/WebSocketConnectionManager.ts +282 -0
  148. package/src/devicePair/DevicePair.ts +95 -25
  149. package/src/devicePair/DevicePairPressureSensorDataManager.ts +27 -7
  150. package/src/scanner/BaseScanner.ts +49 -11
  151. package/src/scanner/NobleScanner.ts +76 -14
  152. package/src/sensor/MotionSensorDataManager.ts +21 -6
  153. package/src/sensor/PressureSensorDataManager.ts +37 -8
  154. package/src/sensor/SensorConfigurationManager.ts +73 -22
  155. package/src/sensor/SensorDataManager.ts +104 -23
  156. package/src/server/BaseClient.ts +150 -36
  157. package/src/server/BaseServer.ts +50 -2
  158. package/src/server/ServerUtils.ts +39 -9
  159. package/src/server/udp/UDPServer.ts +73 -22
  160. package/src/server/udp/UDPUtils.ts +9 -2
  161. package/src/server/websocket/WebSocketClient.ts +27 -7
  162. package/src/server/websocket/WebSocketUtils.ts +4 -2
  163. package/src/utils/Console.ts +62 -9
  164. package/src/utils/ParseUtils.ts +24 -5
  165. package/src/utils/ThrottleUtils.ts +62 -0
  166. package/src/utils/Timer.ts +1 -1
  167. package/src/vibration/VibrationManager.ts +166 -40
@@ -1,3 +1,4 @@
1
+ import { ConnectionType } from "./connection/BaseConnectionManager.ts";
1
2
  import Device, { SendMessageCallback } from "./Device.ts";
2
3
  import { createConsole } from "./utils/Console.ts";
3
4
  import { isInBrowser } from "./utils/environment.ts";
@@ -8,7 +9,14 @@ import autoBind from "auto-bind";
8
9
 
9
10
  const _console = createConsole("InformationManager", { log: false });
10
11
 
11
- export const DeviceTypes = ["leftInsole", "rightInsole", "leftGlove", "rightGlove", "glasses", "generic"] as const;
12
+ export const DeviceTypes = [
13
+ "leftInsole",
14
+ "rightInsole",
15
+ "leftGlove",
16
+ "rightGlove",
17
+ "glasses",
18
+ "generic",
19
+ ] as const;
12
20
  export type DeviceType = (typeof DeviceTypes)[number];
13
21
 
14
22
  export const Sides = ["left", "right"] as const;
@@ -44,8 +52,13 @@ export interface InformationEventMessages {
44
52
  getCurrentTime: { currentTime: number };
45
53
  }
46
54
 
47
- export type InformationEventDispatcher = EventDispatcher<Device, InformationEventType, InformationEventMessages>;
48
- export type SendInformationMessageCallback = SendMessageCallback<InformationMessageType>;
55
+ export type InformationEventDispatcher = EventDispatcher<
56
+ Device,
57
+ InformationEventType,
58
+ InformationEventMessages
59
+ >;
60
+ export type SendInformationMessageCallback =
61
+ SendMessageCallback<InformationMessageType>;
49
62
 
50
63
  class InformationManager {
51
64
  constructor() {
@@ -89,7 +102,9 @@ class InformationManager {
89
102
  _console.assertTypeWithError(updatedBatteryCurrent, "number");
90
103
  this.#batteryCurrent = updatedBatteryCurrent;
91
104
  _console.log({ batteryCurrent: this.#batteryCurrent });
92
- this.#dispatchEvent("getBatteryCurrent", { batteryCurrent: this.#batteryCurrent });
105
+ this.#dispatchEvent("getBatteryCurrent", {
106
+ batteryCurrent: this.#batteryCurrent,
107
+ });
93
108
  }
94
109
 
95
110
  #id!: string;
@@ -116,13 +131,11 @@ class InformationManager {
116
131
  }
117
132
  async setName(newName: string) {
118
133
  _console.assertTypeWithError(newName, "string");
119
- _console.assertWithError(
120
- newName.length >= MinNameLength,
121
- `name must be greater than ${MinNameLength} characters long ("${newName}" is ${newName.length} characters long)`
122
- );
123
- _console.assertWithError(
124
- newName.length < MaxNameLength,
125
- `name must be less than ${MaxNameLength} characters long ("${newName}" is ${newName.length} characters long)`
134
+ _console.assertRangeWithError(
135
+ "newName",
136
+ newName.length,
137
+ MinNameLength,
138
+ MaxNameLength
126
139
  );
127
140
  const setNameData = textEncoder.encode(newName);
128
141
  _console.log({ setNameData });
@@ -145,7 +158,10 @@ class InformationManager {
145
158
  }
146
159
  #assertValidDeviceTypeEnum(typeEnum: number) {
147
160
  _console.assertTypeWithError(typeEnum, "number");
148
- _console.assertWithError(typeEnum in DeviceTypes, `invalid typeEnum ${typeEnum}`);
161
+ _console.assertWithError(
162
+ typeEnum in DeviceTypes,
163
+ `invalid typeEnum ${typeEnum}`
164
+ );
149
165
  }
150
166
  updateType(updatedType: DeviceType) {
151
167
  this.#assertValidDeviceType(updatedType);
@@ -227,7 +243,8 @@ class InformationManager {
227
243
 
228
244
  #onCurrentTime(currentTime: number) {
229
245
  _console.log({ currentTime });
230
- this.#isCurrentTimeSet = currentTime != 0 || Math.abs(Date.now() - currentTime) < Uint16Max;
246
+ this.#isCurrentTimeSet =
247
+ currentTime != 0 || Math.abs(Date.now() - currentTime) < Uint16Max;
231
248
  if (!this.#isCurrentTimeSet) {
232
249
  this.#setCurrentTime(false);
233
250
  }
@@ -237,7 +254,10 @@ class InformationManager {
237
254
  const dataView = new DataView(new ArrayBuffer(8));
238
255
  dataView.setBigUint64(0, BigInt(Date.now()), true);
239
256
  const promise = this.waitForEvent("getCurrentTime");
240
- this.sendMessage([{ type: "setCurrentTime", data: dataView.buffer }], sendImmediately);
257
+ this.sendMessage(
258
+ [{ type: "setCurrentTime", data: dataView.buffer }],
259
+ sendImmediately
260
+ );
241
261
  await promise;
242
262
  }
243
263
 
@@ -276,7 +296,10 @@ class InformationManager {
276
296
  break;
277
297
  case "getMtu":
278
298
  let mtu = dataView.getUint16(0, true);
279
- if (isInBrowser) {
299
+ if (
300
+ this.connectionType != "webSocket" &&
301
+ this.connectionType != "udp"
302
+ ) {
280
303
  mtu = Math.min(mtu, 512);
281
304
  }
282
305
  _console.log({ mtu });
@@ -295,6 +318,8 @@ class InformationManager {
295
318
  clear() {
296
319
  this.#isCurrentTimeSet = false;
297
320
  }
321
+
322
+ connectionType?: ConnectionType;
298
323
  }
299
324
 
300
325
  export default InformationManager;
@@ -8,6 +8,7 @@ import { parseTimestamp } from "./utils/MathUtils.ts";
8
8
  import { SensorType } from "./sensor/SensorDataManager.ts";
9
9
  import Device, { SendMessageCallback } from "./Device.ts";
10
10
  import autoBind from "auto-bind";
11
+ import { FileConfiguration as BaseFileConfiguration } from "./FileTransferManager.ts";
11
12
 
12
13
  const _console = createConsole("TfliteManager", { log: false });
13
14
 
@@ -34,6 +35,17 @@ export type TfliteMessageType = (typeof TfliteMessageTypes)[number];
34
35
  export const TfliteEventTypes = TfliteMessageTypes;
35
36
  export type TfliteEventType = (typeof TfliteEventTypes)[number];
36
37
 
38
+ export const RequiredTfliteMessageTypes: TfliteMessageType[] = [
39
+ "getTfliteName",
40
+ "getTfliteTask",
41
+ "getTfliteSampleRate",
42
+ "getTfliteSensorTypes",
43
+ "tfliteIsReady",
44
+ "getTfliteCaptureDelay",
45
+ "getTfliteThreshold",
46
+ "getTfliteInferencingEnabled",
47
+ ];
48
+
37
49
  export const TfliteTasks = ["classification", "regression"] as const;
38
50
  export type TfliteTask = (typeof TfliteTasks)[number];
39
51
 
@@ -54,14 +66,36 @@ export interface TfliteInference {
54
66
  values: number[];
55
67
  maxValue?: number;
56
68
  maxIndex?: number;
69
+ maxClass?: string;
70
+ classValues?: { [key: string]: number };
57
71
  }
58
72
 
59
- export type TfliteEventDispatcher = EventDispatcher<Device, TfliteEventType, TfliteEventMessages>;
73
+ export type TfliteEventDispatcher = EventDispatcher<
74
+ Device,
75
+ TfliteEventType,
76
+ TfliteEventMessages
77
+ >;
60
78
  export type SendTfliteMessageCallback = SendMessageCallback<TfliteMessageType>;
61
79
 
62
- export const TfliteSensorTypes: SensorType[] = ["pressure", "linearAcceleration", "gyroscope", "magnetometer"] as const;
80
+ export const TfliteSensorTypes = [
81
+ "pressure",
82
+ "linearAcceleration",
83
+ "gyroscope",
84
+ "magnetometer",
85
+ ] as const satisfies readonly SensorType[];
63
86
  export type TfliteSensorType = (typeof TfliteSensorTypes)[number];
64
87
 
88
+ export interface TfliteFileConfiguration extends BaseFileConfiguration {
89
+ type: "tflite";
90
+ name: string;
91
+ sensorTypes: TfliteSensorType[];
92
+ task: TfliteTask;
93
+ sampleRate: number;
94
+ captureDelay?: number;
95
+ threshold?: number;
96
+ classes?: string[];
97
+ }
98
+
65
99
  class TfliteManager {
66
100
  constructor() {
67
101
  autoBind(this);
@@ -73,7 +107,10 @@ class TfliteManager {
73
107
  _console.assertEnumWithError(task, TfliteTasks);
74
108
  }
75
109
  #assertValidTaskEnum(taskEnum: number) {
76
- _console.assertWithError(taskEnum in TfliteTasks, `invalid taskEnum ${taskEnum}`);
110
+ _console.assertWithError(
111
+ taskEnum in TfliteTasks,
112
+ `invalid taskEnum ${taskEnum}`
113
+ );
77
114
  }
78
115
 
79
116
  eventDispatcher!: TfliteEventDispatcher;
@@ -116,7 +153,10 @@ class TfliteManager {
116
153
  const promise = this.waitForEvent("getTfliteName");
117
154
 
118
155
  const setNameData = textEncoder.encode(newName);
119
- this.sendMessage([{ type: "setTfliteName", data: setNameData.buffer }], sendImmediately);
156
+ this.sendMessage(
157
+ [{ type: "setTfliteName", data: setNameData.buffer }],
158
+ sendImmediately
159
+ );
120
160
 
121
161
  await promise;
122
162
  }
@@ -147,7 +187,10 @@ class TfliteManager {
147
187
  const promise = this.waitForEvent("getTfliteTask");
148
188
 
149
189
  const taskEnum = TfliteTasks.indexOf(newTask);
150
- this.sendMessage([{ type: "setTfliteTask", data: Uint8Array.from([taskEnum]).buffer }], sendImmediately);
190
+ this.sendMessage(
191
+ [{ type: "setTfliteTask", data: Uint8Array.from([taskEnum]).buffer }],
192
+ sendImmediately
193
+ );
151
194
 
152
195
  await promise;
153
196
  }
@@ -164,7 +207,9 @@ class TfliteManager {
164
207
  #updateSampleRate(sampleRate: number) {
165
208
  _console.log({ sampleRate });
166
209
  this.#sampleRate = sampleRate;
167
- this.#dispatchEvent("getTfliteSampleRate", { tfliteSampleRate: sampleRate });
210
+ this.#dispatchEvent("getTfliteSampleRate", {
211
+ tfliteSampleRate: sampleRate,
212
+ });
168
213
  }
169
214
  async setSampleRate(newSampleRate: number, sendImmediately?: boolean) {
170
215
  _console.assertTypeWithError(newSampleRate, "number");
@@ -182,40 +227,56 @@ class TfliteManager {
182
227
 
183
228
  const dataView = new DataView(new ArrayBuffer(2));
184
229
  dataView.setUint16(0, newSampleRate, true);
185
- this.sendMessage([{ type: "setTfliteSampleRate", data: dataView.buffer }], sendImmediately);
230
+ this.sendMessage(
231
+ [{ type: "setTfliteSampleRate", data: dataView.buffer }],
232
+ sendImmediately
233
+ );
186
234
 
187
235
  await promise;
188
236
  }
189
237
 
190
238
  static AssertValidSensorType(sensorType: SensorType) {
191
239
  SensorDataManager.AssertValidSensorType(sensorType);
192
- _console.assertWithError(TfliteSensorTypes.includes(sensorType), `invalid tflite sensorType "${sensorType}"`);
240
+ const tfliteSensorType = sensorType as TfliteSensorType;
241
+ _console.assertWithError(
242
+ TfliteSensorTypes.includes(tfliteSensorType),
243
+ `invalid tflite sensorType "${sensorType}"`
244
+ );
193
245
  }
194
246
 
195
- #sensorTypes: SensorType[] = [];
247
+ #sensorTypes: TfliteSensorType[] = [];
196
248
  get sensorTypes() {
197
249
  return this.#sensorTypes.slice();
198
250
  }
199
251
  #parseSensorTypes(dataView: DataView) {
200
252
  _console.log("parseSensorTypes", dataView);
201
- const sensorTypes: SensorType[] = [];
253
+ const sensorTypes: TfliteSensorType[] = [];
202
254
  for (let index = 0; index < dataView.byteLength; index++) {
203
255
  const sensorTypeEnum = dataView.getUint8(index);
204
- const sensorType = SensorTypes[sensorTypeEnum];
256
+ const sensorType = SensorTypes[sensorTypeEnum] as TfliteSensorType;
205
257
  if (sensorType) {
206
- sensorTypes.push(sensorType);
258
+ if (TfliteSensorTypes.includes(sensorType)) {
259
+ sensorTypes.push(sensorType);
260
+ } else {
261
+ _console.error(`invalid tfliteSensorType ${sensorType}`);
262
+ }
207
263
  } else {
208
264
  _console.error(`invalid sensorTypeEnum ${sensorTypeEnum}`);
209
265
  }
210
266
  }
211
267
  this.#updateSensorTypes(sensorTypes);
212
268
  }
213
- #updateSensorTypes(sensorTypes: SensorType[]) {
269
+ #updateSensorTypes(sensorTypes: TfliteSensorType[]) {
214
270
  _console.log({ sensorTypes });
215
271
  this.#sensorTypes = sensorTypes;
216
- this.#dispatchEvent("getTfliteSensorTypes", { tfliteSensorTypes: sensorTypes });
272
+ this.#dispatchEvent("getTfliteSensorTypes", {
273
+ tfliteSensorTypes: sensorTypes,
274
+ });
217
275
  }
218
- async setSensorTypes(newSensorTypes: SensorType[], sendImmediately?: boolean) {
276
+ async setSensorTypes(
277
+ newSensorTypes: SensorType[],
278
+ sendImmediately?: boolean
279
+ ) {
219
280
  newSensorTypes.forEach((sensorType) => {
220
281
  TfliteManager.AssertValidSensorType(sensorType);
221
282
  });
@@ -223,10 +284,17 @@ class TfliteManager {
223
284
  const promise = this.waitForEvent("getTfliteSensorTypes");
224
285
 
225
286
  newSensorTypes = arrayWithoutDuplicates(newSensorTypes);
226
- const newSensorTypeEnums = newSensorTypes.map((sensorType) => SensorTypes.indexOf(sensorType)).sort();
287
+ const newSensorTypeEnums = newSensorTypes
288
+ .map((sensorType) => SensorTypes.indexOf(sensorType))
289
+ .sort();
227
290
  _console.log(newSensorTypes, newSensorTypeEnums);
228
291
  this.sendMessage(
229
- [{ type: "setTfliteSensorTypes", data: Uint8Array.from(newSensorTypeEnums).buffer }],
292
+ [
293
+ {
294
+ type: "setTfliteSensorTypes",
295
+ data: Uint8Array.from(newSensorTypeEnums).buffer,
296
+ },
297
+ ],
230
298
  sendImmediately
231
299
  );
232
300
 
@@ -263,7 +331,9 @@ class TfliteManager {
263
331
  #updateCaptueDelay(captureDelay: number) {
264
332
  _console.log({ captureDelay });
265
333
  this.#captureDelay = captureDelay;
266
- this.#dispatchEvent("getTfliteCaptureDelay", { tfliteCaptureDelay: captureDelay });
334
+ this.#dispatchEvent("getTfliteCaptureDelay", {
335
+ tfliteCaptureDelay: captureDelay,
336
+ });
267
337
  }
268
338
  async setCaptureDelay(newCaptureDelay: number, sendImmediately: boolean) {
269
339
  _console.assertTypeWithError(newCaptureDelay, "number");
@@ -276,7 +346,10 @@ class TfliteManager {
276
346
 
277
347
  const dataView = new DataView(new ArrayBuffer(2));
278
348
  dataView.setUint16(0, newCaptureDelay, true);
279
- this.sendMessage([{ type: "setTfliteCaptureDelay", data: dataView.buffer }], sendImmediately);
349
+ this.sendMessage(
350
+ [{ type: "setTfliteCaptureDelay", data: dataView.buffer }],
351
+ sendImmediately
352
+ );
280
353
 
281
354
  await promise;
282
355
  }
@@ -297,7 +370,10 @@ class TfliteManager {
297
370
  }
298
371
  async setThreshold(newThreshold: number, sendImmediately: boolean) {
299
372
  _console.assertTypeWithError(newThreshold, "number");
300
- _console.assertWithError(newThreshold >= 0, `threshold must be positive (got ${newThreshold})`);
373
+ _console.assertWithError(
374
+ newThreshold >= 0,
375
+ `threshold must be positive (got ${newThreshold})`
376
+ );
301
377
  if (this.#threshold == newThreshold) {
302
378
  _console.log(`redundant threshold assignment ${newThreshold}`);
303
379
  return;
@@ -307,7 +383,10 @@ class TfliteManager {
307
383
 
308
384
  const dataView = new DataView(new ArrayBuffer(4));
309
385
  dataView.setFloat32(0, newThreshold, true);
310
- this.sendMessage([{ type: "setTfliteThreshold", data: dataView.buffer }], sendImmediately);
386
+ this.sendMessage(
387
+ [{ type: "setTfliteThreshold", data: dataView.buffer }],
388
+ sendImmediately
389
+ );
311
390
 
312
391
  await promise;
313
392
  }
@@ -324,16 +403,23 @@ class TfliteManager {
324
403
  #updateInferencingEnabled(inferencingEnabled: boolean) {
325
404
  _console.log({ inferencingEnabled });
326
405
  this.#inferencingEnabled = inferencingEnabled;
327
- this.#dispatchEvent("getTfliteInferencingEnabled", { tfliteInferencingEnabled: inferencingEnabled });
406
+ this.#dispatchEvent("getTfliteInferencingEnabled", {
407
+ tfliteInferencingEnabled: inferencingEnabled,
408
+ });
328
409
  }
329
- async setInferencingEnabled(newInferencingEnabled: boolean, sendImmediately: boolean = true) {
410
+ async setInferencingEnabled(
411
+ newInferencingEnabled: boolean,
412
+ sendImmediately: boolean = true
413
+ ) {
330
414
  _console.assertTypeWithError(newInferencingEnabled, "boolean");
331
415
  if (!newInferencingEnabled && !this.isReady) {
332
416
  return;
333
417
  }
334
418
  this.#assertIsReady();
335
419
  if (this.#inferencingEnabled == newInferencingEnabled) {
336
- _console.log(`redundant inferencingEnabled assignment ${newInferencingEnabled}`);
420
+ _console.log(
421
+ `redundant inferencingEnabled assignment ${newInferencingEnabled}`
422
+ );
337
423
  return;
338
424
  }
339
425
 
@@ -375,7 +461,11 @@ class TfliteManager {
375
461
  _console.log({ timestamp });
376
462
 
377
463
  const values: number[] = [];
378
- for (let index = 0, byteOffset = 2; byteOffset < dataView.byteLength; index++, byteOffset += 4) {
464
+ for (
465
+ let index = 0, byteOffset = 2;
466
+ byteOffset < dataView.byteLength;
467
+ index++, byteOffset += 4
468
+ ) {
379
469
  const value = dataView.getFloat32(byteOffset, true);
380
470
  values.push(value);
381
471
  }
@@ -398,6 +488,15 @@ class TfliteManager {
398
488
  _console.log({ maxIndex, maxValue });
399
489
  inference.maxIndex = maxIndex;
400
490
  inference.maxValue = maxValue;
491
+ if (this.#configuration?.classes) {
492
+ const { classes } = this.#configuration;
493
+ inference.maxClass = classes[maxIndex];
494
+ inference.classValues = {};
495
+ values.forEach((value, index) => {
496
+ const key = classes[index];
497
+ inference.classValues![key] = value;
498
+ });
499
+ }
401
500
  }
402
501
 
403
502
  this.#dispatchEvent("tfliteInference", { tfliteInference: inference });
@@ -445,6 +544,53 @@ class TfliteManager {
445
544
  throw Error(`uncaught messageType ${messageType}`);
446
545
  }
447
546
  }
547
+
548
+ #configuration?: TfliteFileConfiguration;
549
+ get configuration() {
550
+ return this.#configuration;
551
+ }
552
+ sendConfiguration(
553
+ configuration: TfliteFileConfiguration,
554
+ sendImmediately?: boolean
555
+ ) {
556
+ if (configuration == this.#configuration) {
557
+ _console.log("redundant tflite configuration assignment");
558
+ return;
559
+ }
560
+ this.#configuration = configuration;
561
+ _console.log("assigned new tflite configuration", this.configuration);
562
+ if (!this.configuration) {
563
+ return;
564
+ }
565
+ const { name, task, captureDelay, sampleRate, threshold, sensorTypes } =
566
+ this.configuration;
567
+ this.setName(name, false);
568
+ this.setTask(task, false);
569
+ if (captureDelay != undefined) {
570
+ this.setCaptureDelay(captureDelay, false);
571
+ }
572
+ this.setSampleRate(sampleRate, false);
573
+ if (threshold != undefined) {
574
+ this.setThreshold(threshold, false);
575
+ }
576
+ this.setSensorTypes(sensorTypes, sendImmediately);
577
+ }
578
+
579
+ clear() {
580
+ this.#configuration = undefined;
581
+ this.#inferencingEnabled = false;
582
+ this.#sensorTypes = [];
583
+ this.#sampleRate = 0;
584
+ this.#isReady = false;
585
+ }
586
+
587
+ requestRequiredInformation() {
588
+ _console.log("requesting required tflite information");
589
+ const messages = RequiredTfliteMessageTypes.map((messageType) => ({
590
+ type: messageType,
591
+ }));
592
+ this.sendMessage(messages, false);
593
+ }
448
594
  }
449
595
 
450
596
  export default TfliteManager;