button: HW timer-based driver for GPIO buttons

Defines

BUTTON_DEFAULT_CONFIG()

Typedefs

typedef struct button *button_handle_t

Opaque button handle.

typedef void (*button_event_cb_t)(const button_event_t *event, void *ctx)

Callback prototype.

The callback is always invoked from the ESP timer task context. It must not block or call back into the library.

Param event:

Button event

Param ctx:

User context

Enums

enum button_mode_t

Button detection mode.

Values:

enumerator BUTTON_MODE_POLL

Periodic timer polling (default)

enumerator BUTTON_MODE_INTERRUPT

GPIO interrupt with debounce timer.

enum button_state_t

Button states/events.

When autorepeat is disabled, the event sequence for a short press is: PRESSED -> RELEASED -> CLICKED and for a long press: PRESSED -> PRESSED_LONG -> RELEASED

When autorepeat is enabled, long press detection is disabled. Instead, holding the button generates repeated CLICKED events: PRESSED -> CLICKED -> CLICKED -> … -> RELEASED

Values:

enumerator BUTTON_PRESSED
enumerator BUTTON_RELEASED
enumerator BUTTON_CLICKED
enumerator BUTTON_PRESSED_LONG

Functions

esp_err_t button_create(const button_config_t *config, button_handle_t *handle)

Create a new button.

When using BUTTON_MODE_INTERRUPT, the GPIO ISR service must be installed by the caller before creating any interrupt-mode buttons (via gpio_install_isr_service()).

Parameters:
  • config – Button configuration

  • handle[out] Button handle, populated on success

Returns:

ESP_OK on success

esp_err_t button_delete(button_handle_t handle)

Delete a button.

Parameters:

handle – Button handle

Returns:

ESP_OK on success

struct button_event_t
#include <button.h>

Button event.

Public Members

button_state_t type

Event type.

button_handle_t sender

Button handle.

struct button_config_t
#include <button.h>

Button configuration.

Public Members

gpio_num_t gpio

GPIO pin number.

button_mode_t mode

Detection mode (poll or interrupt)

uint8_t pressed_level

Logic level when button is pressed (0 or 1)

bool enable_internal_pull

Enable internal pull-up/pull-down resistor.

bool autorepeat

Enable autorepeat (mutually exclusive with long press)

uint32_t dead_time_us

Dead time after press / debounce delay (microseconds)

uint32_t long_press_time_us

Long press threshold in microseconds (ignored when autorepeat is enabled)

uint32_t autorepeat_timeout_us

Time before autorepeat starts, in microseconds.

uint32_t autorepeat_interval_us

Autorepeat interval in microseconds.

uint32_t poll_interval_us

Polling / monitoring interval (microseconds)

button_event_cb_t callback

Event callback (required)

void *callback_ctx

User context passed to callback.