forked from Yannicked/node-cue-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
505 lines (452 loc) · 14 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"use strict";
var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');
var Enum = require('enum');
var enums = require(__dirname+'/lib/enums.js');
var utils = require(__dirname+'/lib/utils.js')
var path = require('path');
var ArrayType = require('ref-array');
/**
* The id of a led on the keyboard
* @typedef CorsairLedId
* @type {number}
*/
var CorsairLedId = ref.types.int;
/**
* The access mode to be reported to the SDK.
* @typedef CorsairAccessMode
* @type {number}
*/
var CorsairAccessMode = ref.types.int;
/**
* A error code as reported by the SDK.
* @typedef CorsairError
* @type {number}
*/
var CorsairError = ref.types.int;
/**
* A rgb color
* @typedef color
* @type {Array}
* @property {number} 0 - Red intensity value 0..255
* @property {number} 1 - Green intensity value 0..255
* @property {number} 2 - Blue intensity value 0..255
*/
/**
* A rgb color with CorsairLedId included
* @typedef CorsairLedColor
* @type {Array}
* @property {number|string} 0 - Key id or name.
* @property {number} 1 - Red intensity value 0..255
* @property {number} 2 - Green intensity value 0..255
* @property {number} 3 - Blue intensity value 0..255
*/
var CorsairLedColor = StructType({
ledId: CorsairLedId,
r: ref.types.int,
g: ref.types.int,
b: ref.types.int
});
var CorsairPhysicalLayout = ref.types.int;
var CorsairLogicalLayout = ref.types.int;
var CorsairDeviceInfo = StructType({
type: ref.types.int,
model: ref.types.CString,
physicalLayout: CorsairPhysicalLayout,
logicalLayout: CorsairLogicalLayout,
capsMask: ref.types.int
});
var CorsairDeviceInfoPtr = ref.refType(CorsairDeviceInfo);
/**
* @typedef CorsairLedPosition
* @property {CorsairLedId} ledId - The id of the led.
* @property {number} top - The key's distance from the top of the keyboard.
* @property {number} left - The key's distance from the left of the keyboard.
* @property {number} height - The key's height.
* @property {number} width - The key's width.
*/
var CorsairLedPosition = StructType({
ledId: CorsairLedId,
top: ref.types.double,
left: ref.types.double,
height: ref.types.double,
width: ref.types.double
});
var CorsairLedPositionArr = ArrayType(CorsairLedPosition);
var CorsairLedPositions = StructType({
numberOfLeds: ref.types.int,
pLedPosition: CorsairLedPositionArr
});
var CorsairLedPositionsPtr = ref.refType(CorsairLedPositions);
/**
* The protocol details as reported by the SDK.
* @typedef CorsairProtocolDetails
* @type {Object}
* @property {string} sdkVersion - The version number of the SDK.
* @property {string} serverVersion - The version number of the server.
* @property {number} sdkProtocolVersion - The protocol version number.
* @property {number} serverProtocolVersion - The server's protocol version number.
* @property {boolean} breakingChanges - True if this update contained breaking changes.
*/
var CorsairProtocolDetails = StructType({
sdkVersion: ref.types.CString,
serverVersion: ref.types.CString,
sdkProtocolVersion: ref.types.int,
serverProtocolVersion: ref.types.int,
breakingChanges: ref.types.bool
});
var count = 0;
/**
* A error with the CUE sdk.
* @private
*/
function CueError(err) {
this.name = "CueError";
this.message = enums.CorsairError.get(Math.pow(2, err)).key+((err>3&&err!=5)?' -- this might be an error in the CueSDK wrapper, please contact the developer.':'');
let error = new Error(this.message);
error.name = this.name;
this.stack = error.stack.split('\n')[0]+'\n'+error.stack.split('\n').splice(3, error.stack.split('\n').length).join('\n');
}
CueError.prototype = Error.prototype;
/**
* The main CueSDK class.
@property {CorsairProtocolDetails} this.details - The protocol details as reported by the SDK.
@property {CorsairError} this.lastError - The last error as reported by the SDK.
@property {number} this.fps - The frames-per-second for fading.
@property {string} this.fadeType - The type of fading to be used.
*/
class CueSDK {
/**
* Create a Cue SDK object and do a handshake with the sdk.
* @param {boolean} [clear=false] - If true, clear the all the leds.
* @param {boolean} [exclusive=false] - If true, enable exclusive mode.
*/
constructor(clear = false, exclusive = false) {
this.CueSDKLib = ffi.Library(path.join(__dirname, 'lib/bin/', process.arch, 'CUESDK_2015v3.dll'), {
'CorsairSetLedsColors': ['bool', ['int', 'pointer']],
'CorsairSetLedsColorsAsync': ['bool', ['int', 'pointer', 'pointer', 'pointer']],
'CorsairGetDeviceCount': ['int', []],
'CorsairGetDeviceInfo': [CorsairDeviceInfoPtr, ['int']],
'CorsairGetLedPositions': [CorsairLedPositionsPtr, []],
'CorsairGetLedIdForKeyName': [CorsairLedId, ['char']],
'CorsairRequestControl': ['bool', [CorsairAccessMode]],
'CorsairPerformProtocolHandshake': [CorsairProtocolDetails, []],
'CorsairSetLayerPriority': ['bool', ['int']],
'CorsairGetLastError': [CorsairError, []],
'CorsairGetLedPositionsByDeviceIndex': [CorsairLedPositionsPtr, ['int']],
});
this.details = this.CueSDKLib.CorsairPerformProtocolHandshake().toObject();
this.lastError = 0;
this._error();
// Request exclusive access to keyboard LEDs
if(exclusive) this.CueSDKLib.CorsairRequestControl(enums.CorsairAccessMode.CAM_ExclusiveLightingControl);
this.fps = 30;
this.fade_helper = new utils.fade();
this.fadeType = 'Wheel';
if (clear) {
this.clear();
}
return this;
}
/**
* This callback runs after the leds have been set asynchronously.
*
* @callback setCallback
*/
/**
* Set the led colors.
* @param {(CorsairLedColor[]|...number|...string)} led - The key id with rgb values as an array.
* @param {setCallback} [callback] - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
set() {
count = 0;
if (arguments[0] instanceof Array) {
//console.log('instanceof Array');
if (typeof(arguments[1]) === 'function') {
// console.log('return this.setAsync(...arguments);');
return this.setAsync(...arguments);
} else {
// console.log('return this.setSync(...arguments);');
return this.setSync(...arguments);
}
} else {
if (typeof(arguments[4]) === 'function') {
return this.setIndividualAsync(...arguments);
} else {
return this.setIndividualSync(...arguments);
}
}
}
/**
* Set multiple led colors synchronously.
* @param {CorsairLedColor[]} a - The key ids or names with rgb values as an array.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
setSync(a, ids = false) {
let l = [];
for (let i = 0; i<a.length; i++) {
let [key, r, g, b] = a[i];
if(isNaN(key)){
l[i] = this._getLedColor(this._getLedIdForKeyName(key), r, g, b).ref();
}else{
if(key != undefined){
l[i] = this._getLedColor(key, r, g, b).ref();
}
}
}
let r = this.CueSDKLib.CorsairSetLedsColors(l.length, Buffer.concat(l));
if (r) {
return this;
} else {
this._error();
return this;
}
}
/**
* Set an individual led colors synchronously.
* @param {(number|string)} key - The key id or name.
* @param {number} r - Red intensity value 0..255
* @param {number} g - Green intensity value 0..255
* @param {number} b - Blue intensity value 0..255
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
setIndividualSync(key, r, g, b, ids = false) {
let l = null;
if(isNaN(key)){
l = this._getLedColor(ids?key:this._getLedIdForKeyName(key), r, g, b).ref();
}else{
l = this._getLedColor(key, r, g, b).ref();
}
let re = this.CueSDKLib.CorsairSetLedsColors(1, l);
if (re) {
return this;
} else {
this._error();
return this;
}
}
/**
* Set multiple led colors synchronously.
* @param {CorsairLedColor[]} a - The key id with rgb values as an array.
* @param {setCallback} callback - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
setAsync(a, callback, ids = false) {
let l = [];
for (let i = 0; i<a.length; i++) {
let [key, r, g, b] = a[i];
if(isNaN(key)){
l[i] = this._getLedColor(this._getLedIdForKeyName(key), r, g, b).ref();
}else{
l[i] = this._getLedColor(key, r, g, b).ref();
}
}
let asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], (context, success, error) => {
if (success) {
callback();
} else {
this._error();
}
});
let re = this.CueSDKLib.CorsairSetLedsColorsAsync(l.length, Buffer.concat(l), asyncFunc, ref.NULL);
if (re) {
return asyncFunc;
} else {
this._error();
return asyncFunc;
}
}
/**
* Set an individual led colors synchronously.
* @param {(number|string)} key - The key id or name.
* @param {number} r - Red intensity value 0..255
* @param {number} g - Green intensity value 0..255
* @param {number} b - Blue intensity value 0..255
* @param {setCallback} callback - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
setIndividualAsync(key, r, g, b, callback, ids = false) {
let l = null;
if(isNaN(key) && key != undefined){
l = this._getLedColor(ids?key:this._getLedIdForKeyName(key), r, g, b).ref();
}else{
l = this._getLedColor(key, r, g, b).ref();
}
let asyncFunc = ffi.Callback('void', ['pointer', 'bool', CorsairError], (context, succes, error) => {
if (succes) {
callback();
} else {
this._error();
}
});
let re = this.CueSDKLib.CorsairSetLedsColorsAsync(1, l, asyncFunc, ref.NULL);
if (re) {
return asyncFunc;
} else {
this._error();
return asyncFunc;
}
}
/**
* This callback runs after the leds have been faded asynchronously.
*
* @callback fadeCallback
*/
/**
* Fade multiple or individual leds.
* @param {(number|number[])} k - Key or arrray of keys to be faded.
* @param {color} f - The starting color.
* @param {color} t - The color to fade to.
* @param {number} l - Time to fade in miliseconds
* @param {fadeCallback} cb - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
fade() {
if (arguments[0] instanceof Array) {
return this.fadeAsync(...arguments);
} else {
return this.fadeIndividualAsync(...arguments);
}
}
fadeSingleColour() {
if (arguments[0] instanceof Array) {
return this.fadeSingleAsync(...arguments);
} else {
return this.fadeIndividualAsync(...arguments);
}
}
/**
* Fade multiple or individual leds.
* @param {number[]} k - Key or arrray of keys to be faded.
* @param {color} f - The starting color.
* @param {color} t - The color to fade to.
* @param {number} l - Time to fade in miliseconds
* @param {fadeCallback} cb - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
fadeSingleAsync(k, f, t, l, cb = () => {}, ids = false) {
this.fadeType = "RGB";
this.fade_helper[this.fadeType](f, t, l, this.fps, (r, g, b) => {
let a = [];
for (let i = 0; i<k.length; i++) {
a.push([k[i], r, g, b]);
}
this.setAsync(a, cb, ids);
});
}
fadeAsync(k, f, t, l, cb = () => {}, ids = false) {
this.fadeType = 'Wheel';
this.fade_helper[this.fadeType](f, t, l, this.fps, (r, g, b) => {
let a = [];
for (let i = 0; i<k.length; i++) {
a.push([k[i], r, g, b]);
}
this.setAsync(a, cb, ids);
});
}
/**
* Fade multiple or individual leds.
* @param {number} k - Key or arrray of keys to be faded.
* @param {color} f - The starting color.
* @param {color} t - The color to fade to.
* @param {number} l - Time to fade in miliseconds
* @param {fadeCallback} cb - The callback ran after completing the asynchonous request.
* @param {boolean} [ids=false] - If true, use the key ids as-is.
*/
fadeIndividualAsync( k, f, t, l, cb = () => {}, ids = false) { // k = array of leds, f = from color, t = to color [r, g, b], l = time in ms
this.fade_helper[this.fadeType](f, t, l, this.fps, (r, g, b) => {
this.setIndividualAsync(k, r, g, b, (r == t[0] && g == t[1] && b == t[2])?cb:(() => {}), ids);
});
}
/**
* Set all leds to black.
*/
clear() {
let l = [];
for (let i = 1; i<=154; i++) {
l.push([i, 0, 0, 0]);
}
this.set(l, true);
}
clearAll(ids) {
let l = [];
for (let i = 0; i<=ids.length; i++) {
var tl = ids[i]
if(tl != undefined){
l.push([tl, 0, 0, 0]);
}
}
this.set(l, true);
}
/**
* Get the leds as reported by the CueSDK
* @return {CorsairLedPosition[]} l - An array containing the led's positions as reported by the sdk.
*/
getLeds() {
let p = this.CueSDKLib.CorsairGetLedPositions().deref();
let l = p['pLedPosition'];
l.length = p['numberOfLeds'];
return l;
}
getCorsairPositionsByDeviceIndex(typeId) {
let p = this.CueSDKLib.CorsairGetLedPositionsByDeviceIndex(typeId).deref();
let l = p['pLedPosition'];
l.length = p['numberOfLeds'];
return l;
}
getCorsairDeviceType(typeId){
let p = this.CueSDKLib.CorsairGetDeviceInfo(typeId).deref();
return p
}
getCorsairDeviceCount(){
let p = this.CueSDKLib.CorsairGetDeviceCount();
return p;
}
/**
* Close the connection with the SDK and release all keys.
*/
close() {
this.CueSDKLib._dl.close();
this.CueSDKLib = {};
}
/**
* Renew the keyboard by assigning a normal priority. http://forum.corsair.com/v3/showthread.php?t=153701
*/
renew() {
this.CueSDKLib.CorsairSetLayerPriority(127);
}
/**
* Release the keyboard by setting a low priority. http://forum.corsair.com/v3/showthread.php?t=153701
*/
release() {
this.CueSDKLib.CorsairSetLayerPriority(120);
}
/**
* Create a CorsairLedColor.
* @private
*/
_getLedColor(ledId, r, g, b) {
let keyColor = new CorsairLedColor({ledId, r, g, b});
return keyColor;
}
/**
* Try to get a ledId for a key's name.
* @private
*/
_getLedIdForKeyName(key) {
return enums.CorsairLedId.get('CLK_'+key).value;
}
/**
* Check if the SDK returned a error.
* @private
*/
_error() {
this.lastError = this.CueSDKLib.CorsairGetLastError();
if (this.lastError != 'CE_Success' && this.lastError != 0) {
throw new CueError(this.lastError);
}
}
}
module.exports = {CueSDK};