switching cleanup

multi-mode
Ondřej Hruška 9 years ago
parent 0f07ce0a4a
commit b4fe9dc546
  1. 34
      project/main.c
  2. 84
      project/mode_audio.c
  3. 6
      project/mode_audio.h
  4. 2
      project/scrolltext.c

@ -30,12 +30,9 @@ typedef enum {
static void poll_subsystems(void); static void poll_subsystems(void);
static void gamepad_rx(ComIface *iface); static void gamepad_rx(ComIface *iface);
static void boot_animation(void);
static void switch_mode(void *unused); // circle b/w modes static void switch_mode(void *unused); // circle b/w modes
static void activate_mode(void); // activate currently selected mode static void activate_mode(void); // activate currently selected mode
static task_pid_t capture_task_id;
static GameMode app_mode; static GameMode app_mode;
@ -58,26 +55,19 @@ static void activate_mode(void)
if (app_mode == MODE_AUDIO) { if (app_mode == MODE_AUDIO) {
info("MODE: Audio"); info("MODE: Audio");
scrolltext("Audio FFT", SCROLL_STEP); scrolltext("Audio FFT", SCROLL_STEP);
audio_mode_active = true; mode_audio_start();
enable_periodic_task(capture_task_id, true);
} else { } else {
audio_mode_active = false; mode_audio_stop();
enable_periodic_task(capture_task_id, false);
} }
// --- Game Of Life --- // --- Game Of Life ---
if (app_mode == MODE_LIFE) { if (app_mode == MODE_LIFE) {
info("MODE: Life"); info("MODE: Life");
scrolltext("Game of Life", SCROLL_STEP); scrolltext("Game of Life", SCROLL_STEP);
dmtx_clear(dmtx);
dmtx_set(dmtx, 5, 5, 1);
dmtx_show(dmtx);
// //
} else { } else {
// //
@ -90,9 +80,6 @@ static void activate_mode(void)
scrolltext("Snake", SCROLL_STEP); scrolltext("Snake", SCROLL_STEP);
dmtx_clear(dmtx);
dmtx_set(dmtx, 13, 13, 1);
dmtx_show(dmtx);
// //
} else { } else {
// //
@ -107,13 +94,12 @@ int main(void)
banner("*** FFT dot matrix display ***"); banner("*** FFT dot matrix display ***");
banner_info("(c) Ondrej Hruska, 2016"); banner_info("(c) Ondrej Hruska, 2016");
scrolltext("STM32 F103", SCROLL_STEP); scrolltext("STM32 LED MATRIX DEMO", SCROLL_STEP);
boot_animation();
gamepad_iface->rx_callback = gamepad_rx; 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; ms_time_t last;
while (1) { 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) static void gamepad_rx(ComIface *iface)
{ {

@ -1,15 +1,14 @@
#include "mode_audio.h" #include "mode_audio.h"
#include <arm_math.h> #include <arm_math.h>
#include "bus/event_queue.h" #include "bus/event_queue.h"
#include "utils/timebase.h"
#include "dotmatrix.h" #include "dotmatrix.h"
bool audio_mode_active = true; static bool audio_mode_active = true;
static volatile bool capture_pending = false; static volatile bool capture_pending = false;
static volatile bool print_next_fft = false; //static volatile bool print_next_fft = false;
static float virt_zero_value = 2045.0f;
#define SAMP_BUF_LEN 256 #define SAMP_BUF_LEN 256
@ -22,10 +21,51 @@ union samp_buf_union {
// sample buffers (static - invalidated when sampling starts anew). // sample buffers (static - invalidated when sampling starts anew).
static union samp_buf_union samp_buf; static union samp_buf_union samp_buf;
static task_pid_t capture_task_id;
// prototypes // prototypes
static void audio_capture_done(void* unused); 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) static void start_adc_dma(uint32_t *memory, uint32_t count)
{ {
ADC_Cmd(ADC1, DISABLE); 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) void DMA1_Channel1_IRQHandler(void)
{ {
DMA_ClearITPendingBit(DMA1_IT_TC1); DMA_ClearITPendingBit(DMA1_IT_TC1);
@ -70,6 +111,7 @@ void DMA1_Channel1_IRQHandler(void)
} }
/** Capture done callback */
static void audio_capture_done(void* unused) static void audio_capture_done(void* unused)
{ {
(void)unused; (void)unused;
@ -87,20 +129,18 @@ static void audio_capture_done(void* unused)
// normalize // normalize
float mean; float mean;
arm_mean_f32(samp_buf.floats, samp_count, &mean); arm_mean_f32(samp_buf.floats, samp_count, &mean);
virt_zero_value = mean;
for (int i = 0; i < samp_count; i++) { for (int i = 0; i < samp_count; i++) {
samp_buf.floats[i] -= virt_zero_value; samp_buf.floats[i] -= mean;
} }
// if (print_next_fft) {
if (print_next_fft) { // printf("--- Raw (adjusted) ---\n");
printf("--- Raw (adjusted) ---\n"); // for(int i = 0; i < samp_count; i++) {
for(int i = 0; i < samp_count; i++) { // printf("%.2f, ", samp_buf.floats[i]);
printf("%.2f, ", samp_buf.floats[i]); // }
} // printf("\n");
printf("\n"); // }
}
for (int i = samp_count - 1; i >= 0; i--) { for (int i = samp_count - 1; i >= 0; i--) {
bins[i * 2 + 1] = 0; // imaginary 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_cfft_f32(S, bins, 0, true); // bit reversed FFT
arm_cmplx_mag_f32(bins, bins, bin_count); // get magnitude (extract real values) arm_cmplx_mag_f32(bins, bins, bin_count); // get magnitude (extract real values)
if (print_next_fft) { // if (print_next_fft) {
printf("--- Bins ---\n"); // printf("--- Bins ---\n");
for(int i = 0; i < bin_count; i++) { // for(int i = 0; i < bin_count; i++) {
printf("%.2f, ", bins[i]); // printf("%.2f, ", bins[i]);
} // }
printf("\n"); // printf("\n");
} // }
// normalize // normalize
dmtx_clear(dmtx); dmtx_clear(dmtx);
@ -141,7 +181,7 @@ static void audio_capture_done(void* unused)
dmtx_show(dmtx); dmtx_show(dmtx);
print_next_fft = false; // print_next_fft = false;
capture_pending = false; capture_pending = false;
} }

@ -3,8 +3,10 @@
#include "main.h" #include "main.h"
extern bool audio_mode_active;
void capture_audio(void *unused); void capture_audio(void *unused);
void mode_audio_init(void);
void mode_audio_start(void);
void mode_audio_stop(void);
#endif // MODE_AUDIO_H #endif // MODE_AUDIO_H

@ -17,7 +17,7 @@ static void printtext(const char *text, int x, int y)
ch -= ' '; // normalize for font table ch -= ' '; // normalize for font table
if (ch == 0) { // space if (ch == 0) { // space
totalX += 3; totalX += 4;
continue; continue;
} }

Loading…
Cancel
Save