-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unicorn_pHAT_accessory.js
809 lines (695 loc) · 37.7 KB
/
Unicorn_pHAT_accessory.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// ================================================================================================
// Pimoroni Unicorn pHAT (for RPi Zero) HomeKit accessory plugin for HAP-NodeJS + rpi-ws281x-native
// (c) 2018-2019 Karl-Henrik Henriksson - homekit@xoblite.net - http://homekit.xoblite.net/
// ================================================================================================
var Accessory = require('../').Accessory;
var Service = require('../').Service;
var Characteristic = require('../').Characteristic;
var uuid = require('../').uuid;
// ================================================================================
const os = require('os');
const { performance } = require('perf_hooks');
const driver = require('../node_modules/rpi-ws281x-native'); // -> https://www.npmjs.com/package/rpi-ws281x-native
const numLeds = 32; // Number of LEDs on the Unicorn pHAT (also matching the length of all related arrays elsewhere in the code)
const numCols = 8, numRows = 4; // Number of columns and rows of the LED display (cf. 8x4 = 32 LEDs as per above)
// LED DRIVER INIT PARAMETERS - DON'T CHANGE THESE UNLESS YOU HAVE PROBLEMS AND KNOW WHAT YOU'RE DOING...
const DRIVER_FREQUENCY = 800000;
const DRIVER_DMA_NUMBER = 10; // Note: The sometimes used DMA 5 causes networking issues on (at least) RPi Zero W, 3B+ and 3A+!
const DRIVER_GPIO_PIN = 18;
const DRIVER_INVERT = 0;
const DRIVER_BRIGHTNESS = 255;
// Default dynamic Unicorn pHAT colour array -> 8x4 pixels, all white LEDs ("white light")
var leds =
[ 0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,
0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,
0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,
0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff ];
// Static Unicorn pHAT colour arrays for the "icon" mode -> 8x4 pixels, with varying RGB colours
const iconDefault =
[ 0x0000ff, 0x000000, 0x000000, 0x0000ff, 0xcc0000, 0xcc0000, 0xcc0000, 0x000000,
0x0000ff, 0x000000, 0x000000, 0x0000ff, 0xcc0000, 0x000000, 0x000000, 0xcc0000,
0x0000ff, 0x000000, 0x000000, 0x0000ff, 0xcc0000, 0xcc0000, 0xcc0000, 0x000000,
0x000000, 0x0000ff, 0x0000ff, 0x000000, 0xcc0000, 0x000000, 0x000000, 0x000000 ];
const icon10percent =
[ 0xaa66ff, 0x006666, 0x006633, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000,
0xaa66ff, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x006666, 0x006633, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000 ];
const icon20percent =
[ 0xaa66ff, 0x00aaaa, 0x006633, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000,
0xaa66ff, 0x00aaaa, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x006633, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000 ];
const icon30percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000,
0x000000, 0x00aaaa, 0x00cc66, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x00aaaa, 0x00cc66, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x006600, 0x336600, 0x666600, 0x663300, 0x660000 ];
const icon40percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x336600, 0x666600, 0x663300, 0x660000,
0x000000, 0x000000, 0x00cc66, 0x00cc00, 0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x00cc66, 0x00cc00, 0x000000, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x336600, 0x666600, 0x663300, 0x660000 ];
const icon50percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0x666600, 0x663300, 0x660000,
0x000000, 0x000000, 0x000000, 0x00cc00, 0x66cc00, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x00cc00, 0x66cc00, 0x000000, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0x666600, 0x663300, 0x660000 ];
const icon60percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0x663300, 0x660000,
0x000000, 0x000000, 0x000000, 0x000000, 0x66cc00, 0xcccc00, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000, 0x66cc00, 0xcccc00, 0x000000, 0x000000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0x663300, 0x660000 ];
const icon70percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0x660000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xcccc00, 0xcc6600, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xcccc00, 0xcc6600, 0x000000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0x660000 ];
const icon80percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0xff0000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xcc6600, 0xff0000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xcc6600, 0xff0000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0xff0000 ];
const icon90percent =
[ 0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0xff0000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xff0000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0xff0000,
0xaa66ff, 0x00aaaa, 0x00cc66, 0x00cc00, 0x66cc00, 0xcccc00, 0xcc6600, 0xff0000 ];
const iconHappy =
[ 0x008800, 0x008800, 0xffffff, 0x008800, 0x008800, 0xffffff, 0x008800, 0x008800,
0x008800, 0x008800, 0x008800, 0x008800, 0x008800, 0x008800, 0x008800, 0x008800,
0x008800, 0xffffff, 0x008800, 0x008800, 0x008800, 0x008800, 0xffffff, 0x008800,
0x008800, 0x008800, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x008800, 0x008800 ];
const iconMeh =
[ 0x888800, 0x888800, 0xffffff, 0x888800, 0x888800, 0xffffff, 0x888800, 0x888800,
0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800,
0x888800, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x888800,
0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800 ];
const iconSad =
[ 0x880000, 0x880000, 0xffffff, 0x880000, 0x880000, 0xffffff, 0x880000, 0x880000,
0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000,
0x880000, 0x880000, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x880000, 0x880000,
0x880000, 0xffffff, 0x880000, 0x880000, 0x880000, 0x880000, 0xffffff, 0x880000 ];
const iconSkull =
[ 0xffffff, 0xffffff, 0x991111, 0xffffff, 0xffffff, 0x991111, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0x333333, 0x666666, 0x333333, 0x666666, 0x666666, 0x333333, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ];
const iconPacMan =
[ 0x000000, 0xaaaa00, 0xaaaa00, 0x666600, 0xaaaa00, 0x000000, 0x000000, 0x000000,
0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0x000000, 0x000000,
0xaaaa00, 0xaaaa00, 0xaaaa00, 0x000000, 0x000000, 0x660000, 0x000066, 0x660000,
0x000000, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0x000000, 0x000000, 0x000000 ];
const iconHeart =
[ 0xaa6666, 0xbb7777, 0xff4444, 0xaa6666, 0xff4444, 0xaa6666, 0xbb7777, 0xcc8888,
0xcc8888, 0xff4444, 0xff4444, 0xff4444, 0xff4444, 0xff4444, 0xcc8888, 0xbb7777,
0xaa6666, 0xcc8888, 0xff4444, 0xff4444, 0xff4444, 0xbb7777, 0xbb7777, 0xaa6666,
0xbb7777, 0xaa6666, 0xbb7777, 0xff4444, 0xaa6666, 0xcc8888, 0xaa6666, 0xcc8888 ];
const iconCake =
[ 0xaaaa00, 0x000000, 0x996600, 0x000000, 0xaaaa00, 0x000000, 0x996600, 0x000000,
0xcccccc, 0x000000, 0xcccccc, 0x000000, 0xcccccc, 0x000000, 0xcccccc, 0x000000,
0x888888, 0x660000, 0x888888, 0x662200, 0x888888, 0x660000, 0x888888, 0x000000,
0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x000000 ];
const iconMail =
[ 0x000000, 0x886600, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0x886600, 0x000000, 0x000000,
0x000000, 0xaaaa00, 0x886600, 0xaaaa00, 0x886600, 0xaaaa00, 0x000000, 0x000000,
0x000000, 0xaaaa00, 0xaaaa00, 0x886600, 0xaaaa00, 0xaaaa00, 0x000000, 0x000000,
0x000000, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0xaaaa00, 0x000000, 0x000000 ];
const iconQuestionMark =
[ 0x000000, 0x000000, 0x000000, 0x000000, 0x00ff00, 0x000000, 0x000000, 0x00ff00,
0x00ff00, 0x00ff00, 0x000000, 0x00ff00, 0x000000, 0x00ff00, 0x000000, 0x00ff00,
0x00ff00, 0x00ff00, 0x000000, 0x00ff00, 0x000000, 0x00ff00, 0x000000, 0x00ff00,
0x000000, 0x000000, 0x000000, 0x00ff00, 0x000000, 0x000000, 0x00ff00, 0x000000 ];
const iconExclamationMark =
[ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0xff0000, 0xff0000, 0x000000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000,
0xff0000, 0xff0000, 0x000000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ];
const iconRightUp =
[ 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000099, 0x000000,
0x000066, 0x000066, 0x000077, 0x000077, 0x000088, 0x000088, 0x000099, 0x0000aa,
0x000066, 0x000066, 0x000077, 0x000077, 0x000088, 0x000088, 0x000099, 0x0000aa,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000099, 0x000000 ];
const iconLeftDown =
[ 0x000000, 0x000099, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0x0000aa, 0x000099, 0x000088, 0x000088, 0x000077, 0x000077, 0x000066, 0x000066,
0x0000aa, 0x000099, 0x000088, 0x000088, 0x000077, 0x000077, 0x000066, 0x000066,
0x000000, 0x000099, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ];
const iconSweden =
[ 0x000088, 0x000088, 0x000088, 0x888800, 0x888800, 0x000088, 0x000088, 0x000088,
0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800,
0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800, 0x888800,
0x000088, 0x000088, 0x000088, 0x888800, 0x888800, 0x000088, 0x000088, 0x000088 ];
const iconNorway =
[ 0x880000, 0x880000, 0x880000, 0x888888, 0x000088, 0x880000, 0x880000, 0x880000,
0x888888, 0x888888, 0x888888, 0x888888, 0x000088, 0x888888, 0x888888, 0x888888,
0x000088, 0x000088, 0x000088, 0x888888, 0x000088, 0x000088, 0x000088, 0x000088,
0x880000, 0x880000, 0x880000, 0x000088, 0x888888, 0x880000, 0x880000, 0x880000 ]
const iconDenmark =
[ 0x880000, 0x880000, 0x880000, 0x888888, 0x888888, 0x880000, 0x880000, 0x880000,
0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888,
0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888,
0x880000, 0x880000, 0x880000, 0x888888, 0x888888, 0x880000, 0x880000, 0x880000 ];
const iconFinland =
[ 0x888888, 0x888888, 0x888888, 0x000088, 0x000088, 0x888888, 0x888888, 0x888888,
0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088,
0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088, 0x000088,
0x888888, 0x888888, 0x888888, 0x000088, 0x000088, 0x888888, 0x888888, 0x888888 ];
const iconIceland =
[ 0x000088, 0x000088, 0x000088, 0x888888, 0x880000, 0x000088, 0x000088, 0x000088,
0x888888, 0x888888, 0x888888, 0x888888, 0x880000, 0x880000, 0x880000, 0x880000,
0x880000, 0x880000, 0x880000, 0x880000, 0x888888, 0x888888, 0x888888, 0x888888,
0x000088, 0x000088, 0x000088, 0x880000, 0x888888, 0x000088, 0x000088, 0x000088 ]
const iconUK =
[ 0x888888, 0x000088, 0x000088, 0x888888, 0x880000, 0x000088, 0x000088, 0x880000,
0x880000, 0x888888, 0x888888, 0x888888, 0x880000, 0x880000, 0x880000, 0x888888,
0x888888, 0x880000, 0x880000, 0x880000, 0x888888, 0x888888, 0x888888, 0x880000,
0x880000, 0x000088, 0x000088, 0x880000, 0x888888, 0x000088, 0x000088, 0x888888 ]
const iconUSA =
[ 0x000088, 0x888888, 0x000088, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000,
0x000088, 0x888888, 0x000088, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888,
0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000, 0x880000,
0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888, 0x888888 ]
// ================================================================================
var LightController = {
name: "Unicorn pHAT", // Name of accessory (changeable by the user)
pincode: "031-45-154", // Pin code of accessory -> "Default" HAP-NodeJS accessory pin code
username: "FA:3C:ED:5A:1A:1C", // MAC like address used by HomeKit to differentiate accessories
manufacturer: "homekit.xoblite.net", // Manufacturer (optional)
model: "Unicorn pHAT", // Model (optional, not changeable by the user)
firmwareRevision: "19.1.6", // Firmware version (optional)
serialNumber: "HAP-NodeJS", // Serial number (optional)
power: true, // Default power status
// brightness: 1, // Default brightness (>=10) or display mode (<10), where 1 -> CPU load display mode
brightness: 2, // TEMPORARY :)
hue: 0, // Default hue
saturation: 10, // Default saturation
outputLogs: true, // Enable logging to the console?
currentDisplayMode: 0, // Current display mode, e.g. Regular Light (0), CPU load (1), Rainbow (2) ...
currentDisplayModeInterval: null, // Pointer to a setInterval object when any dynamic/animated display mode has been enabled
cpuLoadModeIdleTime: 0, // Last CPU idle time measurement, used when calculating the current CPU load
cpuLoadModeTimeStamp: 0, // Last millisecond time stamp, used when calculating the current CPU load
cpuLoadHistory: new Array(numCols), // CPU load history (N last measurements of the 3-second averaged current CPU load)
rainbowModeParam: 0.0, // Dynamic variable used by the Rainbow display mode animation function
swirlCounter: 0, // // Dynamic variable used by the Swirl display mode animation function
// ====================
// Update Unicorn pHAT display...
updateUnicorn: function(updateDisplayMode) {
// if (updateDisplayMode)
{
// Stop any previously running dynamic (e.g. CPU load) or animated (e.g. Rainbow) display modes...
if (this.currentDisplayMode > 0)
{
if (this.outputLogs)
{
if (this.currentDisplayMode == 1) console.log("%s -> MODE -> Disabling CPU load display mode.", this.name);
else if (this.currentDisplayMode == 2) console.log("%s -> MODE -> Disabling CPU history display mode.", this.name);
else if (this.currentDisplayMode == 3) console.log("%s -> MODE -> Disabling Fire display mode.", this.name);
else if (this.currentDisplayMode == 4) console.log("%s -> MODE -> Disabling Swirl display mode.", this.name);
else if (this.currentDisplayMode == 5) console.log("%s -> MODE -> Disabling Rainbow display mode.", this.name);
}
clearInterval(this.currentDisplayModeInterval);
this.currentDisplayModeInterval = null;
}
// Switch to the newly selected display mode...
if (this.power && (this.brightness > 0))
{
if (this.brightness == 1 || this.brightness == 2) // CPU load display mode: Current averaged load || Load history (last 8 measurements)
{
if (this.outputLogs)
{
if (this.brightness == 1) console.log("%s -> MODE -> Enabling CPU load display mode.", this.name);
else console.log("%s -> MODE -> Enabling CPU history display mode.", this.name);
}
this.currentDisplayMode = this.brightness;
this.cpuLoadModeIdleTime = this.cpuLoadModeTimeStamp = 0;
for (var n = 0; n < numCols; n++) { this.cpuLoadHistory[n] = 0.0; }
this.cpuloadUnicorn();
this.currentDisplayModeInterval = setInterval(this.cpuloadUnicorn, 3000);
}
else if (this.brightness == 3) // Fire display mode
{
if (this.outputLogs) console.log("%s -> MODE -> Enabling Fire display mode.", this.name);
this.currentDisplayMode = this.brightness;
driver.setBrightness(25);
this.fireUnicorn();
this.currentDisplayModeInterval = setInterval(this.fireUnicorn, 250);
}
else if (this.brightness == 4) // Swirl display mode
{
if (this.outputLogs) console.log("%s -> MODE -> Enabling Swirl display mode.", this.name);
this.currentDisplayMode = this.brightness;
this.swirlCounter = 0;
driver.setBrightness(10);
this.swirlUnicorn();
this.currentDisplayModeInterval = setInterval(this.swirlUnicorn, 125);
}
else if (this.brightness == 5) // Rainbow display mode
{
if (this.outputLogs) console.log("%s -> MODE -> Enabling Rainbow display mode.", this.name);
this.currentDisplayMode = this.brightness;
driver.setBrightness(50);
this.rainbowModeParam = 0.0;
this.rainbowUnicorn();
this.currentDisplayModeInterval = setInterval(this.rainbowUnicorn, 20); // -> 50 FPS... ;)
}
else if (this.brightness == 9) // Icons display mode
{
if (this.outputLogs) console.log("%s -> MODE -> Icons display mode -> Displaying icon #%s.", this.name, this.hue);
this.currentDisplayMode = 0; // -> This is a non-animated display mode
var minBrightness = this.saturation;
if (this.saturation < 10) minBrightness = 10;
driver.setBrightness(minBrightness);
switch (this.hue)
{
case 360: driver.render(iconSweden); break;
case 359: driver.render(iconNorway); break;
case 358: driver.render(iconDenmark); break;
case 357: driver.render(iconFinland); break;
case 356: driver.render(iconIceland); break;
case 355: driver.render(iconUK); break;
case 354: driver.render(iconUSA); break;
case 201: driver.render(iconMail); break;
case 111: driver.render(iconCake); break;
case 110: driver.render(iconHeart); break;
case 105: driver.render(iconPacMan); break;
case 104: driver.render(iconSkull); break;
case 103: driver.render(iconSad); break;
case 102: driver.render(iconMeh); break;
case 101: driver.render(iconHappy); break;
case 14: driver.render(iconRightUp); break;
case 13: driver.render(iconLeftDown); break;
case 12: driver.render(iconExclamationMark); break;
case 11: driver.render(iconQuestionMark); break;
case 9: driver.render(icon90percent); break;
case 8: driver.render(icon80percent); break;
case 7: driver.render(icon70percent); break;
case 6: driver.render(icon60percent); break;
case 5: driver.render(icon50percent); break;
case 4: driver.render(icon40percent); break;
case 3: driver.render(icon30percent); break;
case 2: driver.render(icon20percent); break;
case 1: driver.render(icon10percent); break;
default: driver.render(iconDefault);
}
}
else // Regular Light display mode
{
if (this.outputLogs) console.log("%s -> MODE -> Regular Light display mode.", this.name);
this.currentDisplayMode = 0; // -> This is a non-animated display mode
// Calculate output RGB from input HSV...
// (Nb. We're using full brightness (v) for these calculations; actual LED brightness applied later.)
var h = (this.hue / 360), s = (this.saturation / 100), v = 1;
var r, g, b, i, f, p, q, t;
if (s == 0) {
r = g = b = v;
}
else
{
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
}
// Merge calculated r,g,b to RBG... (0xrrggbb)
r = Math.round(r * 255) * 256 * 256;
g = Math.round(g * 255) * 256;
b = Math.round(b * 255);
var rgb = r+g+b;
// var rgb = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
for (var n = 0; n < numLeds; n++) {
leds[n] = rgb;
}
// var unicornBrightness = Math.round(this.brightness * 2.55); // The Unicorn pHAT takes values 0-255, HomeKit uses percentages 0-100%
// if (this.power && (this.brightness > 0)) driver.setBrightness(unicornBrightness);
driver.setBrightness(this.brightness);
driver.render(leds);
}
}
else // Power off display
{
if (this.outputLogs) console.log("%s -> INFO -> Powering off display.", this.name);
this.currentDisplayMode = 0;
driver.setBrightness(0);
driver.render(iconDefault);
}
}
},
// ====================
cpuloadUnicorn: function() {
// NOTE: Because this function is not only called directly but also spawned continuously through setInterval(),
// we can not use this.xxx references in here, but need to address directly using LightController.xxx .
var minBrightness = LightController.saturation;
if (LightController.saturation < 10) minBrightness = 10;
driver.setBrightness(minBrightness);
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Calculating CPU load...", this.name);
// Calculate the CPU load as 3 second average... (cf. top's update frequency, see also setInterval() @ 3000 msec above)
// Note that the following loop iterates over the number of available CPU cores, which means it
// should work both on the Pi Zero (1 core ARMv6) as well as the Pi 3 (4 core ARMv7) ...
var currentIdleTime = 0;
for (var n = 0; n < os.cpus().length; n++) {
currentIdleTime += (os.cpus()[n].times.idle / 10);
}
// ...but since we're only using a single percentage metric for our CPU load/history display modes, we then
// also need to divide the total idle time by the number of CPU cores... (i.e. averaging across *all* cores)
currentIdleTime /= os.cpus().length;
var currentTimeStamp = performance.now();
var load = 1 - ((currentIdleTime - LightController.cpuLoadModeIdleTime) / (currentTimeStamp - LightController.cpuLoadModeTimeStamp));
// Push the new CPU load measurement into the top [position 0] of the history array [of size numCols]...
for (var n = 1; n < numCols; n++) {
if (LightController.cpuLoadHistory[numCols-n-1] != NaN) LightController.cpuLoadHistory[numCols-n] = LightController.cpuLoadHistory[numCols-n-1];
else LightController.cpuLoadHistory[numCols-n] = 0.0;
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Updating history position [%s] -> %s.", LightController.name, numCols-n, LightController.cpuLoadHistory[numCols-n]);
}
LightController.cpuLoadHistory[0] = load;
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Updating history position [0] -> %s.", LightController.name, LightController.cpuLoadHistory[0]);
if (LightController.brightness == 1) // Regular CPU load display mode (latest measurement, x-axis oriented -> 8 LEDs -> higher resolution)
{
if (LightController.cpuLoadModeTimeStamp == 0) driver.render(icon10percent);
else
{
if (load < 0.15) driver.render(icon10percent);
else if (load < 0.25) driver.render(icon20percent);
else if (load < 0.35) driver.render(icon30percent);
else if (load < 0.45) driver.render(icon40percent);
else if (load < 0.55) driver.render(icon50percent);
else if (load < 0.65) driver.render(icon60percent);
else if (load < 0.75) driver.render(icon70percent);
else if (load < 0.85) driver.render(icon80percent);
else driver.render(icon90percent);
}
}
else // Alternative CPU load history display mode (8 last measurements, but y-axis oriented -> 4 LEDs -> lower resolution ~ 0/25/50/75 percent steps)
{
for (var n = 0; n < numLeds; n++) { leds[n] = 0x000000; }; // Flush LEDs array (all black pixels)
for (var n = 0; n < numCols; n++)
{
// if (LightController.cpuLoadHistory[n] == NaN) leds[31-n] = 0x666666;
if (LightController.cpuLoadModeTimeStamp == 0) leds[31-n] = 0x666666;
if (n == 0)
{
if (LightController.cpuLoadHistory[n] > 0.75) leds[7-n] = icon80percent[7];
if (LightController.cpuLoadHistory[n] > 0.50) leds[15-n] = icon60percent[5];
if (LightController.cpuLoadHistory[n] > 0.25) leds[23-n] = icon40percent[3];
if (LightController.cpuLoadHistory[n] >= 0.10) leds[31-n] = icon20percent[1];
else if (LightController.cpuLoadHistory[n] < 0.10) leds[31-n] = 0x666666;
}
else
{
if (LightController.cpuLoadHistory[n] > 0.75) leds[7-n] = icon80percent[7];
if (LightController.cpuLoadHistory[n] > 0.50) leds[15-n] = icon60percent[5];
if (LightController.cpuLoadHistory[n] > 0.25) leds[23-n] = icon40percent[3];
if (LightController.cpuLoadHistory[n] >= 0.10) leds[31-n] = icon20percent[1];
else if (LightController.cpuLoadHistory[n] < 0.10) leds[31-n] = 0x666666;
}
}
driver.render(leds);
}
LightController.cpuLoadModeIdleTime = currentIdleTime;
LightController.cpuLoadModeTimeStamp = currentTimeStamp;
},
// ====================
// NOTE: The following function is a mostly direct port to this framework
// of the rainbow.py Unicorn pHAT python example code by Pimoroni. Thanks guys... :)
// -> https://github.com/pimoroni/unicorn-hat
rainbowUnicorn: function() {
// NOTE: Because this function is not only called directly but also spawned continuously through setInterval(),
// we can not use this.xxx references in here, but need to address directly using LightController.xxx .
// var minBrightness = this.saturation;
// if (LightController.saturation < 10) minBrightness = 10;
// driver.setBrightness(minBrightness);
LightController.rainbowModeParam += 0.3;
var offset = 30;
var r = 0, g = 0, b = 0;
for (var y = 0; y < numRows; y++) {
for (var x = 0; x < numCols; x++) {
r = (Math.cos((x+LightController.rainbowModeParam)/2.0) + Math.cos((y+LightController.rainbowModeParam)/2.0)) * 64.0 + 128.0;
g = (Math.sin((x+LightController.rainbowModeParam)/1.5) + Math.sin((y+LightController.rainbowModeParam)/2.0)) * 64.0 + 128.0;
b = (Math.sin((x+LightController.rainbowModeParam)/2.0) + Math.cos((y+LightController.rainbowModeParam)/1.5)) * 64.0 + 128.0;
r = Math.max(0, Math.min(255, r + offset));
g = Math.max(0, Math.min(255, g + offset));
b = Math.max(0, Math.min(255, b + offset));
leds[x+(y*8)] = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}
}
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Updating Rainbow...", LightController.name);
driver.render(leds);
},
// ====================
// NOTE: The following function is a adapted and modified port to this framework
// of the random_blinky.py Unicorn pHAT python example code by Pimoroni. Thanks guys... :)
// -> https://github.com/pimoroni/unicorn-hat
fireUnicorn: function() {
// NOTE: Because this function is not only called directly but also spawned continuously through setInterval(),
// we can not use this.xxx references in here, but need to address directly using LightController.xxx .
// var minBrightness = this.saturation;
// if (LightController.saturation < 10) minBrightness = 10;
// driver.setBrightness(minBrightness);
var r = 0, g = 0, b = 0, h = 0, s = 0, v = 0, rndm = 0, i, f, p, q, t;
for (var y = 0; y < numRows; y++)
{
for (var x = 0; x < numCols; x++)
{
rndm = Math.random();
h = 0.1 * rndm;
s = 0.8;
// v = rndm;
v = 0.2 + (0.8 * rndm);
// h = (LightController.hue / 360) * rndm;
// s = (LightController.saturation / 100); // 0.8;
// v = 1;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255) * 256 * 256;
g = Math.round(g * 255) * 256;
b = Math.round(b * 255);
var rgb = r+g+b;
// leds[x+(y*8)] = rgb;
leds[x+(y*numCols)] = rgb;
}
}
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Updating Fire...", LightController.name);
driver.render(leds);
},
// ====================
// NOTE: The following function was directly inspired by the "LEDs with added trigonometry" blog entry by
// (pre-Pimoroni) Sandy Macdonald -> http://sandyjmacdonald.github.io/2015/01/20/leds-with-added-trigonometry/ . Thanks!
swirlUnicorn: function() {
// NOTE: Because this function is not only called directly but also spawned continuously through setInterval(),
// we can not use this.xxx references in here, but need to address directly using LightController.xxx .
var xc, yc, zc, r, g, b, h, s, v, i, f, p, q, t;
for (var y = 0; y < numRows; y++)
{
for (var x = 0; x < numCols; x++)
{
xc = x + LightController.swirlCounter;
yc = y + LightController.swirlCounter;
zc = Math.sin(xc) + Math.cos(yc);
zc = (zc + 2) / 4;
h = 0.6 * zc;
s = 0.5;
v = zc;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255) * 256 * 256;
g = Math.round(g * 255) * 256;
b = Math.round(b * 255);
var rgb = r+g+b;
leds[x+(y*numCols)] = rgb;
}
}
LightController.swirlCounter++;
if (LightController.swirlCounter == 360) LightController.swirlCounter = 0;
// if (LightController.outputLogs) console.log("%s -> DEBUG -> Updating Swirl...", LightController.name);
driver.render(leds);
},
// ====================
setPower: function(status) { // Set power state
if (this.power != status)
{
if (this.outputLogs) console.log("%s -> SET -> Power -> %s.", this.name, status ? "On" : "Off");
this.power = status;
this.updateUnicorn(true);
}
},
getPower: function() { // Get power state
if (this.outputLogs) console.log("%s -> GET -> Power -> %s.", this.name, this.power ? "On" : "Off");
return this.power;
},
setBrightness: function(brightness) { // Set brightness
if (this.brightness != brightness)
{
if (this.outputLogs) console.log("%s -> SET -> Brightness -> %s.", this.name, brightness);
this.brightness = brightness;
this.newModeSelected = true; // Brightness has changed <-> Display mode (multiplexed into brightness) has changed
if (this.power) this.updateUnicorn(true);
}
},
getBrightness: function() { // Get brightness
if (this.outputLogs) console.log("%s -> GET -> Brightness -> %s.", this.name, this.brightness);
return this.brightness;
},
setSaturation: function(saturation) { // Set saturation
if (this.saturation != saturation)
{
if (this.outputLogs) console.log("%s -> SET -> Saturation -> %s.", this.name, saturation);
this.saturation = saturation;
if (this.power) this.updateUnicorn(false);
}
},
getSaturation: function() { // Get saturation
if (this.outputLogs) console.log("%s -> GET -> Saturation -> %s.", this.name, this.saturation);
return this.saturation;
},
setHue: function(hue) { // Set hue
if (this.hue != hue)
{
if (this.outputLogs) console.log("%s -> SET -> Hue -> %s.", this.name, hue);
this.hue = hue;
if (this.power) this.updateUnicorn(false);
}
},
getHue: function() { // Get hue
if (this.outputLogs) console.log("%s -> GET -> Hue -> %s.", this.name, this.hue);
return this.hue;
},
identify: function() { // Identify the accessory
if (this.outputLogs) console.log("%s -> IDENTIFY -> Hello world! :)", this.name);
}
}
// ================================================================================
// Initialize the Unicorn pHAT and some other stuff to their default state at startup...
if (LightController.outputLogs)
{
console.log("%s -> INFO -> Starting: Version %s, running on HomeCore (HAP-NodeJS) %s / Node.js %s.", LightController.name, LightController.firmwareRevision, require('../package.json').version, process.version);
console.log("%s -> INFO -> Starting: Initializing the pHAT HW.", LightController.name);
}
var driverInitObject = { // Note: All DRIVER_XXX parameters are defined near the top of this file for easy end-user reach ;)
frequency: DRIVER_FREQUENCY,
dmaNum: DRIVER_DMA_NUMBER,
gpioPin: DRIVER_GPIO_PIN,
invert: DRIVER_INVERT,
brightness: DRIVER_BRIGHTNESS };
driver.init(numLeds, driverInitObject);
for (var n = 0; n < numCols; n++) { LightController.cpuLoadHistory[n] = 0.0; }
LightController.updateUnicorn(true);
// Generate a consistent UUID for our light Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "light".
var lightUUID = uuid.generate('hap-nodejs:accessories:light' + LightController.name);
// This is the Accessory that we'll return to HAP-NodeJS that represents our light.
var lightAccessory = exports.accessory = new Accessory(LightController.name, lightUUID);
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
lightAccessory.username = LightController.username;
lightAccessory.pincode = LightController.pincode;
if (LightController.outputLogs) console.log("%s -> INFO -> If not bridged, the HomeKit pincode for this accessory is \x1b[44m\x1b[37m %s \x1b[0m.", LightController.name, LightController.pincode);
// Set some basic properties (these values are arbitrary and setting them is optional)
lightAccessory
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, LightController.manufacturer)
.setCharacteristic(Characteristic.Model, LightController.model)
.setCharacteristic(Characteristic.SerialNumber, LightController.serialNumber)
.setCharacteristic(Characteristic.FirmwareRevision, LightController.firmwareRevision);
// ====================
// Listen for the "identify" event for this Accessory
lightAccessory.on('identify', function(paired, callback) {
LightController.identify();
callback();
});
// Add the actual Lightbulb Service and listen for change events from HomeKit.
// We can see the complete list of Services and Characteristics in `lib/gen/HomeKitTypes.js`
lightAccessory
.addService(Service.Lightbulb, LightController.name) // Services exposed to the user should have "names"
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
LightController.setPower(value);
// Our light is synchronous - this value has been successfully set
// Invoke the callback when you finished processing the request
// If it's going to take more than 1s to finish the request, try to invoke the callback
// after getting the request instead of after finishing it. This avoids blocking other
// requests from HomeKit.
callback();
})
// We want to intercept requests for our current power state so we can query the hardware itself instead of
// allowing HAP-NodeJS to return the cached Characteristic.value.
.on('get', function(callback) {
callback(null, LightController.getPower());
});
// To inform HomeKit about changes occurred outside of HomeKit (like user physically turn on the light)
// Please use Characteristic.updateValue
//
// lightAccessory
// .getService(Service.Lightbulb)
// .getCharacteristic(Characteristic.On)
// .updateValue(true);
// Also add an "optional" Characteristic for Brightness.
lightAccessory
.getService(Service.Lightbulb)
.addCharacteristic(Characteristic.Brightness)
.on('set', function(value, callback) {
LightController.setBrightness(value);
callback();
})
.on('get', function(callback) {
callback(null, LightController.getBrightness());
});
// Also add an "optional" Characteristic for Saturation.
lightAccessory
.getService(Service.Lightbulb)
.addCharacteristic(Characteristic.Saturation)
.on('set', function(value, callback) {
LightController.setSaturation(value);
callback();
})
.on('get', function(callback) {
callback(null, LightController.getSaturation());
});
// Also add an "optional" Characteristic for Hue.
lightAccessory
.getService(Service.Lightbulb)
.addCharacteristic(Characteristic.Hue)
.on('set', function(value, callback) {
LightController.setHue(value);
callback();
})
.on('get', function(callback) {
callback(null, LightController.getHue());
});
// ================================================================================
// Clean up by resetting the Unicorn pHAT hardware if the process is terminated...
process.on('exit', (code) => {
if (LightController.outputLogs) console.log(`%s -> INFO -> Exiting [${code}]: Resetting the pHAT HW.`, LightController.name);
driver.reset();
});
// ================================================================================