-
Notifications
You must be signed in to change notification settings - Fork 1
/
PBSBlade.cpp
856 lines (708 loc) · 17.5 KB
/
PBSBlade.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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/***************************************************************************
* PBSaber
* https://www.artekit.eu/doc/guides/propboard-pbsaber
*
* for Artekit PropBoard
* https://www.artekit.eu/products/devboards/propboard
*
* Written by Ivan Meleca
* Copyright (c) 2018 Artekit Labs
* https://www.artekit.eu
### PBSBlade.cpp
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
***************************************************************************/
#include "PBSBlade.h"
extern uint32_t getRandom(uint32_t min, uint32_t max);
LedStripBlade::LedStripBlade(LedStripeType type, uint32_t count) :
_type(type),
_count(count),
led_strip(NULL)
{
}
bool LedStripBlade::initialize()
{
led_strip = new PBSStrip();
if (!led_strip)
return false;
if (!led_strip->begin(_count, _type))
{
delete led_strip;
return false;
}
// Clear
COLOR color(0,0,0,0);
led_strip->setSection(0, 1, _count);
led_strip->set(0, 0, color);
led_strip->update(0, true);
return true;
}
void LedStripBlade::onIgnition(ignitionMode mode, uint32_t duration)
{
if (!duration)
{
debugMsg(DebugError, "Invalid ignition duration");
return;
}
debugMsg(DebugInfo, "Doing ignition effect on LedStripBlade, duration: %i ms", duration);
/* Reset all and create a new section */
led_strip->stopAllAnimations();
led_strip->resetAllSections();
led_strip->setSection(0, 1, _count);
switch (mode)
{
case ignitionFull:
// Actually a very fast ramp
duration = 50;
/* no break */
case ignitionRamp:
// Start a fade-in effect
led_strip->fadeIn(duration, 0);
break;
case ignitionScroll:
// Start a fade-in effect
led_strip->fadeIn(duration, 0);
// Start a scrolling effect
led_strip->scroll(1, _count, duration, 0);
break;
}
// Do base shimmer
doBaseShimmer();
// Run animations
led_strip->playAnimations();
effectTimeCounter.startTimeoutCounter(duration);
blade_state = bladeStateIgnition;
}
void LedStripBlade::onRetraction(retractionMode mode, uint32_t duration)
{
if (!duration)
{
debugMsg(DebugError, "Invalid retraction duration");
return;
}
debugMsg(DebugInfo, "Doing retraction effect on LedStrip, duration: %i ms", duration);
switch (mode)
{
case retractionFull:
// Actually a very fast ramp
duration = 100;
/* no break */
case retractionRamp:
// Simply fade-out
led_strip->fadeOut(duration, 0);
break;
case retractionScroll:
// Fade-out while doing a scroll effect
led_strip->fadeOut(duration, 0);
led_strip->scroll(_count, 1, duration, true, 0);
break;
}
// We need to know when to turn the blade off
effectTimeCounter.startTimeoutCounter(duration);
blade_state = bladeStateRetraction;
}
void LedStripBlade::onClash(bladeEffect* clash, uint32_t duration)
{
doEffect(clash, duration);
}
void LedStripBlade::onBlaster(bladeEffect* blaster, uint32_t duration)
{
doEffect(blaster, duration);
}
void LedStripBlade::onStab(bladeEffect* stab, uint32_t duration)
{
doEffect(stab, duration);
}
void LedStripBlade::startLockup(bladeEffect* lockup)
{
doEffect(lockup, 0);
}
void LedStripBlade::stopLockup()
{
// Lockup effect doesn't have a duration and, if flashing, it has to be stopped (flash or
// shimmer with infinite duration)
led_strip->stopFlash(0);
led_strip->stopShimmer(0);
}
void LedStripBlade::setShimmerMode(bladeEffect* shimmer)
{
COLOR old_color = default_shimmer.base_color;
COLOR new_color = shimmer->base_color;
default_shimmer = *shimmer;
if (blade_state == bladeStateIdle)
{
// Check if colors have changed
if (old_color != new_color)
{
led_strip->changeBaseShimmerColor(new_color, BLADE_SHIMMER_SWITCH_DURATION, 0);
effectTimeCounter.startTimeoutCounter(BLADE_SHIMMER_SWITCH_DURATION);
blade_state = bladeStateColorChange;
} else {
doBaseShimmer();
}
}
}
void LedStripBlade::update()
{
if (blade_state == bladeStateOff)
return;
led_strip->poll();
if (blade_state == bladeStateRetraction)
{
if (effectTimeCounter.timeout())
{
effectTimeCounter.stopCounter();
off();
}
return;
} else if (blade_state == bladeStateIgnition)
{
if (effectTimeCounter.timeout())
{
effectTimeCounter.stopCounter();
blade_state = bladeStateIdle;
}
return;
}
// Check end of effect and go back to shimmering
if (effectTimeCounter.active() && effectTimeCounter.timeout())
{
effectTimeCounter.stopCounter();
doBaseShimmer();
}
}
void LedStripBlade::asyncUpdate()
{
led_strip->startAsync();
}
void LedStripBlade::syncUpdate()
{
led_strip->stopAsync();
}
void LedStripBlade::doBaseShimmer()
{
led_strip->baseShimmer(&default_shimmer, 0);
blade_state = bladeStateIdle;
}
bool LedStripBlade::doEffect(bladeEffect* effect, uint32_t duration)
{
switch (effect->type)
{
case effectTypeFlash:
case effectTypeFlashFlicker:
led_strip->flash(effect, duration);
break;
case effectTypeShimmer:
led_strip->shimmer(effect, duration, (uint8_t) 0);
break;
case effectTypeFlashSpark:
led_strip->staticFlash(effect->base_color, 40, 100);
// no break
case effectTypeSpark:
{
uint32_t width;
if (effect->randomize)
effect->base_color = randomColor();
if (effect->depth)
width = effect->depth;
else
width = getRandom(10, 20);
led_strip->spark(effect, duration, getRandom(0, _count - 20), width, 100, 0);
break;
}
case effectTypeBigSpark:
{
uint32_t width;
if (effect->randomize)
effect->base_color = randomColor();
led_strip->staticFlash(effect->base_color, 20, 100);
if (effect->depth)
width = effect->depth;
else
width = _count / 3;
int32_t location = (_count / 2) - width;
if (location < 1)
location = 1;
led_strip->spark(effect, duration, location, width, 100, 0);
break;
}
case effectTypeTipSpark:
{
uint32_t width;
if (effect->randomize)
effect->base_color = randomColor();
if (effect->depth)
width = effect->depth;
else
width = getRandom(10, 20);
uint32_t location = _count - width - LED_STRIP_SPARK_DECAY(width);
led_strip->spark(effect, duration, location, width, 100, 0);
break;
}
case effectTypeInvalid:
return false;
default:
debugPrint(DebugWarning, "Invalid blade effect mode for LED strip");
debugPrint(DebugWarning, "Doing static flash with random color");
effect->base_color = randomColor();
duration = 50;
effect->blend = 100;
/* no break */
case effectTypeStatic:
led_strip->staticFlash(effect->base_color, duration, effect->blend);
break;
}
return true;
}
void LedStripBlade::off()
{
COLOR color(0,0,0,0);
led_strip->set(0, 0, color);
led_strip->update(0, true);
blade_state = bladeStateOff;
}
RgbHbLedBlade::RgbHbLedBlade(uint16_t current1, uint16_t current2, uint16_t current3)
{
leds[0] = leds[1] = leds[2] = NULL;
currents[0] = current1;
currents[1] = current2;
currents[2] = current3;
change_shimmer.active = false;
base_shimmer.active = false;
shimmer.active = false;
flash.active = false;
static_flash.active = false;
fade.active = false;
}
bool RgbHbLedBlade::instanceLED(uint8_t num, uint16_t current)
{
// num is zero-based
if (leds[num])
delete leds[num];
leds[num] = new HBLED(num+1);
if (!leds[num])
return false;
return leds[num]->begin(current);
}
void RgbHbLedBlade::deleteLED(uint8_t num)
{
if (leds[num])
delete leds[num];
leds[num] = NULL;
}
bool RgbHbLedBlade::initialize()
{
bool ret = true;
if (!currents[0] && !currents[1] && !currents[2])
return false;
for (uint8_t i = 0; i < 3; i++)
{
if (currents[i])
ret &= instanceLED(i, currents[i]);
}
if (!ret)
{
deleteLED(0);
deleteLED(1);
deleteLED(2);
} else {
// Start timer
add();
}
return ret;
}
void RgbHbLedBlade::setMultiplier(float val)
{
for (uint8_t i = 0; i < 3; i++)
{
if (leds[i])
leds[i]->setMultiplier(val);
}
}
float RgbHbLedBlade::getMultiplier()
{
for (uint8_t i = 0; i < 3; i++)
{
if (leds[i])
return leds[i]->getMultiplier();
}
return 0;
}
void RgbHbLedBlade::onIgnition(ignitionMode mode, uint32_t duration)
{
if (!duration)
{
debugMsg(DebugError, "Invalid ignition duration");
return;
}
// Ignition on High Brightness LED
debugMsg(DebugInfo, "Doing ignition effect on HBLED, duration: %i ms", duration);
switch (mode)
{
default:
// Not supported on HBLED
debugMsg(DebugInfo, "Ignition effect type not supported on HBLED");
debugMsg(DebugInfo, "Doing ramp with duration 50ms");
// no break
case ignitionFull:
// Actually a very fast ramp
duration = 50;
// no break
case ignitionRamp:
setMultiplier(0);
// Start shimmering
doBaseShimmer();
// Set fade parameters
fade.end = 1.0f;
fade.rate = 1.0f / (float) duration;
fade.value = 0;
fade.active = true;
break;
}
// We need to know when this effect ends
effectTimeCounter.startTimeoutCounter(duration);
blade_state = bladeStateIgnition;
}
void RgbHbLedBlade::onRetraction(retractionMode mode, uint32_t duration)
{
if (!duration)
{
debugMsg(DebugError, "Invalid retraction duration");
return;
}
// Ignition on High Brightness LED
debugMsg(DebugInfo, "Doing retraction effect on HBLED, duration: %i ms", duration);
// Check ignition mode
switch (mode)
{
default:
// Not supported on HBLED
debugMsg(DebugInfo, "Retraction effect type not supported on HBLED");
debugMsg(DebugInfo, "Doing ramp with duration 50ms");
// no break
case retractionFull:
// Actually a very fast ramp
duration = 50;
// no break
case retractionRamp:
// Set fade parameters
fade.end = 0.0f;
fade.value = getMultiplier();
fade.rate = 1.0f / (float) duration;
fade.active = true;
break;
}
// We need to know when this effect ends
effectTimeCounter.startTimeoutCounter(duration);
blade_state = bladeStateRetraction;
}
void RgbHbLedBlade::onClash(bladeEffect* clash, uint32_t duration)
{
doFlashEffect(clash, duration);
}
void RgbHbLedBlade::onBlaster(bladeEffect* blaster, uint32_t duration)
{
doFlashEffect(blaster, duration);
}
void RgbHbLedBlade::onStab(bladeEffect* stab, uint32_t duration)
{
doFlashEffect(stab, duration);
}
void RgbHbLedBlade::startLockup(bladeEffect* lockup)
{
doFlashEffect(lockup, 0);
}
void RgbHbLedBlade::stopLockup()
{
shimmer.active = false;
flash.active = false;
}
void RgbHbLedBlade::poll()
{
COLOR color;
if (effectTimeCounter.active())
{
if (effectTimeCounter.timeout())
{
effectTimeCounter.stopCounter();
if (blade_state == bladeStateRetraction)
{
off();
} else if (blade_state == bladeStateIgnition)
{
blade_state = bladeStateIdle;
} else if (blade_state == bladeStateColorChange)
{
blade_state = bladeStateIdle;
doBaseShimmer();
}
}
}
if (blade_state != bladeStateOff)
{
if (change_shimmer.active)
pollChangeShimmer();
if (base_shimmer.active)
color = pollBaseShimmer();
if (shimmer.active)
color = pollShimmer(color);
if (flash.active)
color = pollFlash(color);
if (static_flash.active)
color = pollStatic(color);
if (fade.active)
pollFade();
if (leds[0])
leds[0]->setValue(color.r);
if (leds[1])
leds[1]->setValue(color.g);
if (leds[2])
leds[2]->setValue(color.b);
}
}
void RgbHbLedBlade::setShimmerMode(bladeEffect* shimmer)
{
COLOR old_color = default_shimmer.base_color;
COLOR new_color = shimmer->base_color;
default_shimmer = *shimmer;
if (blade_state == bladeStateIdle)
{
if (old_color == new_color)
{
doBaseShimmer();
return;
}
uint32_t ticks = BLADE_SHIMMER_SWITCH_DURATION;
change_shimmer.rgbw_value[0] = base_shimmer.color.r;
change_shimmer.rgbw_value[1] = base_shimmer.color.g;
change_shimmer.rgbw_value[2] = base_shimmer.color.b;
change_shimmer.rgbw_value[3] = base_shimmer.color.w;
change_shimmer.rgbw_rate[0] = (float) (new_color.r - base_shimmer.color.r) / ticks;
change_shimmer.rgbw_rate[1] = (float) (new_color.g - base_shimmer.color.g) / ticks;
change_shimmer.rgbw_rate[2] = (float) (new_color.b - base_shimmer.color.b) / ticks;
change_shimmer.rgbw_rate[3] = (float) (new_color.w - base_shimmer.color.w) / ticks;
change_shimmer.ticks = ticks;
change_shimmer.end = new_color;
change_shimmer.active = true;
effectTimeCounter.startTimeoutCounter(BLADE_SHIMMER_SWITCH_DURATION);
blade_state = bladeStateColorChange;
}
}
void RgbHbLedBlade::doShimmer(bladeEffect* params, uint32_t duration, shimmerEffect* dst)
{
uint32_t frequency = params->freq;
if (!frequency || frequency > 25)
frequency = 25;
dst->active = false;
dst->color = params->base_color;
dst->random = params->randomize;
dst->value = 1.0f;
dst->blend = params->blend;
if (!dst->random)
{
uint32_t half_cycle = (1000 / frequency) / 2;
float limit = (float) (100 - params->depth) / 100;
dst->step = (1.0f - limit) / half_cycle;
} else {
dst->value = 1.0f;
}
dst->lower_limit = 100 - params->depth;
if (duration)
{
dst->ticks = duration;
if (!dst->ticks)
dst->ticks = 1;
} else {
dst->ticks = 0;
}
}
void RgbHbLedBlade::doBaseShimmer()
{
doShimmer(&default_shimmer, 0, &base_shimmer);
base_shimmer.blend = 100;
base_shimmer.active = true;
blade_state = bladeStateIdle;
}
void RgbHbLedBlade::doFlashEffect(bladeEffect* effect, uint32_t duration)
{
switch(effect->type)
{
default:
effect->blend = 100;
effect->base_color = randomColor();
duration = 50;
// no break
case effectTypeStatic:
static_flash.active = false;
static_flash.blend = (float) effect->blend / 100;
static_flash.ticks = duration;
static_flash.color = effect->base_color;
static_flash.active = true;
break;
case effectTypeShimmer:
doShimmer(effect, duration, &shimmer);
shimmer.active = true;
break;
case effectTypeFlashFlicker:
case effectTypeFlash:
uint32_t frequency = effect->freq;
if (!frequency || frequency > 25)
frequency = 25;
flash.flicker = (effect->type == effectTypeFlashFlicker);
flash.color = effect->base_color;
flash.blend = (float) effect->blend / 100;
flash.current_color = effect->base_color;
flash.ticks = 1000 / (frequency * 2);
flash.toggle = false;
if (!flash.ticks)
flash.ticks = 1;
if (duration && duration < flash.ticks * 2)
duration = flash.ticks * 2;
flash.duration = duration;
flash.active = true;
break;
}
}
void RgbHbLedBlade::off()
{
base_shimmer.active = shimmer.active = flash.active = fade.active = false;
for (uint8_t i = 0; i < 3; i++)
{
if (leds[i])
leds[i]->setValue(0);
}
blade_state = bladeStateOff;
}
void RgbHbLedBlade::processShimmerEffect(shimmerEffect* effect)
{
if (!effect->random)
{
float limit = (float) effect->lower_limit / 100;
effect->value -= effect->step;
if (effect->value >= 1.0f || effect->value <= limit)
effect->step *= -1;
} else {
effect->value = (float) getRandom(effect->lower_limit, 100) / 100;
}
}
COLOR RgbHbLedBlade::pollBaseShimmer()
{
processShimmerEffect(&base_shimmer);
COLOR color = base_shimmer.color * base_shimmer.value;
return color;
}
COLOR& RgbHbLedBlade::pollShimmer(COLOR& in)
{
processShimmerEffect(&shimmer);
COLOR color = shimmer.color * shimmer.value;
if (shimmer.blend != 100)
{
float blend = (float) shimmer.blend / 100;
in.blend(color, blend);
} else {
in = color;
}
if (shimmer.ticks)
{
shimmer.ticks--;
if (!shimmer.ticks)
shimmer.active = false;
}
return in;
}
COLOR& RgbHbLedBlade::pollFlash(COLOR& in)
{
flash.tick_count++;
if (flash.tick_count >= flash.ticks)
{
flash.tick_count = 0;
flash.toggle = !flash.toggle;
if (flash.toggle)
{
flash.current_color = 0;
} else {
flash.current_color = flash.color;
if (flash.flicker)
{
float mult = (float) getRandom(50, 100) / 100;
flash.current_color *= mult;
}
}
}
if (flash.current_color != 0)
{
COLOR color = flash.current_color;
if (flash.blend != 1)
in.blend(color, flash.blend);
else
in = color;
}
if (flash.duration)
{
flash.duration--;
if (!flash.duration)
flash.active = false;
}
return in;
}
COLOR& RgbHbLedBlade::pollStatic(COLOR& in)
{
if (static_flash.blend != 1)
in.blend(static_flash.color, static_flash.blend);
else
in = static_flash.color;
if (static_flash.ticks)
{
static_flash.ticks--;
if (static_flash.ticks == 0)
static_flash.active = false;
}
return in;
}
void RgbHbLedBlade::pollFade()
{
if (fade.end == 0)
{
// Fade out
fade.value -= fade.rate;
if (fade.value <= 0)
{
fade.value = 0;
fade.active = false;
}
} else {
// Fade in
fade.value += fade.rate;
if (fade.value >= fade.end)
{
fade.value = fade.end;
fade.active = false;
}
}
setMultiplier(fade.value);
}
void RgbHbLedBlade::pollChangeShimmer()
{
change_shimmer.ticks--;
if (!change_shimmer.ticks)
{
base_shimmer.color = change_shimmer.end;
change_shimmer.active = false;
} else {
change_shimmer.rgbw_value[0] += change_shimmer.rgbw_rate[0];
change_shimmer.rgbw_value[1] += change_shimmer.rgbw_rate[1];
change_shimmer.rgbw_value[2] += change_shimmer.rgbw_rate[2];
change_shimmer.rgbw_value[3] += change_shimmer.rgbw_rate[3];
base_shimmer.color = RGBW((uint8_t) change_shimmer.rgbw_value[0],
(uint8_t) change_shimmer.rgbw_value[1],
(uint8_t) change_shimmer.rgbw_value[2],
(uint8_t) change_shimmer.rgbw_value[3]);
}
}