From 5140512b564051b438070ff43baaab7fcdc21d32 Mon Sep 17 00:00:00 2001 From: Thomas Schmid Date: Sun, 19 Jun 2022 14:07:40 +0200 Subject: [PATCH] add app framework add a framework to allow for multiple applications and switching between them Signed-off-by: Thomas Schmid --- firmware/include/app.h | 27 +++++-- firmware/include/zauberstab.h | 1 + .../src/{ => applications}/beat_detect.cpp | 12 ++-- firmware/src/applications/fft_test.cpp | 70 +++++++++++++++++++ .../{temp => src/applications}/vumeter.cpp | 15 ++-- firmware/src/main.cpp | 36 ++++++++++ firmware/temp/main.cpp | 10 --- 7 files changed, 145 insertions(+), 26 deletions(-) rename firmware/src/{ => applications}/beat_detect.cpp (97%) create mode 100644 firmware/src/applications/fft_test.cpp rename firmware/{temp => src/applications}/vumeter.cpp (88%) create mode 100644 firmware/src/main.cpp delete mode 100644 firmware/temp/main.cpp diff --git a/firmware/include/app.h b/firmware/include/app.h index d8b9b58..cf2353a 100644 --- a/firmware/include/app.h +++ b/firmware/include/app.h @@ -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 \ No newline at end of file +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(); +}; \ No newline at end of file diff --git a/firmware/include/zauberstab.h b/firmware/include/zauberstab.h index 7c85f25..57a875b 100644 --- a/firmware/include/zauberstab.h +++ b/firmware/include/zauberstab.h @@ -1,5 +1,6 @@ #pragma once +#include "Arduino.h" #include #define LED_PIN 12 diff --git a/firmware/src/beat_detect.cpp b/firmware/src/applications/beat_detect.cpp similarity index 97% rename from firmware/src/beat_detect.cpp rename to firmware/src/applications/beat_detect.cpp index 76de9bd..4587887 100644 --- a/firmware/src/beat_detect.cpp +++ b/firmware/src/applications/beat_detect.cpp @@ -1,5 +1,6 @@ #include +#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); diff --git a/firmware/src/applications/fft_test.cpp b/firmware/src/applications/fft_test.cpp new file mode 100644 index 0000000..1d9956d --- /dev/null +++ b/firmware/src/applications/fft_test.cpp @@ -0,0 +1,70 @@ +#include "app.h" +#include "fft.h" +#include "zauberstab.h" + +#define N_SAMPLES 256 + +std::complex samples[N_SAMPLES]; +std::complex 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(); + } + } +} \ No newline at end of file diff --git a/firmware/temp/vumeter.cpp b/firmware/src/applications/vumeter.cpp similarity index 88% rename from firmware/temp/vumeter.cpp rename to firmware/src/applications/vumeter.cpp index 6c95c0d..03533e5 100644 --- a/firmware/temp/vumeter.cpp +++ b/firmware/src/applications/vumeter.cpp @@ -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 vu_pt1_fast{1.f, 1.f}; -Pt1 vu_pt1_slow{1.f, 0.05f}; +Pt1 vu_pt1_fast{1.f, 0.05f}; +Pt1 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) { diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp new file mode 100644 index 0000000..1620cc6 --- /dev/null +++ b/firmware/src/main.cpp @@ -0,0 +1,36 @@ +#include "zauberstab.h" +#include +#include "app.h" + +struct BeatDetectApp beat_detect_app{}; +struct VuMeterApp vu_meter_app {}; +struct FFTTestApp fft_test_app {}; + +std::vector> apps = { + std::ref(beat_detect_app), + std::ref(vu_meter_app), + std::ref(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(); +} \ No newline at end of file diff --git a/firmware/temp/main.cpp b/firmware/temp/main.cpp deleted file mode 100644 index 4ea6ade..0000000 --- a/firmware/temp/main.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "zauberstab.h" - -void setup() -{ - zauberstab_init(); -} - -void loop() -{ -} \ No newline at end of file