Skip to content

Commit 69455c0

Browse files
committed
Add Spectrogram example
1 parent 5403754 commit 69455c0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
// This example requires the use of https://github.com/stnkl/FastLEDHub_AudioViz to send
4+
// spectrum data to the ESP32/ESP8266 running FastLEDHub.
5+
6+
#include <FastLEDHub.h>
7+
8+
#include <math.h>
9+
10+
extern CRGB leds[];
11+
12+
class Spectrogram : public Animation
13+
{
14+
public:
15+
using Animation::Animation;
16+
17+
uint8_t interpolateSpectrum(uint16_t led)
18+
{
19+
float stepSize = (float)(SPECTRUM_LENGTH - 1) / ((float)FastLEDHub.size() - 1);
20+
float spectrumPosition = led * stepSize;
21+
int16_t spectrumFloorIndex = (int16_t)spectrumPosition % SPECTRUM_LENGTH;
22+
int16_t spectrumFloor = FastLEDHub.spectrumData[spectrumFloorIndex];
23+
int16_t spectrumCeil = FastLEDHub.spectrumData[(spectrumFloorIndex + 1) % SPECTRUM_LENGTH];
24+
uint8_t intensity = spectrumFloor + fmod(spectrumPosition, 1) * (spectrumCeil - spectrumFloor);
25+
return intensity;
26+
}
27+
28+
void reset()
29+
{
30+
}
31+
32+
void loop()
33+
{
34+
for (uint16_t i = 0; i < FastLEDHub.size(); i++)
35+
{
36+
CRGB color = CRGB::White;
37+
leds[i] = color.nscale8(interpolateSpectrum(i));
38+
}
39+
40+
FastLEDHub.show();
41+
}
42+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <FastLEDHub.h>
2+
3+
#include "Animations/Spectrogram.h"
4+
5+
#define NUM_LEDS 100
6+
#define LIGHTSTRIP_PIN 5
7+
#define LED_TYPE WS2812B
8+
9+
CRGB leds[NUM_LEDS];
10+
11+
void setup()
12+
{
13+
FastLEDHub.initialize("Spectrogram Example");
14+
FastLEDHub.addLeds<LED_TYPE, LIGHTSTRIP_PIN, GRB>(leds, NUM_LEDS);
15+
16+
FastLEDHub.registerAnimation(new Spectrogram("Spectrogram"));
17+
}
18+
19+
void loop()
20+
{
21+
FastLEDHub.handle();
22+
}

0 commit comments

Comments
 (0)