Skip to content

Commit 72dd681

Browse files
committed
Rename LEDMatrix to LEDOutput
1 parent 822c3eb commit 72dd681

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

examples/tests/fft/fft-ledmatrix/fft-ledmatrix.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "AudioTools.h"
22
#include "AudioLibs/AudioKit.h"
33
#include "AudioLibs/AudioRealFFT.h" // or AudioKissFFT
4-
#include "AudioLibs/LEDMatrix.h"
4+
#include "AudioLibs/LEDOutput.h"
55

66
#define PIN_LEDS 22
77
#define LED_X 32
@@ -10,7 +10,7 @@
1010
AudioKitStream kit; // Audio source
1111
AudioRealFFT fft; // or AudioKissFFT
1212
StreamCopy copier(fft, kit); // copy mic to fft
13-
LEDMatrix led(fft); // output to LED matrix
13+
LEDOutput led(fft); // output to LED matrix
1414

1515
void setup() {
1616
Serial.begin(115200);
@@ -33,7 +33,7 @@ void setup() {
3333
lcfg.y = LED_Y;
3434
led.begin(lcfg);
3535

36-
// addLeds
36+
// add LEDs
3737
FastLED.addLeds<WS2812B, PIN_LEDS, GRB>(led.ledData(), led.ledCount());
3838
}
3939

src/AudioLibs/LEDMatrix.h renamed to src/AudioLibs/LEDOutput.h

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,49 @@
33
#include "AudioLibs/AudioFFT.h"
44

55
namespace audio_tools {
6+
class LEDOutput;
7+
struct LEDOutputConfig;
68

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);
812

913
/**
1014
* LED Matrix Configuration. Provide the number of leds in x and y direction and
1115
* the data pin.
1216
*/
13-
struct LEDMatrixConfig {
17+
struct LEDOutputConfig {
18+
/// Number of leds in x direction
1419
int x = 0;
20+
/// Number of leds in y direction
1521
int y = 0;
22+
/// Default color
1623
int color = CRGB::Blue;
24+
/// optinal custom logic to select color
1725
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
1829
int update_frequency = 1; // update every call
1930
};
2031

2132
/**
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.
2334
* This displays the result of the FFT to a LED matrix.
2435
*/
25-
class LEDMatrix {
36+
class LEDOutput {
2637
public:
27-
LEDMatrixConfig defaultConfig() { return cfg; }
38+
LEDOutputConfig defaultConfig() { return cfg; }
2839

29-
LEDMatrix(AudioFFTBase &fft) {
30-
selfLEDMatrix = this;
40+
LEDOutput(AudioFFTBase &fft) {
41+
selfLEDOutput = this;
3142
p_fft = &fft;
3243
AudioFFTConfig &fft_cfg = p_fft->config();
3344
fft_cfg.callback = fftCallback;
3445
}
3546

3647
/// Setup Led matrix
37-
bool begin(LEDMatrixConfig config) {
48+
bool begin(LEDOutputConfig config) {
3849
cfg = config;
3950
if (!*p_fft) {
4051
LOGE("fft not started");
@@ -51,6 +62,8 @@ class LEDMatrix {
5162

5263
// number of bins
5364
magnitudes.resize(p_fft->size());
65+
66+
return true;
5467
}
5568

5669
// Provides the number of LEDs: call begin() first!
@@ -71,42 +84,20 @@ class LEDMatrix {
7184
}
7285

7386
/// Updates the display: call this method in your loop
74-
void update() {
87+
virtual void update() {
7588
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);
9091
}
9192
}
9293

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
10495
CRGB &xyLed(uint8_t x, uint8_t y) {
10596
int index = (y * cfg.x) + x;
10697
return leds[index];
10798
}
10899

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
110101
/// need to combine values from the magnitudes array if this is much bigger.
111102
float getMagnitude(int x) {
112103
float total = 0;
@@ -119,23 +110,50 @@ class LEDMatrix {
119110
total += magnitudes[x];
120111
}
121112
// 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;
125115
}
126116
return total;
127117
}
128118

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+
129130
/// callback method which provides updated data from fft
130131
static void fftCallback(AudioFFTBase &fft) {
131132
// just save magnitudes to be displayed
132-
LEDMatrix* self = static_cast<LEDMatrix*>(selfLEDMatrix);
133-
134133
for (int j = 0; j < fft.size(); j++) {
135134
float value = fft.magnitude(j);
136-
self->magnitudes[j] = value;
135+
selfLEDOutput->magnitudes[j] = value;
137136
}
138137
};
139138
};
140139

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+
141159
} // namespace audio_tools

0 commit comments

Comments
 (0)