brilliantsole 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/build/brilliantsole.cjs +5957 -0
- package/build/brilliantsole.cjs.map +1 -0
- package/build/brilliantsole.js +5448 -0
- package/build/brilliantsole.js.map +1 -0
- package/build/brilliantsole.ls.js +4872 -0
- package/build/brilliantsole.ls.js.map +1 -0
- package/build/brilliantsole.min.js +6 -0
- package/build/brilliantsole.min.js.map +1 -0
- package/build/brilliantsole.module.d.ts +908 -0
- package/build/brilliantsole.module.js +5412 -0
- package/build/brilliantsole.module.js.map +1 -0
- package/build/brilliantsole.module.min.d.ts +908 -0
- package/build/brilliantsole.module.min.js +6 -0
- package/build/brilliantsole.module.min.js.map +1 -0
- package/build/brilliantsole.node.module.d.ts +908 -0
- package/build/brilliantsole.node.module.js +5906 -0
- package/build/brilliantsole.node.module.js.map +1 -0
- package/build/dts/BS.d.ts +25 -0
- package/build/dts/Device.d.ts +136 -0
- package/build/dts/DeviceInformationManager.d.ts +56 -0
- package/build/dts/DeviceManager.d.ts +67 -0
- package/build/dts/FileTransferManager.d.ts +84 -0
- package/build/dts/FirmwareManager.d.ts +71 -0
- package/build/dts/InformationManager.d.ts +66 -0
- package/build/dts/TfliteManager.d.ts +92 -0
- package/build/dts/connection/BaseConnectionManager.d.ts +59 -0
- package/build/dts/connection/ClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/WebSocketClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/bluetooth/BluetoothConnectionManager.d.ts +10 -0
- package/build/dts/connection/bluetooth/NobleConnectionManager.d.ts +42 -0
- package/build/dts/connection/bluetooth/WebBluetoothConnectionManager.d.ts +20 -0
- package/build/dts/connection/bluetooth/bluetoothUUIDs.d.ts +14 -0
- package/build/dts/connection/webSocket/ClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/webSocket/WebSocketClientConnectionManager.d.ts +23 -0
- package/build/dts/devicePair/DevicePair.d.ts +60 -0
- package/build/dts/devicePair/DevicePairPressureSensorDataManager.d.ts +25 -0
- package/build/dts/devicePair/DevicePairSensorDataManager.d.ts +33 -0
- package/build/dts/scanner/BaseScanner.d.ts +66 -0
- package/build/dts/scanner/NobleScanner.d.ts +17 -0
- package/build/dts/scanner/Scanner.d.ts +3 -0
- package/build/dts/sensor/BarometerSensorDataManager.d.ts +16 -0
- package/build/dts/sensor/MotionSensorDataManager.d.ts +69 -0
- package/build/dts/sensor/PressureSensorDataManager.d.ts +36 -0
- package/build/dts/sensor/SensorConfigurationManager.d.ts +44 -0
- package/build/dts/sensor/SensorDataManager.d.ts +40 -0
- package/build/dts/server/BaseClient.d.ts +85 -0
- package/build/dts/server/BaseServer.d.ts +48 -0
- package/build/dts/server/ServerUtils.d.ts +23 -0
- package/build/dts/server/udp/UDPServer.d.ts +11 -0
- package/build/dts/server/udp/UDPUtils.d.ts +9 -0
- package/build/dts/server/websocket/WebSocketClient.d.ts +17 -0
- package/build/dts/server/websocket/WebSocketServer.d.ts +13 -0
- package/build/dts/server/websocket/WebSocketUtils.d.ts +9 -0
- package/build/dts/utils/ArrayBufferUtils.d.ts +7 -0
- package/build/dts/utils/ArrayUtils.d.ts +2 -0
- package/build/dts/utils/CenterOfPressureHelper.d.ts +15 -0
- package/build/dts/utils/Console.d.ts +34 -0
- package/build/dts/utils/EventDispatcher.d.ts +50 -0
- package/build/dts/utils/EventUtils.d.ts +6 -0
- package/build/dts/utils/MathUtils.d.ts +21 -0
- package/build/dts/utils/ParseUtils.d.ts +5 -0
- package/build/dts/utils/RangeHelper.d.ts +8 -0
- package/build/dts/utils/Text.d.ts +6 -0
- package/build/dts/utils/Timer.d.ts +14 -0
- package/build/dts/utils/TypeScriptUtils.d.ts +19 -0
- package/build/dts/utils/cbor.d.ts +6 -0
- package/build/dts/utils/checksum.d.ts +3 -0
- package/build/dts/utils/environment.d.ts +13 -0
- package/build/dts/utils/mcumgr.d.ts +88 -0
- package/build/dts/utils/stringUtils.d.ts +2 -0
- package/build/dts/vibration/VibrationManager.d.ts +45 -0
- package/build/dts/vibration/VibrationWaveformEffects.d.ts +2 -0
- package/build/index.d.ts +908 -0
- package/build/index.node.d.ts +908 -0
- package/examples/3d/index.html +109 -0
- package/examples/3d/scene.html +57 -0
- package/examples/3d/script.js +419 -0
- package/examples/balance/index.html +138 -0
- package/examples/balance/script.js +243 -0
- package/examples/basic/index.html +327 -0
- package/examples/basic/script.js +1093 -0
- package/examples/center-of-pressure/index.html +132 -0
- package/examples/center-of-pressure/script.js +207 -0
- package/examples/device-pair/index.html +72 -0
- package/examples/device-pair/script.js +187 -0
- package/examples/edge-impulse/index.html +94 -0
- package/examples/edge-impulse/script.js +1033 -0
- package/examples/graph/index.html +83 -0
- package/examples/graph/script.js +469 -0
- package/examples/machine-learning/index.html +366 -0
- package/examples/machine-learning/script.js +1774 -0
- package/examples/pressure/index.html +145 -0
- package/examples/pressure/script.js +201 -0
- package/examples/recording/index.html +187 -0
- package/examples/recording/script.js +736 -0
- package/examples/server/index.html +266 -0
- package/examples/server/script.js +925 -0
- package/examples/utils/aframe/fingertip-button-component.js +201 -0
- package/examples/utils/aframe/fingertip-collider-target-component.js +102 -0
- package/examples/utils/aframe/fingertip-colliders-component.js +147 -0
- package/examples/utils/three/three.module.min.js +24846 -0
- package/examples/webxr/index.html +221 -0
- package/examples/webxr/script.js +1127 -0
- package/package.json +83 -0
- package/src/BS.ts +68 -0
- package/src/Device.ts +734 -0
- package/src/DeviceInformationManager.ts +146 -0
- package/src/DeviceManager.ts +354 -0
- package/src/FileTransferManager.ts +452 -0
- package/src/FirmwareManager.ts +357 -0
- package/src/InformationManager.ts +283 -0
- package/src/TfliteManager.ts +450 -0
- package/src/connection/BaseConnectionManager.ts +255 -0
- package/src/connection/ClientConnectionManager.ts +120 -0
- package/src/connection/bluetooth/BluetoothConnectionManager.ts +34 -0
- package/src/connection/bluetooth/NobleConnectionManager.ts +302 -0
- package/src/connection/bluetooth/WebBluetoothConnectionManager.ts +269 -0
- package/src/connection/bluetooth/bluetoothUUIDs.ts +218 -0
- package/src/devicePair/DevicePair.ts +253 -0
- package/src/devicePair/DevicePairPressureSensorDataManager.ts +82 -0
- package/src/devicePair/DevicePairSensorDataManager.ts +90 -0
- package/src/scanner/BaseScanner.ts +189 -0
- package/src/scanner/NobleScanner.ts +195 -0
- package/src/scanner/Scanner.ts +16 -0
- package/src/sensor/BarometerSensorDataManager.ts +41 -0
- package/src/sensor/MotionSensorDataManager.ts +151 -0
- package/src/sensor/PressureSensorDataManager.ts +112 -0
- package/src/sensor/SensorConfigurationManager.ts +177 -0
- package/src/sensor/SensorDataManager.ts +166 -0
- package/src/server/BaseClient.ts +368 -0
- package/src/server/BaseServer.ts +344 -0
- package/src/server/ServerUtils.ts +93 -0
- package/src/server/udp/UDPServer.ts +229 -0
- package/src/server/udp/UDPUtils.ts +20 -0
- package/src/server/websocket/WebSocketClient.ts +179 -0
- package/src/server/websocket/WebSocketServer.ts +184 -0
- package/src/server/websocket/WebSocketUtils.ts +20 -0
- package/src/utils/ArrayBufferUtils.ts +88 -0
- package/src/utils/ArrayUtils.ts +15 -0
- package/src/utils/CenterOfPressureHelper.ts +39 -0
- package/src/utils/Console.ts +156 -0
- package/src/utils/EventDispatcher.ts +153 -0
- package/src/utils/EventUtils.ts +41 -0
- package/src/utils/MathUtils.ts +53 -0
- package/src/utils/ParseUtils.ts +46 -0
- package/src/utils/RangeHelper.ts +38 -0
- package/src/utils/Text.ts +30 -0
- package/src/utils/Timer.ts +72 -0
- package/src/utils/TypeScriptUtils.ts +22 -0
- package/src/utils/cbor.js +429 -0
- package/src/utils/checksum.ts +41 -0
- package/src/utils/environment.ts +46 -0
- package/src/utils/mcumgr.js +444 -0
- package/src/utils/stringUtils.ts +11 -0
- package/src/vibration/VibrationManager.ts +308 -0
- package/src/vibration/VibrationWaveformEffects.ts +128 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { createConsole } from "../utils/Console.ts";
|
|
2
|
+
import { VibrationWaveformEffect, VibrationWaveformEffects } from "./VibrationWaveformEffects.ts";
|
|
3
|
+
import { concatenateArrayBuffers } from "../utils/ArrayBufferUtils.ts";
|
|
4
|
+
import { SendMessageCallback } from "../Device.ts";
|
|
5
|
+
import autoBind from "auto-bind";
|
|
6
|
+
|
|
7
|
+
const _console = createConsole("VibrationManager");
|
|
8
|
+
|
|
9
|
+
export const VibrationLocations = ["front", "rear"] as const;
|
|
10
|
+
export type VibrationLocation = (typeof VibrationLocations)[number];
|
|
11
|
+
|
|
12
|
+
export const VibrationTypes = ["waveformEffect", "waveform"] as const;
|
|
13
|
+
export type VibrationType = (typeof VibrationTypes)[number];
|
|
14
|
+
|
|
15
|
+
export interface VibrationWaveformEffectSegment {
|
|
16
|
+
effect?: VibrationWaveformEffect;
|
|
17
|
+
delay?: number;
|
|
18
|
+
loopCount?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface VibrationWaveformSegment {
|
|
22
|
+
duration: number;
|
|
23
|
+
amplitude: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const VibrationMessageTypes = ["triggerVibration"] as const;
|
|
27
|
+
export type VibrationMessageType = (typeof VibrationMessageTypes)[number];
|
|
28
|
+
|
|
29
|
+
export const MaxNumberOfVibrationWaveformEffectSegments = 8;
|
|
30
|
+
export const MaxVibrationWaveformSegmentDuration = 2550;
|
|
31
|
+
export const MaxVibrationWaveformEffectSegmentDelay = 1270;
|
|
32
|
+
export const MaxVibrationWaveformEffectSegmentLoopCount = 3;
|
|
33
|
+
export const MaxNumberOfVibrationWaveformSegments = 20;
|
|
34
|
+
export const MaxVibrationWaveformEffectSequenceLoopCount = 6;
|
|
35
|
+
|
|
36
|
+
interface BaseVibrationConfiguration {
|
|
37
|
+
type: VibrationType;
|
|
38
|
+
locations?: VibrationLocation[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface VibrationWaveformEffectConfiguration extends BaseVibrationConfiguration {
|
|
42
|
+
type: "waveformEffect";
|
|
43
|
+
segments: VibrationWaveformEffectSegment[];
|
|
44
|
+
loopCount?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface VibrationWaveformConfiguration extends BaseVibrationConfiguration {
|
|
48
|
+
type: "waveform";
|
|
49
|
+
segments: VibrationWaveformSegment[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type VibrationConfiguration = VibrationWaveformEffectConfiguration | VibrationWaveformConfiguration;
|
|
53
|
+
|
|
54
|
+
export type SendVibrationMessageCallback = SendMessageCallback<VibrationMessageType>;
|
|
55
|
+
|
|
56
|
+
class VibrationManager {
|
|
57
|
+
constructor() {
|
|
58
|
+
autoBind(this);
|
|
59
|
+
}
|
|
60
|
+
sendMessage!: SendVibrationMessageCallback;
|
|
61
|
+
|
|
62
|
+
#verifyLocation(location: VibrationLocation) {
|
|
63
|
+
_console.assertTypeWithError(location, "string");
|
|
64
|
+
_console.assertWithError(VibrationLocations.includes(location), `invalid location "${location}"`);
|
|
65
|
+
}
|
|
66
|
+
#verifyLocations(locations: VibrationLocation[]) {
|
|
67
|
+
this.#assertNonEmptyArray(locations);
|
|
68
|
+
locations.forEach((location) => {
|
|
69
|
+
this.#verifyLocation(location);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
#createLocationsBitmask(locations: VibrationLocation[]) {
|
|
73
|
+
this.#verifyLocations(locations);
|
|
74
|
+
|
|
75
|
+
let locationsBitmask = 0;
|
|
76
|
+
locations.forEach((location) => {
|
|
77
|
+
const locationIndex = VibrationLocations.indexOf(location);
|
|
78
|
+
locationsBitmask |= 1 << locationIndex;
|
|
79
|
+
});
|
|
80
|
+
_console.log({ locationsBitmask });
|
|
81
|
+
_console.assertWithError(locationsBitmask > 0, `locationsBitmask must not be zero`);
|
|
82
|
+
return locationsBitmask;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
#assertNonEmptyArray(array: any[]) {
|
|
86
|
+
_console.assertWithError(Array.isArray(array), "passed non-array");
|
|
87
|
+
_console.assertWithError(array.length > 0, "passed empty array");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#verifyWaveformEffect(waveformEffect: VibrationWaveformEffect) {
|
|
91
|
+
_console.assertWithError(
|
|
92
|
+
VibrationWaveformEffects.includes(waveformEffect),
|
|
93
|
+
`invalid waveformEffect "${waveformEffect}"`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#verifyWaveformEffectSegment(waveformEffectSegment: VibrationWaveformEffectSegment) {
|
|
98
|
+
if (waveformEffectSegment.effect != undefined) {
|
|
99
|
+
const waveformEffect = waveformEffectSegment.effect;
|
|
100
|
+
this.#verifyWaveformEffect(waveformEffect);
|
|
101
|
+
} else if (waveformEffectSegment.delay != undefined) {
|
|
102
|
+
const { delay } = waveformEffectSegment;
|
|
103
|
+
_console.assertWithError(delay >= 0, `delay must be 0ms or greater (got ${delay})`);
|
|
104
|
+
_console.assertWithError(
|
|
105
|
+
delay <= MaxVibrationWaveformEffectSegmentDelay,
|
|
106
|
+
`delay must be ${MaxVibrationWaveformEffectSegmentDelay}ms or less (got ${delay})`
|
|
107
|
+
);
|
|
108
|
+
} else {
|
|
109
|
+
throw Error("no effect or delay found in waveformEffectSegment");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (waveformEffectSegment.loopCount != undefined) {
|
|
113
|
+
const { loopCount } = waveformEffectSegment;
|
|
114
|
+
this.#verifyWaveformEffectSegmentLoopCount(loopCount);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
#verifyWaveformEffectSegmentLoopCount(waveformEffectSegmentLoopCount: number) {
|
|
119
|
+
_console.assertTypeWithError(waveformEffectSegmentLoopCount, "number");
|
|
120
|
+
_console.assertWithError(
|
|
121
|
+
waveformEffectSegmentLoopCount >= 0,
|
|
122
|
+
`waveformEffectSegmentLoopCount must be 0 or greater (got ${waveformEffectSegmentLoopCount})`
|
|
123
|
+
);
|
|
124
|
+
_console.assertWithError(
|
|
125
|
+
waveformEffectSegmentLoopCount <= MaxVibrationWaveformEffectSegmentLoopCount,
|
|
126
|
+
`waveformEffectSegmentLoopCount must be ${MaxVibrationWaveformEffectSegmentLoopCount} or fewer (got ${waveformEffectSegmentLoopCount})`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
#verifyWaveformEffectSegments(waveformEffectSegments: VibrationWaveformEffectSegment[]) {
|
|
131
|
+
this.#assertNonEmptyArray(waveformEffectSegments);
|
|
132
|
+
_console.assertWithError(
|
|
133
|
+
waveformEffectSegments.length <= MaxNumberOfVibrationWaveformEffectSegments,
|
|
134
|
+
`must have ${MaxNumberOfVibrationWaveformEffectSegments} waveformEffectSegments or fewer (got ${waveformEffectSegments.length})`
|
|
135
|
+
);
|
|
136
|
+
waveformEffectSegments.forEach((waveformEffectSegment) => {
|
|
137
|
+
this.#verifyWaveformEffectSegment(waveformEffectSegment);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#verifyWaveformEffectSequenceLoopCount(waveformEffectSequenceLoopCount: number) {
|
|
142
|
+
_console.assertTypeWithError(waveformEffectSequenceLoopCount, "number");
|
|
143
|
+
_console.assertWithError(
|
|
144
|
+
waveformEffectSequenceLoopCount >= 0,
|
|
145
|
+
`waveformEffectSequenceLoopCount must be 0 or greater (got ${waveformEffectSequenceLoopCount})`
|
|
146
|
+
);
|
|
147
|
+
_console.assertWithError(
|
|
148
|
+
waveformEffectSequenceLoopCount <= MaxVibrationWaveformEffectSequenceLoopCount,
|
|
149
|
+
`waveformEffectSequenceLoopCount must be ${MaxVibrationWaveformEffectSequenceLoopCount} or fewer (got ${waveformEffectSequenceLoopCount})`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#verifyWaveformSegment(waveformSegment: VibrationWaveformSegment) {
|
|
154
|
+
_console.assertTypeWithError(waveformSegment.amplitude, "number");
|
|
155
|
+
_console.assertWithError(
|
|
156
|
+
waveformSegment.amplitude >= 0,
|
|
157
|
+
`amplitude must be 0 or greater (got ${waveformSegment.amplitude})`
|
|
158
|
+
);
|
|
159
|
+
_console.assertWithError(
|
|
160
|
+
waveformSegment.amplitude <= 1,
|
|
161
|
+
`amplitude must be 1 or less (got ${waveformSegment.amplitude})`
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
_console.assertTypeWithError(waveformSegment.duration, "number");
|
|
165
|
+
_console.assertWithError(
|
|
166
|
+
waveformSegment.duration > 0,
|
|
167
|
+
`duration must be greater than 0ms (got ${waveformSegment.duration}ms)`
|
|
168
|
+
);
|
|
169
|
+
_console.assertWithError(
|
|
170
|
+
waveformSegment.duration <= MaxVibrationWaveformSegmentDuration,
|
|
171
|
+
`duration must be ${MaxVibrationWaveformSegmentDuration}ms or less (got ${waveformSegment.duration}ms)`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#verifyWaveformSegments(waveformSegments: VibrationWaveformSegment[]) {
|
|
176
|
+
this.#assertNonEmptyArray(waveformSegments);
|
|
177
|
+
_console.assertWithError(
|
|
178
|
+
waveformSegments.length <= MaxNumberOfVibrationWaveformSegments,
|
|
179
|
+
`must have ${MaxNumberOfVibrationWaveformSegments} waveformSegments or fewer (got ${waveformSegments.length})`
|
|
180
|
+
);
|
|
181
|
+
waveformSegments.forEach((waveformSegment) => {
|
|
182
|
+
this.#verifyWaveformSegment(waveformSegment);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#createWaveformEffectsData(
|
|
187
|
+
locations: VibrationLocation[],
|
|
188
|
+
waveformEffectSegments: VibrationWaveformEffectSegment[],
|
|
189
|
+
waveformEffectSequenceLoopCount: number = 0
|
|
190
|
+
) {
|
|
191
|
+
this.#verifyWaveformEffectSegments(waveformEffectSegments);
|
|
192
|
+
this.#verifyWaveformEffectSequenceLoopCount(waveformEffectSequenceLoopCount);
|
|
193
|
+
|
|
194
|
+
let dataArray = [];
|
|
195
|
+
let byteOffset = 0;
|
|
196
|
+
|
|
197
|
+
const hasAtLeast1WaveformEffectWithANonzeroLoopCount = waveformEffectSegments.some((waveformEffectSegment) => {
|
|
198
|
+
const { loopCount } = waveformEffectSegment;
|
|
199
|
+
return loopCount != undefined && loopCount > 0;
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const includeAllWaveformEffectSegments =
|
|
203
|
+
hasAtLeast1WaveformEffectWithANonzeroLoopCount || waveformEffectSequenceLoopCount != 0;
|
|
204
|
+
|
|
205
|
+
for (
|
|
206
|
+
let index = 0;
|
|
207
|
+
index < waveformEffectSegments.length ||
|
|
208
|
+
(includeAllWaveformEffectSegments && index < MaxNumberOfVibrationWaveformEffectSegments);
|
|
209
|
+
index++
|
|
210
|
+
) {
|
|
211
|
+
const waveformEffectSegment = waveformEffectSegments[index] || { effect: "none" };
|
|
212
|
+
if (waveformEffectSegment.effect != undefined) {
|
|
213
|
+
const waveformEffect = waveformEffectSegment.effect;
|
|
214
|
+
dataArray[byteOffset++] = VibrationWaveformEffects.indexOf(waveformEffect);
|
|
215
|
+
} else if (waveformEffectSegment.delay != undefined) {
|
|
216
|
+
const { delay } = waveformEffectSegment;
|
|
217
|
+
dataArray[byteOffset++] = (1 << 7) | Math.floor(delay / 10); // set most significant bit to 1
|
|
218
|
+
} else {
|
|
219
|
+
throw Error("invalid waveformEffectSegment");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const includeAllWaveformEffectSegmentLoopCounts = waveformEffectSequenceLoopCount != 0;
|
|
224
|
+
for (
|
|
225
|
+
let index = 0;
|
|
226
|
+
index < waveformEffectSegments.length ||
|
|
227
|
+
(includeAllWaveformEffectSegmentLoopCounts && index < MaxNumberOfVibrationWaveformEffectSegments);
|
|
228
|
+
index++
|
|
229
|
+
) {
|
|
230
|
+
const waveformEffectSegmentLoopCount = waveformEffectSegments[index]?.loopCount || 0;
|
|
231
|
+
if (index == 0 || index == 4) {
|
|
232
|
+
dataArray[byteOffset] = 0;
|
|
233
|
+
}
|
|
234
|
+
const bitOffset = 2 * (index % 4);
|
|
235
|
+
dataArray[byteOffset] |= waveformEffectSegmentLoopCount << bitOffset;
|
|
236
|
+
if (index == 3 || index == 7) {
|
|
237
|
+
byteOffset++;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (waveformEffectSequenceLoopCount != 0) {
|
|
242
|
+
dataArray[byteOffset++] = waveformEffectSequenceLoopCount;
|
|
243
|
+
}
|
|
244
|
+
const dataView = new DataView(Uint8Array.from(dataArray).buffer);
|
|
245
|
+
_console.log({ dataArray, dataView });
|
|
246
|
+
return this.#createData(locations, "waveformEffect", dataView);
|
|
247
|
+
}
|
|
248
|
+
#createWaveformData(locations: VibrationLocation[], waveformSegments: VibrationWaveformSegment[]) {
|
|
249
|
+
this.#verifyWaveformSegments(waveformSegments);
|
|
250
|
+
const dataView = new DataView(new ArrayBuffer(waveformSegments.length * 2));
|
|
251
|
+
waveformSegments.forEach((waveformSegment, index) => {
|
|
252
|
+
dataView.setUint8(index * 2, Math.floor(waveformSegment.amplitude * 127));
|
|
253
|
+
dataView.setUint8(index * 2 + 1, Math.floor(waveformSegment.duration / 10));
|
|
254
|
+
});
|
|
255
|
+
_console.log({ dataView });
|
|
256
|
+
return this.#createData(locations, "waveform", dataView);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
#verifyVibrationType(vibrationType: VibrationType) {
|
|
260
|
+
_console.assertTypeWithError(vibrationType, "string");
|
|
261
|
+
_console.assertWithError(VibrationTypes.includes(vibrationType), `invalid vibrationType "${vibrationType}"`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#createData(locations: VibrationLocation[], vibrationType: VibrationType, dataView: DataView) {
|
|
265
|
+
_console.assertWithError(dataView?.byteLength > 0, "no data received");
|
|
266
|
+
const locationsBitmask = this.#createLocationsBitmask(locations);
|
|
267
|
+
this.#verifyVibrationType(vibrationType);
|
|
268
|
+
const vibrationTypeIndex = VibrationTypes.indexOf(vibrationType);
|
|
269
|
+
_console.log({ locationsBitmask, vibrationTypeIndex, dataView });
|
|
270
|
+
const data = concatenateArrayBuffers(locationsBitmask, vibrationTypeIndex, dataView.byteLength, dataView);
|
|
271
|
+
_console.log({ data });
|
|
272
|
+
return data;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async triggerVibration(vibrationConfigurations: VibrationConfiguration[], sendImmediately: boolean = true) {
|
|
276
|
+
let triggerVibrationData!: ArrayBuffer;
|
|
277
|
+
vibrationConfigurations.forEach((vibrationConfiguration) => {
|
|
278
|
+
const { type } = vibrationConfiguration;
|
|
279
|
+
|
|
280
|
+
let { locations } = vibrationConfiguration;
|
|
281
|
+
locations = locations || VibrationLocations.slice();
|
|
282
|
+
|
|
283
|
+
let arrayBuffer: ArrayBuffer;
|
|
284
|
+
|
|
285
|
+
switch (type) {
|
|
286
|
+
case "waveformEffect":
|
|
287
|
+
{
|
|
288
|
+
const { segments, loopCount } = vibrationConfiguration;
|
|
289
|
+
arrayBuffer = this.#createWaveformEffectsData(locations, segments, loopCount);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "waveform":
|
|
293
|
+
{
|
|
294
|
+
const { segments } = vibrationConfiguration;
|
|
295
|
+
arrayBuffer = this.#createWaveformData(locations, segments);
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
default:
|
|
299
|
+
throw Error(`invalid vibration type "${type}"`);
|
|
300
|
+
}
|
|
301
|
+
_console.log({ type, arrayBuffer });
|
|
302
|
+
triggerVibrationData = concatenateArrayBuffers(triggerVibrationData, arrayBuffer);
|
|
303
|
+
});
|
|
304
|
+
await this.sendMessage([{ type: "triggerVibration", data: triggerVibrationData }], sendImmediately);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export default VibrationManager;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export const VibrationWaveformEffects = [
|
|
2
|
+
"none",
|
|
3
|
+
"strongClick100",
|
|
4
|
+
"strongClick60",
|
|
5
|
+
"strongClick30",
|
|
6
|
+
"sharpClick100",
|
|
7
|
+
"sharpClick60",
|
|
8
|
+
"sharpClick30",
|
|
9
|
+
"softBump100",
|
|
10
|
+
"softBump60",
|
|
11
|
+
"softBump30",
|
|
12
|
+
"doubleClick100",
|
|
13
|
+
"doubleClick60",
|
|
14
|
+
"tripleClick100",
|
|
15
|
+
"softFuzz60",
|
|
16
|
+
"strongBuzz100",
|
|
17
|
+
"alert750ms",
|
|
18
|
+
"alert1000ms",
|
|
19
|
+
"strongClick1_100",
|
|
20
|
+
"strongClick2_80",
|
|
21
|
+
"strongClick3_60",
|
|
22
|
+
"strongClick4_30",
|
|
23
|
+
"mediumClick100",
|
|
24
|
+
"mediumClick80",
|
|
25
|
+
"mediumClick60",
|
|
26
|
+
"sharpTick100",
|
|
27
|
+
"sharpTick80",
|
|
28
|
+
"sharpTick60",
|
|
29
|
+
"shortDoubleClickStrong100",
|
|
30
|
+
"shortDoubleClickStrong80",
|
|
31
|
+
"shortDoubleClickStrong60",
|
|
32
|
+
"shortDoubleClickStrong30",
|
|
33
|
+
"shortDoubleClickMedium100",
|
|
34
|
+
"shortDoubleClickMedium80",
|
|
35
|
+
"shortDoubleClickMedium60",
|
|
36
|
+
"shortDoubleSharpTick100",
|
|
37
|
+
"shortDoubleSharpTick80",
|
|
38
|
+
"shortDoubleSharpTick60",
|
|
39
|
+
"longDoubleSharpClickStrong100",
|
|
40
|
+
"longDoubleSharpClickStrong80",
|
|
41
|
+
"longDoubleSharpClickStrong60",
|
|
42
|
+
"longDoubleSharpClickStrong30",
|
|
43
|
+
"longDoubleSharpClickMedium100",
|
|
44
|
+
"longDoubleSharpClickMedium80",
|
|
45
|
+
"longDoubleSharpClickMedium60",
|
|
46
|
+
"longDoubleSharpTick100",
|
|
47
|
+
"longDoubleSharpTick80",
|
|
48
|
+
"longDoubleSharpTick60",
|
|
49
|
+
"buzz100",
|
|
50
|
+
"buzz80",
|
|
51
|
+
"buzz60",
|
|
52
|
+
"buzz40",
|
|
53
|
+
"buzz20",
|
|
54
|
+
"pulsingStrong100",
|
|
55
|
+
"pulsingStrong60",
|
|
56
|
+
"pulsingMedium100",
|
|
57
|
+
"pulsingMedium60",
|
|
58
|
+
"pulsingSharp100",
|
|
59
|
+
"pulsingSharp60",
|
|
60
|
+
"transitionClick100",
|
|
61
|
+
"transitionClick80",
|
|
62
|
+
"transitionClick60",
|
|
63
|
+
"transitionClick40",
|
|
64
|
+
"transitionClick20",
|
|
65
|
+
"transitionClick10",
|
|
66
|
+
"transitionHum100",
|
|
67
|
+
"transitionHum80",
|
|
68
|
+
"transitionHum60",
|
|
69
|
+
"transitionHum40",
|
|
70
|
+
"transitionHum20",
|
|
71
|
+
"transitionHum10",
|
|
72
|
+
"transitionRampDownLongSmooth2_100",
|
|
73
|
+
"transitionRampDownLongSmooth1_100",
|
|
74
|
+
"transitionRampDownMediumSmooth1_100",
|
|
75
|
+
"transitionRampDownMediumSmooth2_100",
|
|
76
|
+
"transitionRampDownShortSmooth1_100",
|
|
77
|
+
"transitionRampDownShortSmooth2_100",
|
|
78
|
+
"transitionRampDownLongSharp1_100",
|
|
79
|
+
"transitionRampDownLongSharp2_100",
|
|
80
|
+
"transitionRampDownMediumSharp1_100",
|
|
81
|
+
"transitionRampDownMediumSharp2_100",
|
|
82
|
+
"transitionRampDownShortSharp1_100",
|
|
83
|
+
"transitionRampDownShortSharp2_100",
|
|
84
|
+
"transitionRampUpLongSmooth1_100",
|
|
85
|
+
"transitionRampUpLongSmooth2_100",
|
|
86
|
+
"transitionRampUpMediumSmooth1_100",
|
|
87
|
+
"transitionRampUpMediumSmooth2_100",
|
|
88
|
+
"transitionRampUpShortSmooth1_100",
|
|
89
|
+
"transitionRampUpShortSmooth2_100",
|
|
90
|
+
"transitionRampUpLongSharp1_100",
|
|
91
|
+
"transitionRampUpLongSharp2_100",
|
|
92
|
+
"transitionRampUpMediumSharp1_100",
|
|
93
|
+
"transitionRampUpMediumSharp2_100",
|
|
94
|
+
"transitionRampUpShortSharp1_100",
|
|
95
|
+
"transitionRampUpShortSharp2_100",
|
|
96
|
+
"transitionRampDownLongSmooth1_50",
|
|
97
|
+
"transitionRampDownLongSmooth2_50",
|
|
98
|
+
"transitionRampDownMediumSmooth1_50",
|
|
99
|
+
"transitionRampDownMediumSmooth2_50",
|
|
100
|
+
"transitionRampDownShortSmooth1_50",
|
|
101
|
+
"transitionRampDownShortSmooth2_50",
|
|
102
|
+
"transitionRampDownLongSharp1_50",
|
|
103
|
+
"transitionRampDownLongSharp2_50",
|
|
104
|
+
"transitionRampDownMediumSharp1_50",
|
|
105
|
+
"transitionRampDownMediumSharp2_50",
|
|
106
|
+
"transitionRampDownShortSharp1_50",
|
|
107
|
+
"transitionRampDownShortSharp2_50",
|
|
108
|
+
"transitionRampUpLongSmooth1_50",
|
|
109
|
+
"transitionRampUpLongSmooth2_50",
|
|
110
|
+
"transitionRampUpMediumSmooth1_50",
|
|
111
|
+
"transitionRampUpMediumSmooth2_50",
|
|
112
|
+
"transitionRampUpShortSmooth1_50",
|
|
113
|
+
"transitionRampUpShortSmooth2_50",
|
|
114
|
+
"transitionRampUpLongSharp1_50",
|
|
115
|
+
"transitionRampUpLongSharp2_50",
|
|
116
|
+
"transitionRampUpMediumSharp1_50",
|
|
117
|
+
"transitionRampUpMediumSharp2_50",
|
|
118
|
+
"transitionRampUpShortSharp1_50",
|
|
119
|
+
"transitionRampUpShortSharp2_50",
|
|
120
|
+
"longBuzz100",
|
|
121
|
+
"smoothHum50",
|
|
122
|
+
"smoothHum40",
|
|
123
|
+
"smoothHum30",
|
|
124
|
+
"smoothHum20",
|
|
125
|
+
"smoothHum10",
|
|
126
|
+
] as const;
|
|
127
|
+
|
|
128
|
+
export type VibrationWaveformEffect = (typeof VibrationWaveformEffects)[number];
|