diff --git a/examples/Spectrogram/Animations/Spectrogram.h b/examples/Spectrogram/Animations/Spectrogram.h new file mode 100644 index 0000000..d07a45b --- /dev/null +++ b/examples/Spectrogram/Animations/Spectrogram.h @@ -0,0 +1,42 @@ +#pragma once + +// This example requires the use of https://github.com/stnkl/FastLEDHub_AudioViz to send +// spectrum data to the ESP32/ESP8266 running FastLEDHub. + +#include + +#include + +extern CRGB leds[]; + +class Spectrogram : public Animation +{ +public: + using Animation::Animation; + + uint8_t interpolateSpectrum(uint16_t led) + { + float stepSize = (float)(SPECTRUM_LENGTH - 1) / ((float)FastLEDHub.size() - 1); + float spectrumPosition = led * stepSize; + int16_t spectrumFloorIndex = (int16_t)spectrumPosition % SPECTRUM_LENGTH; + int16_t spectrumFloor = FastLEDHub.spectrumData[spectrumFloorIndex]; + int16_t spectrumCeil = FastLEDHub.spectrumData[(spectrumFloorIndex + 1) % SPECTRUM_LENGTH]; + uint8_t intensity = spectrumFloor + fmod(spectrumPosition, 1) * (spectrumCeil - spectrumFloor); + return intensity; + } + + void reset() + { + } + + void loop() + { + for (uint16_t i = 0; i < FastLEDHub.size(); i++) + { + CRGB color = CRGB::White; + leds[i] = color.nscale8(interpolateSpectrum(i)); + } + + FastLEDHub.show(); + } +}; diff --git a/examples/Spectrogram/Spectrogram.ino b/examples/Spectrogram/Spectrogram.ino new file mode 100644 index 0000000..d0c19ec --- /dev/null +++ b/examples/Spectrogram/Spectrogram.ino @@ -0,0 +1,22 @@ +#include + +#include "Animations/Spectrogram.h" + +#define NUM_LEDS 100 +#define LIGHTSTRIP_PIN 5 +#define LED_TYPE WS2812B + +CRGB leds[NUM_LEDS]; + +void setup() +{ + FastLEDHub.initialize("Spectrogram Example"); + FastLEDHub.addLeds(leds, NUM_LEDS); + + FastLEDHub.registerAnimation(new Spectrogram("Spectrogram")); +} + +void loop() +{ + FastLEDHub.handle(); +}