@@ -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
@@ -39,22 +39,21 @@ extern "C" {
3939#define TEMP_11_BIT 0x5F // 11 bit
4040#define TEMP_12_BIT 0x7F // 12 bit
4141
42+ #define MAX_CONVERSION_TIMEOUT 750
43+
44+ // Alarm handler
4245#define NO_ALARM_HANDLER ((AlarmHandler *)0 )
4346
44- DallasTemperature::DallasTemperature ()
45- {
47+
48+ DallasTemperature::DallasTemperature () {
4649#if REQUIRESALARMS
4750 setAlarmHandler (NO_ALARM_HANDLER);
4851#endif
4952 useExternalPullup = false ;
5053}
51- DallasTemperature::DallasTemperature (OneWire* _oneWire)
52- {
54+
55+ DallasTemperature::DallasTemperature (OneWire* _oneWire) : DallasTemperature() {
5356 setOneWire (_oneWire);
54- #if REQUIRESALARMS
55- setAlarmHandler (NO_ALARM_HANDLER);
56- #endif
57- useExternalPullup = false ;
5857}
5958
6059bool DallasTemperature::validFamily (const uint8_t * deviceAddress) {
@@ -74,8 +73,8 @@ bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
7473 * Constructs DallasTemperature with strong pull-up turned on. Strong pull-up is mandated in DS18B20 datasheet for parasitic
7574 * power (2 wires) setup. (https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf, p. 7, section 'Powering the DS18B20').
7675 */
77- DallasTemperature::DallasTemperature (OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire){
78- setPullupPin (_pullupPin);
76+ DallasTemperature::DallasTemperature (OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
77+ setPullupPin (_pullupPin);
7978}
8079
8180void DallasTemperature::setPullupPin (uint8_t _pullupPin) {
@@ -109,19 +108,19 @@ void DallasTemperature::begin(void) {
109108 while (_wire->search (deviceAddress)) {
110109
111110 if (validAddress (deviceAddress)) {
112-
113- if (!parasite && readPowerSupply (deviceAddress))
114- parasite = true ;
115-
116- bitResolution = max (bitResolution, getResolution (deviceAddress));
117-
118111 devices++;
112+
119113 if (validFamily (deviceAddress)) {
120114 ds18Count++;
115+
116+ if (!parasite && readPowerSupply (deviceAddress))
117+ parasite = true ;
118+
119+ uint8_t b = getResolution (deviceAddress);
120+ if (b > bitResolution) bitResolution = b;
121121 }
122122 }
123123 }
124-
125124}
126125
127126// returns the number of devices found on the bus
@@ -260,69 +259,85 @@ void DallasTemperature::setResolution(uint8_t newResolution) {
260259
261260 bitResolution = constrain (newResolution, 9 , 12 );
262261 DeviceAddress deviceAddress;
263- for (int i = 0 ; i < devices; i++) {
262+ for (uint8_t i = 0 ; i < devices; i++) {
264263 getAddress (deviceAddress, i);
265264 setResolution (deviceAddress, bitResolution, true );
266265 }
267-
268266}
269267
268+ /* PROPOSAL */
269+
270270// set resolution of a device to 9, 10, 11, or 12 bits
271271// if new resolution is out of range, 9 bits is used.
272272bool DallasTemperature::setResolution (const uint8_t * deviceAddress,
273- uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
274-
275- // ensure same behavior as setResolution(uint8_t newResolution)
276- newResolution = constrain (newResolution, 9 , 12 );
277-
278- // return when stored value == new value
279- if (getResolution (deviceAddress) == newResolution)
280- return true ;
281-
282- ScratchPad scratchPad;
283- if (isConnected (deviceAddress, scratchPad)) {
284-
285- // DS1820 and DS18S20 have no resolution configuration register
286- if (deviceAddress[0 ] != DS18S20MODEL) {
287-
288- switch (newResolution) {
289- case 12 :
290- scratchPad[CONFIGURATION] = TEMP_12_BIT;
291- break ;
292- case 11 :
293- scratchPad[CONFIGURATION] = TEMP_11_BIT;
294- break ;
295- case 10 :
296- scratchPad[CONFIGURATION] = TEMP_10_BIT;
297- break ;
298- case 9 :
299- default :
300- scratchPad[CONFIGURATION] = TEMP_9_BIT;
301- break ;
302- }
303- writeScratchPad (deviceAddress, scratchPad);
304-
305- // without calculation we can always set it to max
306- bitResolution = max (bitResolution, newResolution);
307-
308- if (!skipGlobalBitResolutionCalculation
309- && (bitResolution > newResolution)) {
310- bitResolution = newResolution;
311- DeviceAddress deviceAddr;
312- for (int i = 0 ; i < devices; i++) {
313- getAddress (deviceAddr, i);
314- bitResolution = max (bitResolution,
315- getResolution (deviceAddr));
316- }
317- }
318- }
319- return true ; // new value set
320- }
321-
322- return false ;
323-
273+ uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
274+
275+ bool success = false ;
276+
277+ // DS1820 and DS18S20 have no resolution configuration register
278+ if (deviceAddress[0 ] == DS18S20MODEL)
279+ {
280+ success = true ;
281+ }
282+ else
283+ {
284+
285+ // handle the sensors with configuration register
286+ newResolution = constrain (newResolution, 9 , 12 );
287+ uint8_t newValue = 0 ;
288+ ScratchPad scratchPad;
289+
290+ // we can only update the sensor if it is connected
291+ if (isConnected (deviceAddress, scratchPad))
292+ {
293+ switch (newResolution) {
294+ case 12 :
295+ newValue = TEMP_12_BIT;
296+ break ;
297+ case 11 :
298+ newValue = TEMP_11_BIT;
299+ break ;
300+ case 10 :
301+ newValue = TEMP_10_BIT;
302+ break ;
303+ case 9 :
304+ default :
305+ newValue = TEMP_9_BIT;
306+ break ;
307+ }
308+
309+ // if it needs to be updated we write the new value
310+ if (scratchPad[CONFIGURATION] != newValue)
311+ {
312+ scratchPad[CONFIGURATION] = newValue;
313+ writeScratchPad (deviceAddress, scratchPad);
314+ }
315+ // done
316+ success = true ;
317+ }
318+ }
319+
320+ // do we need to update the max resolution used?
321+ if (skipGlobalBitResolutionCalculation == false )
322+ {
323+ bitResolution = newResolution;
324+ if (devices > 1 )
325+ {
326+ for (uint8_t i = 0 ; i < devices; i++)
327+ {
328+ if (bitResolution == 12 ) break ;
329+ DeviceAddress deviceAddr;
330+ getAddress (deviceAddr, i);
331+ uint8_t b = getResolution (deviceAddr);
332+ if (b > bitResolution) bitResolution = b;
333+ }
334+ }
335+ }
336+
337+ return success;
324338}
325339
340+
326341// returns the global resolution
327342uint8_t DallasTemperature::getResolution () {
328343 return bitResolution;
@@ -356,6 +371,7 @@ uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
356371
357372}
358373
374+
359375// sets the value of the waitForConversion flag
360376// TRUE : function requestTemperature() etc returns when conversion is ready
361377// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
@@ -429,16 +445,16 @@ bool DallasTemperature::requestTemperaturesByAddress(
429445// Continue to check if the IC has responded with a temperature
430446void DallasTemperature::blockTillConversionComplete (uint8_t bitResolution) {
431447
432- unsigned long delms = millisToWaitForConversion (bitResolution);
433- if (checkForConversion && !parasite) {
434- unsigned long start = millis ();
435- while (! isConversionComplete () && ( millis () - start < delms))
436- yield ();
437- } else {
438- activateExternalPullup ();
439- delay (delms);
440- deactivateExternalPullup ();
441- }
448+ if (checkForConversion && !parasite) {
449+ unsigned long start = millis ();
450+ while (! isConversionComplete () && ( millis () - start < MAX_CONVERSION_TIMEOUT ))
451+ yield ();
452+ } else {
453+ unsigned long delms = millisToWaitForConversion (bitResolution);
454+ activateExternalPullup ();
455+ delay (delms);
456+ deactivateExternalPullup ();
457+ }
442458
443459}
444460
@@ -485,9 +501,7 @@ float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
485501 if (!getAddress (deviceAddress, deviceIndex)) {
486502 return DEVICE_DISCONNECTED_C;
487503 }
488-
489504 return getTempC ((uint8_t *) deviceAddress);
490-
491505}
492506
493507// Fetch temperature for device index
0 commit comments