Skip to content

Commit 0ac9502

Browse files
committed
Fixed bug in writeScratchPad and added code which prevent writing to eeprom if value in eeprem eq new value.
1 parent c565dba commit 0ac9502

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ build
1313
.classpath
1414
.settings
1515
.gradle
16-
16+
.vscode

DallasTemperature.cpp

+32-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint
155155
if (deviceAddress[0] != DS18S20MODEL) _wire->write(scratchPad[CONFIGURATION]);
156156

157157
_wire->reset();
158-
_wire->select(deviceAddress);
159158

160159
// save the newly written values to eeprom
161160
_wire->select(deviceAddress);
@@ -189,14 +188,20 @@ void DallasTemperature::setResolution(uint8_t newResolution){
189188
for (int i=0; i<devices; i++)
190189
{
191190
getAddress(deviceAddress, i);
192-
setResolution(deviceAddress, bitResolution);
191+
setResolution(deviceAddress, bitResolution, true);
193192
}
194193

195194
}
196195

197196
// set resolution of a device to 9, 10, 11, or 12 bits
198197
// if new resolution is out of range, 9 bits is used.
199-
bool DallasTemperature::setResolution(const uint8_t* deviceAddress, uint8_t newResolution){
198+
bool DallasTemperature::setResolution(const uint8_t* deviceAddress, uint8_t newResolution, bool skipGlobalBitResolutionCalculation){
199+
200+
// ensure same behavior as setResolution(uint8_t newResolution)
201+
newResolution = constrain(newResolution, 9, 12);
202+
203+
// return when stored value == new value
204+
if(getResolution(deviceAddress) == newResolution) return true;
200205

201206
ScratchPad scratchPad;
202207
if (isConnected(deviceAddress, scratchPad)){
@@ -220,6 +225,19 @@ bool DallasTemperature::setResolution(const uint8_t* deviceAddress, uint8_t newR
220225
break;
221226
}
222227
writeScratchPad(deviceAddress, scratchPad);
228+
229+
// without calculation we can always set it to max
230+
bitResolution = max(bitResolution, newResolution);
231+
232+
if(!skipGlobalBitResolutionCalculation && (bitResolution > newResolution)){
233+
bitResolution = newResolution;
234+
DeviceAddress deviceAddr;
235+
for (int i=0; i<devices; i++)
236+
{
237+
getAddress(deviceAddr, i);
238+
bitResolution = max(bitResolution, getResolution(deviceAddr));
239+
}
240+
}
223241
}
224242
return true; // new value set
225243
}
@@ -499,6 +517,9 @@ bool DallasTemperature::isParasitePowerMode(void){
499517
// note if device is not connected it will fail writing the data.
500518
void DallasTemperature::setUserData(const uint8_t* deviceAddress, int16_t data)
501519
{
520+
// return when stored value == new value
521+
if(getUserData(deviceAddress) == data) return;
522+
502523
ScratchPad scratchPad;
503524
if (isConnected(deviceAddress, scratchPad))
504525
{
@@ -594,6 +615,9 @@ the next temperature conversion.
594615
// after a decimal point. valid range is -55C - 125C
595616
void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress, char celsius){
596617

618+
// return when stored value == new value
619+
if(getHighAlarmTemp(deviceAddress) == celsius) return;
620+
597621
// make sure the alarm temperature is within the device's range
598622
if (celsius > 125) celsius = 125;
599623
else if (celsius < -55) celsius = -55;
@@ -610,6 +634,10 @@ void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress, char cels
610634
// accepts a float, but the alarm resolution will ignore anything
611635
// after a decimal point. valid range is -55C - 125C
612636
void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress, char celsius){
637+
638+
// return when stored value == new value
639+
if(getLowAlarmTemp(deviceAddress) == celsius) return;
640+
613641
// make sure the alarm temperature is within the device's range
614642
if (celsius > 125) celsius = 125;
615643
else if (celsius < -55) celsius = -55;
@@ -803,4 +831,4 @@ void DallasTemperature::operator delete(void* p){
803831
free(p); // Free the memory
804832
}
805833

806-
#endif
834+
#endif

DallasTemperature.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class DallasTemperature
110110
uint8_t getResolution(const uint8_t*);
111111

112112
// set resolution of a device to 9, 10, 11, or 12 bits
113-
bool setResolution(const uint8_t*, uint8_t);
113+
bool setResolution(const uint8_t*, uint8_t, bool skipGlobalBitResolutionCalculation = false);
114114

115115
// sets/gets the waitForConversion flag
116116
void setWaitForConversion(bool);
@@ -269,4 +269,4 @@ class DallasTemperature
269269
#endif
270270

271271
};
272-
#endif
272+
#endif

0 commit comments

Comments
 (0)