-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuzzController.js
84 lines (80 loc) · 2.6 KB
/
BuzzController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This class copies the interface from Controller.js and extends it to add support for Buzz controller lights.
*/
var BuzzController = Controller;
const hid_supported = "hid" in window.navigator;
Object.defineProperty(BuzzController, "lights", {
get: function () {
return this._lights;
},
set: function (newData) {
//if newData is negative, wrap it round from the maxiumum
if (newData <= 0) {
newData = 16 + newData;
}
this._lights = newData % 16;
setLights.bind(this);
setLights(this._lights);
}
});
Object.defineProperty(BuzzController, "lights_supported", {
get: function () {
let hw_supported = Controller.supported && hid_supported;
return hw_supported;
}
});
async function runHIDSetup() {
if (hid_supported && !this.getting_hid) {
this.getting_hid = true;
let hid_gamepads = await navigator.hid.getDevices();
if (hid_gamepads.length == 0) {
hid_gamepads = await navigator.hid.requestDevice({
filters: [
{
vendorId: 0x054c, //Logitech wired PS2 controller
productId: 0x0002,
},
{
vendorId: 0x1356, //wireless playstation controller
productId: 0x4096,
},
{
vendorId: 0x54c, // unknown other controller
productId: 0x1000,
}
],
});
}
if (hid_gamepads) {
this.hid_device = hid_gamepads[0];
// console.log(hid_gamepad);
if (!this.hid_device.opened) {
await this.hid_device.open();
}
}
this.getting_hid = false;
} else {
console.log("not supported!");
}
}
async function setLights(state) {
// if there is no device the first time, find one.
if (!this.hid_device) {
runHIDSetup.bind(this);
await runHIDSetup();
}
//if there is still no device, fail.
if (!this.hid_device) {
console.error("No HID device to send light states to.");
return;
}
state %= 16;
// console.log("bin mod", state, (state >>> 0).toString(2).padStart(4, 0));
//convert the decimal coerced number back to binary digits
let binVal = (state >>> 0).toString(2).padStart(4, 0);
let arr = [0]; //padding of one unit
for (let digit of binVal) {
arr.push(parseInt(digit));
}
this.hid_device.sendReport(0, Uint8Array.from(arr));
}