From 635916737bdce1345b30c0cd0a35e20563ea351c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Klomp?= Date: Wed, 9 Jun 2021 06:48:51 +0200 Subject: [PATCH] Update total_pulses_ at every detected pulse (#1875) --- .../pulse_meter/pulse_meter_sensor.cpp | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/esphome/components/pulse_meter/pulse_meter_sensor.cpp b/esphome/components/pulse_meter/pulse_meter_sensor.cpp index 7728930ec1..539da3a2aa 100644 --- a/esphome/components/pulse_meter/pulse_meter_sensor.cpp +++ b/esphome/components/pulse_meter/pulse_meter_sensor.cpp @@ -23,7 +23,6 @@ void PulseMeterSensor::loop() { 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_)) { 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->pulse_width_us_ = 0; } @@ -68,18 +67,15 @@ void ICACHE_RAM_ATTR PulseMeterSensor::gpio_intr(PulseMeterSensor *sensor) { return; } - // Ignore the first detected pulse (we need at least two pulses to measure the width) - if (sensor->last_detected_edge_us_ != 0) { - // Check to see if we should filter this edge out - if ((now - sensor->last_detected_edge_us_) >= sensor->filter_us_) { - // Don't measure the first valid pulse (we need at least two pulses to measure the width) - 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; + // Check to see if we should filter this edge out + if ((now - sensor->last_detected_edge_us_) >= sensor->filter_us_) { + // Don't measure the first valid pulse (we need at least two pulses to measure the width) + 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->last_detected_edge_us_ = now;