File syn_button.h¶
FileList > input > syn_button.h
Go to the source code of this file
Debounced button input with press/release/long-press/repeat. More...
#include "../common/syn_defs.h"#include "../drivers/syn_gpio.h"#include "../port/syn_port_system.h"#include "../util/syn_fsm.h"#include <stdbool.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_Button Button descriptor — owns the FSM, debounce, multi-click, and callback state. |
| struct | SYN_ButtonCombo Combination button descriptor for simultaneous multi-button presses. |
Public Types¶
| Type | Name |
|---|---|
| typedef void(* | SYN_ButtonCallback Button event callback. |
| enum | SYN_ButtonPolarity Button polarity selector. |
| enum | SYN_ButtonState Button FSM states. |
Public Functions¶
| Type | Name |
|---|---|
| void | syn_button_combo_init (SYN_ButtonCombo * combo, const SYN_Button ** buttons, size_t count, SYN_ButtonCallback cb, void * ctx) Initialize a combination button descriptor. |
| void | syn_button_combo_update (SYN_ButtonCombo * combo) Update a combination button descriptor. |
| void | syn_button_init (SYN_Button * btn, SYN_GPIO_Pin pin, SYN_ButtonPolarity polarity, uint16_t debounce_ms) Initialize a button descriptor. |
| void | syn_button_on_double_click (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a double-click callback. |
| void | syn_button_on_long_press (SYN_Button * btn, SYN_ButtonCallback cb, uint16_t hold_ms, void * ctx) Register a long-press callback. |
| void | syn_button_on_multi_click (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a multi-click (4+ taps) callback. |
| void | syn_button_on_press (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a press callback. |
| void | syn_button_on_release (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a release callback. |
| void | syn_button_on_repeat (SYN_Button * btn, SYN_ButtonCallback cb, uint16_t interval_ms, void * ctx) Register an auto-repeat callback. |
| void | syn_button_on_single_click (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a single-click callback. |
| void | syn_button_on_triple_click (SYN_Button * btn, SYN_ButtonCallback cb, void * ctx) Register a triple-click callback. |
| void | syn_button_service (SYN_Button * buttons, size_t count) Service an array of buttons. |
| void | syn_button_set_click_window (SYN_Button * btn, uint16_t window_ms) Set the multi-click evaluation window in milliseconds. |
| void | syn_button_update (SYN_Button * btn) Update a single button's state machine. |
Public Static Functions¶
| Type | Name |
|---|---|
| uint8_t | syn_button_clicks (const SYN_Button * btn) Get the consecutive click count. |
| bool | syn_button_combo_is_active (const SYN_ButtonCombo * combo) Is the combination button currently active (all buttons held)? |
| uint32_t | syn_button_held_ms (const SYN_Button * btn) How long has the button been in its current state (ms)? |
| bool | syn_button_is_pressed (const SYN_Button * btn) Is the button currently pressed (debounced)? |
| uint8_t | syn_button_poll_events (SYN_Button * btn) Read and clear pending events (bitmask of SYN_BUTTON_EVT_*). |
Macros¶
| Type | Name |
|---|---|
| define | PT_WAIT_BUTTON_PRESS (pt, btn) /* multi line expression */ |
| define | PT_WAIT_BUTTON_RELEASE (pt, btn) /* multi line expression */ |
| define | SYN_BUTTON_EVT_DOUBLE_CLICK ((uint8\_t)(1u << 5)) |
| define | SYN_BUTTON_EVT_LONG_PRESS ((uint8\_t)(1u << 2)) |
| define | SYN_BUTTON_EVT_MULTI_CLICK ((uint8\_t)(1u << 7)) |
| define | SYN_BUTTON_EVT_PRESS ((uint8\_t)(1u << 0)) |
| define | SYN_BUTTON_EVT_RELEASE ((uint8\_t)(1u << 1)) |
| define | SYN_BUTTON_EVT_REPEAT ((uint8\_t)(1u << 3)) |
| define | SYN_BUTTON_EVT_SINGLE_CLICK ((uint8\_t)(1u << 4)) |
| define | SYN_BUTTON_EVT_TRIPLE_CLICK ((uint8\_t)(1u << 6)) |
Detailed Description¶
Pure polling — no interrupts required. Call syn_button_update() (or syn_button_service() for an array) from your main loop or a scheduler task. The module handles debouncing, edge detection, and long-press timing internally.
** **
static SYN_Button btn;
syn_button_init(&btn, PIN_BUTTON, SYN_BUTTON_ACTIVE_LOW, 50);
syn_button_on_press(&btn, my_press_handler, NULL);
syn_button_on_long_press(&btn, my_long_handler, 1000, NULL);
// Main loop:
while (1) { syn_button_update(&btn); }
// Or in a protothread:
PT_WAIT_BUTTON_PRESS(pt, &btn);
Public Types Documentation¶
typedef SYN_ButtonCallback¶
Button event callback.
Parameters:
btnThe button that generated the event.user_dataUser-provided context pointer.
enum SYN_ButtonPolarity¶
Button polarity selector.
enum SYN_ButtonState¶
Button FSM states.
enum SYN_ButtonState {
SYN_BUTTON_STATE_IDLE = 0,
SYN_BUTTON_STATE_DEBOUNCING = 1,
SYN_BUTTON_STATE_PRESSED = 2,
SYN_BUTTON_STATE_HELD = 3
};
Public Functions Documentation¶
function syn_button_combo_init¶
Initialize a combination button descriptor.
void syn_button_combo_init (
SYN_ButtonCombo * combo,
const SYN_Button ** buttons,
size_t count,
SYN_ButtonCallback cb,
void * ctx
)
Fires callback when ALL specified buttons are simultaneously in debounced pressed state.
Parameters:
comboCombo descriptor.buttonsArray of pointers to initialized SYN_Button descriptors.countNumber of buttons in array.cbCombo callback.ctxUser context.
function syn_button_combo_update¶
Update a combination button descriptor.
Parameters:
comboCombo descriptor.
function syn_button_init¶
Initialize a button descriptor.
void syn_button_init (
SYN_Button * btn,
SYN_GPIO_Pin pin,
SYN_ButtonPolarity polarity,
uint16_t debounce_ms
)
Parameters:
btnButton to initialize.pinGPIO pin number.polarityActive-high or active-low.debounce_msDebounce window in milliseconds (e.g., 50).
function syn_button_on_double_click¶
Register a double-click callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_on_long_press¶
Register a long-press callback.
void syn_button_on_long_press (
SYN_Button * btn,
SYN_ButtonCallback cb,
uint16_t hold_ms,
void * ctx
)
Parameters:
btnButton.cbCallback (or NULL to disable).hold_msTime the button must be held before firing (ms).ctxUser context.
function syn_button_on_multi_click¶
Register a multi-click (4+ taps) callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_on_press¶
Register a press callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_on_release¶
Register a release callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_on_repeat¶
Register an auto-repeat callback.
void syn_button_on_repeat (
SYN_Button * btn,
SYN_ButtonCallback cb,
uint16_t interval_ms,
void * ctx
)
Fires repeatedly while the button is held, after the initial press.
Parameters:
btnButton.cbCallback (or NULL to disable).interval_msRepeat interval in milliseconds.ctxUser context.
function syn_button_on_single_click¶
Register a single-click callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_on_triple_click¶
Register a triple-click callback.
Parameters:
btnButton.cbCallback (or NULL to disable).ctxUser context.
function syn_button_service¶
Service an array of buttons.
Parameters:
buttonsArray of buttons.countNumber of buttons.
function syn_button_set_click_window¶
Set the multi-click evaluation window in milliseconds.
Parameters:
btnButton.window_msMax gap between taps to group into multi-clicks (default: 250ms, 0 = disable multi-click).
function syn_button_update¶
Update a single button's state machine.
Call this from your main loop, a scheduler task, or a timer callback. Reads the GPIO, runs debouncing, evaluates multi-clicks, and fires callbacks as needed.
Parameters:
btnButton to update.
Public Static Functions Documentation¶
function syn_button_clicks¶
Get the consecutive click count.
Parameters:
btnButton.
Returns:
Number of clicks accumulated in current multi-click sequence.
function syn_button_combo_is_active¶
Is the combination button currently active (all buttons held)?
Parameters:
comboCombo descriptor.
Returns:
true if active.
function syn_button_held_ms¶
How long has the button been in its current state (ms)?
Parameters:
btnButton.
Returns:
Duration in milliseconds.
function syn_button_is_pressed¶
Is the button currently pressed (debounced)?
Parameters:
btnButton.
Returns:
true if pressed.
function syn_button_poll_events¶
Read and clear pending events (bitmask of SYN_BUTTON_EVT_*).
Useful for polling instead of callbacks.
Parameters:
btnButton.
Returns:
Bitmask of events that occurred since last poll.
Macro Definition Documentation¶
define PT_WAIT_BUTTON_PRESS¶
Block until the button is pressed (debounced).
define PT_WAIT_BUTTON_RELEASE¶
Block until the button is released.
define SYN_BUTTON_EVT_DOUBLE_CLICK¶
Double click detected
define SYN_BUTTON_EVT_LONG_PRESS¶
Long press detected
define SYN_BUTTON_EVT_MULTI_CLICK¶
Multi click (4+ taps)
define SYN_BUTTON_EVT_PRESS¶
Button pressed
define SYN_BUTTON_EVT_RELEASE¶
Button released
define SYN_BUTTON_EVT_REPEAT¶
Auto-repeat fire
define SYN_BUTTON_EVT_SINGLE_CLICK¶
Confirmed single click
define SYN_BUTTON_EVT_TRIPLE_CLICK¶
Triple click detected
The documentation for this class was generated from the following file src/syntropic/input/syn_button.h