mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-10 17:59:53 +03:00
refactor ds18 #287
This commit is contained in:
50
src/ds18.cpp
50
src/ds18.cpp
@@ -39,10 +39,7 @@ void DS18::setup(uint8_t gpio, bool parasite) {
|
||||
uint8_t DS18::scan() {
|
||||
_devices.clear();
|
||||
|
||||
uint8_t count;
|
||||
|
||||
// start the search
|
||||
count = loadDevices();
|
||||
uint8_t count = loadDevices(); // start the search
|
||||
|
||||
// If no devices found check again pulling up the line
|
||||
if (count == 0) {
|
||||
@@ -59,27 +56,26 @@ uint8_t DS18::scan() {
|
||||
// scan every 2 seconds
|
||||
void DS18::loop() {
|
||||
static uint32_t last = 0;
|
||||
if (millis() - last < DS18_READ_INTERVAL)
|
||||
if (millis() - last < DS18_READ_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
last = millis();
|
||||
|
||||
// Every second we either start a conversion or read the scratchpad
|
||||
static bool conversion = true;
|
||||
if (conversion) {
|
||||
// Start conversion
|
||||
_wire->reset();
|
||||
_wire->skip();
|
||||
_wire->write(DS18_CMD_START_CONVERSION, _parasite);
|
||||
} else {
|
||||
// Read scratchpads
|
||||
for (unsigned char index = 0; index < _devices.size(); index++) {
|
||||
// Read scratchpad
|
||||
if (_wire->reset() == 0) {
|
||||
// Force a CRC check error
|
||||
_devices[index].data[0] = _devices[index].data[0] + 1;
|
||||
_devices[index].data[0] = _devices[index].data[0] + 1; // Force a CRC check error
|
||||
return;
|
||||
}
|
||||
|
||||
// Read each scratchpad
|
||||
_wire->select(_devices[index].address);
|
||||
_wire->write(DS18_CMD_READ_SCRATCHPAD);
|
||||
|
||||
@@ -89,8 +85,7 @@ void DS18::loop() {
|
||||
}
|
||||
|
||||
if (_wire->reset() != 1) {
|
||||
// Force a CRC check error
|
||||
_devices[index].data[0] = _devices[index].data[0] + 1;
|
||||
_devices[index].data[0] = _devices[index].data[0] + 1; // Force a CRC check error
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -161,8 +156,9 @@ char * DS18::getDeviceString(char * buffer, unsigned char index) {
|
||||
byte 8: SCRATCHPAD_CRC
|
||||
*/
|
||||
int16_t DS18::getRawValue(unsigned char index) {
|
||||
if (index >= _count)
|
||||
if (index >= _count) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t * data = _devices[index].data;
|
||||
|
||||
@@ -178,23 +174,35 @@ int16_t DS18::getRawValue(unsigned char index) {
|
||||
}
|
||||
} else {
|
||||
byte cfg = (data[4] & 0x60);
|
||||
if (cfg == 0x00)
|
||||
if (cfg == 0x00) {
|
||||
raw = raw & ~7; // 9 bit res, 93.75 ms
|
||||
else if (cfg == 0x20)
|
||||
} else if (cfg == 0x20) {
|
||||
raw = raw & ~3; // 10 bit res, 187.5 ms
|
||||
else if (cfg == 0x40)
|
||||
} else if (cfg == 0x40) {
|
||||
raw = raw & ~1; // 11 bit res, 375 ms
|
||||
// 12 bit res, 750 ms
|
||||
}
|
||||
}
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
// return real value as a float
|
||||
// The raw temperature data is in units of sixteenths of a degree, so the value must be divided by 16 in order to convert it to degrees.
|
||||
// return real value as a float, rounded to 2 decimal places
|
||||
float DS18::getValue(unsigned char index) {
|
||||
float value = (float)getRawValue(index) / 16.0;
|
||||
return value;
|
||||
int16_t raw_value = getRawValue(index);
|
||||
|
||||
// check if valid
|
||||
if ((raw_value == DS18_CRC_ERROR) || (raw_value == DS18_DISCONNECTED)) {
|
||||
return (float)DS18_DISCONNECTED;
|
||||
}
|
||||
|
||||
// The raw temperature data is in units of sixteenths of a degree,
|
||||
// so the value must first be divided by 16 in order to convert it to degrees.
|
||||
float new_value = (float)(raw_value / 16.0);
|
||||
|
||||
// round to 2 decimal places
|
||||
// https://arduinojson.org/v6/faq/how-to-configure-the-serialization-of-floats/
|
||||
return (int)(new_value * 100 + 0.5) / 100.0;
|
||||
}
|
||||
|
||||
// check for a supported DS chip version
|
||||
@@ -204,8 +212,9 @@ bool DS18::validateID(unsigned char id) {
|
||||
|
||||
// return the type
|
||||
unsigned char DS18::chip(unsigned char index) {
|
||||
if (index < _count)
|
||||
if (index < _count) {
|
||||
return _devices[index].address[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -214,6 +223,7 @@ uint8_t DS18::loadDevices() {
|
||||
uint8_t address[8];
|
||||
_wire->reset();
|
||||
_wire->reset_search();
|
||||
|
||||
while (_wire->search(address)) {
|
||||
// Check CRC
|
||||
if (_wire->crc8(address, 7) == address[7]) {
|
||||
|
||||
Reference in New Issue
Block a user