Rotary Encoder: Don't call callbacks in the isr (#1456)

This commit is contained in:
mknjc
2021-01-10 20:05:53 +01:00
committed by Jesse Hills
parent c67539cf5b
commit 72002ce70e
2 changed files with 58 additions and 6 deletions
@@ -1,5 +1,7 @@
#pragma once
#include <array>
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/core/automation.h"
@@ -27,8 +29,8 @@ struct RotaryEncoderSensorStore {
int32_t last_read{0};
uint8_t state{0};
CallbackManager<void()> on_clockwise_callback_;
CallbackManager<void()> on_anticlockwise_callback_;
std::array<int8_t, 8> rotation_events{};
bool rotation_events_overflow{false};
static void gpio_intr(RotaryEncoderSensorStore *arg);
};
@@ -66,11 +68,11 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
float get_setup_priority() const override;
void add_on_clockwise_callback(std::function<void()> callback) {
this->store_.on_clockwise_callback_.add(std::move(callback));
this->on_clockwise_callback_.add(std::move(callback));
}
void add_on_anticlockwise_callback(std::function<void()> callback) {
this->store_.on_anticlockwise_callback_.add(std::move(callback));
this->on_anticlockwise_callback_.add(std::move(callback));
}
protected:
@@ -79,6 +81,9 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
GPIOPin *pin_i_{nullptr}; /// Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
RotaryEncoderSensorStore store_{};
CallbackManager<void()> on_clockwise_callback_;
CallbackManager<void()> on_anticlockwise_callback_;
};
template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> {