From e39e8ab4748b10f293d39d510326cb01f2d03a01 Mon Sep 17 00:00:00 2001 From: mitchyboy9 <11429542+mitchyboy9@users.noreply.github.com> Date: Sun, 19 May 2024 00:32:07 +0100 Subject: [PATCH 1/2] Only updates the supplied deviceAddress if a valid index is specified --- DallasTemperature.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/DallasTemperature.cpp b/DallasTemperature.cpp index f6374ca..1201735 100644 --- a/DallasTemperature.cpp +++ b/DallasTemperature.cpp @@ -152,19 +152,19 @@ bool DallasTemperature::validAddress(const uint8_t* deviceAddress) { // finds an address at a given index on the bus // returns true if the device was found bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) { + if (index < devices) { + uint8_t depth = 0; - uint8_t depth = 0; + _wire->reset_search(); - _wire->reset_search(); - - while (depth <= index && _wire->search(deviceAddress)) { - if (depth == index && validAddress(deviceAddress)) - return true; - depth++; + while (depth <= index && _wire->search(deviceAddress)) { + if (depth == index && validAddress(deviceAddress)) + return true; + depth++; + } } return false; - } // attempt to determine if the device at the given address is connected to the bus From 16445c515850ad09ad9f8e2a919a450e264a040e Mon Sep 17 00:00:00 2001 From: mitchyboy9 Date: Wed, 29 May 2024 12:38:26 +0100 Subject: [PATCH 2/2] Add retry for getTempC. I have only implemented this for getTempC because that's the method I use in project (i.e. not getTempF). --- DallasTemperature.cpp | 19 ++++++++++++------- DallasTemperature.h | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/DallasTemperature.cpp b/DallasTemperature.cpp index 1201735..15a4a00 100644 --- a/DallasTemperature.cpp +++ b/DallasTemperature.cpp @@ -723,13 +723,18 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, // the numeric value of DEVICE_DISCONNECTED_RAW is defined in // DallasTemperature.h. It is a large negative number outside the // operating range of the device -int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) { +int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress, byte retryCount = 0) { ScratchPad scratchPad; - if (isConnected(deviceAddress, scratchPad)) - return calculateTemperature(deviceAddress, scratchPad); - return DEVICE_DISCONNECTED_RAW; - + + byte retries = 0; + + while (retries++ <= retryCount) { + if (isConnected(deviceAddress, scratchPad)) + return calculateTemperature(deviceAddress, scratchPad); + } + + return DEVICE_DISCONNECTED_RAW; } // returns temperature in degrees C or DEVICE_DISCONNECTED_C if the @@ -737,8 +742,8 @@ int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) { // the numeric value of DEVICE_DISCONNECTED_C is defined in // DallasTemperature.h. It is a large negative number outside the // operating range of the device -float DallasTemperature::getTempC(const uint8_t* deviceAddress) { - return rawToCelsius(getTemp(deviceAddress)); +float DallasTemperature::getTempC(const uint8_t* deviceAddress, byte retryCount = 0) { + return rawToCelsius(getTemp(deviceAddress, retryCount)); } // returns temperature in degrees F or DEVICE_DISCONNECTED_F if the diff --git a/DallasTemperature.h b/DallasTemperature.h index 068b46c..4fff3b0 100644 --- a/DallasTemperature.h +++ b/DallasTemperature.h @@ -159,10 +159,10 @@ class DallasTemperature { request_t requestTemperaturesByIndex(uint8_t); // returns temperature raw value (12 bit integer of 1/128 degrees C) - int32_t getTemp(const uint8_t*); + int32_t getTemp(const uint8_t*, byte retryCount = 0); // returns temperature in degrees C - float getTempC(const uint8_t*); + float getTempC(const uint8_t*, byte retryCount = 0); // returns temperature in degrees F float getTempF(const uint8_t*);