Skip to content

File syn_encoder.c

FileList > input > syn_encoder.c

Go to the source code of this file

Quadrature rotary encoder implementation. More...

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

Public Static Attributes

Type Name
const int8_t quad_table = /* multi line expression */
Quadrature state transition table.

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.

Detailed Description

Uses a 2-bit state machine with a lookup table for direction decoding. Handles all four quadrature transitions correctly with no missed steps.

Public Static Attributes Documentation

variable quad_table

Quadrature state transition table.

const int8_t quad_table[16];

16 entries: (old_state << 2) | new_state. Index = (old_A<<3 | old_B<<2 | new_A<<1 | new_B). Value: +1 for clockwise, -1 for counter-clockwise, 0 for invalid/no-change.


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.


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