-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
194 lines (162 loc) · 4.99 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const { EventEmitter } = require('events');
const { CentralManager, Peripheral, CharacteristicWriteType } = require('core-bluetooth');
const _ = require('lodash');
const Keys = require('./keys');
const ANNE_PRO_SERVICE_UUID = "F000FFC0-0451-4000-B000-000000000000";
const ANNE_PRO_READ_UUID = "F000FFC1-0451-4000-B000-000000000000";
const ANNE_PRO_WRITE_UUID = "F000FFC2-0451-4000-B000-000000000000";
class AnneProKeyboard extends EventEmitter {
connect() {
this.centralManager = new CentralManager();
this.centralManager.on('stateUpdate', this.statusUpdated.bind(this));
this.centralManager.on('peripheralConnectFail', this.connectionFailed.bind(this));
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
setLightingMode(mode) {
return this.sendQuery([9, 2, 1, mode]);
}
setLightingSettings(speed, brightness) {
this.writeMessage([9, 4, 2, speed, brightness, 0]);
}
getLightingMode() {
return this.sendQuery([9, 1, 8]).then(data => {
return {
code: data[3],
name: _.invert(AnneProKeyboard.LightingModes)[data[3]],
};
});
}
setLayout(layout) {
return this.sendQuery([7, 2, 3, layout]);
}
getLayout() {
return this.sendQuery([7, 1, 4]).then(data => {
return {
code: data[3],
name: _.invert(AnneProKeyboard.Layouts)[data[3]],
};
});
}
getKeyboardId() {
return this.sendQuery([2, 1, 1]);
}
getFirmwareVersion() {
return this.sendQuery([10, 0]);
}
getMacro(num) {
return this.sendQuery([5, 2, 5, num]).then(data => {
const len = data[1];
const index = data[6];
if (len < 0x1d) {
return null;
}
return {
data,
index,
triggerCode: data[8],
triggerKey: Keys.getName(data[8]),
};
});
}
getMacroDefinition(num) {
// new byte[]{(byte) num, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0}
const data = [num, 0, 0, 0, 0, 0, 0, 0];
return this.sendQuery([])
}
sendQuery(message) {
return new Promise((resolve, reject) => {
this.once('dataError', (error) => reject(error));
this.once('receivedData', (data) => resolve(data));
this.writeMessage(message);
});
}
getService(uuid) {
this.log('this.services.map(s => s.uuid)', this.services.map(s => s.uuid));
return this.services.find(s => s.uuid === uuid);
}
get mainService() { return this.getService(ANNE_PRO_SERVICE_UUID); }
getCharacteristic(uuid) {
return this.characteristics.find(c => c.uuid === uuid);
}
get readCharacteristic() { return this.getCharacteristic(ANNE_PRO_READ_UUID); }
get writeCharacteristic() { return this.getCharacteristic(ANNE_PRO_WRITE_UUID); }
writeMessage(data) {
this.peripheral.writeValueForCharacteristic(Buffer.from(data), this.writeCharacteristic, 0);
}
// events
statusUpdated(status) {
this.log('statusUpdated', status);
if (status !== 'poweredOn') { return; }
const connectedPeripherals = this.centralManager.retrieveConnectedPeripherals([ANNE_PRO_SERVICE_UUID]);
if (connectedPeripherals.length) {
this.peripheral = connectedPeripherals[0];
this.peripheral.on('connect', this.keyboardConnected.bind(this));
this.peripheral.on('servicesDiscover', this.servicesDiscovered.bind(this));
this.peripheral.on('connectFail', this.connectionFailed.bind(this));
this.peripheral.on('characteristicValueUpdate', this.receivedData.bind(this));
this.centralManager.connect(this.peripheral);
} else {
this.reject(new Error('Keyboard not connected'));
}
}
keyboardConnected() {
this.log('connected');
this.peripheral.discoverServices();
}
connectionFailed(error) {
console.log('Connection failed', error);
}
servicesDiscovered(services) {
this.log('services', services);
this.services = services;
this.log('mainService', this.mainService);
this.mainService.on('characteristicsDiscover', this.characteristicsDiscovered.bind(this));
this.peripheral.discoverCharacteristicsForService(null, this.mainService);
}
characteristicsDiscovered(characteristics) {
this.log('characteristics', characteristics);
this.characteristics = characteristics;
this.readCharacteristic.setNotifyValue(true);
this.readCharacteristic.on('valueUpdate', this.receivedData.bind(this));
this.resolve(this);
}
receivedData(value, error) {
if (error) {
return this.emit('dataError', error);
}
this.emit('receivedData', value);
}
log(...s) {
if (!this.debug) { return; }
console.log(...s);
}
}
AnneProKeyboard.LightingModes = {
Custom: 128,
Off: 0,
Red: 1,
Yellow: 2,
Green: 3,
Cyan: 4,
Blue: 5,
Purple: 6,
Pink: 7,
Orange: 8,
White: 9,
Breathing: 13,
Rainbow: 14,
SingleLight: 15,
SingleLightLong: 16,
Poptang: 17,
Colorful: 18,
};
AnneProKeyboard.Layouts = {
Windows: 1,
WindowsArrows: 2,
Mac: 3,
Custom: 128,
};
module.exports = AnneProKeyboard;