Skip to content

File syn_encoder.h

FileList > input > syn_encoder.h

Go to the source code of this file

Quadrature rotary encoder decoder. More...

  • #include "../common/syn_defs.h"
  • #include "../drivers/syn_gpio.h"
  • #include "../dsp/syn_signal.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_Encoder
Quadrature encoder instance — pins, position, delta.

Public Types

Type Name
enum SYN_EncoderDir
Encoder rotation direction.

Public Functions

Type Name
int32_t syn_encoder_get_delta (SYN_Encoder * enc)
Get accumulated delta since last call and reset it.
void syn_encoder_init (SYN_Encoder * enc, SYN_GPIO_Pin pin_a, SYN_GPIO_Pin pin_b)
Initialize encoder.
void syn_encoder_set_stats (SYN_Encoder * enc, SYN_Signal * stats)
Attach a signal statistics window for velocity tracking.
void syn_encoder_set_steps_per_detent (SYN_Encoder * enc, uint8_t spd)
Set steps-per-detent (default = 1).
void syn_encoder_update (SYN_Encoder * enc)
Sample pins and update position.

Public Static Functions

Type Name
SYN_EncoderDir syn_encoder_direction (const SYN_Encoder * enc)
Get last direction.
int32_t syn_encoder_position (const SYN_Encoder * enc)
Get absolute position.
void syn_encoder_set_position (SYN_Encoder * enc, int32_t pos)
Set absolute position.

Detailed Description

Decodes A/B quadrature signals from rotary encoders. Supports: * Direction detection (CW/CCW) * Accumulated position * Delta since last read * Optional push-button (use syn_button for debouncing)

Call syn_encoder_update() from a timer ISR or fast loop (≥4× the maximum encoder frequency).

** **

SYN_Encoder enc;
syn_encoder_init(&enc, PIN_A, PIN_B);

// In timer ISR or fast loop:
syn_encoder_update(&enc);

// In main loop:
int32_t delta = syn_encoder_get_delta(&enc);
if (delta != 0) {
    volume += delta;
}

Public Types Documentation

enum SYN_EncoderDir

Encoder rotation direction.

enum SYN_EncoderDir {
    SYN_ENCODER_NONE = 0,
    SYN_ENCODER_CW = 1,
    SYN_ENCODER_CCW = -1
};


Public Functions Documentation

function syn_encoder_get_delta

Get accumulated delta since last call and reset it.

int32_t syn_encoder_get_delta (
    SYN_Encoder * enc
) 

Parameters:

  • enc Encoder instance.

Returns:

Number of steps (positive = CW, negative = CCW).


function syn_encoder_init

Initialize encoder.

void syn_encoder_init (
    SYN_Encoder * enc,
    SYN_GPIO_Pin pin_a,
    SYN_GPIO_Pin pin_b
) 

Parameters:

  • enc Encoder instance.
  • pin_a Channel A GPIO.
  • pin_b Channel B GPIO.

function syn_encoder_set_stats

Attach a signal statistics window for velocity tracking.

void syn_encoder_set_stats (
    SYN_Encoder * enc,
    SYN_Signal * stats
) 

Each call to syn_encoder_get_delta() pushes the delta value into the signal window, giving you velocity min/max/mean/variance.

Parameters:

  • enc Encoder.
  • stats Initialized SYN_Signal, or NULL to detach.

function syn_encoder_set_steps_per_detent

Set steps-per-detent (default = 1).

void syn_encoder_set_steps_per_detent (
    SYN_Encoder * enc,
    uint8_t spd
) 

Many mechanical encoders generate 2 or 4 state changes per physical detent. Setting this to 4 means position and delta only increment once per detent rather than once per state change.

Parameters:

  • enc Encoder instance.
  • spd Steps per detent (1, 2, or 4).

function syn_encoder_update

Sample pins and update position.

void syn_encoder_update (
    SYN_Encoder * enc
) 

Call from a timer ISR or fast polling loop. Must be called at ≥4× the encoder's maximum rotation speed.

Parameters:

  • enc Encoder instance.

Public Static Functions Documentation

function syn_encoder_direction

Get last direction.

static inline SYN_EncoderDir syn_encoder_direction (
    const SYN_Encoder * enc
) 

Parameters:

  • enc Encoder.

Returns:

Last detected direction.


function syn_encoder_position

Get absolute position.

static inline int32_t syn_encoder_position (
    const SYN_Encoder * enc
) 

Parameters:

  • enc Encoder.

Returns:

Accumulated position.


function syn_encoder_set_position

Set absolute position.

static inline void syn_encoder_set_position (
    SYN_Encoder * enc,
    int32_t pos
) 

Parameters:

  • enc Encoder.
  • pos New position value.


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