-
Notifications
You must be signed in to change notification settings - Fork 0
/
RgbController.cpp
170 lines (156 loc) · 4.41 KB
/
RgbController.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
/*
*
* Copyright (C) 2016 Pedro Ruiz
*
* This file is part of rgbStrip.
*
* rgbStrip 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.
*
* rgbStrip 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.
*
* You should have received a copy of the GNU General Public License
* along with rgbStrip. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Arduino.h>
#include "RgbController.h"
void copyRGBColor(rgbColor& from, rgbColor& to)
{
to.r = from.r;
to.g = from.g;
to.b = from.b;
}
RgbController::RgbController(byte r, byte g, byte b)
{
//Set pins
strip.REDP = r;
strip.GREENP = g;
strip.BLUEP = b;
//Setup the brightness to the higher value
strip.brightness = 255;
//Set pinMode
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
}
RgbController::setRGB(rgbColor& color)
{
copyRGBColor(color, strip.currentColor);
//Writing colors
analogWrite(strip.REDP, calculateBrightness(color.r));
analogWrite(strip.GREENP, calculateBrightness(color.g));
analogWrite(strip.BLUEP, calculateBrightness(color.b));
}
rgbColor RgbController::getRGB(void)
{
return strip.currentColor;
}
RgbController::setRed(byte r)
{
analogWrite(strip.REDP, calculateBrightness(r));
strip.currentColor.r = r;
}
RgbController::setGreen(byte g)
{
analogWrite(strip.GREENP, calculateBrightness(g));
strip.currentColor.g = g;
}
RgbController::setBlue(byte b)
{
analogWrite(strip.BLUEP, calculateBrightness(b));
strip.currentColor.b = b;
}
RgbController::setBrightness(byte a)
{
strip.brightness = a;
setRGB(strip.currentColor);
}
byte RgbController::getRed(void)
{
return strip.currentColor.r;
}
byte RgbController::getBlue(void)
{
return strip.currentColor.b;
}
byte RgbController::getGreen(void)
{
return strip.currentColor.g;
}
byte RgbController::getBrightness(void)
{
return strip.brightness;
}
/*
* This function is a dirty hack for set brigthness
* if you can control only R G B and no the positive.
*
* It is recommended to use a fourth control on the positive
* for the brigthness.
*/
byte RgbController::calculateBrightness(byte from)
{
//Dont do any operation if is at high state (255)
if(strip.brightness == 255)
return from;
return ((float)from/(float)255)*(float)strip.brightness;
}
// Calculate the rgb changes needed per milliseconds
RgbController::calculateFadeFrames(void)
{
strip.animation.rgbPerMillis[1] = (float)((strip.animation.color.g - getGreen()) /(float)strip.animation.duration);
strip.animation.rgbPerMillis[2] = (float)((strip.animation.color.b - getBlue()) /(float)strip.animation.duration);
strip.animation.rgbPerMillis[0] = (float)((strip.animation.color.r - getRed()) /(float)strip.animation.duration);
}
//For start animation
RgbController::startAnimation(animationType at, long duration, rgbColor& color)
{
strip.animation.at = at;
strip.animation.duration = duration;
strip.animation.startat = millis();
copyRGBColor(color, strip.animation.color);
copyRGBColor(strip.currentColor, strip.animation.startcolor);
if(at == A_SMOOTHCHANGE)
calculateFadeFrames();
}
//Return animation running
animationType RgbController::isAnimationRunning(void)
{
return strip.animation.at;
}
//Stop animation running
RgbController::stopAnimation(void)
{
strip.animation.at = A_NOTHING;
}
//Needed one time on the main loop for control the animation if needed
RgbController::processAnimation(void)
{
unsigned long time = millis() - strip.animation.startat;
switch (strip.animation.at)
{
case A_SMOOTHCHANGE:
{
//Calculate color that the strip should have at this moment.
if(time < strip.animation.duration)
{
setGreen((time * strip.animation.rgbPerMillis[1]) + strip.animation.startcolor.g);
setBlue((time * strip.animation.rgbPerMillis[2]) + strip.animation.startcolor.b);
setRed((time * strip.animation.rgbPerMillis[0]) + strip.animation.startcolor.r);
}
//If time has terminated, set the color directly.
else
{
setRGB(strip.animation.color);
strip.animation.at = A_NOTHING;
}
}
break;
}
}