forked from svn2github/led-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CctLamp.cpp
300 lines (257 loc) · 7.52 KB
/
CctLamp.cpp
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
/*
* CctLamp.h
*
* Copyright (c) 2011
* Remco Magielse & Serge Offermans
* Intelligent Lighting Institute (ILI), TU/e.
*
* All rights reserved. LAST UPDATE: 13-08-2012
*/
#include "CctLamp.h"
#include "Calculate.h"
/* CONSTRUCTORS */
CctLamp::CctLamp( uint16_t channelWarm, uint16_t channelCool, bool autoWrite )
{
_x = 0;
_y = 0;
_intensity = 0;
_cct = 127;
_channel = channelWarm;
_channelWarm = channelWarm;
_channelCool = channelCool;
_on = true;
_autoWrite = autoWrite;
intensityAnim = new Animation();
cctAnim = new Animation();
if ( _autoWrite )
{
pinMode( _channelWarm, OUTPUT );
pinMode( _channelCool, OUTPUT );
}
_hasNewValue = true;
update();
}
CctLamp::CctLamp( uint16_t channelWarm, uint16_t channelCool, bool autoWrite, uint16_t x, uint16_t y )
{
_x = x;
_y = y;
_intensity = 0;
_cct = 127;
_channel = channelWarm;
_channelWarm = channelWarm;
_channelCool = channelCool;
_on = true;
_autoWrite = autoWrite;
intensityAnim = new Animation();
cctAnim = new Animation();
if ( _autoWrite )
{
pinMode( _channelWarm, OUTPUT );
pinMode( _channelCool, OUTPUT );
}
_hasNewValue = true;
update();
}
CctLamp::~CctLamp()
{
}
/* VOID FUNCTIONS */
/** Allows you to set the channel of the LED. if autoWrite is on, this is the Arduino pin that is actuated
If only one argument is supplied, the green and blue channel are the subsequent channel numbers
**/
void CctLamp::setChannel( uint16_t channelWarm, uint16_t channelCool)
{
if ( channelCool == 0 ) // This is the case if the user does not supply arguments for cool led
{
channelCool = channelWarm + 1;
}
_channel = channelWarm;
_channelWarm = channelWarm;
_channelCool = channelCool;
if ( _autoWrite )
{
pinMode( _channelWarm, OUTPUT );
pinMode( _channelCool, OUTPUT );
}
}
/** Immediatily sets the color temperature of the lamp (range 0 - 255). An ongoing cct animation is stopped unless stopAnimation is set to false.
Make sure to also set the intensity to a value that is greater than 0,
otherwise you won't see the effect of the color temperature change.
**/
void CctLamp::setCct( int cct, bool stopAnimation )
{
if( cct != _cct )
{
_cct = constrain( cct, 0, 255 );
_hasNewValue = true;
if (stopAnimation)
{
cctAnim->stopAnimation();
}
}
}
void CctLamp::setIntensityWarm( uint8_t intensityWarm )
{
if( intensityWarm != _intensityWarm )
{
_intensityWarm = constrain( intensityWarm, 0, 255 );
}
}
void CctLamp::setIntensityCool( uint8_t intensityCool )
{
if( intensityCool != _intensityCool )
{
_intensityCool = constrain( intensityCool, 0, 255 );
}
}
/** Animates the color temperature of the lamp (range 0 - 255) with its duration in millis.
Make sure to also set the intensity to a value that is greater than 0,
otherwise you won't see the effect of the color temperature change.
**/
void CctLamp::cctTo( int cct, uint32_t duration )
{
cctAnim->startAnimation( _cct, constrain(cct, 0, 255), duration );
}
/** Animates CCT Lamp with all its values;
intensity, cct value and duration in millis **/
void CctLamp::cctLampTo( int intens, int cct, uint32_t duration )
{
intensityAnim->startAnimation( _intensity, constrain(intens, 0, 255), duration );
cctAnim->startAnimation( _cct, constrain(cct, 0, 255), duration );
}
/** Sets the animation type for a CCT Lamp. The available animation types are LINEAR (no easing) and QUADRATIC.
QUADRATIC animations allow independant easing in and out **/
void CctLamp::setAnimationType( uint8_t animType, bool easeIn, bool easeOut )
{
intensityAnim->setAnimationType( animType, easeIn, easeOut );
cctAnim->setAnimationType( animType, easeIn, easeOut );
}
/** Update Function; should be called every loop to set, animate and actuate the lamps **/
void CctLamp::update()
{
if( intensityAnim->isAnimating() )
{
setIntensity( intensityAnim->getValue(), false );
}
if( cctAnim->isAnimating() )
{
setCct( cctAnim->getValue(), false );
}
if ( _hasNewValue)
{
/* Here we convert CCT and Intensity values to Warm and Cool white values so they can be actuated */
setIntensityWarm( calculateIntensityWarm() );
setIntensityCool( calculateIntensityCool() );
/* Here we actually actuate the light if autowrite is on */
if( _autoWrite )
{
analogWrite(_channelWarm, _intensityWarm);
analogWrite(_channelCool, _intensityCool);
_hasNewValue = false;
}
}
}
/* INTEGER FUNCTIONS */
/** Returns the channel to which the warm LED should be connected
**/
uint16_t CctLamp::getChannelWarm()
{
return _channelWarm;
}
/** Returns the channel to which the cool LED should be connected **/
uint16_t CctLamp::getChannelCool()
{
return _channelCool;
}
/** Returns the current intensity of the warm LED **/
uint8_t CctLamp::getIntensityWarm()
{
return _intensityWarm;
}
/** Returns the current intensity of the cool LED **/
uint8_t CctLamp::getIntensityCool()
{
return _intensityCool;
}
/** Returns the current color temperature of the LED (range 0 - 255) **/
uint8_t CctLamp::getCct()
{
return _cct;
}
/** Returns the target value of the Cct animation
**/
uint8_t CctLamp::getTargetCct()
{
return cctAnim->getEndValue();
}
uint8_t CctLamp::calculateIntensityWarm()
{
/* Calculate the Warm and Cool values based on the newly calculated cct and intensity values */
uint8_t animatedIntensityWarm = 0;
//uint8_t animatedIntensityCool = 0;
/* first calculate based on the cct value */
if( _cct <= 127 )
{
//animatedIntensityCool = map( _cct, 0, 127, 0, 255 );
animatedIntensityWarm = 255;
}
else
{
animatedIntensityWarm = map( _cct, 127, 255, 255, 0);
//animatedIntensityCool = 255;
}
/* second multiply with intensity to determine the overall level */
animatedIntensityWarm = uint8_t ( (animatedIntensityWarm * _intensity) / 255 );
//animatedIntensityCool = uint8_t ( (animatedIntensityCool * _intensity) / 255 );
return animatedIntensityWarm;
}
uint8_t CctLamp::calculateIntensityCool()
{
/* Calculate the Warm and Cool values based on the newly calculated cct and intensity values */
//uint8_t animatedIntensityWarm = 0;
uint8_t animatedIntensityCool = 0;
/* first calculate based on the cct value */
if( _cct <= 127 )
{
animatedIntensityCool = map( _cct, 0, 127, 0, 255 );
//animatedIntensityWarm = 255;
}
else
{
//animatedIntensityWarm = map( _cct, 127, 255, 255, 0);
animatedIntensityCool = 255;
}
/* second multiply with intensity to determine the overall level */
//animatedIntensityWarm = uint8_t ( (animatedIntensityWarm * _intensity) / 255 );
animatedIntensityCool = uint8_t ( (animatedIntensityCool * _intensity) / 255 );
return animatedIntensityCool;
}
/* BOOL FUNCTIONS */
/** Returns true if the lamp is animating either intensity of cct **/
bool CctLamp::isAnimating()
{
if ( !intensityAnim->isAnimating() && !cctAnim->isAnimating() )
{
return false;
}
else
{
return true;
}
}
/** Returns true if the supplied parameter (CCT or INTENSITY) is animating **/
bool CctLamp::isAnimating(uint8_t param)
{
switch ( param )
{
case PARAM_INTENSITY:
return intensityAnim->isAnimating();
break;
case PARAM_CCT:
return cctAnim->isAnimating();
break;
default:
return false;
break;
}
}