-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (148 loc) · 5.06 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* This library acesses an Xbox 360 Chatpad over serial and maps the keys to ascii where possible.
* This work is based upon the work done by Cliffle.
* Useful webpages can be found here:
* http://cliffle.com/project/chatpad/protocol/
* http://cliffle.com/project/chatpad/pinout/
*/
const SerialPort = require('serialport');
const Util = require('util');
const Map = require('./map');
const CHATPAD_BAUD = 19200;
const CHATPAD_INIT = [ 0x87, 0x02, 0x8C, 0x1F, 0xCC ];
const CHATPAD_AWAKE = [ 0x87, 0x02, 0x8C, 0x1B, 0xD0 ];
const CHATPAD_AWAKE_TIME = 500; // Send CHATPAD_AWAKE every interval (ms)
const CHATPAD_PACKET_LENGTH = 8;
const CHATPAD_STATUS_BYTE = 0xA5;
const CHATPAD_EXPECTED_PACKETS = [ 0xB4, 0xC5 ];
class Chatpad {
constructor(port) {
this.port = new SerialPort(port, {
baudRate: CHATPAD_BAUD,
autoOpen: false,
dataBits: 8,
stopBits: 1,
parity: 'none'
});
this.callbacks = [];
this.keys = {
pressed: [0, 0],
modifier: 0,
caps: false
};
}
on(type, callback) {
this.callbacks[type] = callback;
}
processMessage(data) {
// Because of timing magic packets are always sent as 8 bytes, if there's less
// it's an error.
if (data.length != CHATPAD_PACKET_LENGTH) {
if (this.callbacks['error']) {
this.callbacks['error'](new Error(`Packet data length invalid: ${data.length}`));
}
return;
}
// Ignore status messages
if (data[0] === CHATPAD_STATUS_BYTE) {
return;
}
// Check for expected packet types.
if (!CHATPAD_EXPECTED_PACKETS.includes(data[0])) {
if (this.callbacks['error']) {
this.callbacks['error'](new Error(`unexpected packet type ${data[0]}`));
}
return;
}
// Calculate the checksum
let checksum = 0;
for (let i = 0; i < 7; i++) {
checksum += data[i];
}
checksum = 256 - (checksum % 256);
if (checksum !== data[7]) {
if (this.callbacks['error']) {
this.callbacks['error'](new Error(`checksum failure expected(${checksum}), actual(${data[7]}`));
}
return;
}
const keys = {
pressed: [ data[4], data[5] ],
modifier: data[3],
caps: this.keys.caps
}
const modifier = keys.modifier || this.keys.modifier;
if (this.keys.modifier !== keys.modifier && keys.modifier === Map.MODIFIER_CAPS) {
keys.caps = !keys.caps;
}
function makeEvent(key, pressed) {
const raw = {
key: key,
modifier: modifier
};
return {
raw: raw,
pressed: pressed,
code: Map.map(raw),
caps: keys.caps,
modifier: Map.MODIFIERS[raw.modifier]
};
}
if (this.callbacks['key']) {
for (let i = 0; i < 2; i++) {
if (!keys.pressed.includes(this.keys.pressed[i])) {
// Release this key
this.callbacks['key'](makeEvent(this.keys.pressed[i], false));
}
if (!this.keys.pressed.includes(keys.pressed[i])) {
// Press this key
this.callbacks['key'](makeEvent(keys.pressed[i], true));
}
}
}
if (this.callbacks['modifier']) {
if (this.keys.modifier !== keys.modifier) {
this.callbacks['modifier']({ raw: modifier, pressed: !!keys.modifier, modifier: Map.MODIFIERS[modifier] });
}
}
this.keys = keys;
}
async open() {
this.port.on('data', (data) => {
try {
this.processMessage(data);
} catch (err) {
try {
if (this.callbacks['error']) {
this.callbacks['error'](err);
}
} catch (err) {
console.log(err);
}
}
});
await Util.promisify(this.port.open.bind(this.port))();
// Send messages multiple times incase some are lost.
await this.write(Buffer.from(CHATPAD_INIT));
await this.write(Buffer.from(CHATPAD_INIT));
const awake = async() => {
await this.write(Buffer.from(CHATPAD_AWAKE));
await this.write(Buffer.from(CHATPAD_AWAKE));
this.awakeTimer = setTimeout(awake, CHATPAD_AWAKE_TIME);
};
awake();
}
async close() {
if (this.awakeTimer) {
clearTimeout(this.awakeTimer);
this.awakeTimer = null;
}
if (this.port.isOpen) {
return await Util.promisify(this.port.close.bind(this.port))();
}
}
async write(data) {
return await Util.promisify(this.port.write.bind(this.port))(data);
}
}
module.exports = Chatpad;