-
Notifications
You must be signed in to change notification settings - Fork 32
/
griffin-powermate.html
59 lines (52 loc) · 1.75 KB
/
griffin-powermate.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
<!DOCTYPE html>
<html>
<head>
<title>Griffin Powermate Playground</title>
</head>
<body>
<button id="button">Connect</button>
<pre id="log"></pre>
</body>
<script>
var device;
log.textContent = ('usb' in navigator) ? '' : 'Please enable WebUSB first.';
document.querySelector('button').addEventListener('click', connect);
async function connect() {
try {
device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x077d, productId: 0x0410 }] });
await device.open();
await device.selectConfiguration(1);
await device.claimInterface(0);
log.textContent = device.productName + ' connected';
await watch();
} catch(e) {
log.textContent = 'Argh! ' + e;
}
}
async function watch() {
try {
const event = await device.transferIn(1 /* endpoint */, 6 /* length */);
const state = parseEventData(event.data);
log.textContent = JSON.stringify(state, null, 2);
button.style.transform += 'rotate(' + state.knobDisplacement * 4 /* rotation degree */+ 'deg)';
watch();
} catch(e) {
log.textContent = 'Argh! ' + e;
}
}
function parseEventData(data) {
var state = {
ledStatus: data.getUint8(4),
buttonState: data.getUint8(0),
knobDisplacement: data.getInt8(1),
ledBrightness: data.getUint8(3),
pulseEnabled: (data.getUint8(4) & 1) == 1,
pulseWhileAsleep: (data.getUint8(4) & 4) == 4,
pulseSpeed: ['slower', 'normal', 'faster'][(data.getUint8(4) >> 4) & 3],
pulseStyle: ['style1', 'style2', 'style3'][(data.getUint8(4) >> 6) & 3],
ledMultiplier: data.getUint8(5)
};
return state;
}
</script>
</html>