-
Notifications
You must be signed in to change notification settings - Fork 2
/
MovingPeak.cpp
84 lines (72 loc) · 1.65 KB
/
MovingPeak.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
#include "MovingPeak.h"
#include "Config.h"
#include "Arduino.h"
#define DEFAULT_INTERVAL 10
#define FALLOFF 0.2
#define BOUNCE_LIMIT_DEFAULT 4
#define WINDOW 3
LED_CONTROLLER_NAMESPACE_USING
MovingPeak::MovingPeak(const Color& color) :
moveAndDecayInterval(DEFAULT_INTERVAL),
bounces(0)
{
baseColor = color;
setIntensity(1.0);
restart();
}
void MovingPeak::setIntensity(float intensity) {
this->intensity = constrain(intensity, 0.0, 1.0);
}
void MovingPeak::setPosition(int position) {
this->position = constrain(position, 0, STRIP_LENGTH-1);
}
void MovingPeak::setIncrement(int increment) {
this->increment = increment > 0 ? 1 : -1;
}
void MovingPeak::restart() {
moveAndDecayInterval.setInterval(DEFAULT_INTERVAL);
position = 0;
increment = 1;
}
bool MovingPeak::update() {
moveAndDecayInterval.update();
if (moveAndDecayInterval.isExpired()) {
moveAndDecayInterval.clearExpired();
advance();
return true;
} else {
return false;
}
}
void MovingPeak::advance() {
position += increment;
boolean bounced = false;
if (position >= STRIP_LENGTH) {
bounced = true;
increment = -1;
} else if (position <= 0) {
bounced = true;
increment = 1;
}
if (bounced) {
bounces++;
if (bounces >= BOUNCE_LIMIT_DEFAULT) {
expire();
}
moveAndDecayInterval.setInterval(
moveAndDecayInterval.getInterval()*2);
intensity *= FALLOFF;
}
}
void MovingPeak::apply(Color* stripColors) {
if (isExpired()) {
return;
}
for(int i = max(0, position-WINDOW);
i < min(STRIP_LENGTH, position+WINDOW+1); i++)
{
float localIntensity = intensity *
pow(FALLOFF, abs(i - position));
stripColors[i].add(baseColor.scaled(localIntensity));
}
}