-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoodlamp.cpp
219 lines (186 loc) · 5.2 KB
/
Moodlamp.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
/*
Moodlamp.h - Moodlamp library
Copyright (C) 2013 Oier Bravo Urtasun. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
*/
#include "Arduino.h"
#include "Moodlamp.h"
/*! \brief Constructor
\param redPin Arduino pin for red led
\param greenPin Arduino pin for green led
\param bluePin Arduino pin for blue led
*/
Moodlamp::Moodlamp(int redPin,int greenPin,int bluePin)
{
pinMode(redPin, OUTPUT);
_redPin = redPin;
pinMode(greenPin, OUTPUT);
_greenPin = greenPin;
pinMode(bluePin, OUTPUT);
_bluePin = bluePin;
//We put pins in an array for easy use.
pinMapping[0] = _redPin;
pinMapping[1] = _greenPin;
pinMapping[2] = _bluePin;
//Default values.
_updateTime = 10;
_mode = MODE_MANUAL;
_updateStep = 1;
//Cycle mode.
_cycleUpdateTime = 5000;
_currentCycleIndex = 0;
}
/*
/ PREDEFINED COLORS
*/
int Moodlamp::COLOR_RED[3] = {255,0,0};
int Moodlamp::COLOR_GREEN[3] = {0,255,0};
int Moodlamp::COLOR_BLUE[3] = {0,0,255};
int Moodlamp::COLOR_WHITE[3] = {255,255,255};
int Moodlamp::COLOR_BLACK[3] = {0,0,0};
int Moodlamp::COLOR_ORANGE[3] = {255,120,0};
int Moodlamp::COLOR_YELLOW[3] = {255,255,0};
int Moodlamp::COLOR_CYAN[3] = {0,255,255};
int Moodlamp::COLOR_PURPLE[3] = {255,0,255};
/*! \brief Set a new color.
\param color Array of 3 colors
*/
void Moodlamp::setColor(int* color)
{
//Iterating over the 3 colors
for(int i = 0;i<3;i++){
//Setting finale value.
_finalValues[i] = color[i];
//Comparing finale value with current to set up direction.
if(_finalValues[i] > _currentValues[i]){
//Final value bigger so positibe step.
_updateAmount[i] = _updateStep;
} else if(_finalValues[i] < _currentValues[i]){
//Final value smaller so negative step.
_updateAmount[i] = -_updateStep;
} else if(_finalValues[i] == _currentValues[i]){
//Final equal so positibe no step.
_updateAmount[i] = 0;
}
}
}
/*! \brief Data updater function. Call this as quick as posible.
*/
void Moodlamp::update(){
long now = millis();
update(now);
}
/*! \brief Data updater function. Call this as quick as posible.
\param now Time in millis. Optional.Default millis()
*/
void Moodlamp::update(long now){
//Comparing time with last update
if((now - _lastUpdate) >= _updateTime){
//Iterating over the 3 leds
for (int i = 0;i<3;i++){
//Applyng each step to the current values.
_currentValues[i] += _updateAmount[i];
//If reaches final value then set update amount to zero.
if(_currentValues[i] == _finalValues[i]){
_updateAmount[i] = 0;
}
}
//Sending data to leds.
_updateLed();
//Holding the current time as last update.
_lastUpdate = now;
}
// Cycle MODE
if(_mode == MODE_CYCLE){
if((now - _cycleLastUpdate) >= _cycleUpdateTime){
_cycleUpdate();
}
}
}
/*! \brief Sets update time between steps.
\param time Time in millis.
*/
void Moodlamp::setUpdateTime(long time){
_updateTime = time;
}
/*! \brief Sets mode.
\param mode Moodlamp mode from Moodlamp::MODE
*/
void Moodlamp::setMode(Mode mode){
_mode = mode;
}
/*! \brief Sets update time between cycle steps.
\param time Time in millis.
*/
void Moodlamp::setCycleUpdateTime(long time){
_cycleUpdateTime = time;
}
/*! \brief Sets red color amount. Doesn't affect other colors.
\param value Amount to set.
*/
void Moodlamp::setRed(int value){
int* color = _getFinalColor();
color[0] = value;
setColor(color);
}
/*! \brief Sets green color amount. Doesn't affect other colors.
\param value Amount to set.
*/
void Moodlamp::setGreen(int value){
int* color = _getFinalColor();
color[1] = value;
setColor(color);
}
/*! \brief Sets blue color amount. Doesn't affect other colors.
\param value Amount to set.
*/
void Moodlamp::setBlue(int value){
int* color = _getFinalColor();
color[2] = value;
setColor(color);
}
/*! \brief Sets red,green and blue color amount.
\param red Red amount to set.
\param green Green amount to set.
\param blue Blue amount to set.
*/
void Moodlamp::setRGB(int red,int green, int blue){
int color[3] = {red,green,blue};
setColor(color);
}
/*! \brief Cycle updater function, changes and sets a color from the sequence.
*/
void Moodlamp::_cycleUpdate(){
//Number of colors.
int sequence_num_colors = 9;
//Color sequence.
int* cycleSequence[9] = {COLOR_RED,COLOR_GREEN,COLOR_BLUE,COLOR_WHITE,COLOR_YELLOW,COLOR_PURPLE,COLOR_ORANGE,COLOR_CYAN,COLOR_WHITE};
setColor( cycleSequence[_currentCycleIndex] );
_currentCycleIndex++;
if(_currentCycleIndex == sequence_num_colors){
_currentCycleIndex = 0;
}
_cycleLastUpdate = millis();
}
/*! \brief Sends data to leds
\param mode Moodlamp mode from Moodlamp::MODE
*/
void Moodlamp::_updateLed()
{
//Iterating over the 3 leds.
for (int i = 0;i<3;i++){
analogWrite(pinMapping[i], _currentValues[i]);
}
}
int* Moodlamp::_getFinalColor()
{
int color[] = {0,0,0};
for(int i = 0;i<3;i++){
color[i] = _finalValues[i];
}
return color;
}