-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
148 lines (129 loc) · 3.63 KB
/
Animation.cpp
File metadata and controls
148 lines (129 loc) · 3.63 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
#include "Arduino.h"
#include "Animation.h"
#include "Leds.h"
#include "Enums.h"
Animation::Animation() {
_start = 0;
_duration = 0;
_steps = 0;
_lastStep = -1;
_loop = false;
_alive = false;
}
void Animation::play(AnimationType animationType) {
switch(animationType) {
case ANIMATE_INDICATORS_LEFT:
setAnimation(1000, true, animate_indicator_left, INDICATOR_LEFT_STEPS);
break;
case ANIMATE_INDICATORS_RIGHT:
setAnimation(1000, true, animate_indicator_right, INDICATOR_RIGHT_STEPS);
break;
case ANIMATE_INDICATORS_WARNING:
setAnimation(1000, true, animate_indicator_warning, INDICATOR_WARNING_STEPS);
break;
case ANIMATE_SIRENS:
setAnimation(2000, true, animate_sirens, SIRENS_STEPS);
break;
}
}
void Animation::setAnimation(unsigned long duration, bool loopAnimation, updateFunction update, int steps) {
_start = millis();
_duration = duration;
_steps = steps;
_loop = loopAnimation;
_update = update;
_lastStep = -1;
_alive = true;
}
void Animation::update() {
if (_alive == false || _update == NULL) {
return;
}
unsigned long elapsed_time = millis() - _start;
if (elapsed_time >= _duration) {
if (_loop) {
_start = millis();
} else {
_alive = false;
}
} else {
int step = Animation::step(elapsed_time, _duration, _steps);
if (step != _lastStep) {
_lastStep = step;
_update(elapsed_time, _duration, step, false);
FastLED.show();
}
}
}
void Animation::stop() {
_alive = false;
_update(0, 0, 0, true);
FastLED.show();
}
void animate_indicator_left(unsigned long elapsed_time, unsigned long duration, int step, bool stop) {
// Serial.print("LEFT INDICATOR LED: ");
// Serial.println(step);
// Front left LEDS 0-3, Rear left LEDS 12-15
setColorLoop(0, 4, CRGB::Black);
setColorLoop(12, 4, CRGB::Black);
if(stop) return;
if(step <= 4) {
setColorLoop(0, step, CRGB::OrangeRed);
setColorLoop(12, step, CRGB::OrangeRed);
} else {
setColorLoop(step % 4, 4 - step % 4 , CRGB::OrangeRed);
setColorLoop(12 + step % 4, 4 - step % 4, CRGB::OrangeRed);
}
}
void animate_indicator_right(unsigned long elapsed_time, unsigned long duration, int step, bool stop) {
// Serial.print("RIGHT INDICATOR LED: ");
// Serial.println(INDICATOR_RIGHT_STEPS - step - 1);
// Front right LEDS 4-7, Rear right LEDS 8-11
setColorLoop(4, 8, CRGB::Black);
if(stop) return;
leds[step + 4] = CRGB::OrangeRed;
leds[step + 8] = CRGB::OrangeRed;
}
void animate_indicator_warning(unsigned long elapsed_time, unsigned long duration, int step, bool stop) {
// Serial.print("INDICATORS LED: ");
// Serial.println(step % 2 == 1);
if(stop) {
setColorLoop(0, 15, CRGB::Black);
return;
}
bool on = step % 2 == 1;
CRGB color = on ? CRGB::OrangeRed : CRGB::Black;
// LEDS 0-15
setColorLoop(0, 15, color);
}
void animate_sirens(unsigned long elapsed_time, unsigned long duration, int step, bool stop) {
// Serial.print("SIRENS: ");
// reset all
setColorLoop(16, 12, CRGB::Black);
if(stop) return;
if (step < 12) {
if (step % 2 == 0) {
if (step > 5) {
// LEDS 16-21
setColorLoop(22, 6, CRGB::Blue);
// Serial.println("RIGHT");
} else {
// LEDS 22-27
setColorLoop(16, 6, CRGB::Red);
// Serial.println("LEFT");
}
} else {
// Serial.println("OFF");
}
} else {
if (step % 2 == 0) {
// LEDS 16-27
setColorLoop(16, 5, CRGB::Blue);
setColorLoop(21, 2, CRGB::White);
setColorLoop(23, 5, CRGB::Red);
// Serial.println("BOTH");
} else {
// Serial.println("OFF");
}
}
}