-
Notifications
You must be signed in to change notification settings - Fork 0
/
anim_fx.c
671 lines (607 loc) · 19.1 KB
/
anim_fx.c
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
/*
* Card animation effect-stepping functions
*/
#include "anim_fx_script.h"
#include "anim_fx.h"
#include "leds.h"
#include <prng.h>
#include <misc.h>
#include <unistd.h>
#include <limits.h>
unsigned int
anim_fx_stop(bool first, void **pnext_fx)
{
(void)first;
(void)pnext_fx;
return 3600000;
}
unsigned int
anim_fx_stars_shimmer(bool first, void **pnext_fx)
{
static struct anim_fx_script script;
static const struct anim_fx_script_seg seg_list[] = {
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 5000,
.step_delay_max = 15000},
{.step_num_min = 5,
.step_num_max = 5,
.step_br_off = -3,
.step_delay_min = 40,
.step_delay_max = 40},
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 400,
.step_delay_max = 1000},
{.step_num_min = 5,
.step_num_max = 5,
.step_br_off = 3,
.step_delay_min = 40,
.step_delay_max = 40}
};
static struct anim_fx_script_led led_list[LEDS_STARS_NUM];
static struct anim_fx_script_led_seg
led_seg_list_list[LEDS_STARS_NUM *
ARRAY_SIZE(seg_list)];
unsigned int delay;
if (first) {
anim_fx_script_init(
&script, ARRAY_SIZE(seg_list), seg_list,
LEDS_STARS_NUM, LEDS_STARS_LIST,
led_list, led_seg_list_list,
/* Initial brightness */
LEDS_BR_MAX * 3 / 4,
/* Fade-in/out duration, ms */
3000,
/* Effect body duration, ms (infinity) */
UINT_MAX);
}
if (anim_fx_script_step(&script, &delay)) {
*pnext_fx = anim_fx_balls_random;
}
return delay;
}
unsigned int
anim_fx_topper_fade_in(bool first, void **pnext_fx)
{
static uint8_t step;
size_t i;
if (first) {
step = 0;
}
for (i = 0; i < ARRAY_SIZE(LEDS_TOPPER_LIST); i++) {
LEDS_BR[LEDS_TOPPER_LIST[i]] = step;
}
step++;
if (step == LEDS_BR_MAX) {
*pnext_fx = anim_fx_stop;
}
return 1000 / LEDS_BR_NUM;
}
unsigned int
anim_fx_balls_fade_in_and_out(bool first, void **pnext_fx)
{
static enum {
FADE_IN,
WAIT,
FADE_OUT
} stage;
static int step;
static const uint8_t br_steps[] = {
0,
LEDS_BR_MAX / 4,
LEDS_BR_MAX * 2 / 4,
LEDS_BR_MAX * 3 / 4,
LEDS_BR_MAX
};
int idx;
int br_idx;
int i;
int j;
if (first) {
stage = FADE_IN;
step = 0;
}
if (stage == WAIT) {
stage = FADE_OUT;
return 10000;
}
idx = 0;
for (i = (int)ARRAY_SIZE(LEDS_BALLS_SWNE_LINE_LIST) - 1; i >= 0; i--) {
for (j = 0;
j < (int)ARRAY_SIZE(LEDS_BALLS_SWNE_LINE_LIST[i]) &&
LEDS_BALLS_SWNE_LINE_LIST[i][j] != LEDS_IDX_INVALID;
j++) {
br_idx = step - idx;
if (br_idx < 0) {
br_idx = 0;
} else if (br_idx >= (int)ARRAY_SIZE(br_steps)) {
br_idx = ARRAY_SIZE(br_steps) - 1;
}
LEDS_BR[LEDS_BALLS_SWNE_LINE_LIST[i][j]] = br_steps[br_idx];
idx++;
}
}
if (stage == FADE_IN) {
step++;
if (step == LEDS_BALLS_NUM + ARRAY_SIZE(br_steps) - 1) {
stage = WAIT;
}
} else if (stage == FADE_OUT) {
step--;
if (step == 0) {
*pnext_fx = anim_fx_balls_random;
}
}
return 1500 / (LEDS_BALLS_NUM + ARRAY_SIZE(br_steps) - 1);
}
unsigned int
anim_fx_balls_wave(bool first, void **pnext_fx)
{
/*
* Generated with
* perl -e 'use Math::Trig;
* my $n=64;
* for (my $i=0; $i < $n; $i++) {
* printf("%.0f, ", sin(2*pi*$i/$n-pi/2) * 16 + 47);
* };
* print("\n")'
*/
static const uint8_t wave[] = {
31, 31, 31, 32, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 45,
47, 49, 50, 52, 53, 55, 56, 57, 58, 59, 60, 61, 62, 62, 63, 63,
63, 63, 63, 62, 62, 61, 60, 59, 58, 57, 56, 55, 53, 52, 50, 49,
47, 45, 44, 42, 41, 39, 38, 37, 36, 35, 34, 33, 32, 32, 31, 31,
};
static size_t step;
size_t i, j, k, w;
unsigned int br;
if (first) {
step = 0;
}
/* For each line of balls */
for (i = 0; i < ARRAY_SIZE(LEDS_BALLS_SWNE_LINE_LIST); i++) {
w = step + (i << 2);
/* Calculate brightness */
br = wave[w & (ARRAY_SIZE(wave) - 1)];
/* If fading in */
if ((w & 0x700) == 0) {
br = (br * ((w >> 2) & 0x3f)) >> 6;
/* Else, if fading out */
} else if ((w & 0x700) == 0x700) {
br = (br * (0x40 - ((w >> 2) & 0x3f))) >> 6;
}
/* For each ball on the line */
for (j = 0;
(k = LEDS_BALLS_SWNE_LINE_LIST[i][j]) != LEDS_IDX_INVALID;
j++) {
LEDS_BR[k] = br;
}
}
step++;
if (step == 0x800) {
*pnext_fx = anim_fx_balls_random;
}
return 50;
}
unsigned int
anim_fx_balls_glitter(bool first, void **pnext_fx)
{
static struct anim_fx_script script;
static const struct anim_fx_script_seg seg_list[] = {
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = LEDS_BR_MAX,
/* FIXME Setting this to zero crashes the program eventually */
.step_delay_min = 10,
.step_delay_max = 300},
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = -LEDS_BR_MAX,
.step_delay_min = 10,
.step_delay_max = 10},
};
static struct anim_fx_script_led led_list[LEDS_BALLS_NUM];
static struct anim_fx_script_led_seg
led_seg_list_list[LEDS_BALLS_NUM *
ARRAY_SIZE(seg_list)];
unsigned int delay;
if (first) {
anim_fx_script_init(
&script, ARRAY_SIZE(seg_list), seg_list,
LEDS_BALLS_NUM, LEDS_BALLS_LIST,
led_list, led_seg_list_list,
/* Initial brightness */
0,
/* Fade-in/out duration, ms */
3000,
/* Effect body duration, ms */
60000);
}
if (anim_fx_script_step(&script, &delay)) {
*pnext_fx = anim_fx_balls_random;
}
return delay;
}
unsigned int
anim_fx_balls_cycle_colors(bool first, void **pnext_fx)
{
static unsigned int step;
static enum leds_balls_color prev_color;
static enum leds_balls_color cur_color;
static unsigned int hue;
enum leds_balls_color color;
uint8_t br;
size_t i;
(void)pnext_fx;
if (first) {
step = 0;
prev_color = LEDS_BALLS_COLOR_NUM;
cur_color = 0;
hue = 0;
}
if (step == 12) {
cur_color = LEDS_BALLS_COLOR_NUM;
}
for (color = 0; color < ARRAY_SIZE(LEDS_BALLS_COLOR_LIST); color++) {
if (color == cur_color) {
br = ((hue + 1) * LEDS_BR_MAX) >> 3 ;
} else if (color == prev_color) {
br = ((7 - hue) * LEDS_BR_MAX) >> 3;
} else {
br = 0;
}
for (i = 0; i < ARRAY_SIZE(LEDS_BALLS_COLOR_LIST[color]); i++) {
LEDS_BR[LEDS_BALLS_COLOR_LIST[color][i]] = br;
}
}
hue++;
if (hue >= 8) {
hue = 0;
prev_color = cur_color;
cur_color++;
if (cur_color >= LEDS_BALLS_COLOR_NUM) {
cur_color = 0;
step++;
if (step > 12) {
*pnext_fx = anim_fx_balls_random;
}
}
}
return hue == 1 ? 1100 : 50;
}
unsigned int
anim_fx_balls_snow(bool first, void **pnext_fx)
{
/* True if filling, false if emptying */
static bool fill;
/* The current row being filled/emtpied, destination/origin row */
static uint8_t row;
/* Current row column status */
static bool idle_cols[LEDS_BALLS_COL_NUM];
/* Number of unfilled/unemptied columns at the current row */
static uint8_t idle_cols_num;
/* Current falling ball column */
static uint8_t ball_col;
/* Previous falling ball row */
static uint8_t prev_ball_row;
/* Current falling ball row */
static uint8_t ball_row;
/* Ball brightness */
static uint8_t ball_br;
/* True if we're scheduling lighting the next ball */
bool moving;
size_t i;
if (first) {
/* We're filling */
fill = true;
/* Below the bottom row */
row = LEDS_BALLS_ROW_NUM;
/* No unfilled columns remaining in the row */
idle_cols_num = 0;
/* Ball in the below-the-bottom row */
ball_row = row;
/* Ball finished lighting */
ball_br = LEDS_BR_MAX;
}
/* Else, if the ball in current position is lighted fully */
if (ball_br >= LEDS_BR_MAX) {
/* Reset ball brightness */
ball_br = 0;
/* Current row becomes previous row */
prev_ball_row = ball_row;
/* If the ball is in flight still */
if (fill ? (ball_row < row) : (ball_row <= LEDS_BALLS_ROW_NUM)) {
/* Move to the next row */
ball_row++;
/* Else */
} else {
/* If there are no unfilled columns left */
if (idle_cols_num == 0) {
/* If we are at the top */
if (row == 0) {
if (fill) {
/* Switch to emptying */
fill = false;
/* Emptying row below the bottom */
row = LEDS_BALLS_ROW_NUM + 1;
/* No unemptied columns left */
idle_cols_num = 0;
/* Ball is below the bottom */
ball_row = row;
/* Completely lighted */
ball_br = LEDS_BR_MAX;
/* Wait before emptying */
return 7000;
} else {
/* Run random effects forever */
*pnext_fx = anim_fx_balls_random;
/* Wait to allow satisfaction settle a little */
return 3000;
}
}
/* Move up a row */
row--;
/* Count and mark available columns */
for (i = 0; i < LEDS_BALLS_COL_NUM; i++) {
idle_cols[i] = LEDS_BALLS_ROW_LIST[row][i] != LEDS_IDX_INVALID;
if (idle_cols[i]) {
idle_cols_num++;
}
}
}
/*
* At the current fill/emptying row,
* choose a column from unfilled/unemptied
*/
i = ((prng_next() & 0xffff) * (uint32_t)idle_cols_num) >> 16;
for (ball_col = 0; ball_col < LEDS_BALLS_COL_NUM; ball_col++) {
if (idle_cols[ball_col]) {
if (i == 0) {
break;
} else {
i--;
}
}
}
idle_cols[ball_col] = false;
idle_cols_num--;
if (fill) {
ball_row = 0;
} else {
prev_ball_row = row;
ball_row = row + 1;
}
}
/* Find the row where column is present */
for (; (fill ? (ball_row < row) : (ball_row < LEDS_BALLS_ROW_NUM)) &&
LEDS_BALLS_ROW_LIST[ball_row][ball_col] == LEDS_IDX_INVALID;
ball_row++);
}
/* Check if we're starting the new position */
moving = (ball_br == 0);
/* Increase ball brightness */
ball_br += LEDS_BR_NUM / 8;
if (ball_br > LEDS_BR_MAX) {
ball_br = LEDS_BR_MAX;
}
/* Update brightness of the previous ball, if any */
if (fill ? (prev_ball_row < ball_row)
: (prev_ball_row < LEDS_BALLS_ROW_NUM)) {
LEDS_BR[LEDS_BALLS_ROW_LIST[prev_ball_row][ball_col]] =
LEDS_BR_MAX - ball_br;
}
/* Update brightness of the current ball, if any */
if (ball_row < LEDS_BALLS_ROW_NUM) {
LEDS_BR[LEDS_BALLS_ROW_LIST[ball_row][ball_col]] = ball_br;
}
/*
* Specify how much to wait before applying the change.
* Wait longer before lighting the next ball so fully-lighted ball would
* stay bright longer.
*/
return moving ? 250 : 75;
}
unsigned int
anim_fx_balls_shimmer(bool first, void **pnext_fx)
{
static struct anim_fx_script script;
static const struct anim_fx_script_seg seg_list[] = {
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 0,
.step_delay_max = 3000},
{.step_num_min = 5,
.step_num_max = 5,
.step_br_off = -2,
.step_delay_min = 56,
.step_delay_max = 56},
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 300,
.step_delay_max = 600},
{.step_num_min = 5,
.step_num_max = 5,
.step_br_off = 2,
.step_delay_min = 56,
.step_delay_max = 56}
};
static struct anim_fx_script_led led_list[LEDS_BALLS_NUM];
static struct anim_fx_script_led_seg
led_seg_list_list[LEDS_BALLS_NUM *
ARRAY_SIZE(seg_list)];
unsigned int delay;
if (first) {
anim_fx_script_init(
&script, ARRAY_SIZE(seg_list), seg_list,
LEDS_BALLS_NUM, LEDS_BALLS_LIST,
led_list, led_seg_list_list,
/* Initial brightness */
LEDS_BR_MAX,
/* Fade-in/out duration, ms */
3000,
/* Effect body duration, ms */
60000);
}
if (anim_fx_script_step(&script, &delay)) {
*pnext_fx = anim_fx_balls_random;
}
return delay;
}
unsigned int
anim_fx_balls_flare(bool first, void **pnext_fx)
{
static struct anim_fx_script script;
static const struct anim_fx_script_seg seg_list[] = {
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 3000,
.step_delay_max = 10000},
{.step_num_min = 7,
.step_num_max = 7,
.step_br_off = 9,
.step_delay_min = 22,
.step_delay_max = 22},
{.step_num_min = 1,
.step_num_max = 1,
.step_br_off = 0,
.step_delay_min = 500,
.step_delay_max = 500},
{.step_num_min = 21,
.step_num_max = 21,
.step_br_off = -3,
.step_delay_min = 80,
.step_delay_max = 80}
};
static struct anim_fx_script_led led_list[LEDS_BALLS_NUM];
static struct anim_fx_script_led_seg
led_seg_list_list[LEDS_BALLS_NUM *
ARRAY_SIZE(seg_list)];
unsigned int delay;
if (first) {
anim_fx_script_init(
&script, ARRAY_SIZE(seg_list), seg_list,
LEDS_BALLS_NUM, LEDS_BALLS_LIST,
led_list, led_seg_list_list,
/* Initial brightness */
0,
/* Fade-in/out duration, ms */
2000,
/* Effect body duration, ms */
60000);
}
if (anim_fx_script_step(&script, &delay)) {
*pnext_fx = anim_fx_balls_random;
}
return delay;
}
unsigned int
anim_fx_balls_shoot(bool first, void **pnext_fx)
{
/* True if shooting balls "on", false if "off" */
static bool shooting_on;
/* Remaining number of balls to shoot */
static uint8_t remaining;
/* Index of the ball being shot */
static uint8_t idx;
/* Brightness of the ball being shot */
static int8_t br;
/* True if scheduling a new ball shot */
static bool new;
if (first) {
size_t i;
shooting_on = true;
/* We just shot a non-existing ball, pick another */
remaining = LEDS_BALLS_NUM;
idx = LEDS_BALLS_NUM;
br = LEDS_BR_MAX;
/*
* FIXME Clearing all LEDs to zero brightness, because some effects
* (perhaps anim_fx_balls_wave) might leave them not completely dark,
* breaking this effect. Fix other effects instead, and add
* verification for darkness between effects.
*/
for (i = 0; i < LEDS_BALLS_NUM; i++) {
LEDS_BR[LEDS_BALLS_LIST[i]] = 0;
}
}
/* If we're still shooting the current ball */
if (shooting_on ? (br < LEDS_BR_MAX) : (br > 0)) {
br = shooting_on ? MIN(br + 8, LEDS_BR_MAX)
: MAX(br - 8, 0);
/* Continuing with a ball */
new = false;
/* Else, if there are balls left to shoot */
} else if (remaining > 0) {
size_t pos;
/* Starting a new ball unless it's the first */
new = (remaining < LEDS_BALLS_NUM);
/* Pick a new ball to shoot */
pos = ((prng_next() & 0xffff) * remaining) >> 16;
for (idx = 0; idx < LEDS_BALLS_NUM; idx++) {
if (LEDS_BR[LEDS_BALLS_LIST[idx]] ==
(shooting_on ? 0 : LEDS_BR_MAX)) {
if (pos == 0) {
break;
} else {
pos--;
}
}
}
remaining--;
/* Start changing brightness */
br = shooting_on ? 7 : (LEDS_BR_MAX - 7);
/* Else, there are NO balls left to shoot */
} else {
/* If we were shooting on */
if (shooting_on) {
shooting_on = false;
/* We just shot a non-existing ball, pick another */
remaining = LEDS_BALLS_NUM;
idx = LEDS_BALLS_NUM;
br = 0;
/* Wait for satisfaction */
return 10000;
/* Else, we were shooting off */
} else {
*pnext_fx = anim_fx_balls_random;
/* Wait for satisfaction */
return 3000;
}
}
LEDS_BR[LEDS_BALLS_LIST[idx]] = br;
/* Delay before shooting a new ball */
return new ? 1000 : 75;
}
/** Pool of the balls effect-stepping functions to choose from randomly */
static const anim_fx_fn ANIM_FX_BALLS_RANDOM_POOL[] = {
anim_fx_balls_fade_in_and_out,
anim_fx_balls_wave,
anim_fx_balls_glitter,
anim_fx_balls_cycle_colors,
anim_fx_balls_snow,
anim_fx_balls_shimmer,
anim_fx_balls_shoot,
anim_fx_balls_flare,
};
/** Index of the balls effect-stepping function chosen last */
static size_t ANIM_FX_BALLS_RANDOM_LAST = 0;
unsigned int
anim_fx_balls_random(bool first, void **pnext_fx)
{
size_t i;
(void)first;
(void)pnext_fx;
i = prng_next() % ARRAY_SIZE(ANIM_FX_BALLS_RANDOM_POOL);
if (i == ANIM_FX_BALLS_RANDOM_LAST) {
i = (i + 1) % ARRAY_SIZE(ANIM_FX_BALLS_RANDOM_POOL);
}
*pnext_fx = ANIM_FX_BALLS_RANDOM_POOL[i];
ANIM_FX_BALLS_RANDOM_LAST = i;
return 0;
}