~ruther/uni-mam-arm

92d09587fdadb9df8a0c766db4818343031d5430 — Rutherther 3 months ago caeaf90
feat(arm07): reimplement short/long button press with timer with just one refresh
1 files changed, 20 insertions(+), 26 deletions(-)

M arm07/src/main.c
M arm07/src/main.c => arm07/src/main.c +20 -26
@@ 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;
  }
}


Do not follow this link