@shubham0x13/ffbeast-wheel-webhid-api 0.1.0
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 +70 -0
- package/dist/index.d.ts +526 -0
- package/dist/index.js +683 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shubham Patel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# FFBeast Wheel WebHID API
|
|
2
|
+
|
|
3
|
+
A TypeScript WebHID-based API for communicating with [FFBeast](https://ffbeast.github.io/) force feedback wheel controllers directly from the browser.
|
|
4
|
+
|
|
5
|
+
This library is a TypeScript rewrite of the original C/C++ API, adapted to use the [WebHID API](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API) for browser-based applications. The original C/C++ source code is available in the [`reference_cpp`](https://shubham0x13/ffbeast-wheel-webhid-api/tree/main/reference_cpp) directory for reference.
|
|
6
|
+
|
|
7
|
+
> **Note:** This API includes additional helper methods and properties not present in the original C/C++ implementation, such as `readAllSettings()`, `positionDegrees`, `torqueNormalized`, and more for convenience.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- 🎮 **Direct wheel communication** via WebHID (no drivers or native apps required)
|
|
12
|
+
- 📊 **Real-time device state** - position, torque, firmware version
|
|
13
|
+
- ⚙️ **Full settings control** - effect settings, hardware settings, GPIO, ADC configurations
|
|
14
|
+
- 🔧 **Direct force feedback control** - spring, constant, and periodic forces
|
|
15
|
+
- 📡 **Event-driven architecture** - subscribe to device state changes
|
|
16
|
+
- 🔒 **Type-safe** - full TypeScript support with comprehensive types
|
|
17
|
+
|
|
18
|
+
## Documentation
|
|
19
|
+
|
|
20
|
+
📚 **[Full API Documentation](https://shubham0x13.github.io/ffbeast-wheel-webhid-api/)**
|
|
21
|
+
|
|
22
|
+
## Browser Support
|
|
23
|
+
|
|
24
|
+
> **Note:** Supported in all modern Chromium-based browsers (e.g., Chrome, Edge, Opera, Brave, etc).
|
|
25
|
+
|
|
26
|
+
| Browser | Supported |
|
|
27
|
+
| -------------- | --------- |
|
|
28
|
+
| Chromium-based | ✅ |
|
|
29
|
+
| Firefox | ❌ |
|
|
30
|
+
| Safari | ❌ |
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install github:shubham0x13/ffbeast-wheel-webhid-api
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { WheelApi } from "@shubham0x13/ffbeast-wheel-webhid-api";
|
|
42
|
+
|
|
43
|
+
const wheel = new WheelApi();
|
|
44
|
+
|
|
45
|
+
wheel.on("stateReceived", (state) => {
|
|
46
|
+
console.log("Position:", state.positionDegrees);
|
|
47
|
+
console.log("Torque:", state.torqueNormalized);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
wheel.on("deviceConnected", (device) => {
|
|
51
|
+
console.log("Connected to:", device.productName);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Connect to the wheel (opens browser device picker)
|
|
55
|
+
await wheel.connect();
|
|
56
|
+
|
|
57
|
+
// Cleanup when done
|
|
58
|
+
await wheel.disconnect();
|
|
59
|
+
wheel.destroy();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Requirements
|
|
63
|
+
|
|
64
|
+
- Node.js >= 20.19.0
|
|
65
|
+
- A Chromium-based browser with WebHID support
|
|
66
|
+
- An FFBeast wheel controller
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
This project is licensed under the [MIT License](./LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
|
|
3
|
+
//#region src/constants.d.ts
|
|
4
|
+
/** USB Vendor ID for FFBeast devices. */
|
|
5
|
+
declare const VID = 1115;
|
|
6
|
+
/** USB Product ID for FFBeast wheel controllers. */
|
|
7
|
+
declare const PID = 22999;
|
|
8
|
+
/** Size of HID reports in bytes. */
|
|
9
|
+
declare const REPORT_SIZE = 64;
|
|
10
|
+
/** HID usage page for interface 0. */
|
|
11
|
+
declare const INTERFACE_0_USAGE_PAGE = 65394;
|
|
12
|
+
/** HID usage for interface 0. */
|
|
13
|
+
declare const INTERFACE_0_USAGE = 161;
|
|
14
|
+
/**
|
|
15
|
+
* Maximum raw position value from the device.
|
|
16
|
+
* @remarks Used for converting raw position to degrees.
|
|
17
|
+
*/
|
|
18
|
+
declare const RAW_POSITION_MAX = 10000;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum raw torque value from the device.
|
|
21
|
+
* @remarks Used for normalizing torque to [-100, 100] range.
|
|
22
|
+
*/
|
|
23
|
+
declare const RAW_TORQUE_MAX = 10000;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/enums.d.ts
|
|
26
|
+
declare enum ExtensionMode {
|
|
27
|
+
None = 0,
|
|
28
|
+
Custom = 1,
|
|
29
|
+
}
|
|
30
|
+
declare enum PinMode {
|
|
31
|
+
None = 0,
|
|
32
|
+
Gpio = 1,
|
|
33
|
+
Analog = 2,
|
|
34
|
+
SpiCs = 3,
|
|
35
|
+
SpiSck = 4,
|
|
36
|
+
SpiMiso = 5,
|
|
37
|
+
EnableEffects = 6,
|
|
38
|
+
CenterReset = 7,
|
|
39
|
+
BrakingPwm = 8,
|
|
40
|
+
EffectLed = 9,
|
|
41
|
+
Reboot = 10,
|
|
42
|
+
}
|
|
43
|
+
declare enum ButtonMode {
|
|
44
|
+
None = 0,
|
|
45
|
+
Normal = 1,
|
|
46
|
+
Inverted = 2,
|
|
47
|
+
Pulse = 3,
|
|
48
|
+
PulseInverted = 4,
|
|
49
|
+
}
|
|
50
|
+
declare enum AmplifierGain {
|
|
51
|
+
Gain80 = 0,
|
|
52
|
+
Gain40 = 1,
|
|
53
|
+
Gain20 = 2,
|
|
54
|
+
Gain10 = 3,
|
|
55
|
+
}
|
|
56
|
+
declare enum SpiMode {
|
|
57
|
+
Mode0 = 0,
|
|
58
|
+
Mode1 = 1,
|
|
59
|
+
Mode2 = 2,
|
|
60
|
+
Mode3 = 3,
|
|
61
|
+
}
|
|
62
|
+
declare enum SpiLatchMode {
|
|
63
|
+
LatchUp = 0,
|
|
64
|
+
LatchDown = 1,
|
|
65
|
+
}
|
|
66
|
+
declare enum ReportData {
|
|
67
|
+
CommandReboot = 1,
|
|
68
|
+
CommandSaveSettings = 2,
|
|
69
|
+
CommandDfuMode = 3,
|
|
70
|
+
DataOverrideData = 16,
|
|
71
|
+
DataFirmwareActivationData = 19,
|
|
72
|
+
DataSettingsFieldData = 20,
|
|
73
|
+
CommandResetCenter = 4,
|
|
74
|
+
}
|
|
75
|
+
declare enum ReportType {
|
|
76
|
+
JoystickInput = 1,
|
|
77
|
+
CreateNewEffect = 17,
|
|
78
|
+
PIDBlockLoad = 18,
|
|
79
|
+
PIDPool = 19,
|
|
80
|
+
SetEffect = 17,
|
|
81
|
+
SetEnvelope = 18,
|
|
82
|
+
SetCondition = 19,
|
|
83
|
+
SetPeriodic = 20,
|
|
84
|
+
SetConstantForce = 21,
|
|
85
|
+
SetRampForce = 22,
|
|
86
|
+
EffectOperation = 26,
|
|
87
|
+
PIDState = 18,
|
|
88
|
+
PIDBlockFree = 27,
|
|
89
|
+
PIDDeviceControl = 28,
|
|
90
|
+
DeviceGain = 29,
|
|
91
|
+
HardwareSettingsFeature = 33,
|
|
92
|
+
EffectSettingsFeature = 34,
|
|
93
|
+
FirmwareLicenseFeature = 37,
|
|
94
|
+
GpioSettingsFeature = 161,
|
|
95
|
+
AdcSettingsFeature = 162,
|
|
96
|
+
GenericInputOutput = 163,
|
|
97
|
+
}
|
|
98
|
+
declare enum SettingField {
|
|
99
|
+
DirectXConstantDirection = 0,
|
|
100
|
+
DirectXSpringStrength = 1,
|
|
101
|
+
DirectXConstantStrength = 2,
|
|
102
|
+
DirectXPeriodicStrength = 3,
|
|
103
|
+
TotalEffectStrength = 4,
|
|
104
|
+
MotionRange = 5,
|
|
105
|
+
SoftStopStrength = 6,
|
|
106
|
+
SoftStopRange = 7,
|
|
107
|
+
StaticDampeningStrength = 8,
|
|
108
|
+
SoftStopDampeningStrength = 9,
|
|
109
|
+
ForceEnabled = 11,
|
|
110
|
+
DebugTorque = 12,
|
|
111
|
+
AmplifierGain = 13,
|
|
112
|
+
CalibrationMagnitude = 15,
|
|
113
|
+
CalibrationSpeed = 16,
|
|
114
|
+
PowerLimit = 17,
|
|
115
|
+
BrakingLimit = 18,
|
|
116
|
+
PositionSmoothing = 19,
|
|
117
|
+
SpeedBufferSize = 20,
|
|
118
|
+
EncoderDirection = 21,
|
|
119
|
+
ForceDirection = 22,
|
|
120
|
+
PolePairs = 23,
|
|
121
|
+
EncoderCPR = 24,
|
|
122
|
+
PGain = 25,
|
|
123
|
+
IGain = 26,
|
|
124
|
+
ExtensionMode = 27,
|
|
125
|
+
PinMode = 28,
|
|
126
|
+
ButtonMode = 29,
|
|
127
|
+
SpiMode = 30,
|
|
128
|
+
SpiLatchMode = 31,
|
|
129
|
+
SpiLatchDelay = 32,
|
|
130
|
+
SpiClkPulseLength = 33,
|
|
131
|
+
AdcMinDeadZone = 34,
|
|
132
|
+
AdcMaxDeadZone = 35,
|
|
133
|
+
AdcToButtonLow = 36,
|
|
134
|
+
AdcToButtonHigh = 37,
|
|
135
|
+
AdcSmoothing = 38,
|
|
136
|
+
AdcInvert = 39,
|
|
137
|
+
ResetCenterOnZ0 = 41,
|
|
138
|
+
IntegratedSpringStrength = 43,
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Binary field type identifiers for parsing HID reports.
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
declare enum FieldType {
|
|
145
|
+
Int8 = 0,
|
|
146
|
+
Uint8 = 1,
|
|
147
|
+
Int16 = 2,
|
|
148
|
+
Uint16 = 3,
|
|
149
|
+
Int32 = 4,
|
|
150
|
+
Uint32 = 5,
|
|
151
|
+
Float32 = 6,
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Maps {@link SettingField} values to their corresponding {@link FieldType}.
|
|
155
|
+
*
|
|
156
|
+
* @remarks
|
|
157
|
+
* This mapping is based on the struct definitions in the original C++ `wheel_api.h`.
|
|
158
|
+
*
|
|
159
|
+
* @internal
|
|
160
|
+
*/
|
|
161
|
+
declare const FIELD_TYPE_MAP: Readonly<Record<SettingField, FieldType>>;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/types.d.ts
|
|
164
|
+
interface FirmwareVersion {
|
|
165
|
+
releaseType: number;
|
|
166
|
+
/** Year of the release. */
|
|
167
|
+
major: number;
|
|
168
|
+
/** Minor version (incremented when companion app update is needed). */
|
|
169
|
+
minor: number;
|
|
170
|
+
/** Patch version (incremented on each patch within the same version). */
|
|
171
|
+
patch: number;
|
|
172
|
+
}
|
|
173
|
+
interface FirmwareLicense {
|
|
174
|
+
firmwareVersion: FirmwareVersion;
|
|
175
|
+
/** Serial key components (3 x 32-bit values). */
|
|
176
|
+
serialKey: number[];
|
|
177
|
+
/** Device ID components (3 x 32-bit values). */
|
|
178
|
+
deviceId: number[];
|
|
179
|
+
/** Registration status (0 = unregistered, 1 = registered). */
|
|
180
|
+
isRegistered: number;
|
|
181
|
+
}
|
|
182
|
+
interface EffectSettings {
|
|
183
|
+
/** Motion range in degrees. */
|
|
184
|
+
motionRange: number;
|
|
185
|
+
/** Static dampening strength (0 to 100%). */
|
|
186
|
+
staticDampeningStrength: number;
|
|
187
|
+
/** Soft stop dampening strength (0 to 100%). */
|
|
188
|
+
softStopDampeningStrength: number;
|
|
189
|
+
/** Total effect strength (0 to 100%). */
|
|
190
|
+
totalEffectStrength: number;
|
|
191
|
+
/** Integrated spring strength (0 to 100%). */
|
|
192
|
+
integratedSpringStrength: number;
|
|
193
|
+
/** Soft stop range in degrees (added on top of MotionRange). */
|
|
194
|
+
softStopRange: number;
|
|
195
|
+
/** Soft stop strength (0 to 100%). */
|
|
196
|
+
softStopStrength: number;
|
|
197
|
+
/** DirectX constant force direction (-1 or +1). */
|
|
198
|
+
directXConstantDirection: number;
|
|
199
|
+
/** DirectX spring strength (0 to 100%). */
|
|
200
|
+
directXSpringStrength: number;
|
|
201
|
+
/** DirectX constant strength (0 to 100%). */
|
|
202
|
+
directXConstantStrength: number;
|
|
203
|
+
/** DirectX periodic strength (0 to 100%). */
|
|
204
|
+
directXPeriodicStrength: number;
|
|
205
|
+
}
|
|
206
|
+
interface HardwareSettings {
|
|
207
|
+
/** Encoder counts per revolution (CPR). */
|
|
208
|
+
encoderCPR: number;
|
|
209
|
+
/** Integral gain (I-Gain) for PID. */
|
|
210
|
+
integralGain: number;
|
|
211
|
+
/** Proportional gain (P-Gain) for PID. */
|
|
212
|
+
proportionalGain: number;
|
|
213
|
+
/** Force feedback enabled (0 = Disabled, 1 = Enabled). */
|
|
214
|
+
forceEnabled: number;
|
|
215
|
+
/** Debug torque output enabled (0 = Disabled, 1 = Enabled). */
|
|
216
|
+
debugTorque: number;
|
|
217
|
+
/** Amplifier gain setting. @see {@link AmplifierGain} */
|
|
218
|
+
amplifierGain: number;
|
|
219
|
+
/** Calibration magnitude (0 to 100%). */
|
|
220
|
+
calibrationMagnitude: number;
|
|
221
|
+
/** Calibration speed (0 to 100%). */
|
|
222
|
+
calibrationSpeed: number;
|
|
223
|
+
/** Power limit (0 to 100%). */
|
|
224
|
+
powerLimit: number;
|
|
225
|
+
/** Braking limit (0 to 100%). */
|
|
226
|
+
brakingLimit: number;
|
|
227
|
+
/** Position smoothing (0 to 100%). */
|
|
228
|
+
positionSmoothing: number;
|
|
229
|
+
/** Speed buffer size. */
|
|
230
|
+
speedBufferSize: number;
|
|
231
|
+
/** Encoder direction multiplier (-1 or +1). */
|
|
232
|
+
encoderDirection: number;
|
|
233
|
+
/** Force direction multiplier (-1 or +1). */
|
|
234
|
+
forceDirection: number;
|
|
235
|
+
/** Number of motor pole pairs. */
|
|
236
|
+
polePairs: number;
|
|
237
|
+
}
|
|
238
|
+
interface AdcSettings {
|
|
239
|
+
/** Minimum raw values for the 3 analog axes. */
|
|
240
|
+
rAxisMin: number[];
|
|
241
|
+
/** Maximum raw values for the 3 analog axes. */
|
|
242
|
+
rAxisMax: number[];
|
|
243
|
+
/** Axis smoothing factor.
|
|
244
|
+
* @remarks Divide by 100 to get normalized ratio (0..1).
|
|
245
|
+
*/
|
|
246
|
+
rAxisSmoothing: number[];
|
|
247
|
+
/** Point in % where "Button Low" is triggered.
|
|
248
|
+
* @remarks 0 = Disabled.
|
|
249
|
+
*/
|
|
250
|
+
rAxisToButtonLow: number[];
|
|
251
|
+
/** Point in % where "Button High" is triggered.
|
|
252
|
+
* @remarks 100 = Disabled.
|
|
253
|
+
*/
|
|
254
|
+
rAxisToButtonHigh: number[];
|
|
255
|
+
/** Axis inversion flags (0 or 1). */
|
|
256
|
+
rAxisInvert: number[];
|
|
257
|
+
}
|
|
258
|
+
interface GpioSettings {
|
|
259
|
+
/** Extension mode. @see {@link ExtensionMode} */
|
|
260
|
+
extensionMode: number;
|
|
261
|
+
/** Pin mode configuration for 10 pins. @see {@link PinMode} */
|
|
262
|
+
pinMode: number[];
|
|
263
|
+
/** Button mode configuration for 32 buttons. @see {@link ButtonMode} */
|
|
264
|
+
buttonMode: number[];
|
|
265
|
+
/** SPI communication mode. @see {@link SpiMode} */
|
|
266
|
+
spiMode: number;
|
|
267
|
+
/** SPI latch mode. @see {@link SpiLatchMode} */
|
|
268
|
+
spiLatchMode: number;
|
|
269
|
+
/** SPI latch delay in microseconds. */
|
|
270
|
+
spiLatchDelay: number;
|
|
271
|
+
/** SPI clock pulse length in microseconds. */
|
|
272
|
+
spiClkPulseLength: number;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Direct control forces applied to the wheel.
|
|
276
|
+
* @remarks All force values are normalized range (-10000 to +10000).
|
|
277
|
+
*/
|
|
278
|
+
interface DirectControl {
|
|
279
|
+
/**
|
|
280
|
+
* Spring force acting opposite to wheel rotation.
|
|
281
|
+
* - Range: -10000 to +10000
|
|
282
|
+
* - Default: 0
|
|
283
|
+
*/
|
|
284
|
+
springForce: number;
|
|
285
|
+
/**
|
|
286
|
+
* Constant force moving the wheel in a specific direction.
|
|
287
|
+
* - Range: -10000 to +10000
|
|
288
|
+
* - Default: 0
|
|
289
|
+
*/
|
|
290
|
+
constantForce: number;
|
|
291
|
+
/**
|
|
292
|
+
* Periodic effect force (sine/triangle/etc). Not affected by dampening.
|
|
293
|
+
* - Range: -10000 to +10000
|
|
294
|
+
* - Default: 0
|
|
295
|
+
*/
|
|
296
|
+
periodicForce: number;
|
|
297
|
+
/**
|
|
298
|
+
* Global force scaling factor (inverse).
|
|
299
|
+
* - Formula: `TotalForce = InitialForce * (1 - ForceDrop / 100)`
|
|
300
|
+
* - Range: 0 to 100
|
|
301
|
+
* - Default: 0
|
|
302
|
+
*/
|
|
303
|
+
forceDrop: number;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Real-time device state received from the wheel controller.
|
|
307
|
+
*/
|
|
308
|
+
interface DeviceState {
|
|
309
|
+
firmwareVersion: FirmwareVersion;
|
|
310
|
+
/** Registration status (0 = unregistered, 1 = registered). */
|
|
311
|
+
isRegistered: number;
|
|
312
|
+
/** Raw wheel position value.
|
|
313
|
+
* - Range: -10000 to +10000 (roughly)
|
|
314
|
+
*/
|
|
315
|
+
position: number;
|
|
316
|
+
/** Raw torque value currently being output.
|
|
317
|
+
* - Range: -10000 to +10000
|
|
318
|
+
*/
|
|
319
|
+
torque: number;
|
|
320
|
+
/**
|
|
321
|
+
* Wheel position converted to degrees based on the active motion range.
|
|
322
|
+
* @remarks Computed property. Null if motion range is not yet cached.
|
|
323
|
+
*/
|
|
324
|
+
positionDegrees: number | null;
|
|
325
|
+
/**
|
|
326
|
+
* Current torque normalized to a percentage range (-100 to 100).
|
|
327
|
+
* @remarks Computed property. Positive = Right (CW), Negative = Left (CCW).
|
|
328
|
+
*/
|
|
329
|
+
torqueNormalized: number;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Aggregated settings object containing all configuration groups.
|
|
333
|
+
* @remarks This is a helper interface used by `readAllSettings()` and is not present in the original C++ API.
|
|
334
|
+
*/
|
|
335
|
+
interface DeviceSettings {
|
|
336
|
+
effects: EffectSettings;
|
|
337
|
+
hardware: HardwareSettings;
|
|
338
|
+
gpio: GpioSettings;
|
|
339
|
+
adc: AdcSettings;
|
|
340
|
+
}
|
|
341
|
+
/** Event handler signatures for {@link WheelApi} events. */
|
|
342
|
+
interface WheelEvents {
|
|
343
|
+
/** Emitted when a device is successfully connected. */
|
|
344
|
+
deviceConnected: (device: HIDDevice) => void;
|
|
345
|
+
/** Emitted when the device is disconnected. */
|
|
346
|
+
deviceDisconnected: (device: HIDDevice) => void;
|
|
347
|
+
/** Emitted when new device state is received. */
|
|
348
|
+
stateReceived: (state: DeviceState) => void;
|
|
349
|
+
/** Emitted when an error occurs during background processing (e.g. parsing data).
|
|
350
|
+
* @remarks Direct API calls (like sendSetting) reject their promise instead.
|
|
351
|
+
*/
|
|
352
|
+
error: (error: Error) => void;
|
|
353
|
+
}
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/utils.d.ts
|
|
356
|
+
/**
|
|
357
|
+
* Converts raw position value to degrees.
|
|
358
|
+
*
|
|
359
|
+
* @param rawPosition - The raw position value from the device.
|
|
360
|
+
* @param motionRange - The configured motion range in degrees.
|
|
361
|
+
* @returns The position in degrees.
|
|
362
|
+
*/
|
|
363
|
+
declare function convertPositionToDegrees(rawPosition: number, motionRange?: number): number;
|
|
364
|
+
/**
|
|
365
|
+
* Normalizes raw torque value to a [-100, 100] range.
|
|
366
|
+
*
|
|
367
|
+
* @param rawTorque - The raw torque value from the device.
|
|
368
|
+
* @returns The normalized torque value between -100 and 100.
|
|
369
|
+
*/
|
|
370
|
+
declare function normalizeTorque(rawTorque: number): number;
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/wheel-api.d.ts
|
|
373
|
+
/**
|
|
374
|
+
* WebHID-based API for communicating with the wheel controller.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```ts
|
|
378
|
+
* import { WheelApi } from "@shubham0x13/ffbeast-wheel-webhid-api";
|
|
379
|
+
*
|
|
380
|
+
* const wheel = new WheelApi();
|
|
381
|
+
*
|
|
382
|
+
* wheel.on("stateReceived", (state) => {
|
|
383
|
+
* console.log("Position:", state.position);
|
|
384
|
+
* });
|
|
385
|
+
*
|
|
386
|
+
* await wheel.connect();
|
|
387
|
+
*
|
|
388
|
+
* // ... later
|
|
389
|
+
*
|
|
390
|
+
* await wheel.disconnect();
|
|
391
|
+
* wheel.destroy();
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
declare class WheelApi extends EventEmitter<WheelEvents> {
|
|
395
|
+
/**
|
|
396
|
+
* Last received device state.
|
|
397
|
+
* @returns Device state, or `null` if no state received yet.
|
|
398
|
+
*/
|
|
399
|
+
get lastState(): Readonly<DeviceState> | null;
|
|
400
|
+
/**
|
|
401
|
+
* Current wheel position in degrees.
|
|
402
|
+
* @returns Position in degrees, or `null` if not connected.
|
|
403
|
+
*/
|
|
404
|
+
get positionDegrees(): number | null;
|
|
405
|
+
/**
|
|
406
|
+
* Current torque normalized to [-100, 100] range.
|
|
407
|
+
* @returns Normalized torque, or `null` if not connected.
|
|
408
|
+
*/
|
|
409
|
+
get torqueNormalized(): number | null;
|
|
410
|
+
/**
|
|
411
|
+
* Current firmware release type.
|
|
412
|
+
* @returns Release type number, or `null` if not connected.
|
|
413
|
+
*/
|
|
414
|
+
get firmwareReleaseType(): number | null;
|
|
415
|
+
/**
|
|
416
|
+
* Current firmware version string.
|
|
417
|
+
* @returns Version string (e.g., "1.2.3"), or `null` if not connected.
|
|
418
|
+
*/
|
|
419
|
+
get firmwareVersion(): string | null;
|
|
420
|
+
/**
|
|
421
|
+
* Device connection status.
|
|
422
|
+
* @returns `true` if connected and opened.
|
|
423
|
+
*/
|
|
424
|
+
get isConnected(): boolean;
|
|
425
|
+
private device;
|
|
426
|
+
private _lastState;
|
|
427
|
+
private cachedMotionRange;
|
|
428
|
+
constructor();
|
|
429
|
+
/**
|
|
430
|
+
* Checks if WebHID is supported in the current browser.
|
|
431
|
+
* @returns `true` if supported (Chrome/Edge/Opera).
|
|
432
|
+
*/
|
|
433
|
+
static isSupported(): boolean;
|
|
434
|
+
/**
|
|
435
|
+
* Opens the browser's device picker to connect to a wheel.
|
|
436
|
+
* @returns `true` if connection was successful.
|
|
437
|
+
*/
|
|
438
|
+
connect(): Promise<boolean>;
|
|
439
|
+
/**
|
|
440
|
+
* Attempts to reconnect to a previously authorized device.
|
|
441
|
+
* @returns `true` if a device was found and connected.
|
|
442
|
+
*/
|
|
443
|
+
tryAutoConnect(): Promise<boolean>;
|
|
444
|
+
/**
|
|
445
|
+
* Disconnects from the current device.
|
|
446
|
+
*/
|
|
447
|
+
disconnect(): Promise<void>;
|
|
448
|
+
/**
|
|
449
|
+
* Cleans up all resources and event listeners.
|
|
450
|
+
* Must be called when the API instance is no longer needed.
|
|
451
|
+
*/
|
|
452
|
+
destroy(): Promise<void>;
|
|
453
|
+
/**
|
|
454
|
+
* Reads the current effect settings from the device.
|
|
455
|
+
* @returns Effect settings object.
|
|
456
|
+
*/
|
|
457
|
+
readEffectSettings(): Promise<EffectSettings>;
|
|
458
|
+
/**
|
|
459
|
+
* Reads the current hardware settings from the device.
|
|
460
|
+
* @returns Hardware settings object.
|
|
461
|
+
*/
|
|
462
|
+
readHardwareSettings(): Promise<HardwareSettings>;
|
|
463
|
+
/**
|
|
464
|
+
* Reads the current GPIO extension settings from the device.
|
|
465
|
+
* @returns GPIO settings object.
|
|
466
|
+
*/
|
|
467
|
+
readGpioExtensionSettings(): Promise<GpioSettings>;
|
|
468
|
+
/**
|
|
469
|
+
* Reads the current ADC extension settings from the device.
|
|
470
|
+
* @returns ADC settings object.
|
|
471
|
+
*/
|
|
472
|
+
readAdcExtensionSettings(): Promise<AdcSettings>;
|
|
473
|
+
/**
|
|
474
|
+
* Reads the firmware license information from the device.
|
|
475
|
+
* @returns Firmware license object.
|
|
476
|
+
*/
|
|
477
|
+
readFirmwareLicense(): Promise<FirmwareLicense>;
|
|
478
|
+
/**
|
|
479
|
+
* Reads all settings from the device sequentially.
|
|
480
|
+
* @returns Object containing all settings groups.
|
|
481
|
+
*/
|
|
482
|
+
readAllSettings(): Promise<DeviceSettings>;
|
|
483
|
+
/**
|
|
484
|
+
* Writes a single setting value to the device.
|
|
485
|
+
* @param field - The setting field to write.
|
|
486
|
+
* @param index - Sub-index for array-based settings.
|
|
487
|
+
* @param value - The value to write.
|
|
488
|
+
*/
|
|
489
|
+
sendSetting(field: SettingField, index: number, value: number): Promise<void>;
|
|
490
|
+
/**
|
|
491
|
+
* Sends direct force feedback control to the device.
|
|
492
|
+
* @param control - Force feedback control values.
|
|
493
|
+
*/
|
|
494
|
+
sendDirectControl(control: DirectControl): Promise<void>;
|
|
495
|
+
/**
|
|
496
|
+
* Sends the firmware activation license key to the device.
|
|
497
|
+
* Expected format: "XXXXXXXX-XXXXXXXX-XXXXXXXX" (Hex strings)
|
|
498
|
+
* @param license - The license key string.
|
|
499
|
+
* @returns `true` if the format was valid and sent, `false` otherwise.
|
|
500
|
+
*/
|
|
501
|
+
sendFirmwareActivation(license: string): Promise<boolean>;
|
|
502
|
+
/**
|
|
503
|
+
* Saves current settings to flash and reboots the controller.
|
|
504
|
+
*/
|
|
505
|
+
saveAndReboot(): Promise<void>;
|
|
506
|
+
/**
|
|
507
|
+
* Reboots the controller without saving settings.
|
|
508
|
+
*/
|
|
509
|
+
rebootController(): Promise<void>;
|
|
510
|
+
/**
|
|
511
|
+
* Switches the device to DFU mode for firmware updates.
|
|
512
|
+
*/
|
|
513
|
+
switchToDfu(): Promise<void>;
|
|
514
|
+
/**
|
|
515
|
+
* Sets the current wheel position as center.
|
|
516
|
+
*/
|
|
517
|
+
resetWheelCenter(): Promise<void>;
|
|
518
|
+
private handleConnect;
|
|
519
|
+
private handleDisconnect;
|
|
520
|
+
private handleInputReport;
|
|
521
|
+
private openDevice;
|
|
522
|
+
private sendGenericCommand;
|
|
523
|
+
private getFeatureReport;
|
|
524
|
+
}
|
|
525
|
+
//#endregion
|
|
526
|
+
export { AdcSettings, AmplifierGain, ButtonMode, DeviceSettings, DeviceState, DirectControl, EffectSettings, ExtensionMode, FIELD_TYPE_MAP, FieldType, FirmwareLicense, FirmwareVersion, GpioSettings, HardwareSettings, INTERFACE_0_USAGE, INTERFACE_0_USAGE_PAGE, PID, PinMode, RAW_POSITION_MAX, RAW_TORQUE_MAX, REPORT_SIZE, ReportData, ReportType, SettingField, SpiLatchMode, SpiMode, VID, WheelApi, WheelEvents, convertPositionToDegrees, normalizeTorque };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
|
|
3
|
+
//#region src/constants.ts
|
|
4
|
+
/** USB Vendor ID for FFBeast devices. */
|
|
5
|
+
const VID = 1115;
|
|
6
|
+
/** USB Product ID for FFBeast wheel controllers. */
|
|
7
|
+
const PID = 22999;
|
|
8
|
+
/** Size of HID reports in bytes. */
|
|
9
|
+
const REPORT_SIZE = 64;
|
|
10
|
+
/** HID usage page for interface 0. */
|
|
11
|
+
const INTERFACE_0_USAGE_PAGE = 65394;
|
|
12
|
+
/** HID usage for interface 0. */
|
|
13
|
+
const INTERFACE_0_USAGE = 161;
|
|
14
|
+
/**
|
|
15
|
+
* Maximum raw position value from the device.
|
|
16
|
+
* @remarks Used for converting raw position to degrees.
|
|
17
|
+
*/
|
|
18
|
+
const RAW_POSITION_MAX = 1e4;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum raw torque value from the device.
|
|
21
|
+
* @remarks Used for normalizing torque to [-100, 100] range.
|
|
22
|
+
*/
|
|
23
|
+
const RAW_TORQUE_MAX = 1e4;
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/enums.ts
|
|
27
|
+
let ExtensionMode = /* @__PURE__ */ function(ExtensionMode$1) {
|
|
28
|
+
ExtensionMode$1[ExtensionMode$1["None"] = 0] = "None";
|
|
29
|
+
ExtensionMode$1[ExtensionMode$1["Custom"] = 1] = "Custom";
|
|
30
|
+
return ExtensionMode$1;
|
|
31
|
+
}({});
|
|
32
|
+
let PinMode = /* @__PURE__ */ function(PinMode$1) {
|
|
33
|
+
PinMode$1[PinMode$1["None"] = 0] = "None";
|
|
34
|
+
PinMode$1[PinMode$1["Gpio"] = 1] = "Gpio";
|
|
35
|
+
PinMode$1[PinMode$1["Analog"] = 2] = "Analog";
|
|
36
|
+
PinMode$1[PinMode$1["SpiCs"] = 3] = "SpiCs";
|
|
37
|
+
PinMode$1[PinMode$1["SpiSck"] = 4] = "SpiSck";
|
|
38
|
+
PinMode$1[PinMode$1["SpiMiso"] = 5] = "SpiMiso";
|
|
39
|
+
PinMode$1[PinMode$1["EnableEffects"] = 6] = "EnableEffects";
|
|
40
|
+
PinMode$1[PinMode$1["CenterReset"] = 7] = "CenterReset";
|
|
41
|
+
PinMode$1[PinMode$1["BrakingPwm"] = 8] = "BrakingPwm";
|
|
42
|
+
PinMode$1[PinMode$1["EffectLed"] = 9] = "EffectLed";
|
|
43
|
+
PinMode$1[PinMode$1["Reboot"] = 10] = "Reboot";
|
|
44
|
+
return PinMode$1;
|
|
45
|
+
}({});
|
|
46
|
+
let ButtonMode = /* @__PURE__ */ function(ButtonMode$1) {
|
|
47
|
+
ButtonMode$1[ButtonMode$1["None"] = 0] = "None";
|
|
48
|
+
ButtonMode$1[ButtonMode$1["Normal"] = 1] = "Normal";
|
|
49
|
+
ButtonMode$1[ButtonMode$1["Inverted"] = 2] = "Inverted";
|
|
50
|
+
ButtonMode$1[ButtonMode$1["Pulse"] = 3] = "Pulse";
|
|
51
|
+
ButtonMode$1[ButtonMode$1["PulseInverted"] = 4] = "PulseInverted";
|
|
52
|
+
return ButtonMode$1;
|
|
53
|
+
}({});
|
|
54
|
+
let AmplifierGain = /* @__PURE__ */ function(AmplifierGain$1) {
|
|
55
|
+
AmplifierGain$1[AmplifierGain$1["Gain80"] = 0] = "Gain80";
|
|
56
|
+
AmplifierGain$1[AmplifierGain$1["Gain40"] = 1] = "Gain40";
|
|
57
|
+
AmplifierGain$1[AmplifierGain$1["Gain20"] = 2] = "Gain20";
|
|
58
|
+
AmplifierGain$1[AmplifierGain$1["Gain10"] = 3] = "Gain10";
|
|
59
|
+
return AmplifierGain$1;
|
|
60
|
+
}({});
|
|
61
|
+
let SpiMode = /* @__PURE__ */ function(SpiMode$1) {
|
|
62
|
+
SpiMode$1[SpiMode$1["Mode0"] = 0] = "Mode0";
|
|
63
|
+
SpiMode$1[SpiMode$1["Mode1"] = 1] = "Mode1";
|
|
64
|
+
SpiMode$1[SpiMode$1["Mode2"] = 2] = "Mode2";
|
|
65
|
+
SpiMode$1[SpiMode$1["Mode3"] = 3] = "Mode3";
|
|
66
|
+
return SpiMode$1;
|
|
67
|
+
}({});
|
|
68
|
+
let SpiLatchMode = /* @__PURE__ */ function(SpiLatchMode$1) {
|
|
69
|
+
SpiLatchMode$1[SpiLatchMode$1["LatchUp"] = 0] = "LatchUp";
|
|
70
|
+
SpiLatchMode$1[SpiLatchMode$1["LatchDown"] = 1] = "LatchDown";
|
|
71
|
+
return SpiLatchMode$1;
|
|
72
|
+
}({});
|
|
73
|
+
let ReportData = /* @__PURE__ */ function(ReportData$1) {
|
|
74
|
+
ReportData$1[ReportData$1["CommandReboot"] = 1] = "CommandReboot";
|
|
75
|
+
ReportData$1[ReportData$1["CommandSaveSettings"] = 2] = "CommandSaveSettings";
|
|
76
|
+
ReportData$1[ReportData$1["CommandDfuMode"] = 3] = "CommandDfuMode";
|
|
77
|
+
ReportData$1[ReportData$1["DataOverrideData"] = 16] = "DataOverrideData";
|
|
78
|
+
ReportData$1[ReportData$1["DataFirmwareActivationData"] = 19] = "DataFirmwareActivationData";
|
|
79
|
+
ReportData$1[ReportData$1["DataSettingsFieldData"] = 20] = "DataSettingsFieldData";
|
|
80
|
+
ReportData$1[ReportData$1["CommandResetCenter"] = 4] = "CommandResetCenter";
|
|
81
|
+
return ReportData$1;
|
|
82
|
+
}({});
|
|
83
|
+
let ReportType = /* @__PURE__ */ function(ReportType$1) {
|
|
84
|
+
ReportType$1[ReportType$1["JoystickInput"] = 1] = "JoystickInput";
|
|
85
|
+
ReportType$1[ReportType$1["CreateNewEffect"] = 17] = "CreateNewEffect";
|
|
86
|
+
ReportType$1[ReportType$1["PIDBlockLoad"] = 18] = "PIDBlockLoad";
|
|
87
|
+
ReportType$1[ReportType$1["PIDPool"] = 19] = "PIDPool";
|
|
88
|
+
ReportType$1[ReportType$1["SetEffect"] = 17] = "SetEffect";
|
|
89
|
+
ReportType$1[ReportType$1["SetEnvelope"] = 18] = "SetEnvelope";
|
|
90
|
+
ReportType$1[ReportType$1["SetCondition"] = 19] = "SetCondition";
|
|
91
|
+
ReportType$1[ReportType$1["SetPeriodic"] = 20] = "SetPeriodic";
|
|
92
|
+
ReportType$1[ReportType$1["SetConstantForce"] = 21] = "SetConstantForce";
|
|
93
|
+
ReportType$1[ReportType$1["SetRampForce"] = 22] = "SetRampForce";
|
|
94
|
+
ReportType$1[ReportType$1["EffectOperation"] = 26] = "EffectOperation";
|
|
95
|
+
ReportType$1[ReportType$1["PIDState"] = 18] = "PIDState";
|
|
96
|
+
ReportType$1[ReportType$1["PIDBlockFree"] = 27] = "PIDBlockFree";
|
|
97
|
+
ReportType$1[ReportType$1["PIDDeviceControl"] = 28] = "PIDDeviceControl";
|
|
98
|
+
ReportType$1[ReportType$1["DeviceGain"] = 29] = "DeviceGain";
|
|
99
|
+
ReportType$1[ReportType$1["HardwareSettingsFeature"] = 33] = "HardwareSettingsFeature";
|
|
100
|
+
ReportType$1[ReportType$1["EffectSettingsFeature"] = 34] = "EffectSettingsFeature";
|
|
101
|
+
ReportType$1[ReportType$1["FirmwareLicenseFeature"] = 37] = "FirmwareLicenseFeature";
|
|
102
|
+
ReportType$1[ReportType$1["GpioSettingsFeature"] = 161] = "GpioSettingsFeature";
|
|
103
|
+
ReportType$1[ReportType$1["AdcSettingsFeature"] = 162] = "AdcSettingsFeature";
|
|
104
|
+
ReportType$1[ReportType$1["GenericInputOutput"] = 163] = "GenericInputOutput";
|
|
105
|
+
return ReportType$1;
|
|
106
|
+
}({});
|
|
107
|
+
let SettingField = /* @__PURE__ */ function(SettingField$1) {
|
|
108
|
+
SettingField$1[SettingField$1["DirectXConstantDirection"] = 0] = "DirectXConstantDirection";
|
|
109
|
+
SettingField$1[SettingField$1["DirectXSpringStrength"] = 1] = "DirectXSpringStrength";
|
|
110
|
+
SettingField$1[SettingField$1["DirectXConstantStrength"] = 2] = "DirectXConstantStrength";
|
|
111
|
+
SettingField$1[SettingField$1["DirectXPeriodicStrength"] = 3] = "DirectXPeriodicStrength";
|
|
112
|
+
SettingField$1[SettingField$1["TotalEffectStrength"] = 4] = "TotalEffectStrength";
|
|
113
|
+
SettingField$1[SettingField$1["MotionRange"] = 5] = "MotionRange";
|
|
114
|
+
SettingField$1[SettingField$1["SoftStopStrength"] = 6] = "SoftStopStrength";
|
|
115
|
+
SettingField$1[SettingField$1["SoftStopRange"] = 7] = "SoftStopRange";
|
|
116
|
+
SettingField$1[SettingField$1["StaticDampeningStrength"] = 8] = "StaticDampeningStrength";
|
|
117
|
+
SettingField$1[SettingField$1["SoftStopDampeningStrength"] = 9] = "SoftStopDampeningStrength";
|
|
118
|
+
SettingField$1[SettingField$1["ForceEnabled"] = 11] = "ForceEnabled";
|
|
119
|
+
SettingField$1[SettingField$1["DebugTorque"] = 12] = "DebugTorque";
|
|
120
|
+
SettingField$1[SettingField$1["AmplifierGain"] = 13] = "AmplifierGain";
|
|
121
|
+
SettingField$1[SettingField$1["CalibrationMagnitude"] = 15] = "CalibrationMagnitude";
|
|
122
|
+
SettingField$1[SettingField$1["CalibrationSpeed"] = 16] = "CalibrationSpeed";
|
|
123
|
+
SettingField$1[SettingField$1["PowerLimit"] = 17] = "PowerLimit";
|
|
124
|
+
SettingField$1[SettingField$1["BrakingLimit"] = 18] = "BrakingLimit";
|
|
125
|
+
SettingField$1[SettingField$1["PositionSmoothing"] = 19] = "PositionSmoothing";
|
|
126
|
+
SettingField$1[SettingField$1["SpeedBufferSize"] = 20] = "SpeedBufferSize";
|
|
127
|
+
SettingField$1[SettingField$1["EncoderDirection"] = 21] = "EncoderDirection";
|
|
128
|
+
SettingField$1[SettingField$1["ForceDirection"] = 22] = "ForceDirection";
|
|
129
|
+
SettingField$1[SettingField$1["PolePairs"] = 23] = "PolePairs";
|
|
130
|
+
SettingField$1[SettingField$1["EncoderCPR"] = 24] = "EncoderCPR";
|
|
131
|
+
SettingField$1[SettingField$1["PGain"] = 25] = "PGain";
|
|
132
|
+
SettingField$1[SettingField$1["IGain"] = 26] = "IGain";
|
|
133
|
+
SettingField$1[SettingField$1["ExtensionMode"] = 27] = "ExtensionMode";
|
|
134
|
+
SettingField$1[SettingField$1["PinMode"] = 28] = "PinMode";
|
|
135
|
+
SettingField$1[SettingField$1["ButtonMode"] = 29] = "ButtonMode";
|
|
136
|
+
SettingField$1[SettingField$1["SpiMode"] = 30] = "SpiMode";
|
|
137
|
+
SettingField$1[SettingField$1["SpiLatchMode"] = 31] = "SpiLatchMode";
|
|
138
|
+
SettingField$1[SettingField$1["SpiLatchDelay"] = 32] = "SpiLatchDelay";
|
|
139
|
+
SettingField$1[SettingField$1["SpiClkPulseLength"] = 33] = "SpiClkPulseLength";
|
|
140
|
+
SettingField$1[SettingField$1["AdcMinDeadZone"] = 34] = "AdcMinDeadZone";
|
|
141
|
+
SettingField$1[SettingField$1["AdcMaxDeadZone"] = 35] = "AdcMaxDeadZone";
|
|
142
|
+
SettingField$1[SettingField$1["AdcToButtonLow"] = 36] = "AdcToButtonLow";
|
|
143
|
+
SettingField$1[SettingField$1["AdcToButtonHigh"] = 37] = "AdcToButtonHigh";
|
|
144
|
+
SettingField$1[SettingField$1["AdcSmoothing"] = 38] = "AdcSmoothing";
|
|
145
|
+
SettingField$1[SettingField$1["AdcInvert"] = 39] = "AdcInvert";
|
|
146
|
+
SettingField$1[SettingField$1["ResetCenterOnZ0"] = 41] = "ResetCenterOnZ0";
|
|
147
|
+
SettingField$1[SettingField$1["IntegratedSpringStrength"] = 43] = "IntegratedSpringStrength";
|
|
148
|
+
return SettingField$1;
|
|
149
|
+
}({});
|
|
150
|
+
/**
|
|
151
|
+
* Binary field type identifiers for parsing HID reports.
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
let FieldType = /* @__PURE__ */ function(FieldType$1) {
|
|
155
|
+
FieldType$1[FieldType$1["Int8"] = 0] = "Int8";
|
|
156
|
+
FieldType$1[FieldType$1["Uint8"] = 1] = "Uint8";
|
|
157
|
+
FieldType$1[FieldType$1["Int16"] = 2] = "Int16";
|
|
158
|
+
FieldType$1[FieldType$1["Uint16"] = 3] = "Uint16";
|
|
159
|
+
FieldType$1[FieldType$1["Int32"] = 4] = "Int32";
|
|
160
|
+
FieldType$1[FieldType$1["Uint32"] = 5] = "Uint32";
|
|
161
|
+
FieldType$1[FieldType$1["Float32"] = 6] = "Float32";
|
|
162
|
+
return FieldType$1;
|
|
163
|
+
}({});
|
|
164
|
+
/**
|
|
165
|
+
* Maps {@link SettingField} values to their corresponding {@link FieldType}.
|
|
166
|
+
*
|
|
167
|
+
* @remarks
|
|
168
|
+
* This mapping is based on the struct definitions in the original C++ `wheel_api.h`.
|
|
169
|
+
*
|
|
170
|
+
* @internal
|
|
171
|
+
*/
|
|
172
|
+
const FIELD_TYPE_MAP = {
|
|
173
|
+
[SettingField.MotionRange]: FieldType.Uint16,
|
|
174
|
+
[SettingField.StaticDampeningStrength]: FieldType.Uint16,
|
|
175
|
+
[SettingField.SoftStopDampeningStrength]: FieldType.Uint16,
|
|
176
|
+
[SettingField.TotalEffectStrength]: FieldType.Uint8,
|
|
177
|
+
[SettingField.IntegratedSpringStrength]: FieldType.Uint8,
|
|
178
|
+
[SettingField.SoftStopRange]: FieldType.Uint8,
|
|
179
|
+
[SettingField.SoftStopStrength]: FieldType.Uint8,
|
|
180
|
+
[SettingField.DirectXConstantDirection]: FieldType.Int8,
|
|
181
|
+
[SettingField.DirectXSpringStrength]: FieldType.Uint8,
|
|
182
|
+
[SettingField.DirectXConstantStrength]: FieldType.Uint8,
|
|
183
|
+
[SettingField.DirectXPeriodicStrength]: FieldType.Uint8,
|
|
184
|
+
[SettingField.EncoderCPR]: FieldType.Uint16,
|
|
185
|
+
[SettingField.IGain]: FieldType.Uint16,
|
|
186
|
+
[SettingField.PGain]: FieldType.Uint8,
|
|
187
|
+
[SettingField.ForceEnabled]: FieldType.Uint8,
|
|
188
|
+
[SettingField.DebugTorque]: FieldType.Uint8,
|
|
189
|
+
[SettingField.AmplifierGain]: FieldType.Uint8,
|
|
190
|
+
[SettingField.CalibrationMagnitude]: FieldType.Uint8,
|
|
191
|
+
[SettingField.CalibrationSpeed]: FieldType.Uint8,
|
|
192
|
+
[SettingField.PowerLimit]: FieldType.Uint8,
|
|
193
|
+
[SettingField.BrakingLimit]: FieldType.Uint8,
|
|
194
|
+
[SettingField.PositionSmoothing]: FieldType.Uint8,
|
|
195
|
+
[SettingField.SpeedBufferSize]: FieldType.Uint8,
|
|
196
|
+
[SettingField.EncoderDirection]: FieldType.Int8,
|
|
197
|
+
[SettingField.ForceDirection]: FieldType.Int8,
|
|
198
|
+
[SettingField.PolePairs]: FieldType.Uint8,
|
|
199
|
+
[SettingField.ExtensionMode]: FieldType.Uint8,
|
|
200
|
+
[SettingField.PinMode]: FieldType.Uint8,
|
|
201
|
+
[SettingField.ButtonMode]: FieldType.Uint8,
|
|
202
|
+
[SettingField.SpiMode]: FieldType.Uint8,
|
|
203
|
+
[SettingField.SpiLatchMode]: FieldType.Uint8,
|
|
204
|
+
[SettingField.SpiLatchDelay]: FieldType.Uint8,
|
|
205
|
+
[SettingField.SpiClkPulseLength]: FieldType.Uint8,
|
|
206
|
+
[SettingField.AdcSmoothing]: FieldType.Uint8,
|
|
207
|
+
[SettingField.AdcToButtonLow]: FieldType.Uint8,
|
|
208
|
+
[SettingField.AdcToButtonHigh]: FieldType.Uint8,
|
|
209
|
+
[SettingField.AdcInvert]: FieldType.Uint8,
|
|
210
|
+
[SettingField.AdcMinDeadZone]: FieldType.Uint16,
|
|
211
|
+
[SettingField.AdcMaxDeadZone]: FieldType.Uint16,
|
|
212
|
+
[SettingField.ResetCenterOnZ0]: FieldType.Uint8
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
//#endregion
|
|
216
|
+
//#region src/utils.ts
|
|
217
|
+
/**
|
|
218
|
+
* Helper to read packed C-structs sequentially.
|
|
219
|
+
* Eliminates the need to manually track byte offsets.
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
var StructReader = class {
|
|
223
|
+
constructor(view, offset = 0) {
|
|
224
|
+
this.view = view;
|
|
225
|
+
this.offset = offset;
|
|
226
|
+
}
|
|
227
|
+
u8() {
|
|
228
|
+
return this.view.getUint8(this.offset++);
|
|
229
|
+
}
|
|
230
|
+
i8() {
|
|
231
|
+
return this.view.getInt8(this.offset++);
|
|
232
|
+
}
|
|
233
|
+
u16() {
|
|
234
|
+
const val = this.view.getUint16(this.offset, true);
|
|
235
|
+
this.offset += 2;
|
|
236
|
+
return val;
|
|
237
|
+
}
|
|
238
|
+
i16() {
|
|
239
|
+
const val = this.view.getInt16(this.offset, true);
|
|
240
|
+
this.offset += 2;
|
|
241
|
+
return val;
|
|
242
|
+
}
|
|
243
|
+
u32() {
|
|
244
|
+
const val = this.view.getUint32(this.offset, true);
|
|
245
|
+
this.offset += 4;
|
|
246
|
+
return val;
|
|
247
|
+
}
|
|
248
|
+
array(len, fn) {
|
|
249
|
+
return Array.from({ length: len }, fn);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Converts raw position value to degrees.
|
|
254
|
+
*
|
|
255
|
+
* @param rawPosition - The raw position value from the device.
|
|
256
|
+
* @param motionRange - The configured motion range in degrees.
|
|
257
|
+
* @returns The position in degrees.
|
|
258
|
+
*/
|
|
259
|
+
function convertPositionToDegrees(rawPosition, motionRange = 900) {
|
|
260
|
+
return rawPosition * motionRange / (2 * RAW_POSITION_MAX);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Normalizes raw torque value to a [-100, 100] range.
|
|
264
|
+
*
|
|
265
|
+
* @param rawTorque - The raw torque value from the device.
|
|
266
|
+
* @returns The normalized torque value between -100 and 100.
|
|
267
|
+
*/
|
|
268
|
+
function normalizeTorque(rawTorque) {
|
|
269
|
+
return rawTorque * 100 / RAW_TORQUE_MAX;
|
|
270
|
+
}
|
|
271
|
+
function isTargetDevice(device) {
|
|
272
|
+
return device.vendorId === VID && device.productId === PID && device.collections.some((c) => c.usagePage === INTERFACE_0_USAGE_PAGE && c.usage === INTERFACE_0_USAGE);
|
|
273
|
+
}
|
|
274
|
+
function clamp(val, min, max) {
|
|
275
|
+
return Math.max(min, Math.min(val, max));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/wheel-api.ts
|
|
280
|
+
/**
|
|
281
|
+
* WebHID-based API for communicating with the wheel controller.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* import { WheelApi } from "@shubham0x13/ffbeast-wheel-webhid-api";
|
|
286
|
+
*
|
|
287
|
+
* const wheel = new WheelApi();
|
|
288
|
+
*
|
|
289
|
+
* wheel.on("stateReceived", (state) => {
|
|
290
|
+
* console.log("Position:", state.position);
|
|
291
|
+
* });
|
|
292
|
+
*
|
|
293
|
+
* await wheel.connect();
|
|
294
|
+
*
|
|
295
|
+
* // ... later
|
|
296
|
+
*
|
|
297
|
+
* await wheel.disconnect();
|
|
298
|
+
* wheel.destroy();
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
var WheelApi = class WheelApi extends EventEmitter {
|
|
302
|
+
/**
|
|
303
|
+
* Last received device state.
|
|
304
|
+
* @returns Device state, or `null` if no state received yet.
|
|
305
|
+
*/
|
|
306
|
+
get lastState() {
|
|
307
|
+
return this._lastState ? { ...this._lastState } : null;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Current wheel position in degrees.
|
|
311
|
+
* @returns Position in degrees, or `null` if not connected.
|
|
312
|
+
*/
|
|
313
|
+
get positionDegrees() {
|
|
314
|
+
if (!this.lastState || this.cachedMotionRange === null) return null;
|
|
315
|
+
return convertPositionToDegrees(this.lastState.position, this.cachedMotionRange);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Current torque normalized to [-100, 100] range.
|
|
319
|
+
* @returns Normalized torque, or `null` if not connected.
|
|
320
|
+
*/
|
|
321
|
+
get torqueNormalized() {
|
|
322
|
+
if (!this.lastState) return null;
|
|
323
|
+
return normalizeTorque(this.lastState.torque);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Current firmware release type.
|
|
327
|
+
* @returns Release type number, or `null` if not connected.
|
|
328
|
+
*/
|
|
329
|
+
get firmwareReleaseType() {
|
|
330
|
+
if (!this.lastState) return null;
|
|
331
|
+
return this.lastState.firmwareVersion.releaseType;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Current firmware version string.
|
|
335
|
+
* @returns Version string (e.g., "1.2.3"), or `null` if not connected.
|
|
336
|
+
*/
|
|
337
|
+
get firmwareVersion() {
|
|
338
|
+
if (!this.lastState) return null;
|
|
339
|
+
const v = this.lastState.firmwareVersion;
|
|
340
|
+
return `${v.major}.${v.minor}.${v.patch}`;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Device connection status.
|
|
344
|
+
* @returns `true` if connected and opened.
|
|
345
|
+
*/
|
|
346
|
+
get isConnected() {
|
|
347
|
+
return this.device?.opened ?? false;
|
|
348
|
+
}
|
|
349
|
+
device = null;
|
|
350
|
+
_lastState = null;
|
|
351
|
+
cachedMotionRange = null;
|
|
352
|
+
constructor() {
|
|
353
|
+
super();
|
|
354
|
+
if (!WheelApi.isSupported()) throw new Error("WebHID is not supported in this browser.");
|
|
355
|
+
navigator.hid.addEventListener("connect", this.handleConnect);
|
|
356
|
+
navigator.hid.addEventListener("disconnect", this.handleDisconnect);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Checks if WebHID is supported in the current browser.
|
|
360
|
+
* @returns `true` if supported (Chrome/Edge/Opera).
|
|
361
|
+
*/
|
|
362
|
+
static isSupported() {
|
|
363
|
+
return "hid" in navigator;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Opens the browser's device picker to connect to a wheel.
|
|
367
|
+
* @returns `true` if connection was successful.
|
|
368
|
+
*/
|
|
369
|
+
async connect() {
|
|
370
|
+
const devices = await navigator.hid.requestDevice({ filters: [{
|
|
371
|
+
vendorId: VID,
|
|
372
|
+
productId: PID,
|
|
373
|
+
usagePage: INTERFACE_0_USAGE_PAGE,
|
|
374
|
+
usage: INTERFACE_0_USAGE
|
|
375
|
+
}] });
|
|
376
|
+
if (devices.length === 0) return false;
|
|
377
|
+
return await this.openDevice(devices[0]);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Attempts to reconnect to a previously authorized device.
|
|
381
|
+
* @returns `true` if a device was found and connected.
|
|
382
|
+
*/
|
|
383
|
+
async tryAutoConnect() {
|
|
384
|
+
const validDevice = (await navigator.hid.getDevices()).find((d) => isTargetDevice(d));
|
|
385
|
+
if (validDevice) return await this.openDevice(validDevice);
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Disconnects from the current device.
|
|
390
|
+
*/
|
|
391
|
+
async disconnect() {
|
|
392
|
+
if (!this.device) return;
|
|
393
|
+
const deviceToDisconnect = this.device;
|
|
394
|
+
try {
|
|
395
|
+
this.device.removeEventListener("inputreport", this.handleInputReport);
|
|
396
|
+
await this.device.close();
|
|
397
|
+
} catch {} finally {
|
|
398
|
+
this.device = null;
|
|
399
|
+
this._lastState = null;
|
|
400
|
+
this.cachedMotionRange = null;
|
|
401
|
+
this.emit("deviceDisconnected", deviceToDisconnect);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Cleans up all resources and event listeners.
|
|
406
|
+
* Must be called when the API instance is no longer needed.
|
|
407
|
+
*/
|
|
408
|
+
async destroy() {
|
|
409
|
+
await this.disconnect();
|
|
410
|
+
navigator.hid.removeEventListener("connect", this.handleConnect);
|
|
411
|
+
navigator.hid.removeEventListener("disconnect", this.handleDisconnect);
|
|
412
|
+
this.removeAllListeners();
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Reads the current effect settings from the device.
|
|
416
|
+
* @returns Effect settings object.
|
|
417
|
+
*/
|
|
418
|
+
async readEffectSettings() {
|
|
419
|
+
const r = new StructReader(await this.getFeatureReport(ReportType.EffectSettingsFeature));
|
|
420
|
+
return {
|
|
421
|
+
motionRange: r.u16(),
|
|
422
|
+
staticDampeningStrength: r.u16(),
|
|
423
|
+
softStopDampeningStrength: r.u16(),
|
|
424
|
+
totalEffectStrength: r.u8(),
|
|
425
|
+
integratedSpringStrength: r.u8(),
|
|
426
|
+
softStopRange: r.u8(),
|
|
427
|
+
softStopStrength: r.u8(),
|
|
428
|
+
directXConstantDirection: r.i8(),
|
|
429
|
+
directXSpringStrength: r.u8(),
|
|
430
|
+
directXConstantStrength: r.u8(),
|
|
431
|
+
directXPeriodicStrength: r.u8()
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Reads the current hardware settings from the device.
|
|
436
|
+
* @returns Hardware settings object.
|
|
437
|
+
*/
|
|
438
|
+
async readHardwareSettings() {
|
|
439
|
+
const r = new StructReader(await this.getFeatureReport(ReportType.HardwareSettingsFeature));
|
|
440
|
+
return {
|
|
441
|
+
encoderCPR: r.u16(),
|
|
442
|
+
integralGain: r.u16(),
|
|
443
|
+
proportionalGain: r.u8(),
|
|
444
|
+
forceEnabled: r.u8(),
|
|
445
|
+
debugTorque: r.u8(),
|
|
446
|
+
amplifierGain: r.u8(),
|
|
447
|
+
calibrationMagnitude: r.u8(),
|
|
448
|
+
calibrationSpeed: r.u8(),
|
|
449
|
+
powerLimit: r.u8(),
|
|
450
|
+
brakingLimit: r.u8(),
|
|
451
|
+
positionSmoothing: r.u8(),
|
|
452
|
+
speedBufferSize: r.u8(),
|
|
453
|
+
encoderDirection: r.i8(),
|
|
454
|
+
forceDirection: r.i8(),
|
|
455
|
+
polePairs: r.u8()
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Reads the current GPIO extension settings from the device.
|
|
460
|
+
* @returns GPIO settings object.
|
|
461
|
+
*/
|
|
462
|
+
async readGpioExtensionSettings() {
|
|
463
|
+
const r = new StructReader(await this.getFeatureReport(ReportType.GpioSettingsFeature));
|
|
464
|
+
return {
|
|
465
|
+
extensionMode: r.u8(),
|
|
466
|
+
pinMode: r.array(10, () => r.u8()),
|
|
467
|
+
buttonMode: r.array(32, () => r.u8()),
|
|
468
|
+
spiMode: r.u8(),
|
|
469
|
+
spiLatchMode: r.u8(),
|
|
470
|
+
spiLatchDelay: r.u8(),
|
|
471
|
+
spiClkPulseLength: r.u8()
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Reads the current ADC extension settings from the device.
|
|
476
|
+
* @returns ADC settings object.
|
|
477
|
+
*/
|
|
478
|
+
async readAdcExtensionSettings() {
|
|
479
|
+
const r = new StructReader(await this.getFeatureReport(ReportType.AdcSettingsFeature));
|
|
480
|
+
return {
|
|
481
|
+
rAxisMin: r.array(3, () => r.u16()),
|
|
482
|
+
rAxisMax: r.array(3, () => r.u16()),
|
|
483
|
+
rAxisSmoothing: r.array(3, () => r.u8()),
|
|
484
|
+
rAxisToButtonLow: r.array(3, () => r.u8()),
|
|
485
|
+
rAxisToButtonHigh: r.array(3, () => r.u8()),
|
|
486
|
+
rAxisInvert: r.array(3, () => r.u8())
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Reads the firmware license information from the device.
|
|
491
|
+
* @returns Firmware license object.
|
|
492
|
+
*/
|
|
493
|
+
async readFirmwareLicense() {
|
|
494
|
+
const r = new StructReader(await this.getFeatureReport(ReportType.FirmwareLicenseFeature));
|
|
495
|
+
return {
|
|
496
|
+
firmwareVersion: {
|
|
497
|
+
releaseType: r.u8(),
|
|
498
|
+
major: r.u8(),
|
|
499
|
+
minor: r.u8(),
|
|
500
|
+
patch: r.u8()
|
|
501
|
+
},
|
|
502
|
+
serialKey: r.array(3, () => r.u32()),
|
|
503
|
+
deviceId: r.array(3, () => r.u32()),
|
|
504
|
+
isRegistered: r.u8()
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Reads all settings from the device sequentially.
|
|
509
|
+
* @returns Object containing all settings groups.
|
|
510
|
+
*/
|
|
511
|
+
async readAllSettings() {
|
|
512
|
+
return {
|
|
513
|
+
effects: await this.readEffectSettings(),
|
|
514
|
+
hardware: await this.readHardwareSettings(),
|
|
515
|
+
gpio: await this.readGpioExtensionSettings(),
|
|
516
|
+
adc: await this.readAdcExtensionSettings()
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Writes a single setting value to the device.
|
|
521
|
+
* @param field - The setting field to write.
|
|
522
|
+
* @param index - Sub-index for array-based settings.
|
|
523
|
+
* @param value - The value to write.
|
|
524
|
+
*/
|
|
525
|
+
async sendSetting(field, index, value) {
|
|
526
|
+
if (!this.device?.opened) throw new Error("Device is not connected");
|
|
527
|
+
const type = FIELD_TYPE_MAP[field];
|
|
528
|
+
const buffer = new ArrayBuffer(REPORT_SIZE);
|
|
529
|
+
const view = new DataView(buffer);
|
|
530
|
+
view.setUint8(0, ReportData.DataSettingsFieldData);
|
|
531
|
+
view.setUint8(1, field);
|
|
532
|
+
view.setUint8(2, index);
|
|
533
|
+
const VAL_OFFSET = 3;
|
|
534
|
+
switch (type) {
|
|
535
|
+
case FieldType.Int8:
|
|
536
|
+
view.setInt8(VAL_OFFSET, clamp(value, -128, 127));
|
|
537
|
+
break;
|
|
538
|
+
case FieldType.Uint8:
|
|
539
|
+
view.setUint8(VAL_OFFSET, clamp(value, 0, 255));
|
|
540
|
+
break;
|
|
541
|
+
case FieldType.Int16:
|
|
542
|
+
view.setInt16(VAL_OFFSET, clamp(value, -32768, 32767), true);
|
|
543
|
+
break;
|
|
544
|
+
case FieldType.Uint16:
|
|
545
|
+
view.setUint16(VAL_OFFSET, clamp(value, 0, 65535), true);
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
await this.device.sendReport(ReportType.GenericInputOutput, new Uint8Array(buffer));
|
|
549
|
+
if (field === SettingField.MotionRange) this.cachedMotionRange = value;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Sends direct force feedback control to the device.
|
|
553
|
+
* @param control - Force feedback control values.
|
|
554
|
+
*/
|
|
555
|
+
async sendDirectControl(control) {
|
|
556
|
+
if (!this.device?.opened) throw new Error("Device is not connected");
|
|
557
|
+
const buffer = new ArrayBuffer(REPORT_SIZE);
|
|
558
|
+
const view = new DataView(buffer);
|
|
559
|
+
view.setUint8(0, ReportData.DataOverrideData);
|
|
560
|
+
view.setInt16(1, clamp(control.springForce, -1e4, 1e4), true);
|
|
561
|
+
view.setInt16(3, clamp(control.constantForce, -1e4, 1e4), true);
|
|
562
|
+
view.setInt16(5, clamp(control.periodicForce, -1e4, 1e4), true);
|
|
563
|
+
view.setUint8(7, clamp(control.forceDrop, 0, 100));
|
|
564
|
+
await this.device.sendReport(ReportType.GenericInputOutput, new Uint8Array(buffer));
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Sends the firmware activation license key to the device.
|
|
568
|
+
* Expected format: "XXXXXXXX-XXXXXXXX-XXXXXXXX" (Hex strings)
|
|
569
|
+
* @param license - The license key string.
|
|
570
|
+
* @returns `true` if the format was valid and sent, `false` otherwise.
|
|
571
|
+
*/
|
|
572
|
+
async sendFirmwareActivation(license) {
|
|
573
|
+
if (!this.device?.opened) throw new Error("Device is not connected");
|
|
574
|
+
if (this._lastState?.isRegistered === 1) return true;
|
|
575
|
+
const chunks = license.trim().split("-");
|
|
576
|
+
if (chunks.length !== 3) throw new Error("Invalid license format: Incorrect number of segments.");
|
|
577
|
+
const buffer = new ArrayBuffer(REPORT_SIZE);
|
|
578
|
+
const view = new DataView(buffer);
|
|
579
|
+
view.setUint8(0, ReportData.DataFirmwareActivationData);
|
|
580
|
+
for (let i = 0; i < 3; i++) {
|
|
581
|
+
const chunk = chunks[i];
|
|
582
|
+
if (!/^[0-9A-Fa-f]{8}$/.test(chunk)) throw new Error(`Invalid license format: Segment ${i + 1} is not a valid 8-character hex string.`);
|
|
583
|
+
const value = parseInt(chunk, 16);
|
|
584
|
+
view.setUint32(1 + i * 4, value, true);
|
|
585
|
+
}
|
|
586
|
+
await this.device.sendReport(ReportType.GenericInputOutput, new Uint8Array(buffer));
|
|
587
|
+
return true;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Saves current settings to flash and reboots the controller.
|
|
591
|
+
*/
|
|
592
|
+
async saveAndReboot() {
|
|
593
|
+
await this.sendGenericCommand(ReportData.CommandSaveSettings);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Reboots the controller without saving settings.
|
|
597
|
+
*/
|
|
598
|
+
async rebootController() {
|
|
599
|
+
await this.sendGenericCommand(ReportData.CommandReboot);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Switches the device to DFU mode for firmware updates.
|
|
603
|
+
*/
|
|
604
|
+
async switchToDfu() {
|
|
605
|
+
await this.sendGenericCommand(ReportData.CommandDfuMode);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Sets the current wheel position as center.
|
|
609
|
+
*/
|
|
610
|
+
async resetWheelCenter() {
|
|
611
|
+
await this.sendGenericCommand(ReportData.CommandResetCenter);
|
|
612
|
+
}
|
|
613
|
+
handleConnect = (event) => {
|
|
614
|
+
if (this.device?.opened) return;
|
|
615
|
+
if (isTargetDevice(event.device)) this.openDevice(event.device).catch((error) => {
|
|
616
|
+
this.emit("error", new Error("Failed to auto-connect to device", { cause: error }));
|
|
617
|
+
});
|
|
618
|
+
};
|
|
619
|
+
handleDisconnect = (event) => {
|
|
620
|
+
if (event.device === this.device) this.disconnect();
|
|
621
|
+
};
|
|
622
|
+
handleInputReport = (event) => {
|
|
623
|
+
try {
|
|
624
|
+
if (event.reportId !== ReportType.GenericInputOutput) return;
|
|
625
|
+
const r = new StructReader(event.data);
|
|
626
|
+
const newState = {
|
|
627
|
+
firmwareVersion: {
|
|
628
|
+
releaseType: r.u8(),
|
|
629
|
+
major: r.u8(),
|
|
630
|
+
minor: r.u8(),
|
|
631
|
+
patch: r.u8()
|
|
632
|
+
},
|
|
633
|
+
isRegistered: r.u8(),
|
|
634
|
+
position: r.i16(),
|
|
635
|
+
torque: r.i16(),
|
|
636
|
+
positionDegrees: null,
|
|
637
|
+
torqueNormalized: 0
|
|
638
|
+
};
|
|
639
|
+
newState.positionDegrees = this.cachedMotionRange !== null ? convertPositionToDegrees(newState.position, this.cachedMotionRange) : null;
|
|
640
|
+
newState.torqueNormalized = normalizeTorque(newState.torque);
|
|
641
|
+
this._lastState = newState;
|
|
642
|
+
this.emit("stateReceived", newState);
|
|
643
|
+
} catch (error) {
|
|
644
|
+
this.emit("error", new Error("Failed to process input report", { cause: error }));
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
async openDevice(device) {
|
|
648
|
+
try {
|
|
649
|
+
this.cachedMotionRange = null;
|
|
650
|
+
this._lastState = null;
|
|
651
|
+
this.device = device;
|
|
652
|
+
if (!this.device.opened) await this.device.open();
|
|
653
|
+
try {
|
|
654
|
+
this.cachedMotionRange = (await this.readEffectSettings()).motionRange;
|
|
655
|
+
} catch {
|
|
656
|
+
this.cachedMotionRange = null;
|
|
657
|
+
}
|
|
658
|
+
this.device.addEventListener("inputreport", this.handleInputReport);
|
|
659
|
+
this.emit("deviceConnected", this.device);
|
|
660
|
+
return true;
|
|
661
|
+
} catch {
|
|
662
|
+
if (this.device?.opened) await this.device.close().catch(() => {});
|
|
663
|
+
this.device = null;
|
|
664
|
+
this.cachedMotionRange = null;
|
|
665
|
+
return false;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
async sendGenericCommand(reportData) {
|
|
669
|
+
if (!this.device?.opened) throw new Error("Device is not connected");
|
|
670
|
+
const data = new Uint8Array(REPORT_SIZE);
|
|
671
|
+
data[0] = reportData;
|
|
672
|
+
await this.device.sendReport(ReportType.GenericInputOutput, data);
|
|
673
|
+
}
|
|
674
|
+
async getFeatureReport(reportId) {
|
|
675
|
+
if (!this.device?.opened) throw new Error("Device is not connected");
|
|
676
|
+
const view = await this.device.receiveFeatureReport(reportId);
|
|
677
|
+
const offset = 1;
|
|
678
|
+
return new DataView(view.buffer, view.byteOffset + offset, view.byteLength - offset);
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
//#endregion
|
|
683
|
+
export { AmplifierGain, ButtonMode, ExtensionMode, FIELD_TYPE_MAP, FieldType, INTERFACE_0_USAGE, INTERFACE_0_USAGE_PAGE, PID, PinMode, RAW_POSITION_MAX, RAW_TORQUE_MAX, REPORT_SIZE, ReportData, ReportType, SettingField, SpiLatchMode, SpiMode, VID, WheelApi, convertPositionToDegrees, normalizeTorque };
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shubham0x13/ffbeast-wheel-webhid-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WebHID based API for FFBeast Wheel",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ffbeast",
|
|
7
|
+
"webhid",
|
|
8
|
+
"hid",
|
|
9
|
+
"force-feedback",
|
|
10
|
+
"ffb",
|
|
11
|
+
"sim-racing",
|
|
12
|
+
"racing-wheel",
|
|
13
|
+
"steering-wheel",
|
|
14
|
+
"typescript"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/shubham0x13/ffbeast-wheel-webhid-api#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/shubham0x13/ffbeast-wheel-webhid-api/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/shubham0x13/ffbeast-wheel-webhid-api.git"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Shubham Patel",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown",
|
|
39
|
+
"dev": "tsdown --watch",
|
|
40
|
+
"docs": "typedoc",
|
|
41
|
+
"format": "prettier --write .",
|
|
42
|
+
"format:check": "prettier --check .",
|
|
43
|
+
"lint": "eslint .",
|
|
44
|
+
"lint:fix": "eslint . --fix",
|
|
45
|
+
"prepare": "husky",
|
|
46
|
+
"prepublishOnly": "npm run build",
|
|
47
|
+
"test": "echo \"No tests specified\" && exit 0",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
49
|
+
},
|
|
50
|
+
"lint-staged": {
|
|
51
|
+
"package.json": [
|
|
52
|
+
"sort-package-json"
|
|
53
|
+
],
|
|
54
|
+
"*.{ts,tsx,js,jsx}": [
|
|
55
|
+
"eslint --fix",
|
|
56
|
+
"prettier --write"
|
|
57
|
+
],
|
|
58
|
+
"*.{json,yml,yaml,md}": [
|
|
59
|
+
"prettier --write"
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"eventemitter3": "^5.0.1"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@eslint/js": "^9.39.2",
|
|
67
|
+
"@tsconfig/strictest": "^2.0.8",
|
|
68
|
+
"@types/node": "^25.0.9",
|
|
69
|
+
"@types/w3c-web-hid": "^1.0.6",
|
|
70
|
+
"eslint": "^9.39.2",
|
|
71
|
+
"eslint-config-prettier": "^10.1.8",
|
|
72
|
+
"husky": "^9.1.7",
|
|
73
|
+
"lint-staged": "^16.2.7",
|
|
74
|
+
"prettier": "^3.8.0",
|
|
75
|
+
"sort-package-json": "^3.6.0",
|
|
76
|
+
"tsdown": "^0.19.0",
|
|
77
|
+
"typedoc": "^0.28.16",
|
|
78
|
+
"typescript": "^5.0.0",
|
|
79
|
+
"typescript-eslint": "^8.53.0"
|
|
80
|
+
},
|
|
81
|
+
"engines": {
|
|
82
|
+
"node": ">=18"
|
|
83
|
+
}
|
|
84
|
+
}
|