-
Notifications
You must be signed in to change notification settings - Fork 24
/
calibration-via-webhid.html
75 lines (64 loc) · 2.11 KB
/
calibration-via-webhid.html
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
<!DOCTYPE html>
<input type=button value=Request onclick='doRequest()'>
<pre id=calibrationOut>...</pre>
<script>
const knownDevices = new Set();
async function startDevice(device) {
if (knownDevices.has(device)) {
console.log('Already known device');
return;
}
knownDevices.add(device);
await device.open();
console.log('Opened HID device:', device.productName, device.collections);
readConfig(device);
}
function getPage(device, requestedPage) {
const request = new Uint16Array(new ArrayBuffer(68));
request[1] = requestedPage;
return new Promise(resolve => {
device.oninputreport = e => {
const pageNum = e.data.getUint16(2);
if (pageNum !== requestedPage) return;
device.oninputreport = undefined;
resolve(e.data);
};
device.sendFeatureReport(0, request);
});
}
async function readConfig(device) {
const kPageSize = 64;
const page0 = await getPage(device, 0);
// Now that we have the first page, we know the length.
// Add 4 to also give space for the length itself.
const length = page0.getUint32(4) + 4;
const finalBytes = new Uint8Array(length);
const numPages = Math.ceil(length / 64);
for (let i = 0; i < numPages; ++i) {
const page = i === 0 ? page0 : await getPage(device, i);
const pageOffset = i * kPageSize;
const srcEnd = Math.min(finalBytes.length - pageOffset, kPageSize);
const src = new Uint8Array(page.buffer, 4, srcEnd);
finalBytes.set(src, pageOffset);
}
const s = new TextDecoder().decode(new Uint8Array(finalBytes.buffer, 4));
const j = JSON.parse(s);
console.log(j);
calibrationOut.textContent = JSON.stringify(j, undefined, 2);
}
navigator.hid.getDevices().then(devices => {
for (const device of devices) {
startDevice(device);
}
});
async function doRequest() {
const devices = await navigator.hid.requestDevice({
filters: [
//{ vendorId: 0x04d8, productId: 0xef7e }, // Looking Glass 2K
],
});
for (const device of devices) {
startDevice(device);
}
}
</script>