-
Notifications
You must be signed in to change notification settings - Fork 42
/
Light.cpp
1708 lines (1578 loc) · 56.5 KB
/
Light.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
/*
* Light.cpp
*
* author: Sebastien CAPOU (neskweek@gmail.com) and Andras Kun (kun.andras@yahoo.de)
* Source : https://github.com/Protonerd/FX-SaberOS
*/
#include "Light.h"
#include "Config_HW.h"
#include "Config_SW.h"
#include "ConfigMenu.h"
#include "Soundfont.h"
#if defined PIXELBLADE
#include <WS2812.h>
#endif
// global Saber state and Sub State variables
extern SaberStateEnum SaberState;
extern SaberStateEnum PrevSaberState;
extern ActionModeSubStatesEnum ActionModeSubStates;
extern ConfigModeSubStatesEnum ConfigModeSubStates;
extern ActionModeSubStatesEnum PrevActionModeSubStates;
extern ConfigModeSubStatesEnum PrevConfigModeSubStates;
//extern SubStateEnum SubState;
extern bool lockuponclash;
extern bool tipmeltonclash;
extern long tipmeltStart;
extern int8_t modification;
extern struct StoreStruct {
// This is for mere detection if they are our settings
char version[5];
// The settings
uint8_t volume;// 0 to 31
uint8_t soundFont;// as many as Sound font you have defined in Soundfont.h Max:253
struct Profile {
cRGB mainColor;
cRGB clashColor;
cRGB blasterboltColor;
uint16_t swingSensitivity;
uint8_t flickerType;
uint8_t poweronoffType;
}sndProfile[SOUNDFONT_QUANTITY];
}storage;
extern SoundFont soundFont;
extern uint8_t ledPins[];
#if defined ACCENT_LED or defined PIXEL_ACCENT
unsigned long lastAccent = millis();
#endif
#ifdef JUKEBOX
#define SAMPLESIZEAVERAGE 30
#endif
const uint8_t TIP_PIXELS=25; // the number of pixels to animate in tipmelt mode
bool fireblade=false;
#ifdef PIXELBLADE // FIREBLADE
#ifdef ANIBLADE
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 50, suggested range 20-100
static uint8_t Fire_Cooling = 100;//50;
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
static uint8_t Fire_Sparking = 50;//100;
//#ifdef CROSSGUARDSABER
//static byte heat[MN_STRIPE];
//static byte heat_cg[CG_STRIPE];
//#else
//#endif
#define PIXELSTEP 2 // how many pixel to treat as a group to save on processing capability
static byte heat[NUMPIXELS/PIXELSTEP];
#endif ANIBLADE
#endif // PIXELBLADE
/* ***************** UNIFIED LIGHT FUNCTIONS *********************/
#define I_BEGINNEXTSEGMENT 50
#define R_BEGINNEXTSEGMENT 100
#define PULSEFLICKERDEPTH 100
#define PULSEDURATION 500
uint8_t pulseflicker_pwm=0;
bool pulsedir=true;
static uint8_t flickerPos = 0;
static long lastFlicker = millis();
#if defined PIXELBLADE
extern WS2812 pixels;
#endif
#ifdef PIXEL_ACCENT
extern WS2812 accentPixels;
#endif
#if defined STAR_LED or defined PIXELBLADE or defined ADF_PIXIE_BLADE
extern cRGB currentColor;
#endif
#ifdef ADF_PIXIE_BLADE
SoftwareSerial pixieSerial(-1, PIXIEPIN);
Adafruit_Pixie strip = Adafruit_Pixie(NUMPIXELS, &pixieSerial);
#endif
#ifdef COLOR_PROFILE
// define an array for the 15 color profiles
cRGB colorProfiles[15];
//colorProfiles[0].r=255;
#endif
// ====================================================================================
// === LED FUNCTIONS ===
// ====================================================================================
/* ***************** UNIFIED LIGHT FUNCTIONS *********************/
void lightOn(uint8_t ledPins[], int8_t segment = -1, cRGB color={0,0,0}, int8_t StartPixel=-1, int8_t StopPixel=-1) {
// Light On
#if defined LEDSTRINGS
if (segment == -1) {
for (uint8_t i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
}
} else {
digitalWrite(ledPins[segment], HIGH);
}
#endif
#if defined STAR_LED
// Light On
analogWrite(ledPins[0], color.r); // RED
analogWrite(ledPins[1], color.g); // GREEN
analogWrite(ledPins[2], color.b); // BLUE
#endif
#ifdef ADF_PIXIE_BLADE
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, color.r, color.g, color.b);
}
strip.show();
#endif
#ifdef PIXELBLADE
// Light On
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe
for (uint8_t i = 0; i <= NUMPIXELS; i++) {
pixels.set_crgb_at(i, color);
}
} else {
for (uint8_t i = StartPixel; i <= StopPixel; i++) {
pixels.set_crgb_at(i, color);
}
}
pixels.sync();
#endif
} //lightOn
void lightOff(uint8_t ledPins[], int8_t segment = -1, int8_t StartPixel=-1, int8_t StopPixel=-1) {
#if defined LEDSTRINGS
// shut Off
//Shut down PWM
TCCR0A &= ~((1 << COM0A1) | (1 << COM0B1));
TCCR1A &= ~((1 << COM1A1) | (1 << COM1B1));
TCCR2A &= ~((1 << COM2A1) | (1 << COM2B1));
//Shut down everything at once
PORTB &= B11010001;
PORTD &= B10010111;
#endif
#if defined STAR_LED
// shut Off
digitalWrite(LED_RED, LOW); // RED
digitalWrite(LED_GREEN, LOW); // GREEN
digitalWrite(LED_BLUE, LOW); // BLUE
#endif
#ifdef ADF_PIXIE_BLADE
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
#endif
#ifdef PIXELBLADE
// shut Off
cRGB value;
value.b = 0;
value.g = 0;
value.r = 0; // RGB Value -> Off
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe
for (uint16_t i = 0; i <= NUMPIXELS; i++) {
pixels.set_crgb_at(i, value);
}
}
else {
for (uint8_t i = StartPixel; i <= StopPixel; i++) {
pixels.set_crgb_at(i, value);
}
}
pixels.sync();
#endif
} //lightOff
void getColor(cRGB color={0,0,0}) {
#if defined LEDSTRINGS
#endif
#if defined STAR_LED
currentColor.r = color.r;
currentColor.g = color.g;
currentColor.b = color.b;
#endif
#ifdef ADF_PIXIE_BLADE
currentColor.r = color.r;
currentColor.g = color.g;
currentColor.b = color.b;
#endif
#ifdef PIXELBLADE
currentColor.r = color.r;
currentColor.g = color.g;
currentColor.b = color.b;
#endif
} // getColor
void RampBlade(uint16_t RampDuration, bool DirectionUpDown, int8_t StartPixel=-1, int8_t StopPixel=-1) {
#if defined LEDSTRINGS
#endif
#if defined STAR_LED
#endif
#ifdef ADF_PIXIE_BLADE
#endif
#ifdef PIXELBLADE
unsigned long ignitionStart = millis(); //record start of ramp function
cRGB value;
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe // neopixel ramp code from jbkuma
StartPixel=0;
StopPixel= NUMPIXELS;
}
if (fireblade) { // #ifdef FIREBLADE
#ifdef ANIBLADE
for (unsigned int i=StartPixel; i<StopPixel; (i=i+5)) { // turn on/off one LED at a time
FireBlade(storage.sndProfile[storage.soundFont].flickerType-2);
for(unsigned int j=0; j<StopPixel; j++ ) { // fill up string with data
if ((DirectionUpDown and j<=i) or (!DirectionUpDown and j<=StopPixel-1-i)){
}
else if ((DirectionUpDown and j>i) or (!DirectionUpDown and j>StopPixel-1-i)){
value.r=0;
value.g=0;
value.b=0;
//heat[j]=0;
pixels.set_crgb_at(j, value); // Set value at LED found at index j
}
}
pixels.sync(); // Sends the data to the LEDs
}
#endif ANIBLADE
} // fireblade
else { //#else
for (unsigned int i = StartPixel; i < StopPixel; i = StopPixel*(millis()-ignitionStart)/RampDuration) { // turn on/off the number of LEDs that match rap timing
//generate a flicker effect between 65% and 115% of MAX_BRIGHTNESS, with a 1 in 115 chance of flicking to 0
int flickFactor = random(0,115);
if (flickFactor < 65 && flickFactor > 0) { flickFactor = 100; }
for(uint8_t j=StartPixel; j<=StopPixel; j++ ) { // fill up string with data
if ((DirectionUpDown and j<=i)){
value.r = MAX_BRIGHTNESS * i / NUMPIXELS * currentColor.r / rgbFactor * flickFactor / 100;
value.g = MAX_BRIGHTNESS * i / NUMPIXELS * currentColor.g / rgbFactor * flickFactor / 100;
value.b = MAX_BRIGHTNESS * i / NUMPIXELS * currentColor.b / rgbFactor * flickFactor / 100;
} else if (!DirectionUpDown and j<=NUMPIXELS-1-i){
value.r = MAX_BRIGHTNESS * (NUMPIXELS - i) / NUMPIXELS * currentColor.r / rgbFactor * flickFactor / 100;
value.g = MAX_BRIGHTNESS * (NUMPIXELS - i) / NUMPIXELS * currentColor.g / rgbFactor * flickFactor / 100;
value.b = MAX_BRIGHTNESS * (NUMPIXELS - i) / NUMPIXELS * currentColor.b / rgbFactor * flickFactor / 100;
} else if ((DirectionUpDown and j>i) or (!DirectionUpDown and j>NUMPIXELS-1-i)){
value.r=0;
value.g=0;
value.b=0;
}
pixels.set_crgb_at(j, value);
}
pixels.sync(); // Sends the data to the LEDs
delay(RampDuration/(StopPixel-StartPixel)); //match the ramp duration to the number of pixels in the string
}
} // #endif
#endif
} // RampBlade
void lightIgnition(uint8_t ledPins[], uint16_t time, uint8_t type, cRGB color={0,0,0}, int8_t StartPixel=-1, int8_t StopPixel=-1) {
#if defined LEDSTRINGS
uint8_t LS_Status[6];
bool ongoing=true;
switch (type) {
default:
case 0:
for (uint8_t i=0; i<6; i++) {
LS_Status[i]=0;
}
while (ongoing) { // do the loops as long the variable is set to false, when the last segment finsihed the ramp
for (uint8_t i = 0; i < 6; i++) {
analogWrite(ledPins[i], LS_Status[i]);
if (i==0 and LS_Status[i]<255) {
LS_Status[i]++;
}
else if (i>0 and LS_Status[i-1]>=I_BEGINNEXTSEGMENT and LS_Status[i]<255) {
LS_Status[i]++;
}
if (LS_Status[5]==255) {
ongoing=false;
}
}
delayMicroseconds(time * (1000/(5*I_BEGINNEXTSEGMENT+255)));
}
// ramp down to MAX_BRIGHTNESS
for (uint8_t j = 255; j >= MAX_BRIGHTNESS; j--) {
for (uint8_t i = 0; i < 6; i++) {
analogWrite(ledPins[i], j);
}
delay(3);
}
/*
// Light up the ledstrings Movie-like
for (uint8_t i = 0; i < 6; i++) {
for (uint8_t j=0; j<=MAX_BRIGHTNESS;j+=10) {
analogWrite(ledPins[i], j);
delay(time / (6*25));
}
//delay(time / (5*10));
}
*/
break;
case 1:
for (int8_t i = 5; i >= 0; i--) {
for (uint8_t j = 0; j <= i; j++) {
if (j > 0) {
digitalWrite(ledPins[j - 1], LOW);
}
digitalWrite(ledPins[j], HIGH);
delay(time / 20);
}
}
break;
}
#endif
#if defined STAR_LED
// Fade in to Maximum brightness
for (uint8_t fadeIn = 0; fadeIn < 255; fadeIn++) {
analogWrite(LED_RED, color.r * fadeIn / 255); // RED
analogWrite(LED_GREEN, color.g * fadeIn / 255); // GREEN
analogWrite(LED_BLUE, color.b * fadeIn / 255); // BLUE
delay(time / 255);
}
#endif
#ifdef ADF_PIXIE_BLADE
// Fade in to Maximum brightness
for (uint8_t fadeIn = 0; fadeIn < 255; fadeIn++) {
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, color.r * fadeIn / 255, color.g * fadeIn / 255, color.b * fadeIn / 255);
}
strip.show();
delay(time / 255);
}
#endif
#ifdef PIXELBLADE
cRGB value;
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe // neopixel ramp code from jbkuma
StartPixel=0;
StopPixel= NUMPIXELS;
}
value.r = MAX_BRIGHTNESS * color.r / rgbFactor;
value.g = MAX_BRIGHTNESS * color.g / rgbFactor;
value.b = MAX_BRIGHTNESS * color.b / rgbFactor;
//switch (type) {
// case 0:
// // Light up the ledstrings Movie-like
RampBlade(time, true, StartPixel, StopPixel);
//}
#endif
} // lightIgnition
void lightRetract(uint8_t ledPins[], uint16_t time, uint8_t type,cRGB color={0,0,0}, int8_t StartPixel=-1, int8_t StopPixel=-1) {
#if defined LEDSTRINGS
uint8_t LS_Status[6];
bool ongoing=true;
switch (type) {
default:
case 0:
// Light off the ledstrings Movie Like
for (uint8_t i=0; i<6; i++) {
LS_Status[i]=MAX_BRIGHTNESS;
}
while (ongoing) { // do the loops as long the variable is set to false, when the last segment finsihed the ramp
for (uint8_t i = 0; i < 6; i++) {
if (i==5 and LS_Status[i]>0) {
LS_Status[i]--;
}
else if (i<5 and LS_Status[i+1]<=R_BEGINNEXTSEGMENT and LS_Status[i]>0) {
LS_Status[i]--;
}
if (LS_Status[0]==0) {
ongoing=false;
}
analogWrite(ledPins[i], LS_Status[i]);
}
delayMicroseconds(time * (1000/(5*(MAX_BRIGHTNESS-R_BEGINNEXTSEGMENT)+MAX_BRIGHTNESS)));
}
break;
case 1:
// Light off the ledstrings invert
for (int8_t i = 5; i >= 0; i--) {
for (uint8_t j = 0; j <= i; j++) {
if (j > 0) {
digitalWrite(ledPins[j - 1], HIGH);
}
digitalWrite(ledPins[j], LOW);
delay(time / 20);
}
}
break;
}
#endif
#if defined STAR_LED
// Fade in to Maximum brightness
for (uint8_t fadeIn = 255; fadeIn > 0; fadeIn--) {
analogWrite(LED_RED, color.r * fadeIn / 255); // RED
analogWrite(LED_GREEN, color.g * fadeIn / 255); // GREEN
analogWrite(LED_BLUE, color.b * fadeIn / 255); // BLUE
delay(time / 255);
}
#endif
#ifdef ADF_PIXIE_BLADE
// Fade in to Maximum brightness
for (uint8_t fadeIn = 255; fadeIn > 0; fadeIn--) {
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, color.r * fadeIn / 255, color.g * fadeIn / 255, color.b * fadeIn / 255);
}
strip.show();
delay(time / 255);
}
#endif
#ifdef PIXELBLADE
//switch (type) {
// default:
// case 0:
// Light off the ledstrings Movie Like
cRGB value;
value.b = 0;
value.g = 0;
value.r = 0; // RGB Value -> Off
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe // neopixel ramp code from jbkuma
StartPixel=0;
StopPixel= NUMPIXELS;
}
RampBlade(time, false, StartPixel, StopPixel);
//break;
if (fireblade) { // #ifdef FIREBLADE
#ifdef ANIBLADE
for(unsigned int j=0; j<sizeof(heat); j++ ) { // clear the heat static variables
heat[j]=0;
}
#endif // ANIBLADE
}
#endif
} // lightRetract
void lightFlicker(uint8_t ledPins[],uint8_t type, uint8_t value = 0,cRGB maincolor={0,0,0}, cRGB clashcolor={0,0,0},uint8_t AState=0, int8_t StartPixel=-1 , int8_t StopPixel=-1) {
uint8_t brightness;
#if defined LEDSTRINGS
switch (type) {
default:
case 0:
// // AudioTracker Flickering
brightness = constrain(MAX_BRIGHTNESS
- (abs(analogRead(SPK1) - analogRead(SPK2)))*31/storage.volume,0,255);
for (uint8_t i = 0; i <= 5; i++) {
analogWrite(ledPins[i], brightness);
}
break;
case 1:
// anarchic Flickering
brightness = constrain(MAX_BRIGHTNESS
- random(FLICKERDEPTH),0,255);
for (uint8_t i = 0; i <= 5; i++) {
if (i != flickerPos)
analogWrite(ledPins[i], brightness);
else
analogWrite(ledPins[i], MAX_BRIGHTNESS);
}
if ((flickerPos != 0
and millis() - lastFlicker > (120 - (100 - 15 * flickerPos)))
or (flickerPos == 0 and millis() - lastFlicker > 300)) {
flickerPos++;
lastFlicker = millis();
if (flickerPos == 6) {
flickerPos = 0;
}
}
break;
case 2:
// pulse Flickering
if (((millis()-lastFlicker>=PULSEDURATION/PULSEFLICKERDEPTH) and AState != AS_BLADELOCKUP) or ((millis()-lastFlicker>=2) and AState == AS_BLADELOCKUP)) {
lastFlicker=millis();
for (uint8_t i = 0; i <= 5; i++) {
analogWrite(ledPins[i],MAX_BRIGHTNESS - pulseflicker_pwm);
}
if (pulsedir) {
pulseflicker_pwm++;
}
else {
pulseflicker_pwm--;
}
if (pulseflicker_pwm == PULSEFLICKERDEPTH) {
pulsedir=false;
}
else if (pulseflicker_pwm == 0) {
pulsedir=true;
}
}
break;
}
#endif
#if defined STAR_LED
switch (type) {
default:
case 0: // AudioTracker Flickering
brightness = constrain(MAX_BRIGHTNESS
- (abs(analogRead(SPK1) - analogRead(SPK2)))*31/storage.volume,0,255);
if (AState==AS_BLADELOCKUP) { //animate blade in lockup mode
// gives 25% chance to flick larger range for better randomization
int lockupFlick = random(0,39);
if (lockupFlick < 25) {
analogWrite(LED_RED, (brightness * maincolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, (brightness * maincolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, (brightness * maincolor.b / rgbFactor)); // BLUE
} else if (lockupFlick < 35) {
analogWrite(LED_RED, (brightness * clashcolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, (brightness * clashcolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, (brightness * clashcolor.b / rgbFactor)); // BLUE
}
else { // simple white
analogWrite(LED_RED, MAX_BRIGHTNESS); // RED
analogWrite(LED_GREEN, MAX_BRIGHTNESS); // GREEN
analogWrite(LED_BLUE, MAX_BRIGHTNESS); // BLUE
}
}
else if (AState==AS_CLASH) {
analogWrite(LED_RED, (brightness * clashcolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, (brightness * clashcolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, (brightness * clashcolor.b / rgbFactor)); // BLUE
}
else {
analogWrite(LED_RED, (brightness * maincolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, (brightness * maincolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, (brightness * maincolor.b / rgbFactor)); // BLUE
}
break;
case 1: // pulse flickering
if (((millis()-lastFlicker>=PULSEDURATION/PULSEFLICKERDEPTH) and AState != AS_BLADELOCKUP) or ((millis()-lastFlicker>=2) and AState == AS_BLADELOCKUP)) {
lastFlicker=millis();
if (AState==AS_CLASH) {
analogWrite(LED_RED, ((MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, ((MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, ((MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.b / rgbFactor)); // BLUE
}
else {
analogWrite(LED_RED, ((MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.r / rgbFactor)); // RED
analogWrite(LED_GREEN, ((MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.g / rgbFactor)); // GREEN
analogWrite(LED_BLUE, ((MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.b / rgbFactor)); // BLUE
}
if (pulsedir) {
pulseflicker_pwm++;
}
else {
pulseflicker_pwm--;
}
if (pulseflicker_pwm == PULSEFLICKERDEPTH) {
pulsedir=false;
}
else if (pulseflicker_pwm == 0) {
pulsedir=true;
}
}
break;
case 2: // static blade
if (AState==AS_CLASH) {
analogWrite(LED_RED, clashcolor.r ); // RED
analogWrite(LED_GREEN, clashcolor.g); // GREEN
analogWrite(LED_BLUE, clashcolor.b); // BLUE
}
else {
analogWrite(LED_RED, maincolor.r ); // RED
analogWrite(LED_GREEN, maincolor.g); // GREEN
analogWrite(LED_BLUE, maincolor.b); // BLUE
}
break;
}
#endif
#ifdef ADF_PIXIE_BLADE
for(uint8_t i=0; i< NUMPIXELS; i++) {
Serial.print("\t");Serial.print(brightness);Serial.print("\t");Serial.print(maincolor.g);Serial.print("\t");Serial.println((brightness * maincolor.r) / rgbFactor);
//strip.setPixelColor(i, ((brightness * maincolor.r) / rgbFactor), ((brightness * maincolor.r) / rgbFactor), ((brightness * maincolor.r) / rgbFactor));//maincolor.r, maincolor.g, maincolor.b);
strip.setPixelColor(i, maincolor.r,maincolor.g, maincolor.b);//maincolor.r, maincolor.g, maincolor.b);
}
strip.show();
#endif
#ifdef PIXELBLADE
if (StartPixel == -1 or StopPixel==-1 or StopPixel<StartPixel or StartPixel>NUMPIXELS or StopPixel>NUMPIXELS) { // if neither start nor stop is defined or invalid range, go through the whole stripe // neopixel ramp code from jbkuma
StartPixel=0;
#ifdef TIP_MELT
if (AState==AS_TIPMELT)
StopPixel=NUMPIXELS-TIP_PIXELS;
else
#endif
StopPixel=NUMPIXELS;
}
int flickFactor = random(0,255);
if (flickFactor > 3 && flickFactor < 170) { flickFactor = 255; }
//brightness = 255 * flickFactor / 100;
brightness = flickFactor;
cRGB color;
switch (type) {
default:
case 0:
// use random generated values instead of AudioTracker values for neopixel to reduce loop time i.e. improve motion sensitivity and reaction
brightness = constrain(MAX_BRIGHTNESS
- random(FLICKERDEPTH),0,255); // AudioTracker Flickering
if (AState==AS_BLADELOCKUP) { //animate blade in lockup mode
// gives 25% chance to flick larger range for better randomization
int lockupFlick = random(0,39);
if (lockupFlick < 20) {
color.r = brightness * maincolor.r / rgbFactor;
color.g = brightness * maincolor.g / rgbFactor;
color.b = brightness * maincolor.b / rgbFactor;
} else if (lockupFlick < 30) {
color.r = brightness * clashcolor.r / rgbFactor;
color.g = brightness * clashcolor.g / rgbFactor;
color.b = brightness * clashcolor.b / rgbFactor;
}
else { // simple white
color.r = MAX_BRIGHTNESS;
color.g = MAX_BRIGHTNESS;
color.b = MAX_BRIGHTNESS;
}
}
else if (AState==AS_CLASH) {
color.r = brightness * clashcolor.r / rgbFactor;
color.g = brightness * clashcolor.g / rgbFactor;
color.b = brightness * clashcolor.b / rgbFactor;
}
else { //normal operation
color.r = brightness * maincolor.r / rgbFactor;
color.g = brightness * maincolor.g / rgbFactor;
color.b = brightness * maincolor.b / rgbFactor;
}
for (uint16_t i = StartPixel; i <= StopPixel; i++) {
pixels.set_crgb_at(i, color);
}
pixels.sync();
break;
case 1:
// Pulse flickering
// pulse Flickering
if (((millis()-lastFlicker>=PULSEDURATION/PULSEFLICKERDEPTH) and AState != AS_BLADELOCKUP) or (AState == AS_BLADELOCKUP)) {
lastFlicker=millis();
if (AState==AS_CLASH) {
color.r = (MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.r / rgbFactor;
color.g = (MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.g / rgbFactor;
color.b = (MAX_BRIGHTNESS - pulseflicker_pwm) * clashcolor.b / rgbFactor;
}
else {
color.r = (MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.r / rgbFactor;
color.g = (MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.g / rgbFactor;
color.b = (MAX_BRIGHTNESS - pulseflicker_pwm) * maincolor.b / rgbFactor;
}
if (pulsedir) {
pulseflicker_pwm++;
}
else {
pulseflicker_pwm--;
}
if (pulseflicker_pwm == PULSEFLICKERDEPTH) {
pulsedir=false;
}
else if (pulseflicker_pwm == 0) {
pulsedir=true;
}
for (uint16_t i = StartPixel; i <= StopPixel; i++) {
pixels.set_crgb_at(i, color);
}
pixels.sync();
}
break;
case 2: // fire blade red
if (fireblade) { // #ifdef FIREBLADE
#ifdef ANIBLADE
if (AState==AS_BLADELOCKUP or AState==AS_TIPMELT) {
Fire_Cooling=150;
Fire_Sparking=50;
}
else {
Fire_Cooling=50;
Fire_Sparking=100;
}
FireBlade(0);
pixels.sync(); // Sends the data to the LEDs
#endif // ANIBLADE
}
break;
case 3: // fire blade green
if (fireblade) { // #ifdef FIREBLADE
#ifdef ANIBLADE
if (AState==AS_BLADELOCKUP or AState==AS_TIPMELT) {
Fire_Cooling=200;
Fire_Sparking=70;
}
else {
Fire_Cooling=100;
Fire_Sparking=150;
}
FireBlade(1);
pixels.sync(); // Sends the data to the LEDs
#endif // ANIBLADE
}
break;
case 4: // fire blade blue
if (fireblade) { // #ifdef FIREBLADE
#ifdef ANIBLADE
if (AState==AS_BLADELOCKUP or AState==AS_TIPMELT) {
Fire_Cooling=100;
Fire_Sparking=20;
}
else {
Fire_Cooling=50;
Fire_Sparking=100;
}
FireBlade(2);
pixels.sync(); // Sends the data to the LEDs
#endif // ANIBLADE
}
break;
case 5: // spark blade
// use random generated values instead of AudioTracker values for neopixel to reduce loop time i.e. improve motion sensitivity and reaction
//brightness = constrain(MAX_BRIGHTNESS
//- (abs(analogRead(SPK1) - analogRead(SPK2)))*31/storage.volume/8,0,255);
brightness = constrain(MAX_BRIGHTNESS
- random(FLICKERDEPTH),0,255); // AudioTracker Flickering
color.r = brightness * maincolor.r / rgbFactor;
color.g = brightness * maincolor.g / rgbFactor;
color.b = brightness * maincolor.b / rgbFactor;
for (uint16_t i = StartPixel; i <= StopPixel; i++) {
pixels.set_crgb_at(i, color);
}
pixels.sync();
lightBlasterEffect(ledPins, random(5, NUMPIXELS - 3), map(NUMPIXELS, 10, NUMPIXELS-10, 1, 2), 0, storage.sndProfile[storage.soundFont].blasterboltColor);
break;
}
#ifdef TIP_MELT
if (AState==AS_TIPMELT) { //animate blade tip in tipmelt mode
Fire_Cooling = 55;
Fire_Sparking = 120;
static byte heat[TIP_PIXELS];
int cooldown;
// Cool down every cell a little
for( int i = 0; i < TIP_PIXELS; i++) {
cooldown = random(0, ((Fire_Cooling * 10) / TIP_PIXELS) + 2);
if(cooldown>heat[i]) {
heat[i]=0;
} else {
heat[i]=heat[i]-cooldown;
}
}
// Heat from each cell drifts 'up' and diffuses a little
for(int k= TIP_PIXELS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Randomly ignite new 'sparks' near the bottom
if(random(255) < Fire_Sparking) {
int y = random(7);
heat[y] = heat[y] + random(160,255);
}
int flickRange = 3 + constrain((millis()-tipmeltStart)/1000,0,100); // gradually increase fire effect over time
int flick = random(0,flickRange);
for (uint16_t i = 0; i <= TIP_PIXELS; i++) { // flash clash color
cRGB tipcolor;
if (flick < 2) { // clash color
color.r = brightness * clashcolor.r / rgbFactor;
color.g = brightness * clashcolor.g / rgbFactor;
color.b = brightness * clashcolor.b / rgbFactor;
if (i>=5)
tipcolor = CombineColors(maincolor,color,i*100/TIP_PIXELS); // gradient into main color
else
tipcolor = color;
} else if (flick == 2) { // flash white
color.r = MAX_BRIGHTNESS;
color.g = MAX_BRIGHTNESS;
color.b = MAX_BRIGHTNESS;
if (i>=5)
tipcolor = CombineColors(maincolor,color,i*100/TIP_PIXELS); // gradient into main color
else
tipcolor = color;
} else { // gradually heaten up the tip and show flame effect
tipcolor = HeatColor(heat[i], 0);
}
pixels.set_crgb_at(NUMPIXELS-i, tipcolor);
}
pixels.sync();
}
#endif // TIP_MELT
#endif
} // lightFlicker
cRGB CombineColors(cRGB color1, cRGB color2, int8_t percentage1) {
cRGB color;
color.r = (percentage1*color1.r + (100-percentage1)*color2.r)/100;
color.g = (percentage1*color1.g + (100-percentage1)*color2.g)/100;
color.b = (percentage1*color1.b + (100-percentage1)*color2.b)/100;
return color;
}
void ColorMixing(cRGB colorID={0,0,0}, int8_t mod=-1, uint8_t maxBrightness=MAX_BRIGHTNESS, bool Saturate=false) {
#if defined LEDSTRINGS
#endif
#if defined PIXELBLADE or defined STAR_LED or defined ADF_PIXIE_BLADE
cRGB mixedColor;
mixedColor.r=colorID.r;
mixedColor.g=colorID.g;
mixedColor.b=colorID.b;
switch(mod) {
default:
case(0):
if (Saturate) {
mixedColor.r=maxBrightness;
}
else {
mixedColor.r=constrain(colorID.r+1,0,255);
}
break;
case(1):
if (Saturate) {
mixedColor.r=0;
}
else {
mixedColor.r=constrain(colorID.r-1,0,255);
}
break;
case(2):
if (Saturate) {
mixedColor.g=maxBrightness;
}
else {
mixedColor.g=constrain(colorID.g+1,0,255);
}
break;
case(3):
if (Saturate) {
mixedColor.g=0;
}
else {
mixedColor.g=constrain(colorID.g-1,0,255);
}
break;
case(4):
if (Saturate) {
mixedColor.b=maxBrightness;
}
else {
mixedColor.b=constrain(colorID.b+1,0,255);
}
break;
case(5):
if (Saturate) {
mixedColor.b=0;
}
else {
mixedColor.b=constrain(colorID.b-1,0,255);
}
break;
}
getColor(mixedColor);
//lightOn(mixedColor, 0, NUMPIXELS-6);
#if defined LS_DEBUG
//Serial.print(storage.sndProfile[storage.soundFont].mainColor);
Serial.print("\tR:");
Serial.print(currentColor.r);
Serial.print("\tG:");
Serial.print(currentColor.g);
Serial.print(" \tB:");
Serial.println(currentColor.b);
#endif
#if defined STAR_LED
// LED_RED, LED_GREEN, LED_BLUE
analogWrite(LED_RED,currentColor.r); // RED
analogWrite(LED_GREEN, currentColor.g); // GREEN
analogWrite(LED_BLUE, currentColor.b); // BLUE
#endif
#ifdef ADF_PIXIE_BLADE
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, currentColor.r, currentColor.g, currentColor.b);
}
strip.show();
#endif
#endif
} // ColorMixing
void lightBlasterEffect(uint8_t ledPins[], uint8_t pixel, uint8_t range, uint16_t B_time=BLASTER_FX_DURATION, cRGB SndFnt_MainColor={0,0,0}) {
#if defined LEDSTRINGS
analogWrite(ledPins[random(1,5)], LOW);
delay(BLASTER_FX_DURATION);
#endif
#if defined STAR_LED
lightOn(ledPins, -1, currentColor);
delay(BLASTER_FX_DURATION);
#endif
#ifdef ADF_PIXIE_BLADE
for(uint8_t i=0; i< NUMPIXELS; i++) {
strip.setPixelColor(i, currentColor.r, currentColor.g, currentColor.b);
}
strip.show();
delay(BLASTER_FX_DURATION);
#endif
#ifdef PIXELBLADE
cRGB blastcolor;
cRGB fadecolor;
blastcolor.r=currentColor.r;
blastcolor.g=currentColor.g;
blastcolor.g=currentColor.b;
getColor(SndFnt_MainColor); // get the main blade color for the fading effect
for (uint8_t i = 0; i<=2*range-1;i++) {
if (fireblade) {
#ifdef ANIBLADE
// fully cool down (switch off LED) of a small segment of the blade, which will go up afterwards
heat[(pixel/PIXELSTEP)-i] = 0; // white hot fire burst along the whole blade
#endif // ANIBLADE
}
else {
uint8_t j=i+pixel;
if (j==pixel or j==pixel+2*range) { // 2 pixels at the edges shall be shut down
fadecolor.r = 0;
fadecolor.g = 0;
fadecolor.b = 0;
pixels.set_crgb_at(j, fadecolor);
}
else if (j==pixel+range+1) { // middle pixel full white
fadecolor.r = MAX_BRIGHTNESS;
fadecolor.g = MAX_BRIGHTNESS;
fadecolor.b = MAX_BRIGHTNESS;
pixels.set_crgb_at(j, fadecolor);
}
else { // rest of the pixels between middle and edge
pixels.set_crgb_at(j, blastcolor);
}
/*
for (uint8_t j = 0; j <=range; j++) {
if (j==i) {
pixels.set_crgb_at(pixel-j, blastcolor);