2022-06-17 03:31:17 +02:00
|
|
|
#include "zauberstab.h"
|
2022-06-18 11:33:56 +02:00
|
|
|
#include "pt1.h"
|
2022-06-17 03:31:17 +02:00
|
|
|
|
|
|
|
unsigned long last_sample_time;
|
|
|
|
static int sample_counter = 0;
|
|
|
|
unsigned int top_led_pos = 0;
|
|
|
|
float rms_avg = 0;
|
|
|
|
float vu_filt = 0.0f;
|
|
|
|
float vu_filt_slow = 0.0f;
|
2022-06-18 11:33:56 +02:00
|
|
|
float dt;
|
|
|
|
|
|
|
|
struct pt1_state vu_pt1_fast;
|
|
|
|
struct pt1_state vu_pt1_slow;
|
2022-06-17 03:31:17 +02:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
zauberstab_init();
|
|
|
|
Serial.begin(115200);
|
|
|
|
FastLED.setBrightness(100);
|
2022-06-18 11:33:56 +02:00
|
|
|
|
|
|
|
pt1_init(&vu_pt1_slow, 1, 1.f);
|
|
|
|
pt1_init(&vu_pt1_fast, 1, 0.05);
|
2022-06-17 03:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
if (micros()-last_sample_time >= 500){
|
|
|
|
last_sample_time = micros();
|
|
|
|
int32_t sample = get_sample();
|
|
|
|
float in = sample*sample;
|
|
|
|
sample_counter++;
|
|
|
|
rms_avg += (in - rms_avg)/(sample_counter + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
EVERY_N_MILLIS(10){
|
|
|
|
|
|
|
|
float vu = 20 * log10f(rms_avg);
|
2022-06-18 11:33:56 +02:00
|
|
|
vu_filt = pt1_update(&vu_pt1_fast, vu, 0.01f);
|
|
|
|
vu_filt_slow = pt1_update(&vu_pt1_slow, vu_filt, 0.01f);
|
2022-06-17 03:31:17 +02:00
|
|
|
//Serial.println(vu);
|
|
|
|
int max_led = vu_filt;
|
|
|
|
int top_led = vu_filt_slow;
|
|
|
|
|
|
|
|
max_led = max_led > 0xFF ? 0xFF : max_led;
|
|
|
|
|
|
|
|
if (top_led < max_led){
|
2022-06-18 11:33:56 +02:00
|
|
|
vu_pt1_slow.y_n1 = vu_filt;
|
2022-06-17 03:31:17 +02:00
|
|
|
top_led = max_led;
|
|
|
|
}
|
|
|
|
|
|
|
|
fill_solid(leds, NUM_LEDS, CRGB::Black);
|
|
|
|
for(int i = 0; i < max_led; i++) {
|
|
|
|
int idx = map(i, 0, NUM_LEDS, 0, 0xFF);
|
|
|
|
leds[i] = ColorFromPalette(RainbowColors_p, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
leds[top_led] = CRGB::White;
|
|
|
|
|
|
|
|
FastLED.show();
|
|
|
|
rms_avg = 0.0f;
|
|
|
|
sample_counter = 0;
|
|
|
|
}
|
|
|
|
}
|