Rotary Encoder Sensor

See Application::make_rotary_encoder_sensor().

API Reference

class RotaryEncoderSensor : public sensor::Sensor, public Component

Public Functions

RotaryEncoderSensor(const std::string &name, GPIOPin *pin_a, GPIOPin *pin_b)
void set_resolution(RotaryEncoderResolution mode)

Set the resolution of the rotary encoder.

By default, this component will increment the counter by 1 with every A-B input cycle. You can however change this behavior to have more coarse resolutions like 4 counter increases per A-B cycle.

  • mode: The new mode of the encoder.

void set_reset_pin(const GPIOInputPin &pin_i)
void setup()

Where the component’s initialization should happen.

Analogous to Arduino’s setup(). This method is guaranteed to only be called once. Defaults to doing nothing.

void dump_config()
void loop()

This method will be called repeatedly.

Analogous to Arduino’s loop(). setup() is guaranteed to be called before this. Defaults to doing nothing.

std::string unit_of_measurement()

Override this to set the Home Assistant unit of measurement for this sensor.

Return “” to disable this feature.

The icon of this sensor, for example “°C”.

std::string icon()

Override this to set the Home Assistant icon for this sensor.

Return “” to disable this feature.

The icon of this sensor, for example “mdi:battery”.

int8_t accuracy_decimals()

Return the accuracy in decimals for this sensor.

float get_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

The setup priority of this component

Protected Functions

void ICACHE_RAM_ATTR sensor::RotaryEncoderSensor::process_state_machine_()

Process the state machine state of this rotary encoder. Called from encoder_isr_.

Protected Attributes

GPIOPin *pin_a_
GPIOPin *pin_b_
GPIOPin *pin_i_ = {nullptr}
volatile int32_t counter_ = {0}

Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.

volatile bool has_changed_ = {true}

The internal counter for steps.

uint16_t state_ = {0}
RotaryEncoderResolution resolution_ = {ROTARY_ENCODER_1_PULSE_PER_CYCLE}

Protected Static Functions

void ICACHE_RAM_ATTR sensor::RotaryEncoderSensor::encoder_isr_()

The ISR that handles pushing all interrupts to process_state_machine_ of all rotary encoders.