-
Notifications
You must be signed in to change notification settings - Fork 1
/
RGBFade.cpp
73 lines (61 loc) · 1.71 KB
/
RGBFade.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
#include "Arduino.h"
#include "RGBFade.h"
#include "Part.h"
RGBFade::RGBFade(int count_) {
count = count_;
parts = new Part[count];
for ( int i = 0; i < count; i++) {
parts[i].setRGB(0,0,0);
}
}
void RGBFade::setPart(int id, int r, int g, int b) {
if (id <= count) {
parts[id].setRGB(r, g, b);
}
}
void RGBFade::update( int pos) {
if (count > 1) {
float step = 100/(count-1);
float start = 0;
float end = 0;
int tmpPos = 0;
int counter = 0;
float percentStart = 0;
float percentEnd = 0;
while ( end < pos) {
if (start < pos) {
start = tmpPos-step;
}
end = tmpPos;
tmpPos += step;
counter++;
}
float percent = ((pos-start) * 100) / (end - start);
percentStart = 0 + (100-percent);
percentEnd = 100 - (100-percent);
int startR = parts[constrain(counter-2, 0, count)].getR() ;
int endR = parts[constrain(counter-1, 0, count)].getR() ;
startR = int( startR * (percentStart/ 100));
endR = int( endR * (percentEnd/ 100));
masterR = (startR + endR)/2;
int startG = parts[constrain(counter-2, 0, count)].getG() ;
int endG = parts[constrain(counter-1, 0, count)].getG() ;
startG = int( startG * (percentStart/ 100));
endG = int( endG * (percentEnd/ 100));
masterG = (startG + endG)/2;
int startB = parts[constrain(counter-2, 0, count)].getB() ;
int endB = parts[constrain(counter-1, 0, count)].getB() ;
startB = int( startB * (percentStart/ 100));
endB = int( endB * (percentEnd/ 100));
masterB = int((startB + endB)/2);
}
}
int RGBFade::getR() {
return masterR;
}
int RGBFade::getG() {
return masterG;
}
int RGBFade::getB() {
return masterB;
}