|
px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
|
This module debounces a digital button input and reports button events.
File(s):
This module must be configured by supplying a project specific "px_btn_cfg.h". "px_btn_cfg_template.h" can be copied, renamed and modified to supply compile time options.
It is ideal for a noisy digital input such as a button. When a switch is pressed, the input will bounce between high and low until it finally settles to a valid state.
This module is able to debounce a digital input by looking at successive values and deciding if it is a valid low or high state. It is also able to detect and remember a button pressed event (debounced rising edge) or button released event (debounced falling edge). Finally, if PX_BTN_CFG_LONG_COUNT is defined with a large non-zero value, a long press or long release is also detected.
Single, double or triple clicks can be detected and reported by setting PX_BTN_CFG_CLICKS_MAX to the maximum number of clicks. A click is defined as a button press exceeding PX_BTN_CFG_CLICK_PRESS_MIN followed by a button release. The maximum inter-click time is set with PX_BTN_CFG_CLICK_RELEASE_MAX.
Debouncing is implemented as follows: a counter is incremented each time the current digital input state is HI. If the counter reaches the high watermark threshold, the debounced state is considered HI. Conversely, the counter is decremented each time the current digital input state is LO. If the counter reaches the low watermark threshold, the debounced state is considered LO. The minimum counter value is 0 and the maximum value is PX_BTN_CFG_COUNT_MAX. This scheme provides sufficient hysteresis to debounce a noisy digital input.
If no hysteresis is required, then PX_BTN_CFG_THRESHOLD_LO can be set to 0 and PX_BTN_CFG_THRESHOLD_HI can be set to PX_BTN_CFG_COUNT_MAX.
Example:
Data Structures | |
| struct | px_btn_t |
| Structure to track state of button. More... | |
| struct | px_btn_t.flag |
Macros | |
| #define | PX_BTN_CFG_COUNT_MAX 9 |
| Max HI counter value (counter starts at 0) More... | |
| #define | PX_BTN_CFG_THRESHOLD_LO 2 |
| Low threshold for valid LO. More... | |
| #define | PX_BTN_CFG_THRESHOLD_HI 7 |
| High threshold for valid HI. More... | |
| #define | PX_BTN_CFG_LONG_COUNT 300 |
| Count threshold for long LO/ HI; Set to 0 to disable option. More... | |
| #define | PX_BTN_CFG_CLICKS_MAX 2 |
| Maximum number of button clicks to detect; Minimum value is 1. More... | |
| #define | PX_BTN_CFG_CLICK_PRESS_MIN 3 |
| A button press count longer than this value is considered a click. More... | |
| #define | PX_BTN_CFG_CLICK_RELEASE_MAX 20 |
| A button release count shorter than this value is considered part of the same click sequence. More... | |
Functions | |
| void | px_btn_init (px_btn_t *btn, bool btn_is_pressed) |
| Initialise button state. More... | |
| void | px_btn_update (px_btn_t *btn, bool btn_is_pressed) |
| Update debounced button state and if event is detected set flag. More... | |
| bool | px_btn_is_pressed (const px_btn_t *btn) |
| Is button being pressed (debounced state). More... | |
| bool | px_btn_is_released (const px_btn_t *btn) |
| Is button released (debounced state). More... | |
| bool | px_btn_event_press (px_btn_t *btn) |
| Button press event detected? More... | |
| bool | px_btn_event_release (px_btn_t *btn) |
| Button release event detected? More... | |
| bool | px_btn_event_long_press (px_btn_t *btn) |
| Button long press event detected? More... | |
| bool | px_btn_event_long_release (px_btn_t *btn) |
| Button long release event detected? More... | |
| uint8_t | px_btn_event_click (px_btn_t *btn) |
| Button click (or clicks) event detected? More... | |
| struct px_btn_t |
| Data Fields | ||
|---|---|---|
| struct px_btn_t.flag | flag | |
| uint8_t | counter | LO/HI counter. |
| uint16_t | state_counter | debounced state counter |
| uint8_t | nr_of_clicks | Number of clicks detected. |
| struct px_btn_t.flag |
| #define PX_BTN_CFG_COUNT_MAX 9 |
Max HI counter value (counter starts at 0)
Definition at line 34 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_THRESHOLD_LO 2 |
Low threshold for valid LO.
Definition at line 37 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_THRESHOLD_HI 7 |
High threshold for valid HI.
Definition at line 40 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_LONG_COUNT 300 |
Count threshold for long LO/ HI; Set to 0 to disable option.
Definition at line 43 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_CLICKS_MAX 2 |
Maximum number of button clicks to detect; Minimum value is 1.
Definition at line 46 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_CLICK_PRESS_MIN 3 |
A button press count longer than this value is considered a click.
Definition at line 49 of file px_btn_cfg_template.h.
| #define PX_BTN_CFG_CLICK_RELEASE_MAX 20 |
A button release count shorter than this value is considered part of the same click sequence.
Definition at line 52 of file px_btn_cfg_template.h.
| void px_btn_init | ( | px_btn_t * | btn, |
| bool | btn_is_pressed | ||
| ) |
| void px_btn_update | ( | px_btn_t * | btn, |
| bool | btn_is_pressed | ||
| ) |
| bool px_btn_is_pressed | ( | const px_btn_t * | btn | ) |
| bool px_btn_is_released | ( | const px_btn_t * | btn | ) |
| bool px_btn_event_press | ( | px_btn_t * | btn | ) |
| bool px_btn_event_release | ( | px_btn_t * | btn | ) |
| bool px_btn_event_long_press | ( | px_btn_t * | btn | ) |
| bool px_btn_event_long_release | ( | px_btn_t * | btn | ) |