Skip to content

Commit

Permalink
Add Spectrogram example
Browse files Browse the repository at this point in the history
  • Loading branch information
stnkl committed Jun 28, 2022
1 parent 5403754 commit 69455c0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/Spectrogram/Animations/Spectrogram.h
Original file line number Diff line number Diff line change
@@ -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 <FastLEDHub.h>

#include <math.h>

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();
}
};
22 changes: 22 additions & 0 deletions examples/Spectrogram/Spectrogram.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <FastLEDHub.h>

#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<LED_TYPE, LIGHTSTRIP_PIN, GRB>(leds, NUM_LEDS);

FastLEDHub.registerAnimation(new Spectrogram("Spectrogram"));
}

void loop()
{
FastLEDHub.handle();
}

0 comments on commit 69455c0

Please sign in to comment.