ESPHome  2024.4.1
rotary_encoder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
6 #include "esphome/core/hal.h"
9 
10 namespace esphome {
11 namespace rotary_encoder {
12 
17 };
18 
22  0x4400,
25 };
26 
30 
31  volatile int32_t counter{0};
33  int32_t min_value{INT32_MIN};
34  int32_t max_value{INT32_MAX};
35  int32_t last_read{0};
36  uint8_t state{0};
37  bool first_read{true};
38 
39  std::array<int8_t, 8> rotation_events{};
41 
42  static void gpio_intr(RotaryEncoderSensorStore *arg);
43 };
44 
46  public:
47  void set_pin_a(InternalGPIOPin *pin_a) { pin_a_ = pin_a; }
48  void set_pin_b(InternalGPIOPin *pin_b) { pin_b_ = pin_b; }
49 
57  void set_restore_mode(RotaryEncoderRestoreMode restore_mode);
58 
66  void set_resolution(RotaryEncoderResolution mode);
67 
69  void set_value(int value) {
70  this->store_.counter = value;
71  this->loop();
72  }
73 
74  void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
75  void set_min_value(int32_t min_value);
76  void set_max_value(int32_t max_value);
77  void set_publish_initial_value(bool publish_initial_value) { publish_initial_value_ = publish_initial_value; }
78 
79  // ========== INTERNAL METHODS ==========
80  // (In most use cases you won't need these)
81  void setup() override;
82  void dump_config() override;
83  void loop() override;
84 
85  float get_setup_priority() const override;
86 
87  void add_on_clockwise_callback(std::function<void()> callback) {
88  this->on_clockwise_callback_.add(std::move(callback));
89  }
90 
91  void add_on_anticlockwise_callback(std::function<void()> callback) {
92  this->on_anticlockwise_callback_.add(std::move(callback));
93  }
94 
95  void register_listener(std::function<void(uint32_t)> listener) { this->listeners_.add(std::move(listener)); }
96 
97  protected:
100  GPIOPin *pin_i_{nullptr};
104 
106 
107  CallbackManager<void()> on_clockwise_callback_{};
108  CallbackManager<void()> on_anticlockwise_callback_{};
110 };
111 
112 template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> {
113  public:
114  RotaryEncoderSetValueAction(RotaryEncoderSensor *encoder) : encoder_(encoder) {}
115  TEMPLATABLE_VALUE(int, value)
116 
117  void play(Ts... x) override { this->encoder_->set_value(this->value_.value(x...)); }
118 
119  protected:
121 };
122 
124  public:
126  parent->add_on_clockwise_callback([this]() { this->trigger(); });
127  }
128 };
129 
131  public:
133  parent->add_on_anticlockwise_callback([this]() { this->trigger(); });
134  }
135 };
136 
137 } // namespace rotary_encoder
138 } // namespace esphome
void setup()
RotaryEncoderResolution
All possible resolutions for the rotary encoder.
void loop()
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
RotaryEncoderClockwiseTrigger(RotaryEncoderSensor *parent)
void register_listener(std::function< void(uint32_t)> listener)
uint16_t x
Definition: tt21100.cpp:17
RotaryEncoderSetValueAction(RotaryEncoderSensor *encoder)
increment counter by 2 with every A-B cycle
static void gpio_intr(RotaryEncoderSensorStore *arg)
RotaryEncoderRestoreMode
All possible restore modes for the rotary encoder.
try to restore counter, otherwise set to zero
TEMPLATABLE_VALUE(int, value) void play(Ts... x) override
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
void add_on_clockwise_callback(std::function< void()> callback)
increment counter by 1 with every A-B cycle, slow response but accurate
void set_pin_b(InternalGPIOPin *pin_b)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool publish_initial_value_
Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
void set_value(int value)
Manually set the value of the counter.
Base-class for all sensors.
Definition: sensor.h:57
void add_on_anticlockwise_callback(std::function< void()> callback)
void set_pin_a(InternalGPIOPin *pin_a)
void set_publish_initial_value(bool publish_initial_value)