Update total_pulses_ at every detected pulse (#1875)

This commit is contained in:
René Klomp 2021-06-09 06:48:51 +02:00 committed by GitHub
parent 65c50e4f01
commit 635916737b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,7 +23,6 @@ void PulseMeterSensor::loop() {
const uint32_t time_since_valid_edge_us = now - this->last_valid_edge_us_; const uint32_t time_since_valid_edge_us = now - this->last_valid_edge_us_;
if ((this->last_valid_edge_us_ != 0) && (time_since_valid_edge_us > this->timeout_us_)) { if ((this->last_valid_edge_us_ != 0) && (time_since_valid_edge_us > this->timeout_us_)) {
ESP_LOGD(TAG, "No pulse detected for %us, assuming 0 pulses/min", time_since_valid_edge_us / 1000000); ESP_LOGD(TAG, "No pulse detected for %us, assuming 0 pulses/min", time_since_valid_edge_us / 1000000);
this->last_detected_edge_us_ = 0;
this->last_valid_edge_us_ = 0; this->last_valid_edge_us_ = 0;
this->pulse_width_us_ = 0; this->pulse_width_us_ = 0;
} }
@ -68,18 +67,15 @@ void ICACHE_RAM_ATTR PulseMeterSensor::gpio_intr(PulseMeterSensor *sensor) {
return; return;
} }
// Ignore the first detected pulse (we need at least two pulses to measure the width) // Check to see if we should filter this edge out
if (sensor->last_detected_edge_us_ != 0) { if ((now - sensor->last_detected_edge_us_) >= sensor->filter_us_) {
// Check to see if we should filter this edge out // Don't measure the first valid pulse (we need at least two pulses to measure the width)
if ((now - sensor->last_detected_edge_us_) >= sensor->filter_us_) { if (sensor->last_valid_edge_us_ != 0) {
// Don't measure the first valid pulse (we need at least two pulses to measure the width) sensor->pulse_width_us_ = (now - sensor->last_valid_edge_us_);
if (sensor->last_valid_edge_us_ != 0) {
sensor->pulse_width_us_ = (now - sensor->last_valid_edge_us_);
}
sensor->total_pulses_++;
sensor->last_valid_edge_us_ = now;
} }
sensor->total_pulses_++;
sensor->last_valid_edge_us_ = now;
} }
sensor->last_detected_edge_us_ = now; sensor->last_detected_edge_us_ = now;