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 pathRGBWWLed.h
More file actions
365 lines (298 loc) · 8.19 KB
/
RGBWWLed.h
File metadata and controls
365 lines (298 loc) · 8.19 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
363
364
365
/**
* @file
* @author Patrick Jahns http://github.com/patrickjahns
* @version 0.7.3
*
* @section LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details at
* https://www.gnu.org/copyleft/gpl.html
*
* @section DESCRIPTION
* This Library provides Methods for controlling LED RGB, WarmWhite and
* ColdWhite Led(stripes) via PWM. The Library can either use the standard
* ARDUINO PWM Implementation or if available the espressif ESP8266 SDK
* Pwm.
*
* The Library can handle different color modes (RGB, RGB+WarmWhite,
* RGB+ColdWhite, RGB+WarmWhite+ColdWhite) and offeres different calculation
* models for outputting HSV colors
*
* The Library offers various adjustments (i.e. maximum brightness of each
* channel, adjusting HSV basecolors etc..)
*
* It also provides a simple interface for creating Animations and
* queuing several animations/colors to a complex animation
*/
#ifndef RGBWWLed_h
#define RGBWWLed_h
#include <Arduino.h>
#ifdef SMING_VERSION
#define RGBWW_USE_ESP_HWPWM
#include "../../SmingCore/SmingCore.h"
#define RGBWW_PWMRESOLUTION 65536
#define RGBWW_CALC_DEPTH 10
#else
#define RGBWW_PWMRESOLUTION 1023
#define RGBWW_CALC_DEPTH 8
#endif
#define RGBWW_VERSION "0.8.1"
#define RGBWW_CALC_WIDTH int(pow(2, RGBWW_CALC_DEPTH))
#define RGBWW_CALC_MAXVAL int(RGBWW_CALC_WIDTH - 1)
#define RGBWW_CALC_HUEWHEELMAX int(RGBWW_CALC_MAXVAL * 6)
#define RGBWW_UPDATEFREQUENCY 50
#define RGBWW_MINTIMEDIFF int(1000 / RGBWW_UPDATEFREQUENCY)
#define RGBWW_ANIMATIONQSIZE 100
#define RGBWW_WARMWHITEKELVIN 2700
#define RGBWW_COLDWHITEKELVIN 6000
#ifndef DEBUG_RGBWW
#define DEBUG_RGBWW 0
#endif
#include "debugUtils.h"
#include "RGBWWconst.h"
#include "RGBWWLedColor.h"
#include "RGBWWLedAnimation.h"
#include "RGBWWLedOutput.h"
class RGBWWLedAnimation;
class RGBWWLedAnimationQ;
class RGBWWColorUtils;
class PWMOutput;
/**
*
*/
class RGBWWLed
{
public:
RGBWWLed();
~RGBWWLed();
/**
* Initialize the the LED Controller
*
* @param redPIN int representing the MC pin for the red channel
* @param greenPIN int representing the MC pin for the green channel
* @param bluePIN int representing the MC pin for the blue channel
* @param wwPIN int representing the MC pin for the warm white channel
* @param cwPIN int representing the MC pin for the cold white channel
* @param pwmFrequency (default 200)
*/
void init(int redPIN, int greenPIN, int bluePIN, int wwPIN, int cwPIN, int pwmFrequency=200);
/**
* Main function for processing animations/color output
* Use this in your loop()
*
*
* @retval true not updating
* @retval false updates applied
*/
bool show();
/**
* Refreshs the current output.
* Usefull when changing brightness, white or color correction
*
*/
void refresh();
/**
* Set Output to given HSVK color.
* Converts HSVK into seperate color channels (r,g,b,w)
* and applies brightness and white correction
*
* @param HSVK& outputcolor
*/
void setOutput(HSVCT& color);
/**
* Sets the output of the Controller to the given RGBWK
* while applying brightness and white correction
*
* @param RGBWK& outputcolor
*/
void setOutput(RGBWCT& color);
/**
* Sets the output of the controller to the specified
* channel values (with internal brightness correction)
*/
void setOutput(ChannelOutput& output);
/**
* Directly set the PWM values without color correction or white balance.
* Assumes the values are in the range of [0, PWMMAXVAL].
*
* @param int& red
* @param int& green
* @param int& blue
* @param int& wwhite
* @param int& cwhite
*/
void setOutputRaw(int& red, int& green, int& blue, int& cwhite, int& wwhite);
/**
* Returns an HSVK object representing the current color
*
* @return HSVK current color
*/
HSVCT getCurrentColor();
/**
* Returns the current values for each channel
*
* #return ChannelOutput current value of all channels
*/
ChannelOutput getCurrentOutput();
/**
* Output specified color
* (until a new color is set)
*
* @param color
* @param queue
*/
void setHSV(HSVCT& color, bool queue = false);
/**
* Output color for time x
* if time x has passed it will continue with the next
* color/transition in the animat queue
*
* @param color
* @param time
* @param queue
*/
void setHSV(HSVCT& color, int time, bool queue = false);
/**
* Fade to specified HSVK color
*
* @param color new color
* @param time duration of transition in ms
* @param direction direction of transition (0= long/ 1=short)
*/
void fadeHSV(HSVCT& color, int time, int direction);
/**
* Fade to specified HSVK color
*
* @param color new color
* @param time duration of transition in ms
* @param queue directly execute fade or queue it
*/
void fadeHSV(HSVCT& color, int time, bool queue);
/**
* Fade to specified HSVK color
*
* @param color new color
* @param time duration of transition in ms
* @param direction direction of transition (0= long/ 1=short)
* @param queue directly execute fade or queue it
*/
void fadeHSV(HSVCT& color, int time, int direction = 1, bool queue = false);
/**
* Fade from one color (colorFrom) to another color
*
* @param colorFrom starting color of transition
* @param color ending color of transition
* @param time duration of transition in ms
* @param direction direction of transition (0= long/ 1=short)
* @param queue directly execute fade or queue it
*/
void fadeHSV(HSVCT& colorFrom, HSVCT& color, int time, int direction = 1, bool q = false);
//TODO: add documentation
/**
*
* @param output
*/
void setRAW(ChannelOutput output, bool queue = false);
//TODO: add documentation
/**
*
* @param output
* @param time
* @param queue
*/
void setRAW(ChannelOutput output, int time, bool queue = false);
//TODO: add documentation
/**
*
* @param output
* @param time
* @param queue
*/
void fadeRAW(ChannelOutput output, int time, bool queue = false );
//TODO: add documentation
/**
*
* @param output_from
* @param output
* @param time
* @param queue
*/
void fadeRAW(ChannelOutput output_from, ChannelOutput output, int time, bool queue = false );
/**
* Set a function as callback when an animation has finished.
*
* Example use case would be, to save the current color to flash
* after an animation has finished to preserve it after a powerloss
*
* @param func
*/
void setAnimationCallback( void (*func)(RGBWWLed* led) );
/**
* Check if an animation is currently active
*
* @retval true if an animation is currently active
* @retval false if no animation is active
*/
bool isAnimationActive();
/**
* Check if the AnimationQueue is full
*
* @retval true queue is full
* @retval false queue is not full
*/
bool isAnimationQFull();
/**
* skip the current animation
*
*/
void skipAnimation();
/**
* Cancel all animations in the queue
*
*/
void clearAnimationQueue();
/**
* Change the speed of the current running animation
*
* @param speed
*/
void setAnimationSpeed(int speed);
/**
* Change the brightness of the current animation
*
* @param brightness
*/
void setAnimationBrightness(int brightness);
/**
* Add animation to animation Qeueue
*
* @param animation Animation object
* @return true on success / false on failure;
*/
bool addToQueue(RGBWWLedAnimation* animation);
//colorutils
RGBWWColorUtils colorutils;
private:
unsigned long last_active;
ChannelOutput _current_output;
HSVCT _current_color;
bool _cancelAnimation;
bool _clearAnimationQueue;
bool _isAnimationActive;
RGBWWLedAnimation* _currentAnimation;
RGBWWLedAnimationQ* _animationQ;
PWMOutput* _pwm_output;
void (*_animationcallback)(RGBWWLed* led) = NULL;
//helpers
void cleanupCurrentAnimation();
void cleanupAnimationQ();
};
#endif //RGBWWLed_h