From b4fe9dc546a1d37568baed442176785461da63f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 1 Jun 2016 15:00:26 +0200 Subject: [PATCH] switching cleanup --- project/main.c | 34 +++--------------- project/mode_audio.c | 84 ++++++++++++++++++++++++++++++++------------ project/mode_audio.h | 6 ++-- project/scrolltext.c | 2 +- 4 files changed, 72 insertions(+), 54 deletions(-) diff --git a/project/main.c b/project/main.c index 0ed2cd9..7c00c77 100644 --- a/project/main.c +++ b/project/main.c @@ -30,12 +30,9 @@ typedef enum { static void poll_subsystems(void); static void gamepad_rx(ComIface *iface); -static void boot_animation(void); static void switch_mode(void *unused); // circle b/w modes static void activate_mode(void); // activate currently selected mode -static task_pid_t capture_task_id; - static GameMode app_mode; @@ -58,26 +55,19 @@ static void activate_mode(void) if (app_mode == MODE_AUDIO) { info("MODE: Audio"); - scrolltext("Audio FFT", SCROLL_STEP); - audio_mode_active = true; - enable_periodic_task(capture_task_id, true); + mode_audio_start(); } else { - audio_mode_active = false; - enable_periodic_task(capture_task_id, false); + mode_audio_stop(); } // --- Game Of Life --- if (app_mode == MODE_LIFE) { info("MODE: Life"); - scrolltext("Game of Life", SCROLL_STEP); - dmtx_clear(dmtx); - dmtx_set(dmtx, 5, 5, 1); - dmtx_show(dmtx); // } else { // @@ -90,9 +80,6 @@ static void activate_mode(void) scrolltext("Snake", SCROLL_STEP); - dmtx_clear(dmtx); - dmtx_set(dmtx, 13, 13, 1); - dmtx_show(dmtx); // } else { // @@ -107,13 +94,12 @@ int main(void) banner("*** FFT dot matrix display ***"); banner_info("(c) Ondrej Hruska, 2016"); - scrolltext("STM32 F103", SCROLL_STEP); - - boot_animation(); + scrolltext("STM32 LED MATRIX DEMO", SCROLL_STEP); gamepad_iface->rx_callback = gamepad_rx; - capture_task_id = add_periodic_task(capture_audio, NULL, 10, false); + mode_audio_init(); + mode_audio_start(); ms_time_t last; while (1) { @@ -148,16 +134,6 @@ static void poll_subsystems(void) } -static void boot_animation(void) -{ - // Boot animation (for FFT) - for(int i = 0; i < 16; i++) { - dmtx_set(dmtx, i, 0, 1); - dmtx_show(dmtx); - delay_ms(25); - } -} - static void gamepad_rx(ComIface *iface) { diff --git a/project/mode_audio.c b/project/mode_audio.c index 94b11a8..332ffa4 100644 --- a/project/mode_audio.c +++ b/project/mode_audio.c @@ -1,15 +1,14 @@ #include "mode_audio.h" #include #include "bus/event_queue.h" +#include "utils/timebase.h" #include "dotmatrix.h" -bool audio_mode_active = true; +static bool audio_mode_active = true; static volatile bool capture_pending = false; -static volatile bool print_next_fft = false; - -static float virt_zero_value = 2045.0f; +//static volatile bool print_next_fft = false; #define SAMP_BUF_LEN 256 @@ -22,10 +21,51 @@ union samp_buf_union { // sample buffers (static - invalidated when sampling starts anew). static union samp_buf_union samp_buf; +static task_pid_t capture_task_id; + + + // prototypes static void audio_capture_done(void* unused); +static void boot_animation(void) +{ + dmtx_clear(dmtx); + + // Boot animation (for FFT) + for(int i = 0; i < 16; i++) { + dmtx_set(dmtx, i, 0, 1); + dmtx_show(dmtx); + delay_ms(20); + } +} + + +/** Init audio mode */ +void mode_audio_init(void) +{ + capture_task_id = add_periodic_task(capture_audio, NULL, 10, false); + enable_periodic_task(capture_task_id, false); +} + +/** Start audio mode */ +void mode_audio_start(void) +{ + boot_animation(); + + audio_mode_active = true; + enable_periodic_task(capture_task_id, true); +} + +/** Stop audio mode */ +void mode_audio_stop(void) +{ + audio_mode_active = false; + enable_periodic_task(capture_task_id, false); +} + +/** Start DMA capture */ static void start_adc_dma(uint32_t *memory, uint32_t count) { ADC_Cmd(ADC1, DISABLE); @@ -52,6 +92,7 @@ static void start_adc_dma(uint32_t *memory, uint32_t count) } +/** IRQ */ void DMA1_Channel1_IRQHandler(void) { DMA_ClearITPendingBit(DMA1_IT_TC1); @@ -70,6 +111,7 @@ void DMA1_Channel1_IRQHandler(void) } +/** Capture done callback */ static void audio_capture_done(void* unused) { (void)unused; @@ -87,20 +129,18 @@ static void audio_capture_done(void* unused) // normalize float mean; arm_mean_f32(samp_buf.floats, samp_count, &mean); - virt_zero_value = mean; for (int i = 0; i < samp_count; i++) { - samp_buf.floats[i] -= virt_zero_value; + samp_buf.floats[i] -= mean; } - - if (print_next_fft) { - printf("--- Raw (adjusted) ---\n"); - for(int i = 0; i < samp_count; i++) { - printf("%.2f, ", samp_buf.floats[i]); - } - printf("\n"); - } +// if (print_next_fft) { +// printf("--- Raw (adjusted) ---\n"); +// for(int i = 0; i < samp_count; i++) { +// printf("%.2f, ", samp_buf.floats[i]); +// } +// printf("\n"); +// } for (int i = samp_count - 1; i >= 0; i--) { bins[i * 2 + 1] = 0; // imaginary @@ -113,13 +153,13 @@ static void audio_capture_done(void* unused) arm_cfft_f32(S, bins, 0, true); // bit reversed FFT arm_cmplx_mag_f32(bins, bins, bin_count); // get magnitude (extract real values) - if (print_next_fft) { - printf("--- Bins ---\n"); - for(int i = 0; i < bin_count; i++) { - printf("%.2f, ", bins[i]); - } - printf("\n"); - } +// if (print_next_fft) { +// printf("--- Bins ---\n"); +// for(int i = 0; i < bin_count; i++) { +// printf("%.2f, ", bins[i]); +// } +// printf("\n"); +// } // normalize dmtx_clear(dmtx); @@ -141,7 +181,7 @@ static void audio_capture_done(void* unused) dmtx_show(dmtx); - print_next_fft = false; +// print_next_fft = false; capture_pending = false; } diff --git a/project/mode_audio.h b/project/mode_audio.h index 6c979a7..445d896 100644 --- a/project/mode_audio.h +++ b/project/mode_audio.h @@ -3,8 +3,10 @@ #include "main.h" -extern bool audio_mode_active; - void capture_audio(void *unused); +void mode_audio_init(void); +void mode_audio_start(void); +void mode_audio_stop(void); + #endif // MODE_AUDIO_H diff --git a/project/scrolltext.c b/project/scrolltext.c index 103f757..df588dd 100644 --- a/project/scrolltext.c +++ b/project/scrolltext.c @@ -17,7 +17,7 @@ static void printtext(const char *text, int x, int y) ch -= ' '; // normalize for font table if (ch == 0) { // space - totalX += 3; + totalX += 4; continue; }