From 9c588a88b9a3f9432d857c7fd2c2e98797b22e74 Mon Sep 17 00:00:00 2001 From: alephbias <176774739+alephbias@users.noreply.github.com> Date: Sat, 3 Aug 2024 16:08:52 -0500 Subject: [PATCH] Update main.c to incorporate PR #24 of base --- firmware/c/main.c | 53 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/firmware/c/main.c b/firmware/c/main.c index 4fec827..5ad5bde 100644 --- a/firmware/c/main.c +++ b/firmware/c/main.c @@ -10,6 +10,7 @@ #include "trigger_basic.pio.h" static bool armed = false; +static bool pulse = false; static bool timeout_active = true; static bool hvp_internal = true; static absolute_time_t timeout_time; @@ -23,6 +24,9 @@ static uint offset = 0xFFFFFFFF; static uint32_t pulse_time; static uint32_t pulse_delay_cycles; static uint32_t pulse_time_cycles; +static bool button_enabled = true; +static int32_t button_alarm_id = 0; + static union float_union {float f; uint32_t ui32;} pulse_power; void arm() { @@ -79,6 +83,35 @@ void fast_trigger() { } +int64_t button_enable(alarm_id_t alarm_id, void *user_data) { + button_enabled = true; + return 0; +} + +void button_press_callback(uint gpio, uint32_t events) { + if(button_enabled) { + button_enabled = false; + + update_timeout(); + if(gpio == PIN_BTN_PULSE) { + pulse = true; + } else if(gpio == PIN_BTN_ARM) { + if(!armed) { + arm(); + } else { + disarm(); + } + } + } else { + cancel_alarm(button_alarm_id); + } + + // Add an alarm to re-enable the buttons in 80ms (aka debounce time) + button_alarm_id = add_alarm_in_ms(80, button_enable, NULL, false); +} + + + int main() { // Initialize USB-UART as STDIO stdio_init_all(); @@ -98,6 +131,10 @@ int main() { pulse_delay_cycles = PULSE_DELAY_CYCLES_DEFAULT; pulse_time_cycles = PULSE_TIME_CYCLES_DEFAULT; + // Set button interrupt and callback function + gpio_set_irq_enabled_with_callback(PIN_BTN_ARM, GPIO_IRQ_LEVEL_HIGH, true, &button_press_callback); + gpio_set_irq_enabled(PIN_BTN_PULSE, GPIO_IRQ_LEVEL_LOW, true); + while(1) { gpio_put(PIN_LED_HV, gpio_get(PIN_IN_CHARGED)); @@ -174,23 +211,11 @@ int main() { } // Pulse - if(gpio_get(PIN_BTN_PULSE)) { - update_timeout(); + if(pulse) { + pulse = false; picoemp_pulse(pulse_time); } - if(gpio_get(PIN_BTN_ARM)) { - update_timeout(); - if(!armed) { - arm(); - } else { - disarm(); - } - // YOLO debouncing - while(gpio_get(PIN_BTN_ARM)); - sleep_ms(100); - } - if(!gpio_get(PIN_IN_CHARGED) && armed) { picoemp_enable_pwm(pulse_power.f); }