@@ 382,25 382,10 @@ void main()
exti_enable_interrupt(&button_exti);
timer_init(&button_timer, TIM2, 2);
- timer_set_refresh(&button_timer, 9999);
+ timer_set_refresh(&button_timer, 10);
timer_configure(&button_timer, 0, 60000, 1);
- pin_t pin_capture;
- pin_init(&pin_capture, GPIOA, 15);
- pin_into_alternate(&pin_capture, 1);
-
- reg_write_bits(&button_timer.periph->CCMR1,
- // TI1 on CCR1 no prescaler - capture every event sampling freq = fdts / 32, N = 8
- // fdts = 24 MHz, fsample = 750kHz
- (1 << TIM_CCMR1_CC1S_Pos) | (0 << TIM_CCMR1_IC1PSC_Pos) | (0xF << TIM_CCMR1_IC1F_Pos),
- TIM_CCMR1_CC1S | TIM_CCMR1_IC1PSC | TIM_CCMR1_IC1F);
- reg_write_bits(
- &button_timer.periph->CCER,
- // enable CC1 falling edge captures
- (1 << TIM_CCER_CC1E_Pos) | (1 << TIM_CCER_CC1NP_Pos) | (0 << TIM_CCER_CC1P_Pos),
- TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC1NP);
-
- timer_enable_interrupt(&button_timer, true, true);
+ timer_enable_interrupt(&button_timer, true, false);
}
__enable_irq();
@@ 409,11 394,15 @@ 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);
}
}
@@ 421,28 410,27 @@ void EXTI15_10_handler(void) {
void TIM2_handler(void) {
if (timer_is_update_interrupt(&button_timer)) {
timer_clear_update_interrupt(&button_timer);
- } else if (timer_is_capture1_interrupt(&button_timer)) {
- uint32_t count = button_timer.periph->CCR1;
- timer_clear_capture1_interrupt(&button_timer);
-
- // 200 Hz => 5 ms
- // 50 ms is 10. That means button was really pressed by user, no bouncing
- // 2 s is 400
- if (count > 10) { // at least 50 ms
- if (count > 400) { // at least 2 s
- auto_toggle = !auto_toggle;
- cycle = 0;
- } else { // less than 2 s
- if (auto_toggle) {
- auto_toggle = false;
- }
- else {
- toggle_next = true;
- }
+
+ bool button_pressed = !pin_read(&user_button);
+
+ if (button_pressed) {
+ timer_overflows++;
+ } else { // user let go of the button, short press
+ if (auto_toggle) {
+ auto_toggle = false;
+ } else {
+ toggle_next = true;
}
- // stop the timer
- timer_disable(&button_timer);
+ return;
+ }
+
+ // long press (over 1 s)
+ if (timer_overflows > 10) {
+ auto_toggle = !auto_toggle;
+ cycle = 0;
+ } else {
+ timer_enable(&button_timer);
}
}
}