@@ 377,12 377,12 @@ void main()
exti_init(&button_exti, 13, EXTI, SYSCFG);
exti_external_interrupt(&button_exti, 2);
- exti_falling_interrupt(&button_exti);
+ exti_both_edges_interrupt(&button_exti);
exti_clear_interrupt(&button_exti);
exti_enable_interrupt(&button_exti);
timer_init(&button_timer, TIM2, 2);
- timer_set_refresh(&button_timer, 10);
+ timer_set_refresh(&button_timer, 400);
timer_configure(&button_timer, 0, 60000, 1);
timer_enable_interrupt(&button_timer, true, false);
@@ 394,44 394,38 @@ void main()
app_loop();
}
-uint16_t timer_overflows = 0;
-
void EXTI15_10_handler(void) {
if (exti_is_interrupt(&button_exti)) {
exti_clear_interrupt(&button_exti);
- timer_overflows = 0;
- timer_set_counter(&button_timer, 0);
- timer_set_refresh(&button_timer, 25);
- timer_enable(&button_timer);
- }
-}
-
-void TIM2_handler(void) {
- if (timer_is_update_interrupt(&button_timer)) {
- timer_clear_update_interrupt(&button_timer);
-
bool button_pressed = !pin_read(&user_button);
+ if (button_pressed) { // start timer on button press
+ timer_set_counter(&button_timer, 0);
+ timer_clear_update_interrupt(&button_timer);
+ timer_enable(&button_timer);
- if (button_pressed) {
- timer_overflows++;
- } else { // user let go of the button, short press
+ // handle short presses, prevent registering bouncing
+ } else if (timer_count(&button_timer) > 10) {
+ timer_disable(&button_timer);
+ timer_set_counter(&button_timer, 0);
+
+ // short press (under 1 s)
if (auto_toggle) {
auto_toggle = false;
} else {
toggle_next = true;
}
-
- return;
}
+ }
+}
+
+void TIM2_handler(void) {
+ if (timer_is_update_interrupt(&button_timer)) {
+ timer_clear_update_interrupt(&button_timer);
// long press (over 1 s)
- if (timer_overflows > 10) {
- auto_toggle = !auto_toggle;
- cycle = 0;
- } else {
- timer_enable(&button_timer);
- }
+ auto_toggle = !auto_toggle;
+ cycle = 0;
}
}