-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorne-keyboard.js
51 lines (42 loc) · 1.43 KB
/
corne-keyboard.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
import HID from 'node-hid';
const VENDOR_ID = 0x4653;
const PRODUCT_ID = 0x0001;
const USAGE_PAGE = 0xFF60;
const USAGE = 0x61;
class CorneKeyboard {
constructor() {
var devices = HID.devices();
this.deviceInfo = devices.find(d => {
var isCorne = d.vendorId === VENDOR_ID && d.productId === PRODUCT_ID;
return isCorne && d.usagePage === USAGE_PAGE && d.usage === USAGE;
});
}
open() {
this.device = new HID.HID(this.deviceInfo.path);
this.device.on("data", data => console.log("HID Data received:", data));
this.device.on("error", err => console.error("HID error: ", err));
}
close() {
this.device.close();
}
sendText(line, text) {
var buffer = Buffer.from(text.padEnd(20), 'utf8');
if (line < 0 || line > 1) {
throw new Error("'line' should be in (0, 1).")
}
if (buffer.length > 29) {
throw new Error("'text' is too long. Must be 29 or fewer characters.");
}
// We need to send 33 bytes for some reason
// otherwise the subsequent packets won't be sent.
// default packet length is 32
var data = Array(33).fill(0);
data[1] = line;
data[2] = buffer.length;
for (var i=0; i<buffer.length; i++) {
data[i+3] = buffer[i]
}
var count = this.device.write(data);
}
}
export default CorneKeyboard;