Add state listeners to rotary_encoder (#6035)

This commit is contained in:
Clyde Stubbs 2024-03-11 18:13:41 +11:00 committed by GitHub
parent 501973e07b
commit dfb14fc6ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 2 deletions

View file

@ -226,6 +226,7 @@ void RotaryEncoderSensor::loop() {
} }
this->store_.last_read = counter; this->store_.last_read = counter;
this->publish_state(counter); this->publish_state(counter);
this->listeners_.call(counter);
this->publish_initial_value_ = false; this->publish_initial_value_ = false;
} }
} }

View file

@ -92,6 +92,8 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
this->on_anticlockwise_callback_.add(std::move(callback)); this->on_anticlockwise_callback_.add(std::move(callback));
} }
void register_listener(std::function<void(uint32_t)> listener) { this->listeners_.add(std::move(listener)); }
protected: protected:
InternalGPIOPin *pin_a_; InternalGPIOPin *pin_a_;
InternalGPIOPin *pin_b_; InternalGPIOPin *pin_b_;
@ -102,8 +104,9 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
RotaryEncoderSensorStore store_{}; RotaryEncoderSensorStore store_{};
CallbackManager<void()> on_clockwise_callback_; CallbackManager<void()> on_clockwise_callback_{};
CallbackManager<void()> on_anticlockwise_callback_; CallbackManager<void()> on_anticlockwise_callback_{};
CallbackManager<void(int32_t)> listeners_{};
}; };
template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> { template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> {