-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
405 lines (335 loc) · 11.8 KB
/
Animation.cpp
File metadata and controls
405 lines (335 loc) · 11.8 KB
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
#include "constants.h"
/*
* Note to self:
* When attempting to integrate with LocoNet library, do the following:
* - add CHSV* getStop() and bool isDone() to Animation
* - consider removing play amount from OHML and adding a termination method to Animation like setStop()
*/
// interface for all animations
class Animation {
protected:
uint8_t funcID;
public:
virtual CHSV* getNext(uint8_t currID) = 0; // gets the next row of colours to display
// bool isDone();
};
// a dynamically-initialized animation
class Waterfall: public Animation {
private:
CHSV *current_colours; // array of colours for the current row
int *positions; // array containing position of each row in the animation
int *random_amount; // tells strip how many times to play shimmer animation before highlight
int random_low_bound, random_high_bound; // user-given range for generating a random value for random_amount
struct Pulse {
CHSV *ani;
uint8_t len;
} *highlight, *shimmer; // animation array with positions
// populates Pulse struct with colours to created a fade-through "breathing" or "pulsing" animation
void pulse_maker(Pulse *pulse, CHSV base_colour, CHSV target_colour, CHSV fade_rate, bool reverse_fade_rate) {
// account for user error
if (fade_rate.hue == 0) fade_rate.hue = 1;
if (fade_rate.sat == 0) fade_rate.sat = 1;
if (fade_rate.val == 0) fade_rate.val = 1;
// calculate animation length
uint8_t hue_steps = ceil(abs((float) (target_colour.hue - base_colour.hue) / fade_rate.hue));
uint8_t sat_steps = ceil(abs((float) (target_colour.sat - base_colour.sat) / fade_rate.sat));
uint8_t val_steps = ceil(abs((float) (target_colour.val - base_colour.val) / fade_rate.val));
// initialize Pulse values
pulse->len = 2 * max(hue_steps, max(sat_steps, val_steps)) + 1;
pulse->ani = new CHSV[pulse->len];
// set fill mode based on boolean reverse_fade_rate
int fill;
CHSV new_target;
if (not reverse_fade_rate) {
fill = 0;
pulse->ani[fill] = base_colour;
new_target = target_colour;
} else {
fill = (pulse->len - 1) / 2;
pulse->ani[fill] = target_colour;
new_target = base_colour;
}
// fill half of the array
while (pulse->ani[fill].hue != new_target.hue || pulse->ani[fill].sat != new_target.sat || pulse->ani[fill].val != new_target.val) {
++fill;
pulse->ani[fill] = pulse->ani[fill - 1];
if (pulse->ani[fill].hue == new_target.hue) {
// do nothing
} else if (abs(pulse->ani[fill].hue - new_target.hue) < fade_rate.hue){
pulse->ani[fill].hue = new_target.hue;
} else if (pulse->ani[fill].hue < new_target.hue) {
pulse->ani[fill].hue += fade_rate.hue;
} else {
pulse->ani[fill].hue -= fade_rate.hue;
}
if (pulse->ani[fill].sat == new_target.sat) {
// do nothing
} else if (abs(pulse->ani[fill].sat - new_target.sat) < fade_rate.sat){
pulse->ani[fill].sat = new_target.sat;
} else if (pulse->ani[fill].sat < new_target.sat) {
pulse->ani[fill].sat += fade_rate.sat;
} else {
pulse->ani[fill].sat -= fade_rate.sat;
}
if (pulse->ani[fill].val == new_target.val) {
// do nothing
} else if (abs(pulse->ani[fill].val - new_target.val) < fade_rate.val){
pulse->ani[fill].val = new_target.val;
} else if (pulse->ani[fill].val < new_target.val) {
pulse->ani[fill].val += fade_rate.val;
} else {
pulse->ani[fill].val -= fade_rate.val;
}
}
// fill other half of array by mirroring the initial half
for (int i = 0; i < (pulse->len - 1) / 2; ++i) {
if (not reverse_fade_rate) {
pulse->ani[pulse->len - 1 - i] = pulse->ani[i];
} else {
pulse->ani[i] = pulse->ani[pulse->len - 1 - i];
}
}
}
// code for stepping through an animation array and adding randomness aspect
void stepStrip(uint8_t strip_num) {
if (positions[strip_num] == highlight->len + shimmer->len - 1) {
if (random_amount[strip_num] == 1) {
positions[strip_num] = 0;
random_amount[strip_num] = random_low_bound + random8(random_high_bound - random_low_bound + 1);
current_colours[strip_num] = highlight->ani[positions[strip_num]];
} else {
positions[strip_num] = highlight->len;
--random_amount[strip_num];
current_colours[strip_num] = shimmer->ani[positions[strip_num] - highlight->len];
}
} else if (positions[strip_num] < highlight->len) {
current_colours[strip_num] = highlight->ani[positions[strip_num]];
} else {
current_colours[strip_num] = shimmer->ani[positions[strip_num] - highlight->len];
}
}
// stopping animation that halts each strip
void stop_gradual() {
for (int strip_num = 0; strip_num < STRIP_AMOUNT; ++strip_num) {
stepStrip(strip_num);
if (positions[strip_num] != 0) ++positions[strip_num];
}
}
// stopping animation that resets all strips instantly
void stop_quick() {
for (int strip_num = 0; strip_num < STRIP_AMOUNT; ++strip_num) {
positions[strip_num] = 0;
}
}
public:
// constructor that initializes class variables and generates Pulse strips
Waterfall(uint8_t id, CHSV base_col, CHSV highlight_col, CHSV shimmer_col, CHSV highlight_rate, CHSV shimmer_rate, bool highlight_reversed, bool shimmer_reversed, uint8_t repeat_low_bound, uint8_t repeat_high_bound) {
funcID = id;
highlight = new Pulse();
shimmer = new Pulse();
current_colours = new CHSV[STRIP_AMOUNT];
positions = new int[STRIP_AMOUNT];
random_amount = new int[STRIP_AMOUNT];
random_low_bound = repeat_low_bound;
random_high_bound = repeat_high_bound;
for (int strip_num = 0; strip_num < STRIP_AMOUNT; ++strip_num) {
positions[strip_num] = 0;
random_amount[strip_num] = random_low_bound + random8(random_high_bound - random_low_bound + 1);
}
pulse_maker(highlight, base_col, highlight_col, highlight_rate, highlight_reversed);
pulse_maker(shimmer, base_col, shimmer_col, shimmer_rate, shimmer_reversed);
}
CHSV* getNext(uint8_t currID) {
if (currID == funcID) {
// play regular animation
for (int strip_num = 0; strip_num < STRIP_AMOUNT; ++strip_num) {
stepStrip(strip_num);
++positions[strip_num];
}
} else {
// check if animation is complete
bool complete = true;
for (int strip_num = 0; strip_num < STRIP_AMOUNT; ++strip_num) {
if (positions[strip_num] != 0) {
complete = false;
break;
}
}
if (complete) return NULL;
// play stopping animation
stop_quick();
}
return current_colours;
}
};
// a hardcoded animation
class OHML: public Animation{
private:
int
run_amount,
pos,
len;
CHSV
fg,
bg,
strip[64][STRIP_AMOUNT];
public:
// constructor that initializes class variables and creates the animation
OHML(uint8_t id, CHSV base, CHSV background, int num_runs = 1){
funcID = id;
fg = base;
bg = background;
run_amount = num_runs;
len = 64;
pos = 0;
if (STRIP_AMOUNT == 5) {
CHSV temp_array[len][5] {
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
//O
{bg, fg, fg, fg, bg},
{fg, fg, bg, fg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, fg, bg, fg, fg},
{bg, fg, fg, fg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
//H
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, fg, fg, fg, fg},
{fg, fg, fg, fg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
//M
{fg, bg, bg, bg, fg},
{fg, fg, bg, fg, fg},
{fg, fg, fg, fg, fg},
{fg, bg, fg, bg, fg},
{fg, bg, fg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{fg, bg, bg, bg, fg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
//L
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, bg, bg, bg, bg},
{fg, fg, fg, fg, fg},
{fg, fg, fg, fg, fg},
//space
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg}
};
memcpy(strip, temp_array, sizeof(CHSV) * len * STRIP_AMOUNT);
}
}
CHSV* getNext(uint8_t currID) {
if (pos == 0) {
if (/*run_amount > 1 &&*/ currID == funcID) {
pos = len;
--run_amount;
return strip[--pos];
} else{
return NULL;
}
} else {
return strip[--pos];
}
}
};
// a hardcoded animation
class Leaf: public Animation{
private:
int
run_amount,
pos,
len;
CHSV
fg,
bg,
strip[23][STRIP_AMOUNT];
public:
// constructor that initializes class variables and creates the animation
Leaf(uint8_t id, CHSV base, CHSV background, int num_runs = 1){
funcID = id;
fg = base;
bg = background;
run_amount = num_runs;
len = 23;
pos = 0;
if (STRIP_AMOUNT == 5) {
CHSV temp_array[len][5] {
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
// leaf
{bg, bg, fg, bg, bg},
{bg, bg, fg, bg, bg},
{fg, fg, fg, fg, fg},
{bg, fg, fg, fg, bg},
{bg, bg, fg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg},
{bg, bg, bg, bg, bg}
};
memcpy(strip, temp_array, sizeof(CHSV) * len * STRIP_AMOUNT);
}
}
CHSV* getNext(uint8_t currID) {
if (pos == 0) {
if (/*run_amount > 1 &&*/ currID == funcID) {
pos = len;
--run_amount;
return strip[--pos];
} else{
return NULL;
}
} else {
return strip[--pos];
}
}
};