mirror of
https://github.com/firewalkwithm3/Sensor-Watch.git
synced 2024-11-22 11:10:29 +08:00
Merge PR #437 - debouncing logic
Mechanical switches exhibit temporary voltage fluctuations when electrical contact is made, which manifest as "bouncing" between the logical high and low states. Sampling the switch's state during this period of stability produces invalid results. The switch must be debounced by ignoring the generated interrupts until the switch's state has stabilized. This is implemented by delaying the input events until an empirically determined time has elapsed. As such this pull request introduces customizable high resolution timers for debouncing button down and up events. This software debouncing unfortunately increases the sensor watch's input latency. This is an acceptable tradeoff due to better usability: watch faces which require holding down buttons such as the pulsometer should work much more reliably after this patch. Reviewed-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com> Tested-on-hardware-by: Krzysztof Gałka <@kshysztof@Discord> GitHub-Pull-Request: https://github.com/joeycastillo/Sensor-Watch/pull/437 References: https://en.wikipedia.org/wiki/Switch#Contact_bounce
This commit is contained in:
commit
a715265af6
|
@ -23,6 +23,16 @@
|
|||
*/
|
||||
|
||||
#define MOVEMENT_LONG_PRESS_TICKS 64
|
||||
#define DEBOUNCE_TICKS_DOWN 0
|
||||
#define DEBOUNCE_TICKS_UP 0
|
||||
/*
|
||||
DEBOUNCE_TICKS_DOWN and DEBOUNCE_TICKS_UP are in terms of fast_cb ticks after a button is pressed.
|
||||
The logic is that pressed of a button are ignored until the cb_fast_tick function runs this variable amount of times.
|
||||
Without modifying the code, the cb_fast_tick frequency is 128Hz, or 7.8125ms.
|
||||
It is not suggested to set this value to one for debouncing, as the callback occurs asynchronously of the button's press,
|
||||
meaning that if a button was pressed and 7ms passed since th elast time cb_fast_tick was called, then there will be only 812.5us
|
||||
of debounce time.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
@ -194,6 +204,9 @@ static inline void _movement_reset_inactivity_countdown(void) {
|
|||
static inline void _movement_enable_fast_tick_if_needed(void) {
|
||||
if (!movement_state.fast_tick_enabled) {
|
||||
movement_state.fast_ticks = 0;
|
||||
movement_state.debounce_ticks_light = 0;
|
||||
movement_state.debounce_ticks_alarm = 0;
|
||||
movement_state.debounce_ticks_mode = 0;
|
||||
watch_rtc_register_periodic_callback(cb_fast_tick, 128);
|
||||
movement_state.fast_tick_enabled = true;
|
||||
}
|
||||
|
@ -202,6 +215,7 @@ static inline void _movement_enable_fast_tick_if_needed(void) {
|
|||
static inline void _movement_disable_fast_tick_if_possible(void) {
|
||||
if ((movement_state.light_ticks == -1) &&
|
||||
(movement_state.alarm_ticks == -1) &&
|
||||
((movement_state.debounce_ticks_light + movement_state.debounce_ticks_mode + movement_state.debounce_ticks_alarm) == 0) &&
|
||||
((movement_state.light_down_timestamp + movement_state.mode_down_timestamp + movement_state.alarm_down_timestamp) == 0)) {
|
||||
movement_state.fast_tick_enabled = false;
|
||||
watch_rtc_disable_periodic_callback(128);
|
||||
|
@ -496,6 +510,7 @@ void app_wake_from_standby(void) {
|
|||
|
||||
static void _sleep_mode_app_loop(void) {
|
||||
movement_state.needs_wake = false;
|
||||
movement_state.ignore_alarm_btn_after_sleep = true;
|
||||
// as long as le_mode_ticks is -1 (i.e. we are in low energy mode), we wake up here, update the screen, and go right back to sleep.
|
||||
while (movement_state.le_mode_ticks == -1) {
|
||||
// we also have to handle background tasks here in the mini-runloop
|
||||
|
@ -661,29 +676,66 @@ static movement_event_type_t _figure_out_button_event(bool pin_level, movement_e
|
|||
// now that that's out of the way, handle falling edge
|
||||
uint16_t diff = movement_state.fast_ticks - *down_timestamp;
|
||||
*down_timestamp = 0;
|
||||
_movement_disable_fast_tick_if_possible();
|
||||
// any press over a half second is considered a long press. Fire the long-up event
|
||||
if (diff > MOVEMENT_LONG_PRESS_TICKS) return button_down_event_type + 3;
|
||||
else return button_down_event_type + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static movement_event_type_t btn_action(bool pin_level, int code, uint16_t *timestamp) {
|
||||
_movement_reset_inactivity_countdown();
|
||||
return _figure_out_button_event(pin_level, code, timestamp);
|
||||
}
|
||||
|
||||
static void light_btn_action(bool pin_level) {
|
||||
event.event_type = btn_action(pin_level, EVENT_LIGHT_BUTTON_DOWN, &movement_state.light_down_timestamp);
|
||||
}
|
||||
|
||||
static void mode_btn_action(bool pin_level) {
|
||||
event.event_type = btn_action(pin_level, EVENT_MODE_BUTTON_DOWN, &movement_state.mode_down_timestamp);
|
||||
}
|
||||
|
||||
static void alarm_btn_action(bool pin_level) {
|
||||
uint8_t event_type = btn_action(pin_level, EVENT_ALARM_BUTTON_DOWN, &movement_state.alarm_down_timestamp);
|
||||
if (movement_state.ignore_alarm_btn_after_sleep){
|
||||
if (event_type == EVENT_ALARM_BUTTON_UP || event_type == EVENT_ALARM_LONG_UP) movement_state.ignore_alarm_btn_after_sleep = false;
|
||||
return;
|
||||
}
|
||||
event.event_type = event_type;
|
||||
}
|
||||
|
||||
static void debounce_btn_press(uint8_t pin, uint8_t *debounce_ticks, uint16_t *down_timestamp, void (*function)(bool)) {
|
||||
if (*debounce_ticks == 0) {
|
||||
bool pin_level = watch_get_pin_level(pin);
|
||||
function(pin_level);
|
||||
*debounce_ticks = pin_level ? DEBOUNCE_TICKS_DOWN : DEBOUNCE_TICKS_UP;
|
||||
if (*debounce_ticks != 0) _movement_enable_fast_tick_if_needed();
|
||||
}
|
||||
else
|
||||
*down_timestamp = 0;
|
||||
}
|
||||
|
||||
static void disable_if_needed(uint8_t *ticks) {
|
||||
if (*ticks > 0 && --*ticks == 0)
|
||||
_movement_disable_fast_tick_if_possible();
|
||||
}
|
||||
|
||||
static void movement_disable_if_debounce_complete(void) {
|
||||
disable_if_needed(&movement_state.debounce_ticks_light);
|
||||
disable_if_needed(&movement_state.debounce_ticks_alarm);
|
||||
disable_if_needed(&movement_state.debounce_ticks_mode);
|
||||
}
|
||||
|
||||
void cb_light_btn_interrupt(void) {
|
||||
bool pin_level = watch_get_pin_level(BTN_LIGHT);
|
||||
_movement_reset_inactivity_countdown();
|
||||
event.event_type = _figure_out_button_event(pin_level, EVENT_LIGHT_BUTTON_DOWN, &movement_state.light_down_timestamp);
|
||||
debounce_btn_press(BTN_LIGHT, &movement_state.debounce_ticks_light, &movement_state.light_down_timestamp, light_btn_action);
|
||||
}
|
||||
|
||||
void cb_mode_btn_interrupt(void) {
|
||||
bool pin_level = watch_get_pin_level(BTN_MODE);
|
||||
_movement_reset_inactivity_countdown();
|
||||
event.event_type = _figure_out_button_event(pin_level, EVENT_MODE_BUTTON_DOWN, &movement_state.mode_down_timestamp);
|
||||
debounce_btn_press(BTN_MODE, &movement_state.debounce_ticks_mode, &movement_state.mode_down_timestamp, mode_btn_action);
|
||||
}
|
||||
|
||||
void cb_alarm_btn_interrupt(void) {
|
||||
bool pin_level = watch_get_pin_level(BTN_ALARM);
|
||||
_movement_reset_inactivity_countdown();
|
||||
event.event_type = _figure_out_button_event(pin_level, EVENT_ALARM_BUTTON_DOWN, &movement_state.alarm_down_timestamp);
|
||||
debounce_btn_press(BTN_ALARM, &movement_state.debounce_ticks_alarm, &movement_state.alarm_down_timestamp, alarm_btn_action);
|
||||
}
|
||||
|
||||
void cb_alarm_btn_extwake(void) {
|
||||
|
@ -696,7 +748,9 @@ void cb_alarm_fired(void) {
|
|||
}
|
||||
|
||||
void cb_fast_tick(void) {
|
||||
movement_state.fast_ticks++;
|
||||
movement_disable_if_debounce_complete();
|
||||
if (movement_state.debounce_ticks_light + movement_state.debounce_ticks_mode + movement_state.debounce_ticks_alarm == 0)
|
||||
movement_state.fast_ticks++;
|
||||
if (movement_state.light_ticks > 0) movement_state.light_ticks--;
|
||||
if (movement_state.alarm_ticks > 0) movement_state.alarm_ticks--;
|
||||
// check timestamps and auto-fire the long-press events
|
||||
|
|
|
@ -272,6 +272,10 @@ typedef struct {
|
|||
|
||||
// low energy mode countdown
|
||||
int32_t le_mode_ticks;
|
||||
uint8_t debounce_ticks_light;
|
||||
uint8_t debounce_ticks_alarm;
|
||||
uint8_t debounce_ticks_mode;
|
||||
bool ignore_alarm_btn_after_sleep;
|
||||
|
||||
// app resignation countdown (TODO: consolidate with LE countdown?)
|
||||
int16_t timeout_ticks;
|
||||
|
|
Loading…
Reference in a new issue