mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 00:48:19 +01:00
Fix for noise in pulse_counter and duty_cycle components (#2646)
This commit is contained in:
parent
d8b3af3815
commit
d54b4e7c44
4 changed files with 22 additions and 17 deletions
|
@ -12,7 +12,6 @@ void DutyCycleSensor::setup() {
|
||||||
this->pin_->setup();
|
this->pin_->setup();
|
||||||
this->store_.pin = this->pin_->to_isr();
|
this->store_.pin = this->pin_->to_isr();
|
||||||
this->store_.last_level = this->pin_->digital_read();
|
this->store_.last_level = this->pin_->digital_read();
|
||||||
this->last_update_ = micros();
|
|
||||||
this->store_.last_interrupt = micros();
|
this->store_.last_interrupt = micros();
|
||||||
|
|
||||||
this->pin_->attach_interrupt(DutyCycleSensorStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);
|
this->pin_->attach_interrupt(DutyCycleSensorStore::gpio_intr, &this->store_, gpio::INTERRUPT_ANY_EDGE);
|
||||||
|
@ -24,19 +23,20 @@ void DutyCycleSensor::dump_config() {
|
||||||
}
|
}
|
||||||
void DutyCycleSensor::update() {
|
void DutyCycleSensor::update() {
|
||||||
const uint32_t now = micros();
|
const uint32_t now = micros();
|
||||||
const bool level = this->store_.last_level;
|
if (this->last_update_ != 0) {
|
||||||
const uint32_t last_interrupt = this->store_.last_interrupt;
|
const bool level = this->store_.last_level;
|
||||||
uint32_t on_time = this->store_.on_time;
|
const uint32_t last_interrupt = this->store_.last_interrupt;
|
||||||
|
uint32_t on_time = this->store_.on_time;
|
||||||
|
|
||||||
if (level)
|
if (level)
|
||||||
on_time += now - last_interrupt;
|
on_time += now - last_interrupt;
|
||||||
|
|
||||||
const float total_time = float(now - this->last_update_);
|
const float total_time = float(now - this->last_update_);
|
||||||
|
|
||||||
const float value = (on_time / total_time) * 100.0f;
|
|
||||||
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
|
|
||||||
this->publish_state(value);
|
|
||||||
|
|
||||||
|
const float value = (on_time / total_time) * 100.0f;
|
||||||
|
ESP_LOGD(TAG, "'%s' Got duty cycle=%.1f%%", this->get_name().c_str(), value);
|
||||||
|
this->publish_state(value);
|
||||||
|
}
|
||||||
this->store_.on_time = 0;
|
this->store_.on_time = 0;
|
||||||
this->store_.last_interrupt = now;
|
this->store_.last_interrupt = now;
|
||||||
this->last_update_ = now;
|
this->last_update_ = now;
|
||||||
|
|
|
@ -30,7 +30,7 @@ class DutyCycleSensor : public sensor::Sensor, public PollingComponent {
|
||||||
InternalGPIOPin *pin_;
|
InternalGPIOPin *pin_;
|
||||||
|
|
||||||
DutyCycleSensorStore store_{};
|
DutyCycleSensorStore store_{};
|
||||||
uint32_t last_update_;
|
uint32_t last_update_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace duty_cycle
|
} // namespace duty_cycle
|
||||||
|
|
|
@ -155,16 +155,20 @@ void PulseCounterSensor::dump_config() {
|
||||||
|
|
||||||
void PulseCounterSensor::update() {
|
void PulseCounterSensor::update() {
|
||||||
pulse_counter_t raw = this->storage_.read_raw_value();
|
pulse_counter_t raw = this->storage_.read_raw_value();
|
||||||
float value = (60000.0f * raw) / float(this->get_update_interval()); // per minute
|
uint32_t now = millis();
|
||||||
|
if (this->last_time_ != 0) {
|
||||||
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
|
uint32_t interval = now - this->last_time_;
|
||||||
this->publish_state(value);
|
float value = (60000.0f * raw) / float(interval); // per minute
|
||||||
|
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
|
||||||
|
this->publish_state(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (this->total_sensor_ != nullptr) {
|
if (this->total_sensor_ != nullptr) {
|
||||||
current_total_ += raw;
|
current_total_ += raw;
|
||||||
ESP_LOGD(TAG, "'%s': Total : %i pulses", this->get_name().c_str(), current_total_);
|
ESP_LOGD(TAG, "'%s': Total : %i pulses", this->get_name().c_str(), current_total_);
|
||||||
this->total_sensor_->publish_state(current_total_);
|
this->total_sensor_->publish_state(current_total_);
|
||||||
}
|
}
|
||||||
|
this->last_time_ = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace pulse_counter
|
} // namespace pulse_counter
|
||||||
|
|
|
@ -65,7 +65,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
|
||||||
protected:
|
protected:
|
||||||
InternalGPIOPin *pin_;
|
InternalGPIOPin *pin_;
|
||||||
PulseCounterStorage storage_;
|
PulseCounterStorage storage_;
|
||||||
uint32_t current_total_ = 0;
|
uint32_t last_time_{0};
|
||||||
|
uint32_t current_total_{0};
|
||||||
sensor::Sensor *total_sensor_;
|
sensor::Sensor *total_sensor_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue