Skip to content

Commit 5e835dc

Browse files
authored
Perform search for setResolution linearly?
As mentioned in #193 the issue stems from repeated usage of getAddress(). It doesn't seem to me to be necessary to use iterators to sort out the performance issue in the library, just a general avoidance of getAddress() outside index based functions. Since getAddress() appears to be used to do a bit more than iterate through addresses I added validAddress() as an additional condition, it might not be needed. I only found two locations where this appears to happen so #193 may be resolved entirely by this. General advice to users of the library may be to avoid index based functions inside loops. This might be where an "iterator" interface may be useful inside the class, such that performing functions across devices remains somewhat linear.
1 parent acd2626 commit 5e835dc

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

DallasTemperature.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,11 @@ void DallasTemperature::setResolution(uint8_t newResolution) {
261261

262262
bitResolution = constrain(newResolution, 9, 12);
263263
DeviceAddress deviceAddress;
264+
_wire->reset_search();
264265
for (uint8_t i = 0; i < devices; i++) {
265-
getAddress(deviceAddress, i);
266-
setResolution(deviceAddress, bitResolution, true);
266+
if(_wire->search(deviceAddress) && validAddress(deviceAddress)) {
267+
setResolution(deviceAddress, bitResolution, true);
268+
}
267269
}
268270
}
269271

@@ -325,12 +327,14 @@ bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
325327
if (skipGlobalBitResolutionCalculation == false) {
326328
bitResolution = newResolution;
327329
if (devices > 1) {
330+
DeviceAddress deviceAddr;
331+
_wire->reset_search();
328332
for (uint8_t i = 0; i < devices; i++) {
329333
if (bitResolution == 12) break;
330-
DeviceAddress deviceAddr;
331-
getAddress(deviceAddr, i);
332-
uint8_t b = getResolution(deviceAddr);
333-
if (b > bitResolution) bitResolution = b;
334+
if (_wire->search(deviceAddr) && validAddress(deviceAddr)) {
335+
uint8_t b = getResolution(deviceAddr);
336+
if (b > bitResolution) bitResolution = b;
337+
}
334338
}
335339
}
336340
}

0 commit comments

Comments
 (0)