-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrangerLights.h
139 lines (127 loc) · 4.21 KB
/
StrangerLights.h
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
//
// strange creepy pattern with a side of demigorgon
//
class StrangerLights: public PatternBase {
enum LightMode { Normal, Stutter, ShortCircuit, BrownOut, Demogorgon };
const int stutter_odds = 12;
const int burnout_odds = 8;
const int short_circuit_odds = 110;
const int brownout_odds = 90;
const float brightness_reduction = 0.6;
CRGB color_bases[ShapedLED::num_leds];
Flicker primary;
Flicker effect;
float stutter_amount = 0.2;
int current_position = 0;
LightMode current_mode = Normal;
int demo_phase = 0;
void randomizeColor(int idx) {
// sometimes skip an led leaving it dark to simulate a burned out bulb
if (random(burnout_odds) == 0) {
color_bases[idx].r = 0;
color_bases[idx].b = 0;
color_bases[idx].b = 0;
} else {
color_bases[idx].r = random(default_palette->getIntensity() * brightness_reduction);
color_bases[idx].g = random(default_palette->getIntensity() * brightness_reduction);
color_bases[idx].b = random(default_palette->getIntensity() * brightness_reduction);
}
}
bool nextPosition() {
bool more_positions = true;
// current led at max intensity, go to next led
if (current_position < (shape->num_leds - 1)) {
current_position++;
randomizeColor(current_position);
} else {
more_positions = false;
}
return more_positions;
}
void badThingsHappen() {
if (primary.getIntensity() > (stutter_amount + 0.2) && random(stutter_odds) == 1) {
current_mode = Stutter;
} else if (random(short_circuit_odds) == 1) {
current_mode = ShortCircuit;
} else if (random(brownout_odds) == 1) {
current_mode = BrownOut;
}
}
void doDemogorgon() {
bool all_fade_complete = true;
switch(demo_phase) {
case 2:
case 0:
for (int index = 0; index < shape->num_leds; index++) {
if (!FadeUtils::fadeTo(shape->leds, index, default_palette->cWhite, 1)) {
all_fade_complete = false;
}
}
break;
case 3:
case 1:
for (int index = 0; index < shape->num_leds; index++) {
if (!FadeUtils::fadeTo(shape->leds, index, color_bases[index], 1)) {
all_fade_complete = false;
}
}
break;
case 4:
for (int index = 0; index < shape->num_leds; index++) {
shape->leds[index] = default_palette->cBlack;
color_bases[index] = default_palette->cBlack;
}
current_position = 0;
current_mode = Normal;
break;
}
if (all_fade_complete) {
demo_phase++;
}
}
public:
StrangerLights(ShapedLED* shape, ColorPalette* default_palette): PatternBase(shape, default_palette) {
pattern_run_delay = 11;
}
void run() {
if (timeToRun()) {
setNextRun();
int num_flickers = 3 + random(6);
float intensity_increment = 0.9 / (float)num_flickers;
switch (current_mode) {
case Normal:
if (primary.flicker(current_position, current_position, shape->leds, 0.1, 1.0, 0.01, false, color_bases, intensity_increment, shape->num_leds)) {
if (!nextPosition()) {
current_mode = Demogorgon;
demo_phase = 0;
}
} else {
badThingsHappen();
}
break;
case Stutter:
if (effect.flicker(current_position, current_position, shape->leds, primary.getIntensity(), primary.getIntensity() - stutter_amount, 0.01, true, color_bases, intensity_increment, shape->num_leds))
{
current_mode = Normal;
}
break;
case ShortCircuit:
// flicker all leds quickly down to almost off and back up
if (effect.flicker(0, current_position, shape->leds, 1.0, 0.2, 0.0, true, color_bases, 0.3, shape->num_leds)) {
current_mode = Normal;
}
break;
case BrownOut:
// flicker all leds slowly down and up just a bit
if (effect.flicker(0, current_position, shape->leds, 1.0, 0.6, 0.3, true, color_bases, 0.02, shape->num_leds)) {
current_mode = Normal;
}
break;
case Demogorgon:
doDemogorgon();
break;
}
FastLED.show();
}
}
};