Add restore_mode to rotary_encoder (#2643)

This commit is contained in:
niklasweber
2021-11-02 19:32:24 +01:00
committed by GitHub
parent 5ea77894b7
commit 379c3e98f5
3 changed files with 63 additions and 0 deletions
@@ -10,6 +10,12 @@
namespace esphome {
namespace rotary_encoder {
/// All possible restore modes for the rotary encoder
enum RotaryEncoderRestoreMode {
ROTARY_ENCODER_RESTORE_DEFAULT_ZERO, /// try to restore counter, otherwise set to zero
ROTARY_ENCODER_ALWAYS_ZERO, /// do not restore counter, always set to zero
};
/// All possible resolutions for the rotary encoder
enum RotaryEncoderResolution {
ROTARY_ENCODER_1_PULSE_PER_CYCLE =
@@ -40,6 +46,15 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
void set_pin_a(InternalGPIOPin *pin_a) { pin_a_ = pin_a; }
void set_pin_b(InternalGPIOPin *pin_b) { pin_b_ = pin_b; }
/** Set the restore mode of the rotary encoder.
*
* By default (if possible) the last known counter state is restored. Otherwise the value 0 is used.
* Restoring the state can also be turned off.
*
* @param restore_mode The restore mode to use.
*/
void set_restore_mode(RotaryEncoderRestoreMode restore_mode);
/** Set the resolution of the rotary encoder.
*
* By default, this component will increment the counter by 1 with every A-B input cycle.
@@ -81,6 +96,8 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
InternalGPIOPin *pin_b_;
GPIOPin *pin_i_{nullptr}; /// Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
bool publish_initial_value_;
ESPPreferenceObject rtc_;
RotaryEncoderRestoreMode restore_mode_{ROTARY_ENCODER_RESTORE_DEFAULT_ZERO};
RotaryEncoderSensorStore store_{};