Skip to content

Commit dd0cc6a

Browse files
committed
Merge pull request #2 from RobTillaart/issue16
+ check return value wire.reset() in readScratchPad
2 parents ad90160 + db51996 commit dd0cc6a

File tree

2 files changed

+235
-269
lines changed

2 files changed

+235
-269
lines changed

DallasTemperature.cpp

+35-69
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DallasTemperature::DallasTemperature() {}
2121
DallasTemperature::DallasTemperature(OneWire* _oneWire)
2222

2323
#if REQUIRESALARMS
24-
: _AlarmHandler(&defaultAlarmHandler)
24+
: _AlarmHandler(&defaultAlarmHandler)
2525
#endif
2626
{
2727
setOneWire(_oneWire);
@@ -101,19 +101,22 @@ bool DallasTemperature::isConnected(const uint8_t* deviceAddress)
101101
// also allows for updating the read scratchpad
102102
bool DallasTemperature::isConnected(const uint8_t* deviceAddress, uint8_t* scratchPad)
103103
{
104-
readScratchPad(deviceAddress, scratchPad);
105-
return (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
104+
bool b = readScratchPad(deviceAddress, scratchPad);
105+
return b && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
106106
}
107107

108108
// read device's scratch pad
109-
void DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad)
109+
// returns false if it fails to reset the Wire bus
110+
bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad)
110111
{
111-
// send the command
112-
_wire->reset();
112+
// send the reset command and fail fast
113+
int b = _wire->reset();
114+
if (b == 0) return false;
115+
113116
_wire->select(deviceAddress);
114117
_wire->write(READSCRATCH);
115118

116-
// TODO => collect all comments & use simple loop
119+
// Read all registers in a simple loop
117120
// byte 0: temperature LSB
118121
// byte 1: temperature MSB
119122
// byte 2: high alarm temp
@@ -126,53 +129,16 @@ void DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* sc
126129
// byte 7: DS18S20: COUNT_PER_C
127130
// DS18B20 & DS1822: store for crc
128131
// byte 8: SCRATCHPAD_CRC
129-
//
130-
// for(int i=0; i<9; i++)
131-
// {
132-
// scratchPad[i] = _wire->read();
133-
// }
134-
135-
136-
// read the response
137-
138-
// byte 0: temperature LSB
139-
scratchPad[TEMP_LSB] = _wire->read();
140-
141-
// byte 1: temperature MSB
142-
scratchPad[TEMP_MSB] = _wire->read();
143-
144-
// byte 2: high alarm temp
145-
scratchPad[HIGH_ALARM_TEMP] = _wire->read();
146-
147-
// byte 3: low alarm temp
148-
scratchPad[LOW_ALARM_TEMP] = _wire->read();
149-
150-
// byte 4:
151-
// DS18S20: store for crc
152-
// DS18B20 & DS1822: configuration register
153-
scratchPad[CONFIGURATION] = _wire->read();
154-
155-
// byte 5:
156-
// internal use & crc
157-
scratchPad[INTERNAL_BYTE] = _wire->read();
158-
159-
// byte 6:
160-
// DS18S20: COUNT_REMAIN
161-
// DS18B20 & DS1822: store for crc
162-
scratchPad[COUNT_REMAIN] = _wire->read();
163-
164-
// byte 7:
165-
// DS18S20: COUNT_PER_C
166-
// DS18B20 & DS1822: store for crc
167-
scratchPad[COUNT_PER_C] = _wire->read();
168-
169-
// byte 8:
170-
// SCTRACHPAD_CRC
171-
scratchPad[SCRATCHPAD_CRC] = _wire->read();
132+
for(uint8_t i = 0; i < 9; i++)
133+
{
134+
scratchPad[i] = _wire->read();
135+
}
172136

173-
_wire->reset();
137+
b = _wire->reset();
138+
return (b == 1);
174139
}
175140

141+
176142
// writes device's scratch pad
177143
void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint8_t* scratchPad)
178144
{
@@ -187,7 +153,7 @@ void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint
187153
_wire->select(deviceAddress); //<--this line was missing
188154
// save the newly written values to eeprom
189155
_wire->write(COPYSCRATCH, parasite);
190-
delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
156+
delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
191157
if (parasite) delay(10); // 10ms delay
192158
_wire->reset();
193159
}
@@ -402,7 +368,7 @@ float DallasTemperature::getTempCByIndex(uint8_t deviceIndex)
402368
{
403369
DeviceAddress deviceAddress;
404370
if (!getAddress(deviceAddress, deviceIndex))
405-
return DEVICE_DISCONNECTED_C;
371+
return DEVICE_DISCONNECTED_C;
406372
return getTempC((uint8_t*)deviceAddress);
407373
}
408374

@@ -411,16 +377,16 @@ float DallasTemperature::getTempFByIndex(uint8_t deviceIndex)
411377
{
412378
DeviceAddress deviceAddress;
413379
if (!getAddress(deviceAddress, deviceIndex))
414-
return DEVICE_DISCONNECTED_F;
380+
return DEVICE_DISCONNECTED_F;
415381
return getTempF((uint8_t*)deviceAddress);
416382
}
417383

418384
// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
419385
int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, uint8_t* scratchPad)
420386
{
421387
int16_t fpTemperature =
422-
(((int16_t) scratchPad[TEMP_MSB]) << 11) |
423-
(((int16_t) scratchPad[TEMP_LSB]) << 3);
388+
(((int16_t) scratchPad[TEMP_MSB]) << 11) |
389+
(((int16_t) scratchPad[TEMP_LSB]) << 3);
424390

425391
/*
426392
DS1820 and DS18S20 have a 9-bit temperature register.
@@ -448,11 +414,11 @@ int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui
448414
*/
449415

450416
if (deviceAddress[0] == DS18S20MODEL)
451-
fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16 +
452-
(
453-
((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7) /
454-
scratchPad[COUNT_PER_C]
455-
);
417+
fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16 +
418+
(
419+
((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7) /
420+
scratchPad[COUNT_PER_C]
421+
);
456422

457423
return fpTemperature;
458424
}
@@ -505,7 +471,7 @@ bool DallasTemperature::isParasitePowerMode(void)
505471
TH and TL Register Format
506472
507473
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
508-
S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
474+
S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
509475
510476
Only bits 11 through 4 of the temperature register are used
511477
in the TH and TL comparison since TH and TL are 8-bit
@@ -576,7 +542,7 @@ void DallasTemperature::resetAlarmSearch()
576542
alarmSearchJunction = -1;
577543
alarmSearchExhausted = 0;
578544
for(uint8_t i = 0; i < 7; i++)
579-
alarmSearchAddress[i] = 0;
545+
alarmSearchAddress[i] = 0;
580546
}
581547

582548
// This is a modified version of the OneWire::search method.
@@ -693,7 +659,7 @@ void DallasTemperature::processAlarms(void)
693659
while (alarmSearch(alarmAddr))
694660
{
695661
if (validAddress(alarmAddr))
696-
_AlarmHandler(alarmAddr);
662+
_AlarmHandler(alarmAddr);
697663
}
698664
}
699665

@@ -710,11 +676,11 @@ void DallasTemperature::defaultAlarmHandler(const uint8_t* deviceAddress)
710676

711677
#endif
712678

713-
// IF alarm is not used one can store a 16 bit int of userdata in the alarm
679+
// IF alarm is not used one can store a 16 bit int of userdata in the alarm
714680
// registers. E.g. an ID of the sensor.
715681
// See github issue #29
716682

717-
// note if device is not connected it will fail writing the data.
683+
// note if device is not connected it will fail writing the data.
718684
void DallasTemperature::setUserData(const uint8_t* deviceAddress, int16_t data)
719685
{
720686
ScratchPad scratchPad;
@@ -730,7 +696,7 @@ int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress)
730696
{
731697
int16_t data = 0;
732698
ScratchPad scratchPad;
733-
if (isConnected(deviceAddress, scratchPad))
699+
if (isConnected(deviceAddress, scratchPad))
734700
{
735701
data = scratchPad[HIGH_ALARM_TEMP] << 8;
736702
data += scratchPad[LOW_ALARM_TEMP];
@@ -770,7 +736,7 @@ float DallasTemperature::toCelsius(float fahrenheit)
770736
float DallasTemperature::rawToCelsius(int16_t raw)
771737
{
772738
if (raw <= DEVICE_DISCONNECTED_RAW)
773-
return DEVICE_DISCONNECTED_C;
739+
return DEVICE_DISCONNECTED_C;
774740
// C = RAW/128
775741
return (float)raw * 0.0078125;
776742
}
@@ -779,7 +745,7 @@ float DallasTemperature::rawToCelsius(int16_t raw)
779745
float DallasTemperature::rawToFahrenheit(int16_t raw)
780746
{
781747
if (raw <= DEVICE_DISCONNECTED_RAW)
782-
return DEVICE_DISCONNECTED_F;
748+
return DEVICE_DISCONNECTED_F;
783749
// C = RAW/128
784750
// F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
785751
return ((float)raw * 0.0140625) + 32;

0 commit comments

Comments
 (0)