-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime.cpp
405 lines (372 loc) · 11.7 KB
/
prime.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
#include <EEPROM.h>
#include "palette.h"
#include "prime.h"
uint8_t _r, _g, _b;
uint8_t r0, g0, b0;
uint8_t r1, g1, b1;
/*******************************************************************************
** BASE ANIMATIONS
******************************************************************************/
void _strobe(Prime* prime, uint8_t c_time, uint8_t b_time) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
}
if (prime->tick < c_time) {
unpackColor(prime->colors[prime->cur_color], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _pulse(Prime* prime, uint8_t c_time, uint8_t b_time) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
}
if (prime->tick < c_time / 2) {
unpackColor(prime->colors[prime->cur_color], r0, g0, b0);
morphColor(prime->tick, c_time / 2,
0, 0, 0, r0, g0, b0, _r, _g, _b);
} else if (prime->tick < c_time) {
unpackColor(prime->colors[prime->cur_color], r0, g0, b0);
morphColor(prime->tick - (c_time / 2), c_time - (c_time / 2),
r0, g0, b0, 0, 0, 0, _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _tracer(Prime* prime, uint8_t c_time, uint8_t b_time) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->cur_color = (prime->cur_color + 1) % (prime->num_colors - 1);
}
if (prime->tick < c_time) {
unpackColor(prime->colors[(prime->cur_color + 1) % prime->num_colors], _r, _g, _b);
} else {
unpackColor(prime->colors[0], _r, _g, _b);
}
}
void _dashdops(Prime* prime, uint8_t c_time, uint8_t d_time, uint8_t b_time, uint8_t dops) {
prime->counter1 = prime->num_colors - 1;
if (prime->tick >= (prime->counter1 * c_time) + ((d_time + b_time) * dops) + b_time) {
prime->tick = 0;
}
if (prime->tick < prime->counter1 * c_time) {
unpackColor(prime->colors[(prime->tick / c_time) + 1], _r, _g, _b);
} else {
prime->counter0 = prime->tick - (prime->counter1 * c_time);
if (prime->counter0 % (d_time + b_time) >= b_time) {
unpackColor(prime->colors[0], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
}
void _blinke(Prime* prime, uint8_t c_time, uint8_t b_time) {
if (prime->tick >= (prime->num_colors * c_time) + b_time) {
prime->tick = 0;
}
if (prime->tick < prime->num_colors * c_time) {
unpackColor(prime->colors[prime->tick / c_time], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _edge(Prime* prime, uint8_t c_time, uint8_t e_time, uint8_t b_time) {
prime->counter0 = prime->num_colors - 1;
if (prime->tick >= (prime->counter0 * c_time * 2) + e_time + b_time) {
prime->tick = 0;
}
if (prime->tick < prime->counter0 * c_time) {
unpackColor(prime->colors[prime->counter0 - (prime->tick / c_time)], _r, _g, _b);
} else if (prime->tick < (prime->counter0 * c_time) + e_time) {
unpackColor(prime->colors[0], _r, _g, _b);
} else if (prime->tick < (prime->counter0 * c_time * 2) + e_time) {
prime->counter1 = ((prime->tick - ((prime->counter0 * c_time) + e_time)) / c_time) + 1;
unpackColor(prime->colors[prime->counter1], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
uint8_t getLegoTime() {
switch (random(0, 3)) {
case 0:
return 4;
case 1:
return 16;
default:
return 32;
}
}
void _lego(Prime* prime, uint8_t b_time) {
if (prime->counter0 == 0) prime->counter0 = getLegoTime();
if (prime->tick >= prime->counter0 + b_time) {
prime->tick = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
prime->counter0 = getLegoTime();
}
if (prime->tick < prime->counter0) {
unpackColor(prime->colors[prime->cur_color], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _chase(Prime* prime, uint8_t c_time, uint8_t b_time, uint8_t steps) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->counter0++;
if (prime->counter0 >= steps - 1) {
prime->counter0 = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
}
}
if (prime->tick < c_time) {
prime->counter1 = prime->tick / (c_time / steps);
if (prime->counter0 == 0) {
unpackColor(prime->colors[prime->cur_color], _r, _g, _b);
} else {
if (prime->counter1 < prime->counter0) {
unpackColor(prime->colors[(prime->cur_color + 1) % prime->num_colors], _r, _g, _b);
} else if (prime->counter1 == prime->counter0) {
_r = 0; _g = 0; _b = 0;
} else {
unpackColor(prime->colors[prime->cur_color], _r, _g, _b);
}
}
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _morph(Prime* prime, uint8_t c_time, uint8_t b_time, uint8_t steps) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->counter0++;
if (prime->counter0 >= steps) {
prime->counter0 = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
}
}
if (prime->tick < c_time) {
unpackColor(prime->colors[prime->cur_color], r0, g0, b0);
unpackColor(prime->colors[(prime->cur_color + 1) % prime->num_colors], r1, g1, b1);
morphColor(prime->tick + ((c_time + b_time) * prime->counter0), (c_time + b_time) * steps,
r0, g0, b0, r1, g1, b1, _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _comet(Prime* prime, uint8_t c_time, uint8_t b_time, uint8_t per_step) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->counter0 += (prime->counter1 == 0) ? per_step : -1 * per_step;
if (prime->counter0 <= 0) {
prime->counter1 = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
} else if (prime->counter0 >= c_time) {
prime->counter1 = 1;
}
}
if (prime->tick <= prime->counter0) {
unpackColor(prime->colors[(prime->cur_color + prime->counter0) % prime->num_colors], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
void _candy(Prime* prime, uint8_t c_time, uint8_t b_time, uint8_t pick, uint8_t repeat) {
if (prime->tick >= c_time + b_time) {
prime->tick = 0;
prime->counter0++;
if (prime->counter0 >= pick) {
prime->counter0 = 0;
prime->counter1++;
if (prime->counter1 >= repeat) {
prime->counter1 = 0;
prime->cur_color = (prime->cur_color + 1) % prime->num_colors;
}
}
}
if (prime->tick < c_time) {
unpackColor(prime->colors[(prime->cur_color + prime->counter0) % prime->num_colors], _r, _g, _b);
} else {
_r = 0; _g = 0; _b = 0;
}
}
/*******************************************************************************
** PRIME CLASS
******************************************************************************/
#ifdef PALETTE_12
Prime::Prime(uint8_t _pattern, uint8_t _num_colors,
uint8_t c00, uint8_t c01, uint8_t c02, uint8_t c03,
uint8_t c04, uint8_t c05, uint8_t c06, uint8_t c07,
uint8_t c08, uint8_t c09, uint8_t c10, uint8_t c11) {
pattern = _pattern;
num_colors = _num_colors;
colors[0] = c00;
colors[1] = c01;
colors[2] = c02;
colors[3] = c03;
colors[4] = c04;
colors[5] = c05;
colors[6] = c06;
colors[7] = c07;
colors[8] = c08;
colors[9] = c09;
colors[10] = c10;
colors[11] = c11;
}
void Prime::load(uint16_t addr) {
pattern = EEPROM.read(addr + 0);
num_colors = EEPROM.read(addr + 1);
colors[0] = EEPROM.read(addr + 2);
colors[1] = EEPROM.read(addr + 3);
colors[2] = EEPROM.read(addr + 4);
colors[3] = EEPROM.read(addr + 5);
colors[4] = EEPROM.read(addr + 6);
colors[5] = EEPROM.read(addr + 7);
colors[6] = EEPROM.read(addr + 8);
colors[7] = EEPROM.read(addr + 9);
colors[8] = EEPROM.read(addr + 10);
colors[9] = EEPROM.read(addr + 11);
colors[10] = EEPROM.read(addr + 12);
colors[11] = EEPROM.read(addr + 13);
}
void Prime::save(uint16_t addr) {
EEPROM.update(addr + 0, pattern);
EEPROM.update(addr + 1, num_colors);
EEPROM.update(addr + 2, colors[0]);
EEPROM.update(addr + 3, colors[1]);
EEPROM.update(addr + 4, colors[2]);
EEPROM.update(addr + 5, colors[3]);
EEPROM.update(addr + 6, colors[4]);
EEPROM.update(addr + 7, colors[5]);
EEPROM.update(addr + 8, colors[6]);
EEPROM.update(addr + 9, colors[7]);
EEPROM.update(addr + 10, colors[8]);
EEPROM.update(addr + 11, colors[9]);
EEPROM.update(addr + 12, colors[10]);
EEPROM.update(addr + 13, colors[11]);
}
#endif
#ifdef PALETTE_16
Prime::Prime(uint8_t _pattern, uint8_t _num_colors,
uint8_t c00, uint8_t c01, uint8_t c02, uint8_t c03,
uint8_t c04, uint8_t c05, uint8_t c06, uint8_t c07,
uint8_t c08, uint8_t c09, uint8_t c10, uint8_t c11,
uint8_t c12, uint8_t c13, uint8_t c14, uint8_t c15) {
pattern = _pattern;
num_colors = _num_colors;
colors[0] = c00;
colors[1] = c01;
colors[2] = c02;
colors[3] = c03;
colors[4] = c04;
colors[5] = c05;
colors[6] = c06;
colors[7] = c07;
colors[8] = c08;
colors[9] = c09;
colors[10] = c10;
colors[11] = c11;
colors[12] = c12;
colors[13] = c13;
colors[14] = c14;
colors[15] = c15;
}
void Prime::load(uint16_t addr) {
pattern = EEPROM.read(addr + 0);
num_colors = EEPROM.read(addr + 1);
colors[0] = EEPROM.read(addr + 2);
colors[1] = EEPROM.read(addr + 3);
colors[2] = EEPROM.read(addr + 4);
colors[3] = EEPROM.read(addr + 5);
colors[4] = EEPROM.read(addr + 6);
colors[5] = EEPROM.read(addr + 7);
colors[6] = EEPROM.read(addr + 8);
colors[7] = EEPROM.read(addr + 9);
colors[8] = EEPROM.read(addr + 10);
colors[9] = EEPROM.read(addr + 11);
colors[10] = EEPROM.read(addr + 12);
colors[11] = EEPROM.read(addr + 13);
colors[12] = EEPROM.read(addr + 14);
colors[13] = EEPROM.read(addr + 15);
colors[14] = EEPROM.read(addr + 16);
colors[15] = EEPROM.read(addr + 17);
}
void Prime::save(uint16_t addr) {
EEPROM.update(addr + 0, pattern);
EEPROM.update(addr + 1, num_colors);
EEPROM.update(addr + 2, colors[0]);
EEPROM.update(addr + 3, colors[1]);
EEPROM.update(addr + 4, colors[2]);
EEPROM.update(addr + 5, colors[3]);
EEPROM.update(addr + 6, colors[4]);
EEPROM.update(addr + 7, colors[5]);
EEPROM.update(addr + 8, colors[6]);
EEPROM.update(addr + 9, colors[7]);
EEPROM.update(addr + 10, colors[8]);
EEPROM.update(addr + 11, colors[9]);
EEPROM.update(addr + 12, colors[10]);
EEPROM.update(addr + 13, colors[11]);
EEPROM.update(addr + 14, colors[12]);
EEPROM.update(addr + 15, colors[13]);
EEPROM.update(addr + 16, colors[14]);
EEPROM.update(addr + 17, colors[15]);
}
#endif
void Prime::render(uint8_t& r, uint8_t& g, uint8_t& b) {
switch (pattern) {
case PRIME_STROBE:
_strobe(this, 10, 16);
break;
case PRIME_HYPER:
_strobe(this, 34, 34);
break;
case PRIME_DOPS:
_strobe(this, 3, 20);
break;
case PRIME_STROBIE:
_strobe(this, 6, 46);
break;
case PRIME_PULSE:
_pulse(this, 200, 50);
break;
case PRIME_SEIZURE:
_strobe(this, 10, 190);
break;
case PRIME_TRACER:
_tracer(this, 6, 46);
break;
case PRIME_DASHDOPS:
_dashdops(this, 22, 3, 20, 7);
break;
case PRIME_BLINKE:
_blinke(this, 10, 100);
break;
case PRIME_EDGE:
_edge(this, 4, 16, 40);
break;
case PRIME_LEGO:
_lego(this, 16);
break;
case PRIME_CHASE:
_chase(this, 100, 20, 5);
break;
case PRIME_MORPH:
_morph(this, 34, 34, 4);
break;
case PRIME_RIBBON:
_strobe(this, 22, 0);
break;
case PRIME_COMET:
_comet(this, 30, 16, 2);
break;
case PRIME_CANDY:
_candy(this, 10, 16, 3, 8);
break;
default:
break;
}
tick++;
r = _r; g = _g; b = _b;
}
void Prime::reset() { tick = cur_color = counter0 = counter1 = 0; }