Skip to content

Commit

Permalink
Add rotary encoder min/max value (esphome#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter authored Feb 28, 2019
1 parent 1e91e7b commit 6088a6d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/esphome/sensor/rotary_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ void ICACHE_RAM_ATTR RotaryEncoderSensor::process_state_machine_() {
bool counter_change = false;

if ((this->state_ & QEIx4_IS_INC) != 0) {
this->counter_++;
this->counter_ = std::min(this->counter_ + 1, this->max_value_);
counter_change = true;
}
if ((this->state_ & QEIx4_IS_DEC) != 0) {
this->counter_--;
this->counter_ = std::max(this->counter_ - 1, this->min_value_);
counter_change = true;
}

Expand Down Expand Up @@ -164,6 +164,8 @@ std::vector<RotaryEncoderSensor *> global_rotary_encoders_;
float RotaryEncoderSensor::get_setup_priority() const {
return setup_priority::HARDWARE_LATE;
}
void RotaryEncoderSensor::set_min_value(int32_t min_value) { this->min_value_ = min_value; }
void RotaryEncoderSensor::set_max_value(int32_t max_value) { this->max_value_ = max_value; }

} // namespace sensor

Expand Down
4 changes: 4 additions & 0 deletions src/esphome/sensor/rotary_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class RotaryEncoderSensor : public Sensor, public Component {
void set_resolution(RotaryEncoderResolution mode);

void set_reset_pin(const GPIOInputPin &pin_i);
void set_min_value(int32_t min_value);
void set_max_value(int32_t max_value);

// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
Expand Down Expand Up @@ -62,6 +64,8 @@ class RotaryEncoderSensor : public Sensor, public Component {
volatile bool has_changed_{true};
uint16_t state_{0};
RotaryEncoderResolution resolution_{ROTARY_ENCODER_1_PULSE_PER_CYCLE};
int32_t min_value_{INT32_MIN};
int32_t max_value_{INT32_MAX};
};

/// Global storage for having multiple rotary encoders with a single ISR
Expand Down

0 comments on commit 6088a6d

Please sign in to comment.