1010#include "trigger_basic.pio.h"
1111
1212static bool armed = false;
13+ static bool pulse = false;
1314static bool timeout_active = true;
1415static bool hvp_internal = true;
1516static absolute_time_t timeout_time ;
@@ -23,6 +24,9 @@ static uint offset = 0xFFFFFFFF;
2324static uint32_t pulse_time ;
2425static uint32_t pulse_delay_cycles ;
2526static uint32_t pulse_time_cycles ;
27+ static bool button_enabled = true;
28+ static int32_t button_alarm_id = 0 ;
29+
2630static union float_union {float f ; uint32_t ui32 ;} pulse_power ;
2731
2832void arm () {
@@ -79,6 +83,35 @@ void fast_trigger() {
7983
8084}
8185
86+ int64_t button_enable (alarm_id_t alarm_id , void * user_data ) {
87+ button_enabled = true;
88+ return 0 ;
89+ }
90+
91+ void button_press_callback (uint gpio , uint32_t events ) {
92+ if (button_enabled ) {
93+ button_enabled = false;
94+
95+ update_timeout ();
96+ if (gpio == PIN_BTN_PULSE ) {
97+ pulse = true;
98+ } else if (gpio == PIN_BTN_ARM ) {
99+ if (!armed ) {
100+ arm ();
101+ } else {
102+ disarm ();
103+ }
104+ }
105+ } else {
106+ cancel_alarm (button_alarm_id );
107+ }
108+
109+ // Add an alarm to re-enable the buttons in 80ms (aka debounce time)
110+ button_alarm_id = add_alarm_in_ms (80 , button_enable , NULL , false);
111+ }
112+
113+
114+
82115int main () {
83116 // Initialize USB-UART as STDIO
84117 stdio_init_all ();
@@ -98,6 +131,10 @@ int main() {
98131 pulse_delay_cycles = PULSE_DELAY_CYCLES_DEFAULT ;
99132 pulse_time_cycles = PULSE_TIME_CYCLES_DEFAULT ;
100133
134+ // Set button interrupt and callback function
135+ gpio_set_irq_enabled_with_callback (PIN_BTN_ARM , GPIO_IRQ_LEVEL_HIGH , true, & button_press_callback );
136+ gpio_set_irq_enabled (PIN_BTN_PULSE , GPIO_IRQ_LEVEL_LOW , true);
137+
101138 while (1 ) {
102139 gpio_put (PIN_LED_HV , gpio_get (PIN_IN_CHARGED ));
103140
@@ -174,23 +211,11 @@ int main() {
174211 }
175212
176213 // Pulse
177- if (gpio_get ( PIN_BTN_PULSE ) ) {
178- update_timeout () ;
214+ if (pulse ) {
215+ pulse = false ;
179216 picoemp_pulse (pulse_time );
180217 }
181218
182- if (gpio_get (PIN_BTN_ARM )) {
183- update_timeout ();
184- if (!armed ) {
185- arm ();
186- } else {
187- disarm ();
188- }
189- // YOLO debouncing
190- while (gpio_get (PIN_BTN_ARM ));
191- sleep_ms (100 );
192- }
193-
194219 if (!gpio_get (PIN_IN_CHARGED ) && armed ) {
195220 picoemp_enable_pwm (pulse_power .f );
196221 }
0 commit comments