volume adjust using gamepad

master
Ondřej Hruška 8 years ago
parent 7e539708b9
commit b38fc87a8b
  1. 14
      User/debounce.c
  2. 3
      User/debounce.h
  3. 39
      User/user_main.c

@ -17,8 +17,7 @@ typedef struct {
ms_time_t debo_time; ///< debouncing time (ms) ms_time_t debo_time; ///< debouncing time (ms)
ms_time_t counter_0; ///< counter for falling edge (ms) ms_time_t counter_0; ///< counter for falling edge (ms)
ms_time_t counter_1; ///< counter for rising edge (ms) ms_time_t counter_1; ///< counter for rising edge (ms)
void (*falling_cb)(uint32_t); void (*callback)(uint32_t, bool);
void (*rising_cb)(uint32_t);
} debo_slot_t; } debo_slot_t;
@ -71,8 +70,7 @@ debo_id_t debo_register_pin(debo_init_t *init)
slot->GPIOx = init->GPIOx; slot->GPIOx = init->GPIOx;
slot->pin = init->pin; slot->pin = init->pin;
slot->falling_cb = init->falling_cb; slot->callback = init->callback;
slot->rising_cb = init->rising_cb;
slot->cb_payload = init->cb_payload; slot->cb_payload = init->cb_payload;
slot->invert = init->invert; slot->invert = init->invert;
slot->counter_0 = 0; slot->counter_0 = 0;
@ -111,8 +109,8 @@ void debo_periodic_task(void *unused)
if (slot->counter_0++ == slot->debo_time) { if (slot->counter_0++ == slot->debo_time) {
slot->state = 0; slot->state = 0;
if (slot->falling_cb != NULL) { if (slot->callback != NULL) {
slot->falling_cb(slot->cb_payload); slot->callback(slot->cb_payload, false);
} }
} }
} else { } else {
@ -121,8 +119,8 @@ void debo_periodic_task(void *unused)
if (slot->counter_1++ == slot->debo_time) { if (slot->counter_1++ == slot->debo_time) {
slot->state = 1; slot->state = 1;
if (slot->rising_cb != NULL) { if (slot->callback != NULL) {
slot->rising_cb(slot->cb_payload); slot->callback(slot->cb_payload, true);
} }
} }
} }

@ -29,8 +29,7 @@ typedef struct {
bool invert; ///< invert value read from GPIO (button to ground) bool invert; ///< invert value read from GPIO (button to ground)
ms_time_t debo_time; ///< debounce time in ms, 0 = default (20 ms) ms_time_t debo_time; ///< debounce time in ms, 0 = default (20 ms)
uint32_t cb_payload; ///< Value passed to the callback func uint32_t cb_payload; ///< Value passed to the callback func
void (*rising_cb)(uint32_t); ///< callback when the pin goes HIGH void (*callback)(uint32_t, bool); ///< callback
void (*falling_cb)(uint32_t); ///< callback when the pin goes LOW
} debo_init_t; } debo_init_t;

@ -35,6 +35,8 @@ static DotMatrix_Cfg *disp;
static volatile bool capture_pending = false; static volatile bool capture_pending = false;
static float waveform_scale = 1;
static void display_wave(); static void display_wave();
static void display_fft(); static void display_fft();
@ -92,7 +94,6 @@ void spread_samples_for_fft()
void display_wave() void display_wave()
{ {
float wave_y_mult = 0.0078125f; float wave_y_mult = 0.0078125f;
float relative = 1;
samples_to_float(); samples_to_float();
@ -112,7 +113,7 @@ void display_wave()
dmtx_clear(disp); dmtx_clear(disp);
for (int i = 0; i < SCREEN_W; i++) { for (int i = 0; i < SCREEN_W; i++) {
dmtx_set(disp, i, 7 + roundf(audio_samples_f[i + x_offset] * wave_y_mult * relative), 1); dmtx_set(disp, i, 7 + roundf(audio_samples_f[i + x_offset] * wave_y_mult * waveform_scale), 1);
} }
dmtx_show(disp); dmtx_show(disp);
} }
@ -140,7 +141,7 @@ static void display_fft()
bins[i] *= factor; bins[i] *= factor;
} }
// TODO implement offset using gamepad buttons // TODO implement offset using gamepad buttons (?)
for (int x = 0; x < SCREEN_W; x++) { for (int x = 0; x < SCREEN_W; x++) {
for (int j = 0; j < 1 + floorf(bins[x]); j++) { for (int j = 0; j < 1 + floorf(bins[x]); j++) {
dmtx_set(dmtx, x, j, 1); dmtx_set(dmtx, x, j, 1);
@ -158,9 +159,21 @@ void HAL_SYSTICK_Callback(void)
timebase_ms_cb(); timebase_ms_cb();
} }
static void gamepad_button_press(uint32_t btn) bool up_pressed = false;
bool down_pressed = false;
static void gamepad_button_cb(uint32_t btn, bool press)
{ {
dbg("Button press %d", btn); dbg("Button press %d, state %d", btn, press);
switch (btn) {
case BTN_UP:
up_pressed = press;
break;
case BTN_DOWN:
down_pressed = press;
break;
}
} }
void user_init() { void user_init() {
@ -188,8 +201,7 @@ void user_init() {
debo_init_t debo; debo_init_t debo;
debo.debo_time = 50; debo.debo_time = 50;
debo.invert = true; debo.invert = true;
debo.falling_cb = NULL; debo.callback = gamepad_button_cb;
debo.rising_cb = gamepad_button_press;
// Central button // Central button
debo.cb_payload = BTN_CENTER; debo.cb_payload = BTN_CENTER;
debo.GPIOx = BTN_CE_GPIO_Port; debo.GPIOx = BTN_CE_GPIO_Port;
@ -228,12 +240,25 @@ void user_main()
ms_time_t counter1 = 0; ms_time_t counter1 = 0;
uint32_t counter2 = 0; uint32_t counter2 = 0;
uint32_t btn_scale_cnt = 0;
while (1) { while (1) {
if (ms_loop_elapsed(&counter1, 500)) { if (ms_loop_elapsed(&counter1, 500)) {
// Blink // Blink
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
} }
if (ms_loop_elapsed(&btn_scale_cnt, 50)) {
if (up_pressed) {
waveform_scale += 0.1;
}
if (down_pressed) {
if (waveform_scale > 0.1) {
waveform_scale -= 0.05;
}
}
}
if (!capture_pending) { if (!capture_pending) {
capture_start(); capture_start();
} }

Loading…
Cancel
Save