From cd435d16fdd9f2f5eb83ce60cd180064f93c6463 Mon Sep 17 00:00:00 2001 From: kimi1991 <69465290+kimi1991@users.noreply.github.com> Date: Thu, 24 Sep 2020 21:39:43 +0200 Subject: [PATCH 1/3] Fixed bug #75 Fixed bug #75, added timeout to "takeForcedMeasurement" and a return value to check the result of the function --- Adafruit_BME280.cpp | 19 ++++++++++++++++--- Adafruit_BME280.h | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 6d58509..3be4c81 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -343,20 +343,33 @@ uint32_t Adafruit_BME280::read24(byte reg) { /*! * @brief Take a new measurement (only possible in forced mode) + @returns true in case of success else false */ -void Adafruit_BME280::takeForcedMeasurement() { +bool Adafruit_BME280::takeForcedMeasurement() { + bool return_value = false; // If we are in forced mode, the BME sensor goes back to sleep after each // measurement and we need to set it to forced mode once at this point, so // it will take the next measurement and then return to sleep again. // In normal mode simply does new measurements periodically. if (_measReg.mode == MODE_FORCED) { + return_value = true; // set to forced mode, i.e. "take next measurement" write8(BME280_REGISTER_CONTROL, _measReg.get()); + // Store current time to measure the timeout + uint32_t timeout_start = millis(); // wait until measurement has been completed, otherwise we would read - // the values from the last measurement - while (read8(BME280_REGISTER_STATUS) & 0x08) + // the values from the last measurement or the timeout occurred after 2 sec. + + while (read8(BME280_REGISTER_STATUS) & 0x08) { + // In case of a timeout, stop the while loop + if((millis() - timeout_start) > 2000) { + return_value = false; + break; + } delay(1); + } } + return return_value; } /*! diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index eec27da..a7271bc 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -225,7 +225,7 @@ public: sensor_filter filter = FILTER_OFF, standby_duration duration = STANDBY_MS_0_5); - void takeForcedMeasurement(); + bool takeForcedMeasurement(); float readTemperature(void); float readPressure(void); float readHumidity(void); From 4bce9380d6da642f8650e4fd2711730935976379 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 30 Sep 2020 08:18:37 +0200 Subject: [PATCH 2/3] Changed to clang format Replace tabs with whitespaces. Changed indentation of function "takeForcedMeasurement" --- Adafruit_BME280.cpp | 25 ++++++++++++------------- Adafruit_BME280.h | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 3be4c81..1332007 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -343,31 +343,30 @@ uint32_t Adafruit_BME280::read24(byte reg) { /*! * @brief Take a new measurement (only possible in forced mode) - @returns true in case of success else false + @returns true in case of success else false */ -bool Adafruit_BME280::takeForcedMeasurement() { +bool Adafruit_BME280::takeForcedMeasurement(void) { bool return_value = false; // If we are in forced mode, the BME sensor goes back to sleep after each // measurement and we need to set it to forced mode once at this point, so // it will take the next measurement and then return to sleep again. // In normal mode simply does new measurements periodically. if (_measReg.mode == MODE_FORCED) { - return_value = true; + return_value = true; // set to forced mode, i.e. "take next measurement" write8(BME280_REGISTER_CONTROL, _measReg.get()); - // Store current time to measure the timeout - uint32_t timeout_start = millis(); - // wait until measurement has been completed, otherwise we would read + // Store current time to measure the timeout + uint32_t timeout_start = millis(); + // wait until measurement has been completed, otherwise we would read the // the values from the last measurement or the timeout occurred after 2 sec. - while (read8(BME280_REGISTER_STATUS) & 0x08) { - // In case of a timeout, stop the while loop - if((millis() - timeout_start) > 2000) { - return_value = false; - break; - } + // In case of a timeout, stop the while loop + if((millis() - timeout_start) > 2000) { + return_value = false; + break; + } delay(1); - } + } } return return_value; } diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index a7271bc..921a996 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -225,7 +225,7 @@ public: sensor_filter filter = FILTER_OFF, standby_duration duration = STANDBY_MS_0_5); - bool takeForcedMeasurement(); + bool takeForcedMeasurement(void); float readTemperature(void); float readPressure(void); float readHumidity(void); From d492906b288c3ad4ca22e957911ea240db239f0a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 30 Sep 2020 08:30:41 +0200 Subject: [PATCH 3/3] Changed to clang format Added whitespace --- Adafruit_BME280.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 1332007..f040743 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -361,7 +361,7 @@ bool Adafruit_BME280::takeForcedMeasurement(void) { // the values from the last measurement or the timeout occurred after 2 sec. while (read8(BME280_REGISTER_STATUS) & 0x08) { // In case of a timeout, stop the while loop - if((millis() - timeout_start) > 2000) { + if ((millis() - timeout_start) > 2000) { return_value = false; break; }