3
3
#include " AudioLibs/AudioFFT.h"
4
4
5
5
namespace audio_tools {
6
+ class LEDOutput ;
7
+ struct LEDOutputConfig ;
6
8
7
- void *selfLEDMatrix=nullptr ;
9
+ LEDOutput *selfLEDOutput=nullptr ;
10
+ // default callback function which implements led update
11
+ void updateLEDOutput (LEDOutputConfig*cfg, LEDOutput *matrix, int max_y);
8
12
9
13
/* *
10
14
* LED Matrix Configuration. Provide the number of leds in x and y direction and
11
15
* the data pin.
12
16
*/
13
- struct LEDMatrixConfig {
17
+ struct LEDOutputConfig {
18
+ // / Number of leds in x direction
14
19
int x = 0 ;
20
+ // / Number of leds in y direction
15
21
int y = 0 ;
22
+ // / Default color
16
23
int color = CRGB::Blue;
24
+ // / optinal custom logic to select color
17
25
int (*get_color)(int x, int y, int magnitude) = nullptr ;
26
+ // / Custom callback logic to update the LEDs - by default we use updateLEDOutput()
27
+ void (*update)(LEDOutputConfig*cfg, LEDOutput *matrix, int max_y) = updateLEDOutput;
28
+ // / Update the leds only ever nth call
18
29
int update_frequency = 1 ; // update every call
19
30
};
20
31
21
32
/* *
22
- * LEDMatrix using the FastLED library. You write the data to the FFT Stream.
33
+ * LEDOutput using the FastLED library. You write the data to the FFT Stream.
23
34
* This displays the result of the FFT to a LED matrix.
24
35
*/
25
- class LEDMatrix {
36
+ class LEDOutput {
26
37
public:
27
- LEDMatrixConfig defaultConfig () { return cfg; }
38
+ LEDOutputConfig defaultConfig () { return cfg; }
28
39
29
- LEDMatrix (AudioFFTBase &fft) {
30
- selfLEDMatrix = this ;
40
+ LEDOutput (AudioFFTBase &fft) {
41
+ selfLEDOutput = this ;
31
42
p_fft = &fft;
32
43
AudioFFTConfig &fft_cfg = p_fft->config ();
33
44
fft_cfg.callback = fftCallback;
34
45
}
35
46
36
47
// / Setup Led matrix
37
- bool begin (LEDMatrixConfig config) {
48
+ bool begin (LEDOutputConfig config) {
38
49
cfg = config;
39
50
if (!*p_fft) {
40
51
LOGE (" fft not started" );
@@ -51,6 +62,8 @@ class LEDMatrix {
51
62
52
63
// number of bins
53
64
magnitudes.resize (p_fft->size ());
65
+
66
+ return true ;
54
67
}
55
68
56
69
// Provides the number of LEDs: call begin() first!
@@ -71,42 +84,20 @@ class LEDMatrix {
71
84
}
72
85
73
86
// / Updates the display: call this method in your loop
74
- void update () {
87
+ virtual void update () {
75
88
if (count++ % cfg.update_frequency == 0 ) {
76
- for (int x = 0 ; x < cfg.x ; x++) {
77
- // max y determined by magnitude
78
- int maxY = mapFloat (getMagnitude (x), 0 .0f , max_y, 0 .0f ,
79
- static_cast <float >(cfg.y ));
80
- // update horizontal bar
81
- for (int y = 0 ; y < maxY; y++) {
82
- int color = cfg.color ;
83
- if (cfg.get_color != nullptr ) {
84
- color = cfg.get_color (x, y, maxY);
85
- }
86
- xyLed (x, y) = color;
87
- }
88
- }
89
- FastLED.show ();
89
+ // use custom update logic defined in config
90
+ cfg.update (&cfg, this , max_y);
90
91
}
91
92
}
92
93
93
- protected:
94
- friend class AudioFFTBase ;
95
- Vector<CRGB> leds{0 };
96
- Vector<float > magnitudes{0 };
97
- LEDMatrixConfig cfg;
98
- int magnitude_div = 1 ;
99
- AudioFFTBase *p_fft = nullptr ;
100
- float max_y = 1000 .0f ;
101
- uint64_t count = 0 ;
102
-
103
- // determine index of the led with the help of the x and y pos
94
+ // / Determine the led with the help of the x and y pos
104
95
CRGB &xyLed (uint8_t x, uint8_t y) {
105
96
int index = (y * cfg.x ) + x;
106
97
return leds[index];
107
98
}
108
99
109
- // / @brief returns the magnitude for the indicated led x position. We might
100
+ // / Returns the magnitude for the indicated led x position. We might
110
101
// / need to combine values from the magnitudes array if this is much bigger.
111
102
float getMagnitude (int x) {
112
103
float total = 0 ;
@@ -119,23 +110,50 @@ class LEDMatrix {
119
110
total += magnitudes[x];
120
111
}
121
112
// determine max y to scale output
122
- LEDMatrix* self = static_cast <LEDMatrix*>(selfLEDMatrix);
123
- if (self->max_y > total) {
124
- self->max_y = total;
113
+ if (selfLEDOutput->max_y > total) {
114
+ selfLEDOutput->max_y = total;
125
115
}
126
116
return total;
127
117
}
128
118
119
+ protected:
120
+ friend class AudioFFTBase ;
121
+ Vector<CRGB> leds{0 };
122
+ Vector<float > magnitudes{0 };
123
+ LEDOutputConfig cfg;
124
+ int magnitude_div = 1 ;
125
+ AudioFFTBase *p_fft = nullptr ;
126
+ float max_y = 1000 .0f ;
127
+ uint64_t count = 0 ;
128
+
129
+
129
130
// / callback method which provides updated data from fft
130
131
static void fftCallback (AudioFFTBase &fft) {
131
132
// just save magnitudes to be displayed
132
- LEDMatrix* self = static_cast <LEDMatrix*>(selfLEDMatrix);
133
-
134
133
for (int j = 0 ; j < fft.size (); j++) {
135
134
float value = fft.magnitude (j);
136
- self ->magnitudes [j] = value;
135
+ selfLEDOutput ->magnitudes [j] = value;
137
136
}
138
137
};
139
138
};
140
139
140
+
141
+ void updateLEDOutput (LEDOutputConfig*cfg, LEDOutput *matrix, int max_y){
142
+ for (int x = 0 ; x < cfg->x ; x++) {
143
+ // max y determined by magnitude
144
+ int maxY = mapFloat (matrix->getMagnitude (x), 0 .0f , max_y, 0 .0f ,
145
+ static_cast <float >(cfg->y ));
146
+ // update horizontal bar
147
+ for (int y = 0 ; y < maxY; y++) {
148
+ int color = cfg->color ;
149
+ if (cfg->get_color != nullptr ) {
150
+ color = cfg->get_color (x, y, maxY);
151
+ }
152
+ matrix->xyLed (x, y) = color;
153
+ }
154
+ }
155
+ FastLED.show ();
156
+ }
157
+
158
+
141
159
} // namespace audio_tools
0 commit comments