Skip to content

Commit f0cf361

Browse files
authored
Merge pull request milesburton#13 from gitkomodo/master
Change timeout when blocking till conversion is complete
2 parents 10dc009 + 6c742a7 commit f0cf361

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

DallasTemperature.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ extern "C" {
1515

1616
// OneWire commands
1717
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
18-
#define COPYSCRATCH 0x48 // Copy EEPROM
19-
#define READSCRATCH 0xBE // Read EEPROM
20-
#define WRITESCRATCH 0x4E // Write to EEPROM
21-
#define RECALLSCRATCH 0xB8 // Reload from last known
18+
#define COPYSCRATCH 0x48 // Copy scratchpad to EEPROM
19+
#define READSCRATCH 0xBE // Read from scratchpad
20+
#define WRITESCRATCH 0x4E // Write to scratchpad
21+
#define RECALLSCRATCH 0xB8 // Recall from EEPROM to scratchpad
2222
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
2323
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
2424

@@ -41,20 +41,15 @@ extern "C" {
4141

4242
#define NO_ALARM_HANDLER ((AlarmHandler *)0)
4343

44-
DallasTemperature::DallasTemperature()
45-
{
44+
DallasTemperature::DallasTemperature() {
4645
#if REQUIRESALARMS
4746
setAlarmHandler(NO_ALARM_HANDLER);
4847
#endif
4948
useExternalPullup = false;
5049
}
51-
DallasTemperature::DallasTemperature(OneWire* _oneWire)
52-
{
50+
51+
DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
5352
setOneWire(_oneWire);
54-
#if REQUIRESALARMS
55-
setAlarmHandler(NO_ALARM_HANDLER);
56-
#endif
57-
useExternalPullup = false;
5853
}
5954

6055
bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
@@ -74,8 +69,8 @@ bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
7469
* Constructs DallasTemperature with strong pull-up turned on. Strong pull-up is mandated in DS18B20 datasheet for parasitic
7570
* power (2 wires) setup. (https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf, p. 7, section 'Powering the DS18B20').
7671
*/
77-
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire){
78-
setPullupPin(_pullupPin);
72+
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
73+
setPullupPin(_pullupPin);
7974
}
8075

8176
void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
@@ -267,7 +262,7 @@ void DallasTemperature::setResolution(uint8_t newResolution) {
267262
}
268263

269264
// set resolution of a device to 9, 10, 11, or 12 bits
270-
// if new resolution is out of range, 9 bits is used.
265+
// if new resolution is out of range, it is constrained.
271266
bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
272267
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
273268

@@ -428,16 +423,16 @@ bool DallasTemperature::requestTemperaturesByAddress(
428423
// Continue to check if the IC has responded with a temperature
429424
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
430425

431-
unsigned long delms = millisToWaitForConversion(bitResolution);
432-
if (checkForConversion && !parasite) {
433-
unsigned long start = millis();
434-
while (!isConversionComplete() && (millis() - start < delms))
435-
yield();
436-
} else {
437-
activateExternalPullup();
438-
delay(delms);
439-
deactivateExternalPullup();
440-
}
426+
if (checkForConversion && !parasite) {
427+
unsigned long start = millis();
428+
while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT ))
429+
yield();
430+
} else {
431+
unsigned long delms = millisToWaitForConversion(bitResolution);
432+
activateExternalPullup();
433+
delay(delms);
434+
deactivateExternalPullup();
435+
}
441436

442437
}
443438

@@ -480,11 +475,10 @@ bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
480475
// Fetch temperature for device index
481476
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
482477

483-
DeviceAddress deviceAddress;
478+
DeviceAddress deviceAddress;
484479
if (!getAddress(deviceAddress, deviceIndex)) {
485480
return DEVICE_DISCONNECTED_C;
486481
}
487-
488482
return getTempC((uint8_t*) deviceAddress);
489483

490484
}

DallasTemperature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define DEVICE_DISCONNECTED_F -196.6
3838
#define DEVICE_DISCONNECTED_RAW -7040
3939

40+
// Other
41+
#define MAX_CONVERSION_TIMEOUT 750
42+
4043
// For readPowerSupply on oneWire bus
4144
#ifndef nullptr
4245
#define nullptr NULL

0 commit comments

Comments
 (0)