Skip to content

File syn_button.c

FileList > input > syn_button.c

Go to the source code of this file

Debounced button implementation using syn_fsm.

  • #include "../drivers/syn_gpio.h"
  • #include "../util/syn_assert.h"
  • #include "syn_button.h"
  • #include <string.h>

Public Types

Type Name
enum syn__button_8c_1a06fc87d81c62e9abb8790b6e5713c55b

Public Static Attributes

Type Name
const SYN_FSM_Transition g_button_transitions = /* multi line expression */
Button FSM transition table.

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
void action_bounce_back (void * ctx)
FSM action: raw release during debounce → return to idle.
void action_confirm_press (void * ctx)
FSM action: debounce confirmed, fire press event.
void action_long_press (void * ctx)
FSM action: held long enough, fire long-press event.
void action_release (void * ctx)
FSM action: button released, fire release event.
void action_start_debounce (void * ctx)
FSM action: start debounce timer on raw press.
void button_fire_event (SYN_Button * btn, uint8_t evt, SYN_ButtonCallback cb, void * ctx)
Set an event flag and invoke the callback.
bool button_read_raw (const SYN_Button * btn)
Read the raw (debounce-agnostic) button state.

Public Types Documentation

enum syn__button_8c_1a06fc87d81c62e9abb8790b6e5713c55b

enum syn__button_8c_1a06fc87d81c62e9abb8790b6e5713c55b {
    BTN_EV_RAW_PRESS = 0,
    BTN_EV_RAW_RELEASE,
    BTN_EV_DEBOUNCE_OK,
    BTN_EV_LONG_PRESS
};

Public Static Attributes Documentation

variable g_button_transitions

Button FSM transition table.

const SYN_FSM_Transition g_button_transitions[];


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:

  • combo Combo descriptor.
  • buttons Array of pointers to initialized SYN_Button descriptors.
  • count Number of buttons in array.
  • cb Combo callback.
  • ctx User context.

function syn_button_combo_update

Update a combination button descriptor.

void syn_button_combo_update (
    SYN_ButtonCombo * combo
) 

Parameters:

  • combo Combo 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:

  • btn Button to initialize.
  • pin GPIO pin number.
  • polarity Active-high or active-low.
  • debounce_ms Debounce window in milliseconds (e.g., 50).

function syn_button_on_double_click

Register a double-click callback.

void syn_button_on_double_click (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User 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:

  • btn Button.
  • cb Callback (or NULL to disable).
  • hold_ms Time the button must be held before firing (ms).
  • ctx User context.

function syn_button_on_multi_click

Register a multi-click (4+ taps) callback.

void syn_button_on_multi_click (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User context.

function syn_button_on_press

Register a press callback.

void syn_button_on_press (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User context.

function syn_button_on_release

Register a release callback.

void syn_button_on_release (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User 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:

  • btn Button.
  • cb Callback (or NULL to disable).
  • interval_ms Repeat interval in milliseconds.
  • ctx User context.

function syn_button_on_single_click

Register a single-click callback.

void syn_button_on_single_click (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User context.

function syn_button_on_triple_click

Register a triple-click callback.

void syn_button_on_triple_click (
    SYN_Button * btn,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button.
  • cb Callback (or NULL to disable).
  • ctx User context.

function syn_button_service

Service an array of buttons.

void syn_button_service (
    SYN_Button * buttons,
    size_t count
) 

Parameters:

  • buttons Array of buttons.
  • count Number of buttons.

function syn_button_set_click_window

Set the multi-click evaluation window in milliseconds.

void syn_button_set_click_window (
    SYN_Button * btn,
    uint16_t window_ms
) 

Parameters:

  • btn Button.
  • window_ms Max 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.

void syn_button_update (
    SYN_Button * btn
) 

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:

  • btn Button to update.

Public Static Functions Documentation

function action_bounce_back

FSM action: raw release during debounce → return to idle.

static void action_bounce_back (
    void * ctx
) 

Parameters:

  • ctx Button (as void*).

function action_confirm_press

FSM action: debounce confirmed, fire press event.

static void action_confirm_press (
    void * ctx
) 

Parameters:

  • ctx Button (as void*).

function action_long_press

FSM action: held long enough, fire long-press event.

static void action_long_press (
    void * ctx
) 

Parameters:

  • ctx Button (as void*).

function action_release

FSM action: button released, fire release event.

static void action_release (
    void * ctx
) 

Parameters:

  • ctx Button (as void*).

function action_start_debounce

FSM action: start debounce timer on raw press.

static void action_start_debounce (
    void * ctx
) 

Parameters:

  • ctx Button (as void*).

function button_fire_event

Set an event flag and invoke the callback.

static void button_fire_event (
    SYN_Button * btn,
    uint8_t evt,
    SYN_ButtonCallback cb,
    void * ctx
) 

Parameters:

  • btn Button instance.
  • evt Event bitmask.
  • cb Callback (may be NULL).
  • ctx User context for callback.

function button_read_raw

Read the raw (debounce-agnostic) button state.

static bool button_read_raw (
    const SYN_Button * btn
) 

Parameters:

  • btn Button instance.

Returns:

true if the GPIO indicates pressed.



The documentation for this class was generated from the following file src/syntropic/input/syn_button.c