add app framework

add a framework to allow for multiple applications and switching between
them

Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
Thomas 2022-06-19 14:07:40 +02:00
parent 6b60c82e3e
commit 5140512b56
7 changed files with 145 additions and 26 deletions

View File

@ -1,10 +1,25 @@
#ifndef ZAUBERSTAB_APP_H
#define ZAUBERSTAB_APP_H
#pragma once
struct App {
void (*loop)(void);
void (*setup)(void);
void virtual init() = 0;
void virtual deinit() = 0;
void virtual loop() = 0;
};
struct App
#endif
struct BeatDetectApp : public App {
void init();
void deinit();
void loop();
};
struct VuMeterApp: public App {
void init();
void deinit();
void loop();
};
struct FFTTestApp: public App {
void init();
void deinit();
void loop();
};

View File

@ -1,5 +1,6 @@
#pragma once
#include "Arduino.h"
#include <FastLED.h>
#define LED_PIN 12

View File

@ -1,5 +1,6 @@
#include <algorithm>
#include "app.h"
#include "biquad.h"
#include "pt1.h"
#include "zauberstab.h"
@ -80,15 +81,18 @@ set_filter()
}
}
void setup()
void BeatDetectApp::init()
{
zauberstab_init();
Serial.begin(250000);
set_filter();
initial_time = micros();
}
void loop()
void BeatDetectApp::deinit()
{
}
void BeatDetectApp::loop()
{
float sample = get_sample();
energy += std::abs(sample) * std::abs(sample);

View File

@ -0,0 +1,70 @@
#include "app.h"
#include "fft.h"
#include "zauberstab.h"
#define N_SAMPLES 256
std::complex<float> samples[N_SAMPLES];
std::complex<float> z[N_SAMPLES];
double vReal[N_SAMPLES];
double vImag[N_SAMPLES];
uint32_t sample_counter = 0;
unsigned long max_dt = 0;
unsigned long last_sample = 0;
void FFTTestApp::init()
{
}
void FFTTestApp::deinit()
{
}
void FFTTestApp::loop()
{
if (micros() - last_sample >= 200)
{
unsigned long dt = micros() - last_sample;
last_sample = micros();
if (dt > max_dt)
{
max_dt = dt;
}
samples[sample_counter++] = get_sample();
if (sample_counter == N_SAMPLES)
{
unsigned long start = micros();
fft(samples, z, N_SAMPLES);
unsigned long end = micros();
float max = 0.f;
for (int i = 0; i < N_SAMPLES / 2; i++)
{
float v = std::log10(std::abs(z[i]));
if (v > max)
{
max = v;
}
}
for (int i = 0; i < N_SAMPLES / 2; i++)
{
float v = std::log10(std::abs(z[i]));
if (v < 2.f)
{
v = 0.f;
}
uint8_t led_value = v * 20;
leds[i].setRGB(led_value, led_value, led_value);
}
sample_counter = 0;
start = micros();
FastLED.show();
end = micros();
}
}
}

View File

@ -1,3 +1,4 @@
#include "app.h"
#include "pt1.h"
#include "zauberstab.h"
@ -9,17 +10,19 @@ float vu_filt = 0.0f;
float vu_filt_slow = 0.0f;
float dt;
Pt1<float> vu_pt1_fast{1.f, 1.f};
Pt1<float> vu_pt1_slow{1.f, 0.05f};
Pt1<float> vu_pt1_fast{1.f, 0.05f};
Pt1<float> vu_pt1_slow{1.f, 1.f};
void setup()
void VuMeterApp::init()
{
zauberstab_init();
Serial.begin(115200);
FastLED.setBrightness(100);
}
void loop()
void VuMeterApp::deinit()
{
}
void VuMeterApp::loop()
{
if (micros() - last_sample_time >= 500)
{

36
firmware/src/main.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "zauberstab.h"
#include <vector>
#include "app.h"
struct BeatDetectApp beat_detect_app{};
struct VuMeterApp vu_meter_app {};
struct FFTTestApp fft_test_app {};
std::vector<std::reference_wrapper<App>> apps = {
std::ref<App>(beat_detect_app),
std::ref<App>(vu_meter_app),
std::ref<App>(fft_test_app),
};
unsigned int current_app = 0;
unsigned int next_app;
void setup()
{
next_app = current_app;
zauberstab_init();
Serial.begin(115200);
apps[current_app].get().init();
}
void loop()
{
if (next_app != current_app) {
apps[current_app].get().deinit();
apps[next_app].get().init();
current_app = next_app;
}
apps[current_app].get().loop();
}

View File

@ -1,10 +0,0 @@
#include "zauberstab.h"
void setup()
{
zauberstab_init();
}
void loop()
{
}