From 37a3984ce828214246f02a243d83c502201d1845 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Jun 2024 17:25:07 +0200 Subject: [PATCH 1/2] ModulinoDistance: get() returns previous reading, add available() for validity --- src/Modulino.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Modulino.h b/src/Modulino.h index 3bd53fc..8046fd1 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -406,6 +406,7 @@ class ModulinoDistance : public Module { if (tof_sensor == nullptr) { return NAN; } + float ret = internal; uint8_t NewDataReady = 0; uint8_t status = tof_sensor->VL53L4CD_CheckForDataReady(&NewDataReady); if (NewDataReady) { @@ -413,15 +414,17 @@ class ModulinoDistance : public Module { tof_sensor->VL53L4CD_GetResult(&results); } if (results.range_status == 0) { - return results.distance_mm; + internal = results.distance_mm; } else { - return NAN; + internal = NAN; } + return ret; } - bool isValid(float distance) { - return !isnan(distance); + bool available() { + return !isnan(internal); } private: VL53L4CD* tof_sensor = nullptr; VL53L4CD_Result_t results; + float internal; }; \ No newline at end of file From dc4478acb4f63dc62a88a6f731277a2f6162608e Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Jun 2024 17:46:40 +0200 Subject: [PATCH 2/2] ModulinoDistance: reverse get() and available() --- src/Modulino.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Modulino.h b/src/Modulino.h index 8046fd1..badef09 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -402,9 +402,9 @@ class ModulinoDistance : public Module { operator bool() { return (tof_sensor != nullptr); } - float get() { + bool available() { if (tof_sensor == nullptr) { - return NAN; + return false; } float ret = internal; uint8_t NewDataReady = 0; @@ -418,13 +418,13 @@ class ModulinoDistance : public Module { } else { internal = NAN; } - return ret; - } - bool available() { return !isnan(internal); } + float get() { + return internal; + } private: VL53L4CD* tof_sensor = nullptr; VL53L4CD_Result_t results; - float internal; + float internal = NAN; }; \ No newline at end of file