Add an optional implementation of button debounce
This commit is contained in:
+15
@@ -67,6 +67,9 @@ typedef struct {
|
|||||||
movement_timeout_index_t timeout_index;
|
movement_timeout_index_t timeout_index;
|
||||||
volatile bool is_down;
|
volatile bool is_down;
|
||||||
volatile rtc_counter_t down_timestamp;
|
volatile rtc_counter_t down_timestamp;
|
||||||
|
#if MOVEMENT_DEBOUNCE_TICKS
|
||||||
|
volatile rtc_counter_t up_timestamp;
|
||||||
|
#endif
|
||||||
} movement_button_t;
|
} movement_button_t;
|
||||||
|
|
||||||
/* Pieces of state that can be modified by the various interrupt callbacks.
|
/* Pieces of state that can be modified by the various interrupt callbacks.
|
||||||
@@ -1270,12 +1273,24 @@ static movement_event_type_t _process_button_event(bool pin_level, movement_butt
|
|||||||
|
|
||||||
uint32_t counter = watch_rtc_get_counter();
|
uint32_t counter = watch_rtc_get_counter();
|
||||||
|
|
||||||
|
#if MOVEMENT_DEBOUNCE_TICKS
|
||||||
|
if (
|
||||||
|
(counter - button->up_timestamp) <= MOVEMENT_DEBOUNCE_TICKS &&
|
||||||
|
(counter - button->down_timestamp) <= MOVEMENT_DEBOUNCE_TICKS
|
||||||
|
) {
|
||||||
|
return event_type;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
button->is_down = pin_level;
|
button->is_down = pin_level;
|
||||||
|
|
||||||
if (pin_level) {
|
if (pin_level) {
|
||||||
button->down_timestamp = counter;
|
button->down_timestamp = counter;
|
||||||
event_type = button->down_event;
|
event_type = button->down_event;
|
||||||
} else {
|
} else {
|
||||||
|
#if MOVEMENT_DEBOUNCE_TICKS
|
||||||
|
button->up_timestamp = counter;
|
||||||
|
#endif
|
||||||
if ((counter - button->down_timestamp) >= MOVEMENT_LONG_PRESS_TICKS) {
|
if ((counter - button->down_timestamp) >= MOVEMENT_LONG_PRESS_TICKS) {
|
||||||
event_type = button->down_event + 3;
|
event_type = button->down_event + 3;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -101,4 +101,10 @@ const watch_face_t watch_faces[] = {
|
|||||||
*/
|
*/
|
||||||
#define MOVEMENT_DEFAULT_LED_DURATION 1
|
#define MOVEMENT_DEFAULT_LED_DURATION 1
|
||||||
|
|
||||||
|
/* Optionally debounce button presses (disable by default).
|
||||||
|
* A value of 4 is a good starting point if you have issues
|
||||||
|
* with multiple button presses firing.
|
||||||
|
*/
|
||||||
|
#define MOVEMENT_DEBOUNCE_TICKS 0
|
||||||
|
|
||||||
#endif // MOVEMENT_CONFIG_H_
|
#endif // MOVEMENT_CONFIG_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user