From 16445c515850ad09ad9f8e2a919a450e264a040e Mon Sep 17 00:00:00 2001 From: mitchyboy9 Date: Wed, 29 May 2024 12:38:26 +0100 Subject: [PATCH] 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*);