forked from svn2github/led-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED.cpp
275 lines (243 loc) · 5.77 KB
/
LED.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
/*
* LED.cpp
*
* Copyright (c) 2011
* Remco Magielse & Serge Offermans
* Intelligent Lighting Institute (ILI), TU/e.
*
* All rights reserved. LAST UPDATE: 13-08-2012
*/
#include "LED.h"
#include "Calculate.h"
/* The animation object that is used *
Animation * intensityAnim;
*/
/* CONSTRUCTORS */
/** Default Constructor, make sure to set the channel correctly later on! **/
LED::LED()
{
_x = 0;
_y = 0;
_intensity = 0;
_channel = 0;
_on = true;
_autoWrite = true;
intensityAnim = new Animation();
_hasNewValue = true;
update();
}
/** Constructor with supplied channel.
autoWrite is true by default but can be set to false.
If autoWrite == true, Arduino will actuate the pin number defined in 'channel' when update() is called.
**/
LED::LED( uint16_t channel, bool autoWrite )
{
_x = 0;
_y = 0;
_intensity = 0;
_channel = channel;
_on = true;
_autoWrite = autoWrite;
intensityAnim = new Animation();
if ( _autoWrite )
{
pinMode( _channel, OUTPUT );
}
_hasNewValue = true;
update();
}
/** Constructor with x and y parameters. This functionality is not yet implemented, but in the future will allow you to move light effects over 2D arrays of LEDs.
**/
LED::LED( uint16_t channel, bool autoWrite, uint16_t x, uint16_t y )
{
_x = x;
_y = y;
_intensity = 0;
_channel = channel;
_on = true;
_autoWrite = autoWrite;
intensityAnim = new Animation();
if ( _autoWrite )
{
pinMode( _channel, OUTPUT );
}
_hasNewValue = true;
update();
}
/** Default Destructor **/
LED::~LED()
{
}
/* VOID FUNCTIONS */
/** Update Function; should be called every loop to set, animate and actuate the lamps **/
void LED::update()
{
if( intensityAnim->isAnimating() )
{
setIntensity( intensityAnim->getValue(), false );
}
/* Here we actually actuate the light if autowrite is on */
if( _autoWrite )
{
if ( hasNewValue() )
{
if ( _on )
{
analogWrite(_channel, _intensity);
}
else
{
analogWrite(_channel, 0);
}
}
}
}
/** Immediatily sets the intensity of the lamp (range 0 - 255).
Values are changed immediatily, but only actuated in the update() function.
An ongoing intensity animation is stopped unless stopAnimation is set to false.
**/
void LED::setBrightness( int intensity, bool stopAnimation )
{
setIntensity(intensity, stopAnimation);
}
void LED::setIntensity( int intensity, bool stopAnimation )
{
if( intensity != _intensity )
{
_intensity = constrain( intensity, 0, 255 );
_hasNewValue = true;
if (stopAnimation)
{
intensityAnim->stopAnimation();
}
}
}
void LED::setX( uint16_t x )
{
_x = x;
}
void LED::setY( uint16_t y )
{
_y = y;
}
void LED::setPosition( uint16_t x, uint16_t y )
{
_x = x;
_y = y;
}
/** Allows you to set the channel of the LED. if autoWrite is on, this is the Arduino pin that is actuated**/
void LED::setChannel( uint16_t channel )
{
_channel = channel;
if ( _autoWrite )
{
pinMode( _channel, OUTPUT );
}
}
/** Allows different animation types with easing to be set.
Animation types are LINEAR and QUADRATIC **/
void LED::setAnimationType( uint8_t animType, bool easeIn, bool easeOut )
{
intensityAnim->setAnimationType( animType, easeIn, easeOut );
}
/** Toggles LED on or off (inverts current state). **/
void LED::toggle()
{
_on = !_on;
_hasNewValue = true;
}
/** Turns the LED on or off depending on the supplied argument **/
void LED::setToggle( bool toggle )
{
_on = toggle;
_hasNewValue = true;
}
/** Animates the intensity of the LED to the desired value (range 0 - 255).
Duration of the animation can be set in millis.
**/
void LED::intensityTo( int to, uint32_t duration )
{
intensityAnim->startAnimation( _intensity, constrain(to, 0, 255), duration );
}
/** Animates the intensity of the LED to the desired value from a set startingpoint (range 0 - 255).
At the start of the animation the LED will jump to the set start value.
Duration of the animation can be set in millis.
**/
void LED::intensityFromTo( int from, int to, uint32_t duration )
{
intensityAnim->startAnimation( constrain(from, 0, 255), constrain(to, 0, 255), duration );
}
void LED::moveTo( uint16_t newX, uint16_t newY, uint32_t duration )
{
//Something will happen here
}
/* bool FUNCTIONS */
/** Returns true if the LED is on; this variable is independant of the intensity
**/
bool LED::isOn()
{
return _on;
}
/** Returns true if the LED is off; this variable is independant of the intensity
**/
bool LED::isOff()
{
return !_on;
}
/** Returns true if the LED is animating.
**/
bool LED::isAnimating()
{
return intensityAnim->isAnimating();
}
/** Returns true if the LED has a new value. This is particularly interesting if autoWrite == false.
If it returns true, you can for instance update your DMX channels.
**/
bool LED::hasNewValue()
{
if (_hasNewValue)
{
_hasNewValue = false;
return true;
}
else
{
return false;
}
}
/* INTEGER FUNCTIONS */
/** Returns the channel of the LED
**/
uint8_t LED::getChannel()
{
return _channel;
}
/** Returns the current intensity of the LED; if the LEd is off it returns 0
**/
uint8_t LED::getIntensity()
{
if (_on)
{
return _intensity;
}
else
{
return 0;
}
}
/** Returns the target value of the Intensity animation
**/
uint8_t LED::getTargetIntensity()
{
// return constrain( intensityAnim->getEndValue(), 0, 255 );
return intensityAnim->getEndValue();
}
/* uint16_t FUNCTIONS */
uint16_t LED::getX()
{
return _x;
}
uint16_t LED::getY()
{
return _y;
}