33// License as published by the Free Software Foundation; either
44// version 2.1 of the License, or (at your option) any later version.
55
6- #include " DallasTemperature.h"
7-
86// for Particle support
97// yield() is not a standard function, but instead wraps Particle process
108// https://community.particle.io/t/syscall-yield-operation/40708/2
@@ -20,6 +18,8 @@ extern "C" {
2018}
2119#endif
2220
21+ #include " DallasTemperature.h"
22+
2323// OneWire commands
2424#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
2525#define COPYSCRATCH 0x48 // Copy scratchpad to EEPROM
@@ -152,19 +152,19 @@ bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
152152// finds an address at a given index on the bus
153153// returns true if the device was found
154154bool DallasTemperature::getAddress (uint8_t * deviceAddress, uint8_t index) {
155+ if (index < devices) {
156+ uint8_t depth = 0 ;
155157
156- uint8_t depth = 0 ;
157-
158- _wire->reset_search ();
158+ _wire->reset_search ();
159159
160- while (depth <= index && _wire->search (deviceAddress)) {
161- if (depth == index && validAddress (deviceAddress))
162- return true ;
163- depth++;
160+ while (depth <= index && _wire->search (deviceAddress)) {
161+ if (depth == index && validAddress (deviceAddress))
162+ return true ;
163+ depth++;
164+ }
164165 }
165166
166167 return false ;
167-
168168}
169169
170170// attempt to determine if the device at the given address is connected to the bus
@@ -723,22 +723,27 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
723723// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
724724// DallasTemperature.h. It is a large negative number outside the
725725// operating range of the device
726- int32_t DallasTemperature::getTemp (const uint8_t * deviceAddress) {
726+ int32_t DallasTemperature::getTemp (const uint8_t * deviceAddress, byte retryCount = 0 ) {
727727
728728 ScratchPad scratchPad;
729- if (isConnected (deviceAddress, scratchPad))
730- return calculateTemperature (deviceAddress, scratchPad);
731- return DEVICE_DISCONNECTED_RAW;
732-
729+
730+ byte retries = 0 ;
731+
732+ while (retries++ <= retryCount) {
733+ if (isConnected (deviceAddress, scratchPad))
734+ return calculateTemperature (deviceAddress, scratchPad);
735+ }
736+
737+ return DEVICE_DISCONNECTED_RAW;
733738}
734739
735740// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
736741// device's scratch pad cannot be read successfully.
737742// the numeric value of DEVICE_DISCONNECTED_C is defined in
738743// DallasTemperature.h. It is a large negative number outside the
739744// operating range of the device
740- float DallasTemperature::getTempC (const uint8_t * deviceAddress) {
741- return rawToCelsius (getTemp (deviceAddress));
745+ float DallasTemperature::getTempC (const uint8_t * deviceAddress, byte retryCount = 0 ) {
746+ return rawToCelsius (getTemp (deviceAddress, retryCount ));
742747}
743748
744749// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
0 commit comments