-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedPixel.h
141 lines (104 loc) · 3.22 KB
/
AnimatedPixel.h
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
#define FADE_IN_TIME_IN_SECONDS 5.0f
#define FADE_OUT_TIME_IN_SECONDS 5.0f
class AnimatedPixel {
public:
enum ColorMode{
COLOR_RGB,
COLOR_HSV
};
enum Mode {
rainbow,
fade,
constant,
clearMe
};
int position;
float hue = 0;
float brightness = 0;
float saturation = 0;
float targetBrightness = 0;
ColorMode colorMode = COLOR_HSV;
float hue_cycle_in_seconds = 120.0f;
bool clearNext = false;
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
float maxBrightness = 255.0f;
Mode mode = constant;
void tickBrightness(float timeFactor){
if(this->clearNext){
//this->hue = 0;
//this->brightness = 0;
//this->saturation = 0;
this->targetBrightness = 0;
// this->mode = fade;
this->clearNext = false;
}
if(this->brightness - this->targetBrightness > 0.01f){
this->brightness = std::max(0.0f,this->brightness - (timeFactor * (maxBrightness/FADE_OUT_TIME_IN_SECONDS)));
}
if(this->brightness - this->targetBrightness < -0.01f){
this->brightness = std::min(255.0f,this->brightness + (timeFactor * (maxBrightness/FADE_IN_TIME_IN_SECONDS)));
}
}
byte getBrightness(){
byte result = map(this->brightness, 0, 255, 0, 255);
return result;
}
void tick(float timeFactor){
switch(this->mode){
case rainbow:
if(hue_cycle_in_seconds != 0){
this->hue += (timeFactor * (256.0f/hue_cycle_in_seconds));
}
break;
case fade:
case constant:
break;
}
this->tickBrightness(timeFactor);
}
void clear(){
this->clear(false);
}
void clear(bool force){
if(force){
this->brightness = 0.0f;
}else{
this->clearNext = true;
}
}
void setTargetBrightness(float brightness){
if(brightness > 0.01f){
this->clearNext = false;
}
this->maxBrightness = brightness;
this->targetBrightness = brightness;
}
void setRainbow(float brightness){
// Serial.println("Setting pixel to rainbow.");
if(this->mode != rainbow){
Serial.println(this->mode);
this->hue = random(0,256);
this->saturation = 255;
this->mode = rainbow;
}else{
// Serial.println("Already a rainbow");
}
this->setTargetBrightness(brightness);
}
void debug(){
Serial.println("Pixel:");
Serial.printf("hue: %f\n", this->hue);
Serial.printf("saturation: %f\n", this->saturation);
Serial.printf("brightness: %f\n", this->brightness);
Serial.printf("target brightness: %f\n", this->targetBrightness);
Serial.printf("color mode: %d\n", this->colorMode);
Serial.printf("hue cycle: %f\n", this->hue_cycle_in_seconds);
Serial.printf("clear next: %s\n", this->clearNext ? "yes" : "no");
Serial.printf("hex: %02x%02x%02x\n", this->r, this->g, this->b);
Serial.printf("max brightness: %f\n", this->maxBrightness);
Serial.printf("mode: %d\n", this->mode);
Serial.println("");
}
};