-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
281 lines (244 loc) · 6.1 KB
/
Copy pathindex.js
File metadata and controls
281 lines (244 loc) · 6.1 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* node-xkeys is a helper library that is designed to make using a PI Engineering XKeys
* controller easy in Node.js applications.
*
* Copyright (C) 2014 Rick Russell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
var HID = require('node-hid');
var device = null;
var device_type = null; // Assign the device type on open
var message = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var xk24_type = 1029;
var xk24_buttons = [0,1,2,3,4,5,8,9,10,11,12,13,16,17,18,19,20,21,24,25,26,27,28,29];
var xk24_backlight_offset = 32;
var xk80_type = 1089;
var xk80_buttons = Array.apply(null, Array(80)).map(function (_, i) {return i;});
var xk80_backlight_offset = 80;
var buttons = null; // Stores the current buttons based on the device_type.
var clearMessage = function() {
message.forEach(function(item) {
item = 0;
});
};
var setButtons = function() {
switch(device_type) {
case xk24_type:
buttons = xk24_buttons;
break;
case xk80_type:
buttons = xk80_buttons;
}
};
var set_blue_backlight = function(device,button, on, flash) {
clearMessage();
message[1] = 181;
message[2] = button;
if(on) {
if(flash) {
message[3] = 2; //flash
} else {
message[3] = 1; //on
}
} else {
message[3] = 0;
}
device.write(message);
return true;
};
var set_red_backlight = function(device, button, on, flash) {
clearMessage();
message[1] = 181;
var offset = 0;
// Which device do we have?
switch(device_type) {
case xk24_type:
offset = xk24_backlight_offset;
break;
case xk80_type:
offset = xk80_backlight_offset;
}
message[2] = button + offset;
if(on) {
if(flash) {
message[3] = 2; //flash
} else {
message[3] = 1; //on
}
} else {
message[3] = 0;
}
device.write(message);
return true;
};
module.exports = {
devices: function(type) {
device_list = HID.devices(1523,type);
var xkeys_list = [];
device_list.forEach(function(entry) {
xkeys_list.push({
path: entry.path,
type: entry.productId
});
});
return xkeys_list;
},
open: function(path) {
if(path)
{
device_list = HID.devices();
device_list.forEach(function(entry) {
if(entry.path == path) {
device_type = entry.productId;
}
});
if(!device_type) {
throw Error("Device not found with that path");
}
device = new HID.HID(path);
setButtons();
return true;
} else {
throw Error("The path is required");
}
},
close: function() {
if(device) {
device.close();
device = null;
return true;
} else {
throw Error("No device was open");
}
},
// Open the first device of the passed in product id
openFirst: function(type) {
if(!type) {
throw Error('Please specify an XKeys type of either xkeys.XK_24 of xkeys.XK_80');
}
var device_list = HID.devices(1523,type);
if(device_list.length) {
this.open(device_list[0].path);
} else {
throw Error("No devices of the type specified was found");
}
},
//Toggle all the backlights
toggleAllBacklights: function() {
clearMessage();
message[1] = 184;
device.write(message);
return true;
},
//Save EEPROM backlight state to the EEPROM
saveEepromBacklightState: function() {
clearMessage();
message[1] = 199;
message[2] = 1;
device.write(message);
return true;
},
//Set the LED state
setLedState: function(green, red) {
var mask = 0;
clearMessage();
message[1] = 186;
//flip the bits in the mask if the LEDs are true
if(green) {
mask = mask | 64;
}
if(red) {
mask = mask | 128;
}
message[2] = mask;
device.write(message);
return true;
},
//Set the Green LED state
setGreenLed: function(on, flash) {
clearMessage();
message[1] = 179;
message[2] = 6;
if(on) {
if(flash) {
message[3] = 2;
} else {
message[3] = 1;
}
} else {
message[3] = 0;
}
device.write(message);
return true;
},
//Set the Red LED state
setRedLed: function(on, flash) {
clearMessage();
message[1] = 179;
message[2] = 7;
if(on) {
if(flash) {
message[3] = 2;
} else {
message[3] = 1;
}
} else {
message[3] = 0;
}
device.write(message);
return true;
},
setBlueBackLight: function(button, on, flash) {
set_blue_backlight(device, button, on, flash);
},
setRedBackLight: function(button, on, flash) {
set_red_backlight(device, button, on, flash);
},
setAllBlueBackLights: function(on, flash) {
buttons.forEach( function(button) {
set_blue_backlight(device, button, on, flash);
});
},
setAllRedBackLights: function(on, flash) {
buttons.forEach( function(button) {
set_red_backlight(device, button, on, flash);
});
}
};
Object.defineProperty(module.exports, "XK_24", {
value: xk24_type,
enumerable: true,
writable: false,
configurable: false
});
Object.defineProperty(module.exports, "XK_24_BUTTONS", {
value: xk24_buttons,
enumerable: true,
writeable: false,
configurable: false
});
Object.defineProperty(module.exports, "XK_80", {
value: xk80_type,
enumerable: true,
writable: false,
configurable: false
});
Object.defineProperty(module.exports, "XK_80_BUTTONS", {
value: xk80_buttons,
enumerable: true,
writeable: false,
configurable: false
});