From 469c0b32d147bf2ff6fcdbf40b1365fe6b99369e Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Sun, 26 Dec 2021 11:56:25 +0100 Subject: [PATCH 1/4] Return proper values --- components/roode/roode.cpp | 18 +++++++++++------- components/roode/roode.h | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/components/roode/roode.cpp b/components/roode/roode.cpp index 80caea8d..50493f2d 100644 --- a/components/roode/roode.cpp +++ b/components/roode/roode.cpp @@ -88,23 +88,24 @@ namespace esphome // ESP_LOGI("Roode loop", "loop took %lu microseconds", delta); } - void Roode::handleSensorStatus() + bool Roode::handleSensorStatus() { statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable + ESP_LOGD(TAG, "Sensor status: %d, Last sensor status: %d", sensor_status, last_sensor_status); + if (last_sensor_status == sensor_status && sensor_status == VL53L1X::RangeStatus::RangeValid) { + if (status_sensor != nullptr) { status_sensor->publish_state(statusString); } + return true; } if (sensor_status != VL53L1X::RangeStatus::RangeValid && sensor_status != VL53L1X::RangeStatus::SignalFail && sensor_status != VL53L1X::RangeStatus::WrapTargetFail) { ESP_LOGE(TAG, "Ranging failed with an error. status: %d, error: %s", sensor_status, statusString); - if (status_sensor != nullptr) - { - status_sensor->publish_state(statusString); - } - return; + + return false; } } void Roode::getZoneDistance() @@ -123,7 +124,10 @@ namespace esphome distance = distanceSensor.read(); distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80); // stop reading sensor_status = distanceSensor.ranging_data.range_status; - handleSensorStatus(); + if (!handleSensorStatus()) + { + return; + } if (use_sampling_) { diff --git a/components/roode/roode.h b/components/roode/roode.h index c79b17d2..f9ccfcdd 100644 --- a/components/roode/roode.h +++ b/components/roode/roode.h @@ -86,7 +86,7 @@ namespace esphome void getZoneDistance(); void sendCounter(uint16_t counter); void recalibration(); - void handleSensorStatus(); + bool handleSensorStatus(); uint16_t distance = 0; VL53L1X::RangeStatus last_sensor_status = VL53L1X::RangeStatus::None; From 42da4588609c441cb87fec925efa9e96ca80de81 Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Sun, 26 Dec 2021 12:20:21 +0100 Subject: [PATCH 2/4] Fix string assignment --- components/roode/roode.cpp | 2 +- components/roode/roode.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/components/roode/roode.cpp b/components/roode/roode.cpp index 50493f2d..954f62ae 100644 --- a/components/roode/roode.cpp +++ b/components/roode/roode.cpp @@ -90,7 +90,7 @@ namespace esphome bool Roode::handleSensorStatus() { - statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable + static const char *statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable ESP_LOGD(TAG, "Sensor status: %d, Last sensor status: %d", sensor_status, last_sensor_status); if (last_sensor_status == sensor_status && sensor_status == VL53L1X::RangeStatus::RangeValid) diff --git a/components/roode/roode.h b/components/roode/roode.h index f9ccfcdd..72187ad4 100644 --- a/components/roode/roode.h +++ b/components/roode/roode.h @@ -91,7 +91,6 @@ namespace esphome uint16_t distance = 0; VL53L1X::RangeStatus last_sensor_status = VL53L1X::RangeStatus::None; VL53L1X::RangeStatus sensor_status = VL53L1X::RangeStatus::None; - const char *statusString = ""; int DIST_THRESHOLD_MAX[2] = {0, 0}; // max treshold of the two zones int DIST_THRESHOLD_MIN[2] = {0, 0}; // min treshold of the two zones int roi_width_{6}; // width of the ROI From b432c90193333809e556cea42a725c8585aec561 Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Sun, 26 Dec 2021 12:41:50 +0100 Subject: [PATCH 3/4] Remove sensorStatusHandling temporary --- components/roode/roode.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/components/roode/roode.cpp b/components/roode/roode.cpp index 954f62ae..ad17b5dc 100644 --- a/components/roode/roode.cpp +++ b/components/roode/roode.cpp @@ -82,7 +82,7 @@ namespace esphome getZoneDistance(); zone++; zone = zone % 2; - // App.feed_wdt(); + App.feed_wdt(); // unsigned long end = micros(); // unsigned long delta = end - start; // ESP_LOGI("Roode loop", "loop took %lu microseconds", delta); @@ -107,7 +107,9 @@ namespace esphome return false; } + return true; } + void Roode::getZoneDistance() { static int PathTrack[] = {0, 0, 0, 0}; @@ -124,10 +126,10 @@ namespace esphome distance = distanceSensor.read(); distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80); // stop reading sensor_status = distanceSensor.ranging_data.range_status; - if (!handleSensorStatus()) - { - return; - } + // if (!handleSensorStatus()) + // { + // return; + // } if (use_sampling_) { From a10126f669f927570faca8275aa24e0c8952b378 Mon Sep 17 00:00:00 2001 From: Kai Bepperling Date: Sun, 26 Dec 2021 12:47:09 +0100 Subject: [PATCH 4/4] Fix range status comparison --- components/roode/roode.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/components/roode/roode.cpp b/components/roode/roode.cpp index ad17b5dc..7990c88f 100644 --- a/components/roode/roode.cpp +++ b/components/roode/roode.cpp @@ -90,10 +90,10 @@ namespace esphome bool Roode::handleSensorStatus() { - static const char *statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable + const char *statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable ESP_LOGD(TAG, "Sensor status: %d, Last sensor status: %d", sensor_status, last_sensor_status); - if (last_sensor_status == sensor_status && sensor_status == VL53L1X::RangeStatus::RangeValid) + if (last_sensor_status == sensor_status && sensor_status == 0) { if (status_sensor != nullptr) { @@ -101,10 +101,9 @@ namespace esphome } return true; } - if (sensor_status != VL53L1X::RangeStatus::RangeValid && sensor_status != VL53L1X::RangeStatus::SignalFail && sensor_status != VL53L1X::RangeStatus::WrapTargetFail) + if (sensor_status != 0 && sensor_status != 2 && sensor_status != 7) { ESP_LOGE(TAG, "Ranging failed with an error. status: %d, error: %s", sensor_status, statusString); - return false; } return true; @@ -126,10 +125,10 @@ namespace esphome distance = distanceSensor.read(); distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80); // stop reading sensor_status = distanceSensor.ranging_data.range_status; - // if (!handleSensorStatus()) - // { - // return; - // } + if (!handleSensorStatus()) + { + return; + } if (use_sampling_) {