This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPixelAnimator.cpp
195 lines (167 loc) · 6.24 KB
/
PixelAnimator.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
#include "PixelAnimator.h"
static CRGB pixels[NUMPIXELS];
#define EXP_BRIGHT_STEP 0.2857
#define EXP_BRIGHT_OFFSET 4.0
#define EXP_BRIGHT_POW 4
int expBrightness(double in){
return (int)pow(EXP_BRIGHT_STEP*(in+EXP_BRIGHT_OFFSET), EXP_BRIGHT_POW);
}
PixelAnimator::PixelAnimator(Config* config){
CONFIG = config;
}
void PixelAnimator::setup(){
passedLow = false;
CSegmentsFull = new CRGB*[NUMPIXELS];
int halfMax = ceil(NUMPIXELS/2.0);
CSegmentsHalved = new CRGB*[halfMax];
FastLED.addLeds<PIXELTYPE, PIXELPIN, GRB>(pixels, NUMPIXELS);
setBrightness(CONFIG->PixelBrightness);
updateColors();
}
void PixelAnimator::setBrightness(uint8_t brightness){
targetBrightness = expBrightness(brightness);
}
void PixelAnimator::updateColors(){
CLow = ColorPicker::at(CONFIG->CurrentProfile->CLow);
CPart1 = ColorPicker::at(CONFIG->CurrentProfile->CPart1);
CPart2 = ColorPicker::at(CONFIG->CurrentProfile->CPart2);
CPart3 = ColorPicker::at(CONFIG->CurrentProfile->CPart3);
CFlash1 = ColorPicker::at(CONFIG->CurrentProfile->CFlash1);
CFlash2 = ColorPicker::at(CONFIG->CurrentProfile->CFlash2, true);
int i = 0;
for (; i < CONFIG->FullSegment1 && i < NUMPIXELS; ++i) CSegmentsFull[i] = &CPart1;
for (; i < CONFIG->FullSegment1+CONFIG->FullSegment2 && i < NUMPIXELS; ++i) CSegmentsFull[i] = &CPart2;
for (; i < NUMPIXELS; ++i) CSegmentsFull[i] = &CPart3;
i = 0;
int halfMax = ceil(NUMPIXELS/2.0);
for (; i < CONFIG->HalfSegment1 && i < halfMax; ++i) CSegmentsHalved[i] = &CPart1;
for (; i < CONFIG->HalfSegment1+CONFIG->HalfSegment2 && i < halfMax; ++i) CSegmentsHalved[i] = &CPart2;
for (; i < halfMax; ++i) CSegmentsHalved[i] = &CPart3;
}
void PixelAnimator::showSegmentPreview(uint8_t segment1, uint8_t segment2, uint8_t blackSegment, bool halved){
int i = 0;
CRGB part1 = blackSegment == 1 ? CRGB::Black : CPart1;
CRGB part2 = blackSegment == 2 ? CRGB::Black : CPart2;
CRGB part3 = blackSegment == 3 ? CRGB::Black : CPart3;
if(!halved){
for (; i < segment1 && i < NUMPIXELS; ++i) pixels[i] = part1;
for (; i < segment1+segment2 && i < NUMPIXELS; ++i) pixels[i] = part2;
for (; i < NUMPIXELS; ++i) pixels[i] = part3;
} else {
int midPoint = ceil(NUMPIXELS/2.0);
for (; i < segment1 && i < midPoint; ++i){ pixels[midPoint-1-i] = part1; pixels[midPoint+i] = part1; }
for (; i < segment1+segment2 && i < midPoint; ++i) { pixels[midPoint-1-i] = part2; pixels[midPoint+i] = part2; }
for (; i < midPoint; ++i) { pixels[midPoint-1-i] = part3; pixels[midPoint+i] = part3; }
}
show();
}
void PixelAnimator::show(){
FastLED.setBrightness(calculate_max_brightness_for_power_mW(targetBrightness, MAX_POWER));
FastLED.show();
}
void PixelAnimator::setRPM(uint16_t rpm){
int litPixels, blackPixels, midPoint;
bool shift = false, blank = false, low = false;
if(rpm >= CONFIG->CurrentProfile->RPMShift){ shift = true; passedLow = true; }
else if(passedLow && rpm <= CONFIG->CurrentProfile->RPMLow && rpm > CONFIG->RPMStationary){ low = true; }
else if(rpm <= CONFIG->CurrentProfile->RPMActivation){
blank = true;
if(rpm < CONFIG->RPMStationary)
passedLow = false;
else if (rpm > CONFIG->CurrentProfile->RPMLow)
passedLow = true;
}
else{
passedLow = true;
float fillRange, fillOffset;
fillOffset = rpm - CONFIG->CurrentProfile->RPMActivation;
fillRange = CONFIG->CurrentProfile->RPMShift - CONFIG->CurrentProfile->RPMActivation;
litPixels = fillOffset / fillRange * NUMPIXELS;
}
// Do shift flashing.
if(shift){
CRGB color = (millis() / ANIM_TURBO % 2) ? CFlash1 : CFlash2;
setFill(color);
}
// Do low RPM indication.
else if(low){
CRGB color = (millis() / ANIM_FAST % 2) ? CLow : CRGB::Black;
setFill(color);
}
// When we show nothing.
else if(blank){
setFill(CRGB::Black);
}
// When not shifting yet.
else{
// Pick a filling style.
switch(CONFIG->CurrentProfile->RPMAnimation){
default:
case RPMAnimation::LeftToRight:
for(int i = 0; i < NUMPIXELS; i++){
pixels[i] = (litPixels >= i ? colorFor(i) : CRGB::Black);
}
break;
case RPMAnimation::RightToLeft:
blackPixels = NUMPIXELS - litPixels - 1;
for(int i = 0; i < NUMPIXELS; i++){
pixels[i] = (blackPixels <= i ? colorFor(NUMPIXELS-1-i) : CRGB::Black);
}
break;
case RPMAnimation::Outward:
midPoint = ceil(NUMPIXELS / 2.0);
blackPixels = midPoint - round((litPixels+1) / 2.0);
for(int i = 0; i < midPoint; i++){
pixels[i] = (blackPixels <= i ? colorFor(midPoint-1-i, true) : CRGB::Black);
pixels[NUMPIXELS-1-i] = (blackPixels <= i ? colorFor(midPoint-1-i, true) : CRGB::Black);
}
break;
case RPMAnimation::Inward:
midPoint = ceil(NUMPIXELS / 2.0);
litPixels = litPixels / 2;
for(int i = 0; i < midPoint; i++){
pixels[i] = (litPixels >= i ? colorFor(i, true) : CRGB::Black);
pixels[NUMPIXELS-1-i] = (litPixels >= i ? colorFor(i, true) : CRGB::Black);
}
break;
case RPMAnimation::Full:
uint8_t progress = 3.0/NUMPIXELS*litPixels;
CRGB color;
switch (progress) {
case 0: color = CPart1; break;
case 1: color = CPart2; break;
case 2: color = CPart3; break;
}
setFill(color);
// float newBright = (float)litPixels / NUMPIXELS * (CONFIG->PixelBrightness-2.0) + 2.0;
// setBrightness(newBright);
break;
}
}
}
void PixelAnimator::setFill(CRGB color){
for(int i = 0; i < NUMPIXELS; i++){
pixels[i] = color;
}
}
void PixelAnimator::setEdges(CRGB color){
pixels[0] = color;
pixels[NUMPIXELS-1] = color;
}
void PixelAnimator::showBlockingRunlight(CRGB color, uint16_t time){
uint8_t length = 5;
uint8_t stepDelay = time / (NUMPIXELS+length);
setFill(CRGB::Black);
for (int i = 0; i < NUMPIXELS+length; ++i){
if(i < NUMPIXELS) pixels[i] = color;
if(i-length >= 0) pixels[i-length] = CRGB::Black;
FastLED.delay(stepDelay);
}
}
CRGB PixelAnimator::colorFor(uint8_t index, bool halved){
if(halved){
return *CSegmentsHalved[index];
} else {
return *CSegmentsFull[index];
}
}