From 6b81b57b029d68c0d0f8c5643e05b23f618b76b3 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 11 Jan 2020 22:12:00 +0100 Subject: [PATCH] refactor ds18 #287 --- src/ds18.cpp | 50 +++++++++++++++++++++++++++++-------------------- src/ems-esp.cpp | 19 +++++++++++-------- src/ems.cpp | 48 ++++++++++++++++++++++++++--------------------- src/ems.h | 3 ++- src/version.h | 2 +- 5 files changed, 71 insertions(+), 51 deletions(-) diff --git a/src/ds18.cpp b/src/ds18.cpp index cf7157709..eba3e6b1a 100644 --- a/src/ds18.cpp +++ b/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]) { diff --git a/src/ems-esp.cpp b/src/ems-esp.cpp index 01b5f0394..747845148 100644 --- a/src/ems-esp.cpp +++ b/src/ems-esp.cpp @@ -494,11 +494,15 @@ void showInfo() { // Dallas external temp sensors if (EMSESP_Settings.dallas_sensors) { myDebug_P(PSTR("")); // newline - char buffer[128] = {0}; - char valuestr[8] = {0}; // for formatting temp + char buffer[128] = {0}; + char valuestr[8] = {0}; // for formatting temp + float sensorValue; myDebug_P(PSTR("%sExternal temperature sensors:%s"), COLOR_BOLD_ON, COLOR_BOLD_OFF); for (uint8_t i = 0; i < EMSESP_Settings.dallas_sensors; i++) { - myDebug_P(PSTR(" Sensor #%d %s: %s C"), i + 1, ds18.getDeviceString(buffer, i), _float_to_char(valuestr, ds18.getValue(i))); + sensorValue = ds18.getValue(i); + if (sensorValue != DS18_DISCONNECTED) { + myDebug_P(PSTR(" Sensor #%d %s: %s C"), i + 1, ds18.getDeviceString(buffer, i), _float_to_char(valuestr, sensorValue)); + } } } @@ -535,9 +539,8 @@ void publishSensorValues() { // see if the sensor values have changed, if so send it on for (uint8_t i = 0; i < EMSESP_Settings.dallas_sensors; i++) { - // round to 2 decimal places. from https://arduinojson.org/v6/faq/how-to-configure-the-serialization-of-floats/ - float sensorValue = (int)(ds18.getValue(i) * 100 + 0.5) / 100.0; - if (sensorValue != DS18_DISCONNECTED && sensorValue != DS18_CRC_ERROR) { + float sensorValue = ds18.getValue(i); + if (sensorValue != DS18_DISCONNECTED) { sprintf(label, PAYLOAD_EXTERNAL_SENSORS, (i + 1)); sensors[label] = sensorValue; hasdata = true; @@ -1192,9 +1195,9 @@ void _showCommands(uint8_t event) { // we set the logging here void TelnetCallback(uint8_t event) { if (event == TELNET_EVENT_CONNECT) { - ems_setLogging(EMS_SYS_LOGGING_DEFAULT); + ems_setLogging(EMS_SYS_LOGGING_DEFAULT, true); } else if (event == TELNET_EVENT_DISCONNECT) { - ems_setLogging(EMS_SYS_LOGGING_NONE); + ems_setLogging(EMS_SYS_LOGGING_NONE, true); } else if ((event == TELNET_EVENT_SHOWCMD) || (event == TELNET_EVENT_SHOWSET)) { _showCommands(event); } diff --git a/src/ems.cpp b/src/ems.cpp index b070a3fa5..a39d6a2cc 100644 --- a/src/ems.cpp +++ b/src/ems.cpp @@ -209,7 +209,7 @@ void ems_init() { strlcpy(EMS_Thermostat.version, "?", sizeof(EMS_Thermostat.version)); // default logging is none - ems_setLogging(EMS_SYS_LOGGING_DEFAULT); + ems_setLogging(EMS_SYS_LOGGING_DEFAULT, true); } // Getters and Setters for parameters @@ -281,27 +281,33 @@ _EMS_SYS_LOGGING ems_getLogging() { } void ems_setLogging(_EMS_SYS_LOGGING loglevel, uint16_t type_id) { - if (loglevel <= EMS_SYS_LOGGING_JABBER) { - EMS_Sys_Status.emsLogging = loglevel; + EMS_Sys_Status.emsLogging_typeID = type_id; + ems_setLogging(EMS_SYS_LOGGING_WATCH, false); +} - if (loglevel == EMS_SYS_LOGGING_NONE) { - myDebug_P(PSTR("System Logging set to None")); - } else if (loglevel == EMS_SYS_LOGGING_BASIC) { - myDebug_P(PSTR("System Logging set to Basic")); - } else if (loglevel == EMS_SYS_LOGGING_VERBOSE) { - myDebug_P(PSTR("System Logging set to Verbose")); - } else if (loglevel == EMS_SYS_LOGGING_THERMOSTAT) { - myDebug_P(PSTR("System Logging set to Thermostat only")); - } else if (loglevel == EMS_SYS_LOGGING_SOLARMODULE) { - myDebug_P(PSTR("System Logging set to Solar Module only")); - } else if (loglevel == EMS_SYS_LOGGING_RAW) { - myDebug_P(PSTR("System Logging set to Raw mode")); - } else if (loglevel == EMS_SYS_LOGGING_JABBER) { - myDebug_P(PSTR("System Logging set to Jabber mode")); - } else if (loglevel == EMS_SYS_LOGGING_WATCH) { - EMS_Sys_Status.emsLogging_typeID = type_id; - myDebug_P(PSTR("System Logging set to Watch mode")); - } +void ems_setLogging(_EMS_SYS_LOGGING loglevel, bool quiet) { + EMS_Sys_Status.emsLogging = loglevel; + + if (quiet) { + return; // no reporting to console + } + + if (loglevel == EMS_SYS_LOGGING_NONE) { + myDebug_P(PSTR("System Logging set to None")); + } else if (loglevel == EMS_SYS_LOGGING_BASIC) { + myDebug_P(PSTR("System Logging set to Basic")); + } else if (loglevel == EMS_SYS_LOGGING_VERBOSE) { + myDebug_P(PSTR("System Logging set to Verbose")); + } else if (loglevel == EMS_SYS_LOGGING_THERMOSTAT) { + myDebug_P(PSTR("System Logging set to Thermostat only")); + } else if (loglevel == EMS_SYS_LOGGING_SOLARMODULE) { + myDebug_P(PSTR("System Logging set to Solar Module only")); + } else if (loglevel == EMS_SYS_LOGGING_RAW) { + myDebug_P(PSTR("System Logging set to Raw mode")); + } else if (loglevel == EMS_SYS_LOGGING_JABBER) { + myDebug_P(PSTR("System Logging set to Jabber mode")); + } else if (loglevel == EMS_SYS_LOGGING_WATCH) { + myDebug_P(PSTR("System Logging set to Watch mode")); } } diff --git a/src/ems.h b/src/ems.h index 5efd8d89c..0548ac3f0 100644 --- a/src/ems.h +++ b/src/ems.h @@ -434,7 +434,8 @@ void ems_setWarmWaterActivated(bool activated); void ems_setWarmWaterOnetime(bool activated); void ems_setWarmTapWaterActivated(bool activated); void ems_setPoll(bool b); -void ems_setLogging(_EMS_SYS_LOGGING loglevel, uint16_t type_id = 0); +void ems_setLogging(_EMS_SYS_LOGGING loglevel, uint16_t type_id); +void ems_setLogging(_EMS_SYS_LOGGING loglevel, bool quiet = false); void ems_setWarmWaterModeComfort(uint8_t comfort); void ems_setModels(); void ems_setTxDisabled(bool b); diff --git a/src/version.h b/src/version.h index 55b458466..32897e3e2 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define APP_VERSION "1.9.5b16" +#define APP_VERSION "1.9.5b17"