Browse Source

dallas: try to safeguard against bogus readings

in case wire reset() somehow allows reading to pass
test/dev
Maxim Prokhorov 2 months ago
parent
commit
e92be04eea
3 changed files with 33 additions and 1 deletions
  1. +2
    -1
      code/espurna/config/types.h
  2. +3
    -0
      code/espurna/sensor.cpp
  3. +28
    -0
      code/espurna/sensors/DallasSensor.h

+ 2
- 1
code/espurna/config/types.h View File

@ -406,9 +406,10 @@
#define SENSOR_ERROR_CONFIG 11 // Configuration values were invalid
#define SENSOR_ERROR_SUPPORT 12 // Not supported
#define SENSOR_ERROR_NOT_FOUND 13 // Not found
#define SENSOR_ERROR_VALUE 14 // Sensor value is invalid
#define SENSOR_ERROR_OTHER 99 // Any other error
#define SENSOR_ERROR_MAX 14
#define SENSOR_ERROR_MAX 15
//------------------------------------------------------------------------------
// OTA Client (not related to the Web OTA support)


+ 3
- 0
code/espurna/sensor.cpp View File

@ -285,6 +285,9 @@ String error(unsigned char error) {
case SENSOR_ERROR_NOT_FOUND:
result = PSTR("Not found");
break;
case SENSOR_ERROR_VALUE:
result = PSTR("Invalid Value");
break;
case SENSOR_ERROR_OTHER:
default:
result = PSTR("Other / Unknown Error");


+ 28
- 0
code/espurna/sensors/DallasSensor.h View File

@ -318,9 +318,37 @@ private:
return SENSOR_ERROR_CRC;
}
if (_dataIsValid(out)) {
return SENSOR_ERROR_VALUE;
}
return SENSOR_ERROR_OK;
}
bool _dataIsValid(const Data& data) {
const auto all_zeroes = std::all_of(
data.begin(), data.end(),
[](uint8_t x) {
return x == 0;
});
if (all_zeroes) {
return true;
}
const auto all_255 = std::all_of(
data.begin(), data.end(),
[](uint8_t x) {
return x == 0xff;
});
if (all_255) {
return true;
}
return false;
}
int _readScratchpad() {
return _readScratchpad(_device, _data);
}


Loading…
Cancel
Save