encoder: HW timer-based driver for incremental rotary encoders

Defines

RE_DEFAULT_BTN_PRESSED_LEVEL
RE_DEFAULT_ENABLE_INTERNAL_PULLUP
ROTARY_ENCODER_DEFAULT_CONFIG()

Typedefs

typedef struct rotary_encoder *rotary_encoder_handle_t
typedef void (*rotary_encoder_event_cb_t)(const rotary_encoder_event_t *event, void *ctx)

Callback type for encoder events.

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

Enums

enum rotary_encoder_btn_state_t

Button state.

Values:

enumerator RE_BTN_RELEASED

Button currently released.

enumerator RE_BTN_PRESSED

Button currently pressed.

enumerator RE_BTN_LONG_PRESSED

Button currently long pressed.

enum rotary_encoder_event_type_t

Event type.

Values:

enumerator RE_ET_CHANGED

Encoder turned.

enumerator RE_ET_BTN_RELEASED

Button released.

enumerator RE_ET_BTN_PRESSED

Button pressed.

enumerator RE_ET_BTN_LONG_PRESSED

Button long pressed (press time (us) > RE_BTN_LONG_PRESS_TIME_US)

enumerator RE_ET_BTN_CLICKED

Button was clicked.

Functions

esp_err_t rotary_encoder_create(const rotary_encoder_config_t *config, rotary_encoder_handle_t *handle)

Create a new rotary encoder.

Parameters:
  • config – Encoder configuration

  • handle[out] Encoder handle, populated on success

Returns:

ESP_OK on success

esp_err_t rotary_encoder_delete(rotary_encoder_handle_t handle)

Delete a rotary encoder.

Parameters:

handle – Encoder handle

Returns:

ESP_OK on success

esp_err_t rotary_encoder_enable_acceleration(rotary_encoder_handle_t handle, uint16_t coeff)

Enable acceleration on the rotary encoder.

Parameters:
  • handle – Encoder handle

  • coeff – Acceleration coefficient. Higher value means faster acceleration

Returns:

esp_err_t

esp_err_t rotary_encoder_disable_acceleration(rotary_encoder_handle_t handle)

Disable acceleration on the rotary encoder.

Parameters:

handle – Encoder handle

Returns:

ESP_OK on success

struct rotary_encoder_event_t
#include <encoder.h>

Event.

Public Members

rotary_encoder_event_type_t type

Event type.

rotary_encoder_handle_t sender

Encoder handle.

int32_t diff

Difference between new and old positions (only if type == RE_ET_CHANGED)

struct rotary_encoder_config_t
#include <encoder.h>

Per-encoder configuration.

Public Members

gpio_num_t pin_a

Encoder pin A.

gpio_num_t pin_b

Encoder pin B.

gpio_num_t pin_btn

Button pin, or GPIO_NUM_NC if unused.

uint8_t btn_pressed_level

GPIO level when button is pressed (0 or 1)

bool enable_internal_pullup

Enable internal pull-up/pull-down resistors on GPIO pins.

uint32_t btn_dead_time_us

Button dead time in microseconds.

uint32_t btn_long_press_time_us

Long press threshold in microseconds.

uint32_t acceleration_threshold_ms

Acceleration threshold in milliseconds (acceleration starts below this interval)

uint32_t acceleration_cap_ms

Acceleration cap in milliseconds (minimum interval, limits max acceleration)

uint32_t polling_interval_us

Polling interval in microseconds.

rotary_encoder_event_cb_t callback

Event callback (required)

void *callback_ctx

User context passed to callback.