-
Notifications
You must be signed in to change notification settings - Fork 1
/
blinkenlights.cpp
1592 lines (1356 loc) · 33.2 KB
/
blinkenlights.cpp
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <ctime>
#include <csignal>
#include <sstream>
#include <fstream>
#include <locale>
#include <iomanip>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <stdint.h>
#include <unistd.h>
#include <yaml.h>
#include <math.h>
using namespace std;
// speed of 6 million causes flicker
// #define SPI_SPEED 6000000
#define SPI_SPEED 3000000
#define LED_BRIGHTNESS 31
#define PI 3.14159265
#define NUM_LEDS 646
#define FADE_VAL 1
#define FAST_FADE_VAL 16
#define EFFECT_DELAY 120
#define WAIT_DELAY 30
#define PERSONAL_EFFECT_TIME 600
// mix effects
#define HARD_MIX 1
#define SUBTRACT 2
#define XOR 3
#define MAX 4
#define REPLACE 5
// number of effects
#define EFFECTS 12
#define CUSTOM_EFFECTS 9
#define STATIC_EFFECTS 2
const static string effects[] = {
// Off
"Off",
// non-customizable
"Rainbow",
"Sparkle",
// customizable
"RandomWhite",
"RandomTwoColorFade",
"RandomTwoColorSparkle",
"RedAlert",
"RainbowSparkles",
"LavaLamp",
"ColorOrgan",
"SlowSparkle",
"SlowTwoColorSparkle"
};
uint8_t display_buffer[NUM_LEDS * 3];
uint8_t buffer1[NUM_LEDS * 3];
uint8_t buffer2[NUM_LEDS * 3];
uint8_t buffer3[NUM_LEDS * 3];
uint8_t buffer4[NUM_LEDS * 3];
int signaled = 0;
uint8_t lights_on = 0;
time_t personal_effect_until;
uint8_t p_r1, p_g1, p_b1, p_r2, p_g2, p_b2;
// functions
void signalHandler(int signum)
{
signaled = signum;
cout << "signal: " << signaled << endl;
}
void DisplayBuffer(uint8_t *buffer)
{
uint8_t led_frame[4];
uint8_t brightness;
// max brightness to reduce end of strip flicker
brightness = LED_BRIGHTNESS;
// start of frame all 0x00
uint8_t buf[1];
for(int i = 0; i < 4; i++) {
buf[0] = 0x00;
wiringPiSPIDataRW(0, buf, 1);
}
// write out frame
for(int i = 0; i < NUM_LEDS; i++)
{
led_frame[0] = 0b11100000 | (0b00011111 & brightness);
led_frame[1] = buffer[i*3];
led_frame[2] = buffer[i*3+1];
led_frame[3] = buffer[i*3+2];
wiringPiSPIDataRW(0, led_frame, 4);
}
// end of frame all FFs
for(int i = 0; i < 4; i++) {
buf[0] = 0xFF;
wiringPiSPIDataRW(0, buf, 1);
}
}
uint8_t InSchedule(void)
{
/*
Function reads in schedule from YAML formatted configuration file to determine if
it's currently time to turn on the lights or not.
*/
uint8_t in_schedule = 0;
time_t now = time(0);
tm *ltm = localtime(&now);
stringstream parse_time;
string hr_time, min_time;
int hr_time_int, min_time_int, start_secs, end_secs, max_end_secs;
const static string dow[] = {
"Su",
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa"
};
const static string mon[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
FILE *fh = fopen("schedule.conf", "r");
yaml_parser_t parser;
yaml_event_t event; /* New variable */
uint8_t in_events = 0;
uint8_t in_sequence = 0;
uint8_t in_mapping = 0;
uint8_t in_read = 0;
string event_name, start_time, end_time, day_of_week, week_day_number, month, day_of_month, year, disabled, open_status;
/* Initialize parser */
if(!yaml_parser_initialize(&parser))
fputs("Failed to initialize parser!\n", stderr);
if(fh == NULL)
fputs("Failed to open schedule file!\n", stderr);
/* Set input file */
yaml_parser_set_input_file(&parser, fh);
/* START new code */
do {
if (!yaml_parser_parse(&parser, &event)) {
printf("Parser error %d\n", parser.error);
exit(EXIT_FAILURE);
}
switch(event.type)
{
case YAML_NO_EVENT:
//puts("No event!");
break;
/* Stream start/end */
case YAML_STREAM_START_EVENT:
//puts("STREAM START");
break;
case YAML_STREAM_END_EVENT:
//puts("STREAM END");
break;
/* Block delimeters */
case YAML_DOCUMENT_START_EVENT:
//puts("Start Document");
break;
case YAML_DOCUMENT_END_EVENT:
//puts("End Document");
break;
case YAML_SEQUENCE_START_EVENT:
//puts("Start Sequence");
break;
case YAML_SEQUENCE_END_EVENT:
//puts("End Sequence");
in_events = 0;
break;
case YAML_MAPPING_START_EVENT:
//puts("Start Mapping");
break;
case YAML_MAPPING_END_EVENT:
//puts("End Mapping");
if(in_events)
{
uint8_t event_in_event = 0;
int secs = (ltm->tm_hour * 3600) + (ltm->tm_min * 60) + ltm->tm_sec;
if(start_time != "")
{
// clear variables
parse_time.clear();
hr_time.clear();
min_time.clear();
parse_time<<start_time;
getline(parse_time, hr_time, ':' );
getline(parse_time, min_time, ':' );
hr_time_int = atoi(hr_time.c_str());
min_time_int = atoi(min_time.c_str());
start_secs = (hr_time_int * 3600) + (min_time_int * 60);
if(secs >= start_secs)
{
// It's past the start time
event_in_event = 1;
}
}
if(end_time != "")
{
// clear variables
parse_time.clear();
hr_time.clear();
min_time.clear();
parse_time<<end_time;
getline(parse_time, hr_time, ':' );
getline(parse_time, min_time, ':' );
hr_time_int = atoi(hr_time.c_str());
min_time_int = atoi(min_time.c_str());
end_secs = (hr_time_int * 3600) + (min_time_int * 60);
// cout << "end_secs: " << end_secs << endl;
if(secs >= end_secs)
{
// It's past the end time
event_in_event = 0;
}
}
if(day_of_week != "")
{
if(day_of_week.find(dow[ltm->tm_wday]) == std::string::npos)
{
// Day of the week not found in schedule
event_in_event = 0;
}
}
if(week_day_number != "")
{
if(atoi(week_day_number.c_str()) != (((ltm->tm_mday - 1)/7)+1))
{
// Day of the week not found in schedule
event_in_event = 0;
}
}
if(month != "")
{
if(month.find(mon[ltm->tm_mon]) == std::string::npos)
{
// Day of the week not found in schedule
event_in_event = 0;
}
}
if(day_of_month != "")
{
if(atoi(day_of_month.c_str()) != ltm->tm_mday)
{
// Day of the week not found in schedule
event_in_event = 0;
}
}
if(year != "")
{
if(atoi(year.c_str()) != (ltm->tm_year + 1900))
{
// Day of the week not found in schedule
event_in_event = 0;
}
}
if(disabled == "true")
{
// Schedule is disabled
event_in_event = 0;
}
in_schedule |= event_in_event;
}
start_time.clear();
end_time.clear();
day_of_week.clear();
week_day_number.clear();
month.clear();
day_of_month.clear();
year.clear();
disabled.clear();
open_status.clear();
break;
/* Data */
case YAML_ALIAS_EVENT:
//printf("Got alias (anchor %s)\n", event.data.alias.anchor);
break;
case YAML_SCALAR_EVENT:
if(in_events)
{
// we've found that we're in the events section so ok to read data
if(in_read)
{
// we've previously found a data type to read so next bit of data goes in there
switch(in_read)
{
case 1:
// read in Event Name next
event_name = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 2:
// read in Start Time next
start_time = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 3:
// read in End Time next
end_time = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 4:
// read in Day of Week next
day_of_week = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 5:
// read in Disabled next
disabled = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 6:
// read in Month next
month = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 7:
// read in Day next
day_of_month = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 8:
// read in Yeak next
year = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 9:
// read in Week day number next
week_day_number = reinterpret_cast<char*>(event.data.scalar.value);
break;
case 10:
// read in open status next
open_status = reinterpret_cast<char*>(event.data.scalar.value);
break;
}
in_read = 0;
}
else
{
// read scalar of variable names
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "event_name") == 0)
{
// read in Event Name next
in_read = 1;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "start_time") == 0)
{
// read in Start Time next
in_read = 2;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "end_time") == 0)
{
// read in End Time next
in_read = 3;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "day_of_week") == 0)
{
// read in Day of Week next
in_read = 4;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "disabled") == 0)
{
// read in Disabled
in_read = 5;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "month") == 0)
{
// read in Day of Week next
in_read = 6;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "day") == 0)
{
// read in Day of Week next
in_read = 7;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "year") == 0)
{
// read in Day of Week next
in_read = 8;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "week_day_number") == 0)
{
// read in Day of Week next
in_read = 9;
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "open_status") == 0)
{
// read in Open Status
in_read = 10;
}
}
}
if(strcmp(reinterpret_cast<const char *>(event.data.scalar.value), "Events") == 0)
{
in_events = 1;
// puts("Found Events");
}
break;
}
if(event.type != YAML_STREAM_END_EVENT)
yaml_event_delete(&event);
} while(event.type != YAML_STREAM_END_EVENT);
yaml_event_delete(&event);
/* END new code */
/* Cleanup */
yaml_parser_delete(&parser);
fclose(fh);
return in_schedule;
}
uint8_t InSemaphor(void)
{
/*
Function reads in semaphor files to determine if we're being triggered to display
*/
uint8_t in_semaphor = 0;
time_t now = time(0);
ifstream ifile("/var/www/html/bl_semaphor/outfile.txt");
if(ifile)
{
puts("Found Semaphor file");
uint8_t cnum = 0;
std::string line;
while(std::getline(ifile,line))
{
// read in colors
if(line.substr(0,1) == "#"
&& line != "#000000"
&& line.find_first_not_of("0123456789abcdefABCDEF",1) == std::string::npos
&& line.length() == 7)
{
// valid color string
cnum++;
if(cnum == 1)
{
p_r1 = std::stoi(line.substr(1,2), 0, 16);
p_g1 = std::stoi(line.substr(3,2), 0, 16);
p_b1 = std::stoi(line.substr(5,2), 0, 16);
}
if(cnum == 2)
{
p_r2 = std::stoi(line.substr(1,2), 0, 16);
p_g2 = std::stoi(line.substr(3,2), 0, 16);
p_b2 = std::stoi(line.substr(5,2), 0, 16);
}
}
}
if(cnum == 1)
{
// only one color found, so set the second color 1/3 the value
p_r2 = p_r1 / 3;
p_g2 = p_g1 / 3;
p_b2 = p_b1 / 3;
}
in_semaphor = 1;
personal_effect_until = now + PERSONAL_EFFECT_TIME;
}
remove("/var/www/html/bl_semaphor/outfile.txt");
return(in_semaphor);
}
// effect sub functions
void FadeBuffer(uint8_t *buffer, uint8_t fade_val)
{
for(int i = 0; i < NUM_LEDS * 3; i++)
{
if(buffer[i] > fade_val)
{
buffer[i] = buffer[i] - fade_val;
}
else if(buffer[i] > 0)
{
buffer[i] = 0;
}
}
}
void MixBuffers(uint8_t *buffer1, uint8_t *buffer2, uint8_t *mixed_buffer, uint8_t mix_effect)
{
if(mix_effect == REPLACE)
{
for(int i = 0; i < NUM_LEDS; i++)
{
switch(mix_effect)
{
case REPLACE:
if(buffer2[i*3] || buffer2[i*3+1] || buffer2[i*3+2])
{
mixed_buffer[i*3] = buffer2[i*3];
mixed_buffer[i*3+1] = buffer2[i*3+1];
mixed_buffer[i*3+2] = buffer2[i*3+2];
}
else
{
mixed_buffer[i*3] = buffer1[i*3];
mixed_buffer[i*3+1] = buffer1[i*3+1];
mixed_buffer[i*3+2] = buffer1[i*3+2];
}
break;
}
}
}
else
{
for(int i = 0; i < NUM_LEDS * 3; i++)
{
switch(mix_effect)
{
case HARD_MIX:
mixed_buffer[i] = std::min((buffer1[i] + buffer2[i]), 255);
break;
case SUBTRACT:
mixed_buffer[i] = std::max((buffer1[i] - buffer2[i]), 0);
break;
case XOR:
mixed_buffer[i] = buffer1[i] ^ buffer2[i];
break;
case MAX:
mixed_buffer[i] = std::max(buffer1[i], buffer2[i]);
break;
}
}
}
}
void FadeToBuffer(uint8_t *buffer1, uint8_t *buffer2, uint8_t mix_percentage)
{
float diff = 0;
for(int i = 0; i < NUM_LEDS * 3; i++)
{
diff = ((float(buffer2[i]) - float(buffer1[i])) * (float (mix_percentage)/100));
// printf("%i %i %i\n", buffer1[i], buffer2[i], diff);
buffer1[i] = uint8_t(float(buffer1[i]) + diff);
}
}
uint8_t RandomColor(uint8_t seed)
{
if((rand() % 255) < seed)
{
return rand() % 255;
}
else
{
return 0;
}
}
void Rotate(uint8_t *buffer, uint8_t direction)
{
uint8_t r, g, b;
if(direction)
{
b = buffer[0];
g = buffer[1];
r = buffer[2];
for(int i = 0; i < (NUM_LEDS - 1); i++)
{
buffer[i*3] = buffer[(i+1)*3];
buffer[i*3+1] = buffer[(i+1)*3+1];
buffer[i*3+2] = buffer[(i+1)*3+2];
}
buffer[(NUM_LEDS-1)*3] = b;
buffer[(NUM_LEDS-1)*3+1] = g;
buffer[(NUM_LEDS-1)*3+2] = r;
}
else
{
b = buffer[(NUM_LEDS-1)*3];
g = buffer[(NUM_LEDS-1)*3+1];
r = buffer[(NUM_LEDS-1)*3+2];
for(int i = (NUM_LEDS - 1); i > 0; i--)
{
buffer[i*3] = buffer[(i-1)*3];
buffer[i*3+1] = buffer[(i-1)*3+1];
buffer[i*3+2] = buffer[(i-1)*3+2];
}
buffer[0] = b;
buffer[1] = g;
buffer[2] = r;
}
}
void Fill(uint8_t *buffer, int start_led, int end_led, float r1, float g1, float b1, float r2, float g2, float b2)
{
float r, g, b, r_inc, g_inc, b_inc;
int steps;
steps = end_led - start_led;
if(steps)
{
r_inc = (r2 - r1)/steps;
g_inc = (g2 - g1)/steps;
b_inc = (b2 - b1)/steps;
r = r1;
g = g1;
b = b1;
for(int i = start_led; i <= end_led; i++)
{
buffer[i*3] = b;
buffer[i*3+1] = g;
buffer[i*3+2] = r;
r = r + r_inc;
g = g + g_inc;
b = b + b_inc;
}
}
else
{
// we can't divide by zero so just set the start LED to color 1
buffer[start_led*3] = b1;
buffer[start_led*3+1] = g1;
buffer[start_led*3+2] = r1;
}
}
void SinFade(uint8_t *buffer, uint8_t mode, int start_led, int size, float r1, float g1, float b1, float r2, float g2, float b2)
{
// sine fade color 1 to color 2 to color 1
float r, g, b;
float result;
float adjval;
for(int i = 0; i <= size; i++)
{
adjval = (180/size)*i;
result = sin(adjval * PI / 180);
r = (r2 * result) + (r1 * (1-result));
g = (g2 * result) + (g1 * (1-result));
b = (b2 * result) + (b1 * (1-result));
int led_num = (start_led + i) % NUM_LEDS;
if(mode)
{
buffer[led_num*3] = uint8_t (std::max(b, float(buffer[led_num*3])));
buffer[led_num*3+1] = uint8_t (std::max(g, float(buffer[led_num*3+1])));
buffer[led_num*3+2] = uint8_t (std::max(r, float(buffer[led_num*3+2])));
}
else
{
buffer[led_num*3] = uint8_t (b);
buffer[led_num*3+1] = uint8_t (g);
buffer[led_num*3+2] = uint8_t (r);
}
}
}
// effect functions
void FadeOut(void) {
// fade out
for (uint8_t loops=0; loops < 16; loops++)
{
FadeBuffer(display_buffer, FAST_FADE_VAL);
DisplayBuffer(display_buffer);
usleep(20);
}
}
void WaitUntil(long num_seconds) {
// fade out
time_t until = time(0) + num_seconds;
while(time(0) < until)
{
usleep(200);
if(signaled)
{
break;
}
}
}
void Sparkle(long num_seconds)
{
uint8_t red_val = 0;
uint8_t green_val = 0;
uint8_t blue_val = 0;
cout << "Random Sparkle\n";
time_t until = time(0) + num_seconds;
while(time(0) < until)
{
int pwmnum = rand() % NUM_LEDS;
red_val = RandomColor(128);
green_val = RandomColor(128);
blue_val = RandomColor(128);
display_buffer[pwmnum*3] = blue_val;
display_buffer[pwmnum*3+1] = green_val;
display_buffer[pwmnum*3+2] = red_val;
DisplayBuffer(display_buffer);
FadeBuffer(display_buffer, FADE_VAL);
usleep(20);
if(signaled)
{
break;
}
}
}
void SlowSparkle(long num_seconds)
{
uint8_t red_val = 0;
uint8_t green_val = 0;
uint8_t blue_val = 0;
cout << "Slow Sparkle\n";
time_t until = time(0) + num_seconds;
while(time(0) < until)
{
int pwmnum = rand() % NUM_LEDS;
red_val = RandomColor(128);
green_val = RandomColor(128);
blue_val = RandomColor(128);
buffer2[pwmnum*3] = blue_val;
buffer2[pwmnum*3+1] = green_val;
buffer2[pwmnum*3+2] = red_val;
pwmnum = rand() % NUM_LEDS;
buffer2[pwmnum*3] = 0;
buffer2[pwmnum*3+1] = 0;
buffer2[pwmnum*3+2] = 0;
pwmnum = rand() % NUM_LEDS;
buffer2[pwmnum*3] = 0;
buffer2[pwmnum*3+1] = 0;
buffer2[pwmnum*3+2] = 0;
DisplayBuffer(display_buffer);
FadeToBuffer(display_buffer, buffer2, 1);
usleep(20);
if(signaled)
{
break;
}
}
}
void RandomTwoColorSparkle(long num_seconds)
{
uint8_t r1, g1, b1, r2, g2, b2;
if(p_r1 || p_r2 || p_g1 || p_g2 || p_b1 || p_b2)
{
r1 = p_r1;
g1 = p_g1;
b1 = p_b1;
b2 = p_b2;
g2 = p_g2;
r2 = p_r2;
}
else
{
r1 = RandomColor(128);
g1 = RandomColor(128);
b1 = RandomColor(128);
b2 = RandomColor(128);
g2 = RandomColor(128);
r2 = RandomColor(128);
}
cout << "Random Two Color Sparkle\n";
time_t until = time(0) + num_seconds;
while(time(0) < until)
{
int pwmnum = rand() % NUM_LEDS;
if(rand() % 255 > 128)
{
display_buffer[pwmnum*3] = b1;
display_buffer[pwmnum*3+1] = g1;
display_buffer[pwmnum*3+2] = r1;
}
else
{
display_buffer[pwmnum*3] = b2;
display_buffer[pwmnum*3+1] = g2;
display_buffer[pwmnum*3+2] = r2;
}
DisplayBuffer(display_buffer);
FadeBuffer(display_buffer, FADE_VAL);
usleep(20);
if(signaled)
{
break;
}
}
}
void SlowTwoColorSparkle(long num_seconds)
{
uint8_t r1, g1, b1, r2, g2, b2;
if(p_r1 || p_r2 || p_g1 || p_g2 || p_b1 || p_b2)
{
r1 = p_r1;
g1 = p_g1;
b1 = p_b1;
b2 = p_b2;
g2 = p_g2;
r2 = p_r2;
}
else
{
r1 = RandomColor(128);
g1 = RandomColor(128);
b1 = RandomColor(128);
b2 = RandomColor(128);
g2 = RandomColor(128);
r2 = RandomColor(128);
}
cout << "Slow Two Color Sparkle\n";
time_t until = time(0) + num_seconds;
while(time(0) < until)
{
int pwmnum = rand() % NUM_LEDS;
if(rand() % 255 > 128)
{
buffer2[pwmnum*3] = b1;
buffer2[pwmnum*3+1] = g1;
buffer2[pwmnum*3+2] = r1;
}
else
{
buffer2[pwmnum*3] = b2;
buffer2[pwmnum*3+1] = g2;
buffer2[pwmnum*3+2] = r2;
}
pwmnum = rand() % NUM_LEDS;
buffer2[pwmnum*3] = 0;
buffer2[pwmnum*3+1] = 0;
buffer2[pwmnum*3+2] = 0;
pwmnum = rand() % NUM_LEDS;
buffer2[pwmnum*3] = 0;
buffer2[pwmnum*3+1] = 0;
buffer2[pwmnum*3+2] = 0;
DisplayBuffer(display_buffer);
FadeToBuffer(display_buffer, buffer2, 1);
usleep(20);
if(signaled)
{
break;
}
}
}
void RandomTwoColorFade(long num_seconds)
{
uint8_t r1, g1, b1, r2, g2, b2;
uint8_t direction = rand() % 2;
cout << "Random Two Color Fade ";
if(direction)
{
cout << "Right\n";
}
else
{
cout << "Left\n";
}
if(p_r1 || p_r2 || p_g1 || p_g2 || p_b1 || p_b2)
{
r1 = p_r1;
g1 = p_g1;
b1 = p_b1;
b2 = p_b2;
g2 = p_g2;
r2 = p_r2;
}
else
{
r1 = RandomColor(128);
g1 = RandomColor(128);
b1 = RandomColor(128);
b2 = RandomColor(128);
g2 = RandomColor(128);
r2 = RandomColor(128);
}
Fill(display_buffer, 0,321,r1,g1,b1,r2,g2,b2);
Fill(display_buffer, 322,645,r2,g2,b2,r1,g1,b1);
DisplayBuffer(display_buffer);