-
Notifications
You must be signed in to change notification settings - Fork 1
/
FastFader.cpp
294 lines (251 loc) · 8.14 KB
/
FastFader.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
||
|| @file FastFader.cpp
|| @version 1.0
|| @author Danne Stayskal
|| @contact [email protected]
||
|| @description
|| | Provides a fast, cross-fading pixelbuffer to the FastLED library
|| #
||
|| @changelog
|| | 1.0 2014-05-29 - Danne Stayskal : Initial Release
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library 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
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
==========================================
*/
#include "FastFader.h"
/*
==========================================
= FastFader::FastFader()
= Class constructor
=
= Takes:
= * leds, a reference to an array of FastLED.h CRGB triples
==========================================
*/
FastFader::FastFader(){}
/*
==========================================
= FastFader::bind
= Bind the pixel buffer and fader to the LED interface
=
= Takes: nothing
= Returns: this, the current FastFader object
==========================================
*/
FastFader& FastFader::bind(int (*pixel_buffer)[3], CRGB *leds, int num_leds, CFastLED& fast_led){
LEDs = leds;
NumLEDs = num_leds;
FLED = fast_led;
PixelBuffer = pixel_buffer;
for (int i = 0; i < NumLEDs; i++) {
LEDs[i] = CRGB(0,0,0);
PixelBuffer[i][0] = 0;
PixelBuffer[i][1] = 0;
PixelBuffer[i][2] = 0;
}
return *this;
}
/*
==========================================
= FastFader::clear
= Empties the current pixelbuffer
=
= Takes: nothing
= Returns: this, the current FastFader object
==========================================
*/
FastFader& FastFader::clear(){
for (int i = 0; i < NumLEDs; i++) {
for (int channel = 0; channel < 3; channel++) {
FastFader::PixelBuffer[i][channel] = 0;
}
}
return *this;
}
/*
==========================================
= FastFader::set_pixel
= Sets an individual pixel in the pixel buffer
=
= Takes:
= * pos, the integer position being set (0..NumLEDs-1)
= * value, (optional, default 255) the luminosity value to set (0..255).
= * If passed an integer, this will apply to all channels.
= * If passed an array[3], this will be mapped to RGB on this pixel.
= * channel, (optional, default all channels) which channel to set.
= * -1 will set all channels.
= * 0..2 will set Red, Green, or Blue, respectively
= * >2 will set all channels.
=
= Returns: this, the current FastFader object
==========================================
*/
FastFader& FastFader::set_pixel(int pos, int value, int channel){
// Clip pos to the range 0..NumLEDs - 1, inclusive
if (pos < 0) {
pos = 0;
}
if (pos > NumLEDs) {
pos = NumLEDs;
}
// Clip value to the range 0..255, inclusive
if (value > 255) {
value = 255;
}
if (value < 0) {
value = 0;
}
// Set the pixel in the buffer
if (channel < 0 || channel > 2) {
for (int c = 0; c < 3; c++) {
PixelBuffer[pos][c] = value;
}
} else {
PixelBuffer[pos][channel] = value;
}
return *this;
}
FastFader& FastFader::set_pixel(int pos, int value[3]){
for (int c = 0; c < 3; c++) {
set_pixel(pos, value[c], c);
}
return *this;
}
FastFader& FastFader::set_pixel(int pos, int value){
for (int c = 0; c < 3; c++) {
set_pixel(pos, value, c);
}
return *this;
}
FastFader& FastFader::set_pixel(int pos){
return set_pixel(pos, 255, -1);
}
/*
==========================================
= FastFader::get_pixel
= Gets an individual pixel from the pixel buffer
=
= Takes:
= * pos, the integer position being set (0..NumLEDs-1)
=
= Returns:
= * pixel, an arrray reference to an [R,G,B] pixel.
==========================================
*/
int* FastFader::get_pixel(int pos){
return PixelBuffer[pos];
}
/*
==========================================
= FastFader::set_frame
= Sets an entire frame of data into the pixel buffer
=
= Takes:
= * frame, a reference to a two-dimensional frame array:
= * Dimension 0: position (0..NumLEDs)
= * Dimension 1: array of values ([int red, int green, int blue)])
=
= Returns: this, the current FastFader object
==========================================
*/
FastFader& FastFader::set_frame(int (*frame)[3]){
for (int i = 0; i < NumLEDs; i++) {
for (int channel = 0; channel < 3; channel++) {
set_pixel(i, frame[i][channel], channel);
}
}
return *this;
}
/*
==========================================
= FastFader::push
= Pushes the present pixelbuffer to the LED array
=
= Takes:
= * fader_delay (optional, default 100), an integer value of how many miliseconds to take when fading in this frame
= * fader_steps (optional, default 10), an integer value of how many steps to take when fading
= * decay_type (optional), determining which weighting decay function to map over the fade:
= * NO_DECAY is the fastest, and will not actually fade the pixels in. It's good for slow boards.
= * LINEAR_DECAY will decay the cross-fade linearly
= * LOGARITHMIC_DECAY (default) will decay the cross-fade logarithmically. It's more expensive, but prettier.
=
= Returns: this, the current FastFader object
==========================================
*/
FastFader& FastFader::push(int fader_delay, int fader_steps, int decay_type){
// Clone the present LED interface into a fader array
int fader_cache[NumLEDs][3];
for (int i = 0; i < NumLEDs; i++) {
for (int channel = 0; channel < 3; channel++) {
fader_cache[i][channel] = LEDs[i][channel];
}
}
// Fade from the previous frame to the next one
int step_delay = int(fader_delay / fader_steps);
double log_base = log(fader_steps);
for (int current_step = 1; current_step <= fader_steps; current_step++) {
double previous_weight;
double current_weight;
double fader_weight;
if (decay_type < -1 || decay_type > 1) {
decay_type = LOGARITHMIC_DECAY;
}
if (decay_type == NO_DECAY) {
previous_weight = 0;
current_weight = 1;
fader_weight = 1;
}
if (decay_type == LINEAR_DECAY) {
// Calculate linear frame weighs for this step
previous_weight = 100 * (fader_steps - current_step);
current_weight = 100 * (current_step - 1);
fader_weight = 100 * (fader_steps - 1);
}
if (decay_type == LOGARITHMIC_DECAY) {
// Logarithmically weight the fade-in, since human perception of luminosity
// is logarithmic, not linear. This is log_n(current_step) where n = fader_steps
current_weight = 100 - (100 * (log(fader_steps - current_step + 1) / log_base));
previous_weight = 100 - (100 * (log(current_step) / log_base));
fader_weight = current_weight + previous_weight;
}
// Send this fader step to the LED interface
for (int i = 0; i < NumLEDs; i++) {
LEDs[i] = CRGB(
int(((PixelBuffer[i][0] * current_weight) + (fader_cache[i][0] * previous_weight)) / fader_weight),
int(((PixelBuffer[i][1] * current_weight) + (fader_cache[i][1] * previous_weight)) / fader_weight),
int(((PixelBuffer[i][2] * current_weight) + (fader_cache[i][2] * previous_weight)) / fader_weight)
);
}
FLED.show();
// Pause for a fraction of a moment
delay(step_delay);
}
return *this;
}
FastFader& FastFader::push(int fader_delay, int fader_steps) {
return push(fader_delay, fader_steps, LOGARITHMIC_DECAY);
}
FastFader& FastFader::push(int fader_delay) {
return push(fader_delay, 10, LOGARITHMIC_DECAY);
}
FastFader& FastFader::push() {
return push(100, 10, LOGARITHMIC_DECAY);
}