-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastPatterns.cpp
297 lines (267 loc) · 8.7 KB
/
FastPatterns.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
#include <FastPatterns.h>
PatternManager::PatternManager(int led_count) : _led_count(led_count) {
}
void PatternManager::begin(){
Serial.println("Starting Pattern manager.");
_led_array = new CRGB[_led_count];
_pattern_mask = new uint8_t[_led_count];
_brightness_array = new uint8_t[_led_count];
for (int i = 0; i < _led_count; i++) {
_brightness_array[i] = 255;
_led_array[i] = CRGB::Black;
_pattern_mask[i] = 0;
}
// Add the pattern classes into the array.
_pattern_array[0] = new PatternBase(_led_array, _pattern_mask,
_brightness_array, _led_count);
_pattern_array[1] = new RainbowCycle(_led_array, _pattern_mask,
_brightness_array, _led_count);
_pattern_array[2] = new TheatreChase(_led_array, _pattern_mask,
_brightness_array, _led_count);
_pattern_array[3] =
new Scanner(_led_array, _pattern_mask, _brightness_array, _led_count);
_pattern_array[4] =
new ColourFade(_led_array, _pattern_mask, _brightness_array, _led_count);
_pattern_array[5] =
new Twinkle(_led_array, _pattern_mask, _brightness_array, _led_count);
Serial.println("Pattern Manager started..");
}
void PatternManager::loop() {
for (int i = 0; i < PATTERN_COUNT; i++) {
_pattern_array[i]->loop();
}
}
PatternBase* PatternManager::get_pattern(uint pattern_id) {
return _pattern_array[pattern_id];
}
LedInfo PatternManager::get_led_info(uint16_t led) {
LedInfo led_info;
if (led < _led_count) {
led_info.id = led;
led_info.pattern = _pattern_mask[led];
led_info.colour = _led_array[led];
led_info.brightness = _brightness_array[led];
} else {
led_info.id = INVALID_ID;
led_info.pattern = 0;
led_info.colour = CRGB::Black;
led_info.brightness = 0;
}
return led_info;
}
bool PatternManager::set_led_colour(uint16_t led, CRGB colour) {
if (led < _led_count) {
_led_array[led] = colour;
_pattern_mask[led] = 0;
uint8_t inverse_brightness = 255 - _brightness_array[led];
_led_array[led].fadeLightBy(inverse_brightness);
return true;
} else {
return false;
}
}
bool PatternManager::set_led_pattern(uint16_t led, uint16_t pattern) {
if (led < _led_count) {
_pattern_mask[led] = pattern;
return true;
} else {
return false;
}
}
bool PatternManager::set_led_brightness(uint16_t led, uint8_t brightness) {
if (led < _led_count && 0 <= brightness && brightness <= 255) {
_brightness_array[led] = brightness;
return true;
} else {
return false;
}
}
PatternManager::~PatternManager() {}
PatternBase::PatternBase(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: _pattern_mask(pattern_mask),
_led_array(led_array),
_brightness_array(brightness_array),
_led_count(led_count) {
strncpy(_pattern_name, "Solid Colour", sizeof(_pattern_name));
_pattern_id = 0;
_total_steps = 255;
set_base_speed(10);
_current_step = 0;
_colour_count = 1;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB::Black;
}
void PatternBase::increment() {
if (!_reverse) {
_current_step++;
if (_current_step >= _total_steps) {
_current_step = 0;
complete();
}
} else {
--_current_step;
if (_current_step <= 0) {
_current_step = _total_steps - 1;
complete();
}
}
}
void PatternBase::write_led(uint16_t led, CRGB colour) {
if (_pattern_mask[led] == _pattern_id) {
_led_array[led] = colour;
uint8_t inverse_brightness = 255 - _brightness_array[led];
_led_array[led].fadeLightBy(inverse_brightness);
}
}
void PatternBase::set_base_speed(uint8_t speed) {
_base_speed = speed;
_interval = _base_speed * _user_speed;
}
void PatternBase::loop() {
unsigned int current_millis = millis();
if (current_millis > _last_update + _interval) {
update();
_last_update = current_millis;
}
}
uint8_t PatternBase::get_user_speed(){
float speed = _user_speed * 50;
uint8_t int_speed = (uint8_t)speed;
return int_speed;
}
bool PatternBase::set_user_speed(uint8_t speed) {
if (1 <= speed && speed <= 100) {
_user_speed = (float)speed / 50;
_interval = _base_speed * _user_speed;
return true;
} else {
return false;
}
}
bool PatternBase::set_colour(uint8_t colour_id, CRGB colour){
if (0 <= colour_id && colour_id <= _colour_count){
_colour_array[colour_id] = colour;
return true;
} else {
return false;
}
}
RainbowCycle::RainbowCycle(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: PatternBase(led_array, pattern_mask, brightness_array, led_count) {
strncpy(_pattern_name, "Rainbow Cycle", sizeof(_pattern_name));
_pattern_id = 1;
_total_steps = 255;
set_base_speed(30);
_current_step = 0;
_colour_count = 1;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB::Black;
}
void RainbowCycle::update() {
CRGB temp_array[_led_count];
fill_rainbow(temp_array, _led_count, _current_step, 255 / _led_count);
for (int i = 0; i < _led_count; i++) {
write_led(i, temp_array[i]);
}
increment();
}
TheatreChase::TheatreChase(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: PatternBase(led_array, pattern_mask, brightness_array, led_count) {
strncpy(_pattern_name, "Theatre Chase", sizeof(_pattern_name));
_pattern_id = 2;
_total_steps = _led_count;
set_base_speed(175);
_current_step = 0;
_colour_count = 2;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB(random8(), random8(), random8());
_colour_array[1] = CRGB(random8(), random8(), random8());
}
void TheatreChase::update() {
for (int i = 0; i < _led_count; i++) {
if ((i + _current_step) % 3 == 0) {
write_led(i, _colour_array[0]);
} else {
write_led(i, _colour_array[1]);
}
}
increment();
}
Scanner::Scanner(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: PatternBase(led_array, pattern_mask, brightness_array, led_count) {
strncpy(_pattern_name, "Scanner", sizeof(_pattern_name));
_pattern_id = 3;
_total_steps = (_led_count - 1) * 2;
set_base_speed(40);
_current_step = 0;
_colour_count = 1;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB::Red;
}
void Scanner::update() {
for (int i = 0; i < _led_count; i++) {
if ((i == _current_step) ||
(i == _total_steps - _current_step)) { // Scan Pixel to the right
write_led(i, _colour_array[0]);
} else { // Fading tail
CRGB temp_colour = _led_array[i];
temp_colour.fadeToBlackBy(100);
write_led(i, temp_colour);
}
}
increment();
}
ColourFade::ColourFade(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: PatternBase(led_array, pattern_mask, brightness_array, led_count) {
strncpy(_pattern_name, "Colour Fade", sizeof(_pattern_name));
_pattern_id = 4;
_total_steps = 255;
set_base_speed(15);
_current_step = 0;
_colour_count = 2;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB(random8(), random8(), random8());
_colour_array[1] = CRGB(random8(), random8(), random8());
_gradient = CRGBPalette256(_colour_array[0], _colour_array[1]);
}
void ColourFade::update() {
CRGB new_colour = ColorFromPalette(_gradient, _current_step);
for (int i = 0; i < _led_count; i++) {
write_led(i, new_colour);
}
increment();
}
void ColourFade::complete() {
_colour_array[0] = _colour_array[1];
_colour_array[1] = CRGB(random8(), random8(), random8());
_gradient = CRGBPalette256(_colour_array[0], _colour_array[1]);
}
Twinkle::Twinkle(CRGB *led_array, uint8_t *pattern_mask,
uint8_t *brightness_array, uint led_count)
: PatternBase(led_array, pattern_mask, brightness_array, led_count) {
strncpy(_pattern_name, "Twinkle Twinkle", sizeof(_pattern_name));
_pattern_id = 5;
_total_steps = 32;
set_base_speed(50);
_current_step = 0;
_colour_count = 1;
_colour_array = new CRGB[_colour_count];
_colour_array[0] = CRGB::White;
}
void Twinkle::update() {
int chance = random(50);
if (chance < 10) {
write_led(random(_led_count - 1), _colour_array[0]);
}
for (int i = 0; i < _led_count; i++) {
CRGB temp_colour = _led_array[i];
temp_colour.fadeToBlackBy(10);
write_led(i, temp_colour);
}
increment();
}