Skip to content

Commit 957e655

Browse files
committed
fix milesburton#151 - optimize setResolution
1 parent f0cf361 commit 957e655

File tree

2 files changed

+75
-56
lines changed

2 files changed

+75
-56
lines changed

DallasTemperature.cpp

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ extern "C" {
3939
#define TEMP_11_BIT 0x5F // 11 bit
4040
#define TEMP_12_BIT 0x7F // 12 bit
4141

42+
#define MAX_CONVERSION_TIMEOUT750
43+
44+
// Alarm handler
4245
#define NO_ALARM_HANDLER ((AlarmHandler *)0)
4346

47+
4448
DallasTemperature::DallasTemperature() {
4549
#if REQUIRESALARMS
4650
setAlarmHandler(NO_ALARM_HANDLER);
@@ -112,7 +116,8 @@ void DallasTemperature::begin(void) {
112116
if (!parasite && readPowerSupply(deviceAddress))
113117
parasite = true;
114118

115-
bitResolution = max(bitResolution, getResolution(deviceAddress));
119+
uint8_t b = getResolution(deviceAddress);
120+
if (b > bitResolution) bitResolution = b;
116121
}
117122
}
118123
}
@@ -254,69 +259,86 @@ void DallasTemperature::setResolution(uint8_t newResolution) {
254259

255260
bitResolution = constrain(newResolution, 9, 12);
256261
DeviceAddress deviceAddress;
257-
for (int i = 0; i < devices; i++) {
262+
for (uint8_t i = 0; i < devices; i++) {
258263
getAddress(deviceAddress, i);
259264
setResolution(deviceAddress, bitResolution, true);
260265
}
261-
262266
}
263267

268+
/* PROPOSAL */
269+
264270
// set resolution of a device to 9, 10, 11, or 12 bits
265-
// if new resolution is out of range, it is constrained.
271+
// if new resolution is out of range, 9 bits is used.
266272
bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
267-
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
268-
269-
// ensure same behavior as setResolution(uint8_t newResolution)
270-
newResolution = constrain(newResolution, 9, 12);
273+
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
271274

272-
// return when stored value == new value
273-
if (getResolution(deviceAddress) == newResolution)
274-
return true;
275-
276-
ScratchPad scratchPad;
277-
if (isConnected(deviceAddress, scratchPad)) {
275+
bool success = false;
278276

279-
// DS1820 and DS18S20 have no resolution configuration register
280-
if (deviceAddress[0] != DS18S20MODEL) {
281-
282-
switch (newResolution) {
283-
case 12:
284-
scratchPad[CONFIGURATION] = TEMP_12_BIT;
285-
break;
286-
case 11:
287-
scratchPad[CONFIGURATION] = TEMP_11_BIT;
288-
break;
289-
case 10:
290-
scratchPad[CONFIGURATION] = TEMP_10_BIT;
291-
break;
292-
case 9:
293-
default:
294-
scratchPad[CONFIGURATION] = TEMP_9_BIT;
295-
break;
296-
}
297-
writeScratchPad(deviceAddress, scratchPad);
298-
299-
// without calculation we can always set it to max
300-
bitResolution = max(bitResolution, newResolution);
301-
302-
if (!skipGlobalBitResolutionCalculation
303-
&& (bitResolution > newResolution)) {
304-
bitResolution = newResolution;
305-
DeviceAddress deviceAddr;
306-
for (int i = 0; i < devices; i++) {
307-
getAddress(deviceAddr, i);
308-
bitResolution = max(bitResolution,
309-
getResolution(deviceAddr));
310-
}
311-
}
312-
}
313-
return true; // new value set
314-
}
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+
288+
uint8_t newValue = 0;
289+
ScratchPad scratchPad;
290+
291+
// we can only update the sensor if it is connected
292+
if (isConnected(deviceAddress, scratchPad))
293+
{
294+
switch (newResolution) {
295+
case 12:
296+
newValue = TEMP_12_BIT;
297+
break;
298+
case 11:
299+
newValue = TEMP_11_BIT;
300+
break;
301+
case 10:
302+
newValue = TEMP_10_BIT;
303+
break;
304+
case 9:
305+
default:
306+
newValue = TEMP_9_BIT;
307+
break;
308+
}
309+
310+
// if it needs to be updated we write the new value
311+
if (scratchPad[CONFIGURATION] != newValue)
312+
{
313+
scratchPad[CONFIGURATION] = newValue;
314+
writeScratchPad(deviceAddress, scratchPad);
315+
}
316+
// done
317+
success = true;
318+
}
319+
}
315320

316-
return false;
321+
// do we need to update the max resolution used?
322+
if (skipGlobalBitResolutionCalculation == false)
323+
{
324+
bitResolution = newResolution;
325+
if (devices > 1)
326+
{
327+
for (uint8_t i = 0; i < devices; i++)
328+
{
329+
if (bitResolution == 12) break;
330+
DeviceAddress deviceAddr;
331+
getAddress(deviceAddr, i);
332+
uint8_t b = getResolution(deviceAddr);
333+
if (b > bitResolution) bitResolution = b;
334+
}
335+
}
336+
}
317337

338+
return success;
318339
}
319340

341+
320342
// returns the global resolution
321343
uint8_t DallasTemperature::getResolution() {
322344
return bitResolution;
@@ -350,6 +372,7 @@ uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
350372

351373
}
352374

375+
353376
// sets the value of the waitForConversion flag
354377
// TRUE : function requestTemperature() etc returns when conversion is ready
355378
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
@@ -475,12 +498,11 @@ bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
475498
// Fetch temperature for device index
476499
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
477500

478-
DeviceAddress deviceAddress;
501+
DeviceAddress deviceAddress;
479502
if (!getAddress(deviceAddress, deviceIndex)) {
480503
return DEVICE_DISCONNECTED_C;
481504
}
482505
return getTempC((uint8_t*) deviceAddress);
483-
484506
}
485507

486508
// Fetch temperature for device index

DallasTemperature.h

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

40-
// Other
41-
#define MAX_CONVERSION_TIMEOUT 750
42-
4340
// For readPowerSupply on oneWire bus
4441
#ifndef nullptr
4542
#define nullptr NULL

0 commit comments

Comments
 (0)