-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
122 lines (90 loc) · 2.95 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { DataReportMode, IRDataType, IRSensitivity } from "./src/const.js";
import WIIMote from "./src/wiimote.js"
let requestButton = document.getElementById("request-hid-device");
var wiimote = undefined;
function setButton(elementId, action) {
document.getElementById(elementId).addEventListener("click", async () => {
action()
})
}
requestButton.addEventListener("click", async () => {
let device;
try {
const devices = await navigator.hid.requestDevice({
filters: [{ vendorId: 0x057e }],
});
device = devices[0];
wiimote = new WIIMote(device)
} catch (error) {
console.log("An error occurred.", error);
}
if (!device) {
console.log("No device was selected.");
} else {
console.log(`HID: ${device.productName}`);
enableControls()
initCanvas()
}
});
function initButtons() {
setButton( "rumble",
() => wiimote.toggleRumble()
)
setButton( "irextended",
() => wiimote.initiateIR(IRDataType.EXTENDED)
)
setButton( "irbasic",
() => wiimote.initiateIR(IRDataType.BASIC)
)
setButton( "irfull",
() => wiimote.initiateIR(IRDataType.FULL)
)
setButton( "coreBtns",
() => wiimote.setDataTracking(DataReportMode.CORE_BUTTONS)
)
setButton( "coreBtnsACC",
() => wiimote.setDataTracking(DataReportMode.CORE_BUTTONS_AND_ACCEL)
)
setButton( "coreBtnsACCIR",
() => wiimote.setDataTracking(DataReportMode.CORE_BUTTONS_ACCEL_IR)
)
// LED buttons
document.getElementById("led1").addEventListener("click", () => wiimote.toggleLed(0))
document.getElementById("led2").addEventListener("click", () => wiimote.toggleLed(1))
document.getElementById("led3").addEventListener("click", () => wiimote.toggleLed(2))
document.getElementById("led4").addEventListener("click", () => wiimote.toggleLed(3))
}
function initCanvas(){
var canvas = document.getElementById("IRcanvas")
let ctx = canvas.getContext("2d")
wiimote.BtnListener = (buttons) => {
var buttonJSON = JSON.stringify(buttons, null, 2);
if(document.getElementById('buttons').innerHTML != buttonJSON){
document.getElementById('buttons').innerHTML = buttonJSON
}
}
wiimote.AccListener = (x,y,z) => {
document.getElementById('accX').innerHTML = x
document.getElementById('accY').innerHTML = y
document.getElementById('accZ').innerHTML = z
}
wiimote.IrListener = (pos) => {
if(pos.length < 1){
return
}
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
ctx.fillStyle = 'white'
pos.forEach( cPos => {
if(cPos != undefined){
ctx.fillRect(cPos.x/(1024/ctx.canvas.width), ctx.canvas.height-(cPos.y/(760/ctx.canvas.height)), 5, 5)
}
})
document.getElementById("IRdebug").innerHTML = JSON.stringify(pos, null, true)
}
}
function enableControls(){
document.getElementById("Controls").classList.remove("hidden")
document.getElementById("instructions").classList.add("hidden")
}
initButtons()