-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoruscate.js
691 lines (570 loc) · 14.7 KB
/
coruscate.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/******************************************************************************
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Alan Thiessen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
******************************************************************************/
const HID = require('node-hid');
const Color = require("color");
const NUM_LEDS = 21; // 20 leds in the GA15DH (LED index 20 controls the top 6)
const SupportedDevices = {
'GA15': {
'vendorId': 0x0b05,
'productId': 0x1866
}
};
const Mode = {
'Static': 0x00,
'Breathe': 0x01,
'ColorCycle': 0x02,
'Rainbow': 0x03,
'Strobe': 0x0a,
'Comet': 0x0b,
'FlashNDash': 0x0c,
'Irradiation': 0x11
};
const Speeds = {
'Slow': 0xe1,
'Medium': 0xeb,
'Fast': 0xf5
}
const Directions = {
'Left': 0x00,
'Right': 0x01
}
const Params = [
{
'mode': Mode.Static,
'params': [
'color1'
]
},
{
'mode': Mode.Breathe,
'params': [
'color1',
'color2',
'speed'
]
},
{
'mode': Mode.ColorCycle,
'params': [
'speed'
]
},
{
'mode': Mode.Rainbow,
'params': [
'speed',
'direction'
]
},
{
'mode': Mode.Strobe,
'params': [
'color1'
]
},
{
'mode': Mode.Comet,
'params': [
'color1'
]
},
{
'mode': Mode.FlashNDash,
'params': [
'color1'
]
},
{
'mode': Mode.Irradiation,
'params': [
]
}
];
/***
* The main Coruscate object delegate
* @type {{Init: (function(): boolean), Close: Close}}
*/
const Coruscate = {
'Init': Init,
'Close': Close,
'Update': Update,
'SetZone': SetZone,
'SetMode': SetMode,
'SetColor': SetColor,
'SetSpeed': SetSpeed,
'SetDirection': SetDirection,
'SetBrightness': SetBrightness,
'SetLed': SetLed,
'FillLeds': FillLeds,
'ClearLed': ClearLed,
'ClearAllLeds': ClearAllLeds,
'InitDirect': InitDirect,
'UpdateDirect': UpdateDirect,
'Off': Off,
'On': On
};
/***
* Creates a new Coruscate object
* @returns {any}
* @constructor
*/
function NewCoruscate(obj = null) {
let coruscateObj = Object.assign(Coruscate);
coruscateObj.Init(obj);
return coruscateObj;
}
/***
* Initializes a Coruscate object
*/
function Init(obj) {
this.aura = null;
this.descriptor = null;
this.zone = 0;
this.mode = Mode.Static;
this.color1 = Color("#000000");
this.color2 = Color("#000000");
this.speed = 0xe1;
this.dir = Directions.Left;
this.leds = Buffer.alloc(NUM_LEDS * 3, 0x00);
// Set any values passed in
this.Update(obj);
let device = FindSupportedDevice();
if(device) {
try {
this.aura = new HID.HID(device.vendorId, device.productId);
}
catch (e) {
console.error(e);
this.aura = null;
this.descriptor = null;
}
finally {
if(this.aura !== null) {
this.descriptor = device;
Handshake(this.aura);
this.SetBrightness(1);
}
}
}
return (this.aura !== null);
}
function Handshake(aura) {
try {
SendIdString();
// NOt sure what the returned data means, or what the query means either.
// But the device won't talk after power-up until this sequence is observed.
aura.getFeatureReport(0x5E, 64);
SendQuery();
aura.getFeatureReport(0x5E, 64);
}
catch(e) {
console.log(e);
}
function SendIdString() {
let setData = Buffer.alloc(64, 0);
let idString = Buffer.from("ASUS Tech.Inc.");
setData[ 0] = 0x5e;
idString.copy(setData, 1);
aura.sendFeatureReport(setData);
}
function SendQuery() {
let setData = Buffer.alloc(64, 0);
let query = Buffer.from([
0x5e,
0x05,
0x20,
0x31,
0x00,
0x10
]);
query.copy(setData, 0);
aura.sendFeatureReport(setData);
}
}
/***
* Close the connected HID device
* @constructor
*/
function Close() {
if(this.aura) {
this.aura.close();
this.aura = null;
}
}
/***
* Forces and update
* @constructor
*/
function Update(obj = null) {
let aura = this;
if(obj !== null) {
if(obj.hasOwnProperty('zone')) {
aura.SetZone(obj.zone);
}
if(obj.hasOwnProperty('mode')) {
aura.SetMode(obj.mode, false);
}
if(obj.hasOwnProperty('color1')) {
let color1 = obj.color1;
let color2 = null;
if(obj.hasOwnProperty('color2')) {
color2 = obj.color2;
}
aura.SetColor(color1, color2, false);
}
if(obj.hasOwnProperty('speed')) {
aura.SetSpeed(obj.speed, false);
}
if(obj.hasOwnProperty('direction')) {
aura.SetDirection(obj.direction, false);
}
}
UpdateDevice(aura);
}
/***
* Set the brightness of the LEDs
* @param level
* @constructor
*/
function SetBrightness(level) {
if((level >= 0) && (level <= 1)) {
let buffer = Buffer.from([
0xC5,
0xC4,
level
]);
WriteAura(this.aura, 0xba, buffer);
}
}
/***
* Set the zone to write
* @param zone
* @constructor
*/
function SetZone(zone) {
this.zone = zone;
}
/***
* Set the mode for operation
* @param mode
* @constructor
*/
function SetMode(mode, update = true) {
let newMode = CheckMode(mode);
if(newMode !== null) {
this.mode = newMode;
if(update) {
UpdateDevice(this);
}
}
}
/***
* Checks the mode and converts to the real value
* @param mode
* @returns {*}
* @constructor
*/
function CheckMode(mode) {
let newMode;
switch(typeof(mode)) {
case 'string':
newMode = Mode.hasOwnProperty(mode) ? Mode[mode] : null;
break;
case "number":
newMode = Object.values(Mode).includes(mode) ? mode : null;
break;
default:
newMode = null;
break;
}
return newMode;
}
/***
* Sets the two colors for the various Modes
* @param color1
* @param color2
* @constructor
*/
function SetColor(color1, color2 = null, update = true) {
this.color1 = Color(color1);
if(color2 !== null) {
this.color2 = Color(color2);
}
if(update) {
UpdateDevice(this);
}
}
/***
* Sets the speed for the modes (Supported by Breathe, ColorCycle, and Rainbow)
* @param speed
* @constructor
*/
function SetSpeed(speed, update = true) {
let newSpeed = null;
if(typeof(speed) === 'string') {
newSpeed = Speeds.hasOwnProperty(speed) ? Speeds[speed] : null;
}
if((typeof(speed) === 'number') && (speed >= 0xe1) && (speed <= 0xff)) {
newSpeed = speed;
}
if(newSpeed !== null) {
this.speed = newSpeed;
if(update) {
UpdateDevice(this);
}
}
}
/***
* Turn of the LEDS by setting them to static black
* @constructor
*/
function Off() {
this.SetBrightness(0);
}
/***
* Turn the LEDS on by setting them to the current configuration
* @constructor
*/
function On() {
this.SetBrightness(1);
}
/***
* Sets the direction of the Mode (Supported by Rainbow)
* @param dir
* @constructor
*/
function SetDirection(dir, update = true) {
if(Directions.hasOwnProperty(dir)) {
// Only update if dir changes
if(this.dir !== Directions[dir]) {
this.dir = Directions[dir];
if(update) {
UpdateDevice(this);
}
}
}
}
/***
* Sets the specific Led to the specified color
* @param led - 0 to NUM_LEDs - 1
* @param color
*/
function SetLed(led, color) {
if((led >= 0) && (led < NUM_LEDS)) {
let ledBuff = Buffer.from(Color(color).array());
let offset = led * 3;
ledBuff.copy(this.leds, offset, 0);
}
}
/***
* Fills the specified LED set with the specific color.
* @param color
* @param ledStart - The first led to fill (0-based)
* @param ledEnd - The last led to fill (0-based), if null, will fill to the end
* @constructor
*/
function FillLeds(color, ledStart, ledEnd = null) {
if((ledEnd == null) || (ledEnd > NUM_LEDS)) {
ledEnd = NUM_LEDS;
}
else if(ledEnd < 0) {
ledEnd = 0;
}
if((ledStart >= 0) && (ledStart < ledEnd)) {
let ledBuff = Buffer.from(Color(color).array());
let offset = ledStart * 3;
let ledCount = ledEnd - ledStart;
while(ledCount--) {
ledBuff.copy(this.leds, offset, 0);
offset += 3;
}
}
}
/***
* Clears the specific LED (sets it to all 0's
* @param led - 0 to NUM_LEDs - 1
* @constructor
*/
function ClearLed(led) {
if((led >= 0) && (led < NUM_LEDS)) {
let ledBuff = Buffer.from([0x00, 0x00, 0x00]);
let offset = led * 3;
ledBuff.copy(this.leds, offset, 0);
}
}
/***
* Clears the set of led Data
*/
function ClearAllLeds() {
this.leds = Buffer.alloc(NUM_LEDS * 3, 0x00);
}
/***
* Writes the current state of the LEDs directly to the controller.
* @constructor
*/
function UpdateDirect() {
WriteAuraDirect(this.aura, this.leds);
}
/***
* Searchs HID devices for a matching device in SupportedDevices.
* @returns {*} or undefined
*/
function FindSupportedDevice() {
let devices = HID.devices();
let device = devices.find((entry) => {
let found = false;
for (let key in SupportedDevices) {
if(SupportedDevices.hasOwnProperty(key)) {
let dev = SupportedDevices[key];
if((dev.vendorId === entry.vendorId) &&
(dev.productId === entry.productId)) {
found = true;
}
}
}
return found;
});
return device;
}
/***
* Builds the message to send to the HID controller
* @param aura
* @constructor
*/
function UpdateDevice(aura) {
let params = GetParams(aura.mode);
let buffer = Buffer.from([
aura.zone, // Zone
aura.mode, // Mode
0x00, 0x00, 0x00, // Color 1
0x00, // speed
0xff, // param
0x00, // Num colors? Breathe only
0x00, 0x00, 0x00 // Color2; Breathe only
]);
if((aura.aura !== null) && Array.isArray(params)) {
if(params.includes('color1')) {
Buffer.from(aura.color1.array()).copy(buffer, 2, 0, 3);
}
// Breathe only
if(params.includes('color2')) {
buffer[7] = 0x01;
Buffer.from(aura.color2.array()).copy(buffer, 8, 0, 3);
}
if(params.includes('speed')) {
buffer[5] = aura.speed;
}
// Rainbow only
if(params.includes('direction')) {
buffer[6] = aura.dir;
}
Idle(aura.aura);
WriteAura(aura.aura, 0xb3, buffer);
WriteAura(aura.aura, 0xb4, Buffer.alloc(0));
WriteAura(aura.aura, 0xb5, Buffer.alloc(0));
}
}
/***
* Sends an Idle to the HID device
* @constructor
*/
function Idle(hid) {
// Unfortunately, node-hid doesn't support Set_Idle
}
/***
* Writes the aura data to the HID device
* @param aura - The HID device
* @param func - The function to write
* @param data Array the data to write
* @returns {*}
* @constructor
*/
function WriteAura(aura, func, data) {
let setData = Buffer.alloc(64, 0);
setData[ 0] = 0x5e;
setData[ 1] = func;
data.copy(setData, 2);
return aura.sendFeatureReport(setData);
}
/***
* Initialize direct mode, and clear all LEDs
* @constructor
*/
function InitDirect() {
let ledSet1 = Buffer.alloc(64, 0);
// Direct headers for both messages
let header1 = Buffer.from([0x5e, 0xbc, 0xd0]);
let header2 = Buffer.from([0x5e, 0xbc, 0xd0, 0x01, 0x02, 0x00, 0x00, 0x10, 0x00]);
let header3 = Buffer.from([0x5e, 0xbc, 0xd0, 0x01, 0x02, 0x00, 0x01, 0x04, 0x00]);
header1.copy(ledSet1, 0);
this.aura.sendFeatureReport(ledSet1);
header2.copy(ledSet1, 0);
this.aura.sendFeatureReport(ledSet1);
header3.copy(ledSet1, 0);
this.aura.sendFeatureReport(ledSet1);
}
/***
* Writes the provided LED data directly to the individual LEDs
* @param aura
* @param ledData
* @constructor
*/
function WriteAuraDirect(aura, ledData) {
let ledSet1 = Buffer.alloc(64, 0);
let ledSet2 = Buffer.alloc(64, 0);
let numLeds1 = 16;
// Direct headers for both messages
let header1 = Buffer.from([0x5e, 0xbc, 0xd0, 0x01, 0x02, 0x00, 0x00, 0x10, 0x00]);
let header2 = Buffer.from([0x5e, 0xbc, 0xd0, 0x01, 0x02, 0x00, 0x01, 0x04, 0x00]);
header1.copy(ledSet1, 0,0);
header2.copy(ledSet2, 0,0);
ledData.copy(ledSet1, header1.length, 0, numLeds1 * 3);
ledData.copy(ledSet2, header2.length, numLeds1 * 3);
aura.sendFeatureReport(ledSet1);
aura.sendFeatureReport(ledSet2);
}
/***
* Returns the parameters for the given mode
* @param reqMode
* @returns {*}
* @constructor
*/
function GetParams(reqMode) {
let mode = CheckMode(reqMode);
if(mode !== null) {
return Params.find((param) => param.mode === mode).params;
}
else {
return mode;
}
}
module.exports = {
'modes': Mode,
'speeds': Speeds,
'directions': Directions,
'Params': GetParams,
'Coruscate': NewCoruscate
}