-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGBFadeControl.h
More file actions
123 lines (104 loc) · 3.73 KB
/
RGBFadeControl.h
File metadata and controls
123 lines (104 loc) · 3.73 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
#ifndef RGBFadeControl_H_INCLUDED
#define RGBFadeControl_H_INCLUDED
typedef struct Color {
uint8_t red;
uint8_t green;
uint8_t blue;
Color(){
this->red = 0;
this->green = 0;
this->blue = 0;
}
Color(uint8_t red, uint8_t green, uint8_t blue){
this->red = red;
this->green = green;
this->blue = blue;
}
} Color;
//maxStep is multiplied by 60 so that steps can be measured in time/second
//(i.e. assumes the update function is in animation frame)
class RGBFadeControl {
public:
RGBFadeControl(uint8_t maxStep, uint8_t id):step(0x00),isDone(false),maxStep(maxStep*60),id(id){}
~RGBFadeControl(){
delete prevColor;
delete curColor;
delete futureColor;
}
void setup(void);
void update(void);
void interpolate(void);
void swapColor(Color *c, Color *oc);
void setColor(Color *c, uint32_t newColor);
void setFutureColor(Color *c);
boolean getIsDone(void) const;
uint32_t getCurrentColor(void) const;
void setDuration(uint8_t duration);
void reset(void);
private:
uint8_t id;
Color *prevColor;
Color *curColor;
Color *futureColor;
boolean isDone;
uint16_t step;
uint16_t maxStep;
static uint8_t scaleFactor;
};
uint8_t RGBFadeControl::scaleFactor = 5;
inline void RGBFadeControl::setup(void){
this->prevColor = new Color();
this->curColor = new Color();
this->futureColor = new Color();
this->setColor(prevColor,0x00);
this->setColor(curColor,0x00);
}
inline void RGBFadeControl::update(void){
if(++this->step <= this->maxStep) this->interpolate();
else if(!this->isDone) {
this->isDone = true;
}
}
inline void RGBFadeControl::interpolate(void){
static int32_t scalePrevRed,scalePrevGreen,scalePrevBlue,scaleFutureRed,scaleFutureGreen,scaleFutureBlue;
scalePrevRed = ((int32_t) this->prevColor->red) << RGBFadeControl::scaleFactor;
scalePrevGreen = ((int32_t) this->prevColor->green) << RGBFadeControl::scaleFactor;
scalePrevBlue = ((int32_t) this->prevColor->blue) << RGBFadeControl::scaleFactor;
scaleFutureRed= ((int32_t) this->futureColor->red) << RGBFadeControl::scaleFactor;
scaleFutureGreen = ((int32_t) this->futureColor->green) << RGBFadeControl::scaleFactor;
scaleFutureBlue = ((int32_t) this->futureColor->blue) << RGBFadeControl::scaleFactor;
this->curColor->red = (scalePrevRed + (((scaleFutureRed - scalePrevRed)/this->maxStep) * this->step)) >> RGBFadeControl::scaleFactor;
this->curColor->green = (scalePrevGreen + (((scaleFutureGreen - scalePrevGreen)/this->maxStep) * this->step)) >> RGBFadeControl::scaleFactor;
this->curColor->blue = (scalePrevBlue + (((scaleFutureBlue - scalePrevBlue)/this->maxStep) * this->step)) >> RGBFadeControl::scaleFactor;
}
inline void RGBFadeControl::setFutureColor(Color *c){
this->swapColor(this->prevColor,this->futureColor);
this->swapColor(this->futureColor,c);
this->isDone = false;
this->step = 0;
}
inline void RGBFadeControl::swapColor(Color *c, Color *oc){
c->red = oc->red;
c->green = oc->green;
c->blue = oc->blue;
}
inline void RGBFadeControl::setColor(Color *c, uint32_t newColor){
c->red = newColor >> 16;
c->green = (newColor & 0x00ff00) >> 8;
c->blue = newColor & 0x0000ff;
}
inline boolean RGBFadeControl::getIsDone(void) const {
return this->isDone;
}
inline uint32_t RGBFadeControl::getCurrentColor(void) const{
return (((uint32_t) this->curColor->red) << 16) | (((uint32_t) this->curColor->green) << 8) | this->curColor->blue;
}
inline void RGBFadeControl::setDuration(uint8_t duration){
this->maxStep = duration * 60;
}
inline void RGBFadeControl::reset(void){
this->step = 0x00;
this->setColor(prevColor,0x00);
this->setColor(curColor,0x00);
}
#endif // RGBFadeControl_H_INCLUDED