forked from patrickjahns/RGBWWLed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGBWWLed.cpp
More file actions
362 lines (276 loc) · 8.16 KB
/
RGBWWLed.cpp
File metadata and controls
362 lines (276 loc) · 8.16 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
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
/**
* 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.
*/
#include "RGBWWLed.h"
#include "RGBWWLedColor.h"
#include "RGBWWLedAnimation.h"
#include "RGBWWLedOutput.h"
/**************************************************************
* setup, init and settings
**************************************************************/
RGBWWLed::RGBWWLed() {
_isAnimationActive = false;
_cancelAnimation = false;
_clearAnimationQueue = false;
_current_color = HSVCT(0, 0, 0);
_current_output = ChannelOutput(0, 0, 0, 0, 0);
_currentAnimation = NULL;
_animationQ = new RGBWWLedAnimationQ(RGBWW_ANIMATIONQSIZE);
_pwm_output = NULL;
last_active = 0;
}
RGBWWLed::~RGBWWLed() {
delete _animationQ;
if (_currentAnimation != NULL) {
delete _currentAnimation;
}
if (_pwm_output != NULL) {
delete _pwm_output;
}
}
void RGBWWLed::init(int redPIN, int greenPIN, int bluePIN, int wwPIN, int cwPIN, int pwmFrequency /* =200 */) {
_pwm_output = new PWMOutput(redPIN, greenPIN, bluePIN, wwPIN, cwPIN, pwmFrequency);
}
/**************************************************************
* OUTPUT
**************************************************************/
void RGBWWLed::refresh() {
setOutput(_current_color);
}
ChannelOutput RGBWWLed::getCurrentOutput() {
return _current_output;
}
HSVCT RGBWWLed::getCurrentColor() {
return _current_color;
}
void RGBWWLed::setOutput(HSVCT& outputcolor) {
RGBWCT rgbwk;
_current_color = outputcolor;
colorutils.HSVtoRGB(outputcolor, rgbwk);
setOutput(rgbwk);
}
void RGBWWLed::setOutput(RGBWCT& outputcolor) {
ChannelOutput output;
colorutils.whiteBalance(outputcolor, output);
setOutput(output);
}
void RGBWWLed::setOutput(ChannelOutput& output) {
if(_pwm_output != NULL) {
colorutils.correctBrightness(output);
_current_output = output;
debugRGBW("R:%i | G:%i | B:%i | WW:%i | CW:%i", output.r, output.g, output.b, output.ww, output.cw);
_pwm_output->setOutput((output.r == -1)?-1:RGBWW_dim_curve[output.r],
(output.g == -1)?-1:RGBWW_dim_curve[output.g],
(output.b == -1)?-1:RGBWW_dim_curve[output.b],
(output.ww == -1)?-1:RGBWW_dim_curve[output.ww],
(output.cw == -1)?-1:RGBWW_dim_curve[output.cw]);
}
};
void RGBWWLed::setOutputRaw(int& red, int& green, int& blue, int& wwhite, int& cwhite) {
if(_pwm_output != NULL) {
_current_output = ChannelOutput(red, green, blue, wwhite, cwhite);
_pwm_output->setOutput(red, green, blue, wwhite, cwhite);
}
}
/**************************************************************
* ANIMATION/TRANSITION
**************************************************************/
bool RGBWWLed::show() {
// check if we need to cancel effect
if (_cancelAnimation || _clearAnimationQueue) {
if (_cancelAnimation ) {
cleanupCurrentAnimation();
}
// cleanup Q if we cancel all effects
if (_clearAnimationQueue) {
cleanupAnimationQ();
}
}
#ifdef ARDUINO
//only need this part when using arduino
long now = millis();
if (now - last_active < RGBWW_MINTIMEDIFF) {
// Interval hasn't passed yet
return true;
}
last_active = now;
#endif // ARDUINO
// Interval has passed
// check if we need to animate or there is any new animation
if (!_isAnimationActive) {
//check if animation otherwise return true
if (_animationQ->isEmpty()) {
return true;
}
_currentAnimation = _animationQ->pop();
_isAnimationActive = true;
}
if (_currentAnimation->run()) {
//callback animation finished
if(_animationcallback != NULL ){
_animationcallback(this);
}
cleanupCurrentAnimation();
}
return false;
}
bool RGBWWLed::addToQueue(RGBWWLedAnimation* animation) {
return _animationQ->push(animation);
}
bool RGBWWLed::isAnimationQFull() {
return _animationQ->isFull();
}
bool RGBWWLed::isAnimationActive() {
return _isAnimationActive;
}
void RGBWWLed::skipAnimation(){
if (_isAnimationActive) {
_cancelAnimation = true;
}
}
void RGBWWLed::clearAnimationQueue() {
_clearAnimationQueue = true;
}
void RGBWWLed::setAnimationCallback( void (*func)(RGBWWLed* led) ) {
_animationcallback = func;
}
void RGBWWLed::setAnimationSpeed(int speed) {
if(_currentAnimation != NULL) {
_currentAnimation->setSpeed(speed);
}
}
void RGBWWLed::setAnimationBrightness(int brightness){
if(_currentAnimation != NULL) {
_currentAnimation->setBrightness(brightness);
}
}
void RGBWWLed::setHSV(HSVCT& color, bool queue /*= false */) {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVSetOutput(color, this));
}
void RGBWWLed::setHSV(HSVCT& color, int time, bool queue /*= false*/) {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVSetOutput(color, this, time));
}
void RGBWWLed::fadeHSV(HSVCT& color, int time, bool queue) {
fadeHSV( color, time, 1, queue);
}
void RGBWWLed::fadeHSV(HSVCT& color, int time, int direction) {
fadeHSV( color, time, direction, false);
}
void RGBWWLed::fadeHSV(HSVCT& color, int time, int direction /* = 1 */, bool queue /* = false */) {
if (time == 0 || time < RGBWW_MINTIMEDIFF) {
// no animation - setting color directly
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVSetOutput(color, this));
} else {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVTransition(color, time, direction, this));
}
}
void RGBWWLed::fadeHSV(HSVCT& colorFrom, HSVCT& color, int time, int direction /* = 1 */, bool queue /* = false */) {
if (colorFrom.h != color.h || colorFrom.s != color.s || colorFrom.v != color.v || colorFrom.ct != color.ct ) {
if (time == 0 || time < RGBWW_MINTIMEDIFF) {
// no animation - setting color directly
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVSetOutput(color, this));
} else {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new HSVTransition(colorFrom, color, time, direction, this));
}
}
}
void RGBWWLed::setRAW(ChannelOutput output, bool queue /* = false */) {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWSetOutput(output, this));
}
void RGBWWLed::setRAW(ChannelOutput output, int time, bool queue /* = false */) {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWSetOutput(output, this, time));
}
void RGBWWLed::fadeRAW(ChannelOutput output, int time, bool queue /* = false */) {
if (time == 0 || time < RGBWW_MINTIMEDIFF) {
// no animation - setting color directly
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWSetOutput(output, this));
} else {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWTransition(output, time, this));
}
}
void RGBWWLed::fadeRAW(ChannelOutput output_from, ChannelOutput output, int time, bool queue /* = false */) {
if (output_from.r != output.r || output_from.g != output.g || output_from.b != output.b ||
output_from.ww != output.ww || output_from.cw != output.cw ) {
if (time == 0 || time < RGBWW_MINTIMEDIFF) {
// no animation - setting color directly
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWSetOutput(output, this));
} else {
if (!queue) {
//not using queue
cleanupAnimationQ();
cleanupCurrentAnimation();
}
_animationQ->push(new RAWTransition(output_from, output, time, this));
}
}
}
void RGBWWLed::cleanupCurrentAnimation() {
if (_currentAnimation != NULL) {
_isAnimationActive = false;
delete _currentAnimation;
_currentAnimation = NULL;
}
}
void RGBWWLed::cleanupAnimationQ() {
_animationQ->clear();
_clearAnimationQueue = false;
}