This repository was archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRGBWWLedAnimation.h
More file actions
333 lines (269 loc) · 6.61 KB
/
RGBWWLedAnimation.h
File metadata and controls
333 lines (269 loc) · 6.61 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
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
/**
* RGBWWLed - simple Library for controlling RGB WarmWhite ColdWhite LEDs via PWM
* @file
* @author Patrick Jahns http://github.com/patrickjahns
*
* All files of this project are provided under the LGPL v3 license.
*/
#ifndef RGBWWLedAnimation_h
#define RGBWWLedAnimation_h
#include "RGBWWLed.h"
#include "RGBWWLedColor.h"
class RGBWWLed;
class RGBWWLedAnimation;
/**
* A simple queue implementation
*
*/
class RGBWWLedAnimationQ
{
public:
RGBWWLedAnimationQ(int qsize);
~RGBWWLedAnimationQ();
/**
* Check if the queue is empty or not
*
* @return bool
* @retval true queue is empty
* @retval false queue is not empty
*/
bool isEmpty();
/**
* Check if the queue is full
*
* @return bool
* @retval true queue is full
* @retval false queue is not full
*/
bool isFull();
/**
* Add an animation to the queue
*
* @param animation
* @return if the object was inserted successfully
* @retval true successfully inserted object
* @retval false failed to insert objec
*/
bool push(RGBWWLedAnimation* animation);
/**
* Empty queue and delete all objects stored
*/
void clear();
/**
* Returns first animation object pointer but keeps it in the queue
*
* @return RGBWWLedAnimation*
*/
RGBWWLedAnimation* peek();
/**
* Returns first animation object pointer and removes it from queue
*
* @return RGBWWLedAnimation*
*/
RGBWWLedAnimation* pop();
private:
int _size, _count, _front, _back;
RGBWWLedAnimation** q;
};
/**
* Abstract class representing the interface for animations
*
*/
class RGBWWLedAnimation
{
public:
virtual ~RGBWWLedAnimation() {};
/**
* Processing method, will be called from main loop
*
* @return status of the animation
* @retval true the animation is finished
* @retval false the animation is not finished yet
*/
virtual bool run() {return true;};
/**
* Generic interface method for changing a variable
* representing the speed of the current active animation
*
* @param newspeed
*/
virtual void setSpeed(int newspeed) {};
/**
* Generic interface method for changing a variable
* representing the brightness of the current active animation
*
* @param newbrightness
*/
virtual void setBrightness(int newbrightness) {};
/**
* Interface to reset the animation so it can run from the beginning
*
*/
virtual void reset() {};
};
/**
* Set output to color without effect/transition
*
*/
class HSVSetOutput: public RGBWWLedAnimation
{
public:
/**
* Set output to color without effect/transition
*
* @param color color to show
* @param ctrl pointer to RGBWWLed controller object
* @param time minimal amount of time the color stays active
*/
HSVSetOutput(const HSVCT& color, RGBWWLed* rgbled, int time = 0);
bool run();
private:
int count;
int steps;
RGBWWLed* rgbwwctrl;
HSVCT outputcolor;
};
struct BresenhamValues {
int delta, error, count, step;
};
/**
* A simple Colorfade utilizing the HSV colorspace
*
*/
class HSVTransition: public RGBWWLedAnimation
{
public:
/**
* Fade from the current color to another color (colorEnd).
*
* @param colorEnd color at the end
* @param time the amount of time the transition takes in ms
* @param direction shortest (direction == 0)/longest (direction == 1) way for transition
* @param ctrl main RGBWWLed object for calling setOutput
*/
HSVTransition(const HSVCT& colorEnd, const int& time, const int& direction, RGBWWLed* ctrl);
/**
* Fade from one color (colorFrom) to another color (colorFinish)
*
* @param colorFrom color at the beginning
* @param colorEnd color at the end of the transition
* @param time the amount of time the transition takes in ms
* @param direction shortest (direction == 0)/longest (direction == 1) way for transition
* @param ctrl main RGBWWLed object for calling setOutput
*/
HSVTransition(const HSVCT& colorFrom, const HSVCT& colorEnd, const int& time, const int& direction, RGBWWLed* ctrl);
void reset();
bool run();
private:
bool init();
HSVCT _basecolor;
HSVCT _currentcolor;
HSVCT _finalcolor;
bool _hasbasecolor;
int _currentstep;
int _steps;
int _huedirection;
BresenhamValues hue;
BresenhamValues sat;
BresenhamValues val;
BresenhamValues ct;
RGBWWLed* rgbwwctrl;
static int bresenham(BresenhamValues& values, int& dx, int& base, int& current);
};
/**
* Set output to a new state without effect/transition
*
*/
class RAWSetOutput: public RGBWWLedAnimation
{
public:
/**
* Set output to color without effect/transition
*
* @param output output to set
* @param ctrl pointer to RGBWWLed controller object
* @param time minimal amount of time the output stays active
*/
RAWSetOutput(const ChannelOutput& output, RGBWWLed* rgbled, int time = 0);
bool run();
private:
int count;
int steps;
RGBWWLed* rgbwwctrl;
ChannelOutput outputcolor;
};
/**
* A simple Fade from one output state to another
*
*/
class RAWTransition: public RGBWWLedAnimation
{
public:
/**
* Fade from the current output to a new one (output)
*
* @param output output at the end of the transition
* @param time the amount of time the transition takes in ms
* @param ctrl main RGBWWLed object for calling setOutput
*/
RAWTransition(const ChannelOutput& output, const int& time, RGBWWLed* ctrl);
/**
* Fade from one output state (output_from) to another(output)
*
* @param output_from output at the beginning
* @param output output at the end of the transition
* @param time the amount of time the transition takes in ms
* @param ctrl main RGBWWLed object for calling setOutput
*/
RAWTransition(const ChannelOutput& output_from, const ChannelOutput& output, const int& time, RGBWWLed* ctrl);
void reset();
bool run();
private:
bool init();
ChannelOutput _basecolor;
ChannelOutput _currentcolor;
ChannelOutput _finalcolor;
bool _hasbasecolor;
int _currentstep;
int _steps;
BresenhamValues red;
BresenhamValues green;
BresenhamValues blue;
BresenhamValues warmwhite;
BresenhamValues coldwhite;
RGBWWLed* rgbwwctrl;
static int bresenham(BresenhamValues& values, int& dx, int& base, int& current);
};
/**
*
*/
class RGBWWAnimationSet: public RGBWWLedAnimation {
public:
/**
*
* @param animations
* @param count
* @param loop
*/
RGBWWAnimationSet(RGBWWLedAnimation** animations, int count, bool loop=false);
~RGBWWAnimationSet();
/**
*
* @param newspeed
*/
void setSpeed(int newspeed);
/**
*
* @param newbrightness
*/
void setBrightness(int newbrightness);
bool run();
private:
int _current = 0;
int _count;
int _brightness = -1;
int _speed = -1;
bool _loop;
RGBWWLedAnimation** q;
};
#endif // RGBWWLedAnimation_h