-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.hpp
275 lines (240 loc) · 9.76 KB
/
animation.hpp
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
#include <Adafruit_NeoPixel.h>
union Color {
uint32_t w; // As packed value...w for 'word', lacking a better option
struct {
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t white;
} c; // As individual components...c for 'components'
};
// Convenience functions to initialize Color instances
// Not constructors so Color can be included in unions
Color ToColor(uint32_t color) {
Color c;
c.w = color;
return c;
}
Color ToColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
Color c;
c.c.red = red;
c.c.green = green;
c.c.blue = blue;
c.c.white = white;
return c;
}
Color ToColor(uint8_t red, uint8_t green, uint8_t blue) {
Color c;
c.c.red = red;
c.c.green = green;
c.c.blue = blue;
c.c.white = 0;
return c;
}
// Animation State
struct CommonState {
uint16_t numPixels;
unsigned long startTime;
unsigned long duration; // ms
};
struct MovingPulseState : public CommonState {
Color color;
uint16_t pulseLength;
float pixelsPerMs;
};
struct PulseState : public CommonState {
Color color;
};
struct RedFlashState : public CommonState {
bool isRgbw;
};
struct SolidColorState : public CommonState {
Color color;
};
union AnimationState {
CommonState common;
MovingPulseState movingPulse;
PulseState pulse;
RedFlashState redFlash;
SolidColorState solid;
};
// Contract of animation implemnations
class Animation {
public:
void commonInit(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) {
CommonState& s = state.common;
s.startTime = now;
s.numPixels = strip.numPixels();
}
virtual void start(unsigned long now, AnimationState& state) {
state.common.startTime = now;
}
virtual void doFrame(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) =0;
// How many cycles have completed?
virtual uint32_t cyclesComplete(unsigned long now, const AnimationState& state) const {
return (now - state.common.startTime) / state.common.duration;
}
// Checks to see if at least one full cycle has completed. Useful for
// transient animations.
bool done(unsigned long now, const AnimationState& state) const {
return cyclesComplete(now, state) > 0;
}
};
// Animation implemntations
// A pulse of light that moves down the strip of pixels
class MovingPulse : public Animation {
public:
// Initialize state to animation start point
void init(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip, Color color) {
commonInit(now, state, strip);
MovingPulseState& s = state.movingPulse;
s.duration = AnimationDuration;
s.color = color;
s.pulseLength = s.numPixels / PulseDivisor;
s.pixelsPerMs = (float) s.numPixels / AnimationDuration;
}
// Draw a new frame of the animation
virtual void doFrame(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) override {
MovingPulseState& s = state.movingPulse;
unsigned long phase = (now - s.startTime) % AnimationDuration; // In ms
uint16_t startPixel = phase * s.pixelsPerMs;
for (uint16_t i = 0; i < s.numPixels; i++) {
if (i >= startPixel && i < startPixel + s.pulseLength) {
strip.setPixelColor(i, s.color.w);
} else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
}
private:
const unsigned long AnimationDuration = 4000l; // ms
const uint16_t PulseDivisor = 8; // 1/PulseDivisor defines the size of the pulse
};
// A 'breathing'-type pulse effect - the LEDs brighten and dim repeatedly
class Pulse : public Animation {
public:
// Initialize state to animation start point
// color - the color of the LEDs, the brightness of which will be modulated
// initialPhase - in range [0.0, 1.0]; the offset within a cycle of the animation at which to start
void init(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip, Color color, double initialPhase = 0.0) {
commonInit(now, state, strip);
PulseState& s = state.pulse;
s.duration = AnimationDuration;
s.color = color;
// Modify the start time to effect a desired initial phase within the cyle
double initialPhaseMs = initialPhase * AnimationDuration;
s.startTime -= initialPhaseMs;
}
// Draw a new frame of the animation
virtual void doFrame(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) override {
PulseState& s = state.pulse;
//Serial.print("Doing pulse frame "); Serial.println(s.color.w, HEX);
unsigned long phase = (now - s.startTime) % AnimationDuration; // In ms
float brightness;
if (phase < BrightPoint) {
brightness = ((float) phase) / BrightPoint;
} else {
brightness= ((float) AnimationDuration - phase) / (AnimationDuration - BrightPoint);
}
float factor = (1.0f - MinBrightness) * brightness + MinBrightness;
uint8_t white = s.color.c.white * factor;
uint8_t red = s.color.c.red * factor;
uint8_t green = s.color.c.green * factor;
uint8_t blue = s.color.c.blue * factor;
for (uint16_t i = 0; i < s.numPixels; i++) {
strip.setPixelColor(i, red, green, blue,white);
}
strip.show();
}
private:
const unsigned long AnimationDuration = 4000l; // ms
const float FullBrightnessPoint = 0.5; // As a fraction of 1. At what point in the cycle should we achieve full brightness?
const unsigned long BrightPoint = AnimationDuration * FullBrightnessPoint;
const float MinBrightness = 0.1;
};
// A white-red-white flash, appropriate for a portal going from owned to unowned
class RedFlash : public Animation {
public:
// Initialize state to animation start point
//void init(AnimationState& state, Adafruit_NeoPixel& strip, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
void init(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip, bool isRgbw) {
commonInit(now, state, strip);
RedFlashState& s = state.redFlash;
s.duration = AnimationDuration;
s.isRgbw = isRgbw;
}
// Draw a new frame of the animation
virtual void doFrame(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) override {
RedFlashState& s = state.redFlash;
unsigned long phase = (now - s.startTime) % AnimationDuration; // In ms
Color c;
if (phase < StartRed || phase > EndRed) {
float pct;
if (phase < StartRed) {
pct = ((float) (StartRed - phase)) / (StartRed - 0L);
} else /* phase > EndRed */ {
pct = ((float) (phase - EndRed)) / (AnimationDuration - EndRed);
}
float whiteAmt = 0xff * pct;
if (s.isRgbw) {
c = ToColor(0, 0, 0, whiteAmt);
} else {
c = ToColor(whiteAmt, whiteAmt, whiteAmt);
}
} else {
float pct;
if (phase < MaxRedStart) {
pct = ((float) (phase - StartRed)) / (MaxRedStart - StartRed);
} else if (phase > MaxRedEnd) {
pct = ((float) (EndRed - phase)) / (EndRed - MaxRedEnd);
} else /* all red */ {
pct = 1.0f;
}
uint8_t redAmt = 0xff * pct;
if (s.isRgbw) {
c = ToColor(redAmt, 0, 0, 0);
} else {
c = ToColor(redAmt, 0, 0, 0);
}
}
for (uint16_t i = 0; i < s.numPixels; i++) {
strip.setPixelColor(i, c.w);
}
strip.show();
}
private:
const unsigned long AnimationDuration = 1000L; // ms
const unsigned long StartRed = AnimationDuration * 0.25; // ms - when we start ramping up red
const unsigned long MaxRedStart = AnimationDuration * 0.45; // ms - when we have full brightness red
const unsigned long MaxRedEnd = AnimationDuration * 0.55; // ms - when we have full brightness red
const unsigned long EndRed = AnimationDuration * 0.75; // ms - when we finish fading red and go back to white
};
// A white-red-white flash, appropriate for a portal going from owned to unowned
class SolidColor : public Animation {
public:
// Initialize state to animation start point
void init(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip, Color c) {
commonInit(now, state, strip);
SolidColorState& s = state.solid;
s.duration = AnimationDuration;
s.color = c;
}
// Draw a new frame of the animation
virtual void doFrame(unsigned long now, AnimationState& state, Adafruit_NeoPixel& strip) override {
SolidColorState& s = state.solid;
for (uint16_t i = 0; i < s.numPixels; i++) {
strip.setPixelColor(i, s.color.w);
}
strip.show();
}
private:
const unsigned long AnimationDuration = 1000L; // ms - arbitrary here
};
// Collection of all supported animations
struct Animations {
MovingPulse movingPulse;
Pulse pulse;
RedFlash redFlash;
SolidColor solid;
};