From a97ecb414a02b0714674472dd6b26bde17290290 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 13 Oct 2019 14:34:28 +0200 Subject: [PATCH] minor code optimizations --- src/MyESP.cpp | 2 +- src/ems-esp.cpp | 49 +++++++++++-------------------------------------- src/ems.cpp | 8 ++++---- src/emsuart.cpp | 4 ++-- 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/src/MyESP.cpp b/src/MyESP.cpp index 7a42e9771..638447c05 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -595,7 +595,7 @@ void MyESP::_telnetConnected() { // show crash dump if just restarted after a fatal crash uint32_t crash_time; EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time); - if ((crash_time != 0) && (crash_time != 0xFFFFFFFF)) { + if ((crash_time) && (crash_time != 0xFFFFFFFF)) { myDebug_P(PSTR("[SYSTEM] There is stack data available from the last system crash. Use 'crash dump' to view and 'crash clear' to reset")); } #endif diff --git a/src/ems-esp.cpp b/src/ems-esp.cpp index c07c11581..27770374d 100644 --- a/src/ems-esp.cpp +++ b/src/ems-esp.cpp @@ -462,7 +462,7 @@ void showInfo() { } // Dallas external temp sensors - if (EMSESP_Settings.dallas_sensors != 0) { + if (EMSESP_Settings.dallas_sensors) { myDebug_P(PSTR("")); // newline char buffer[128] = {0}; char valuestr[8] = {0}; // for formatting temp @@ -678,8 +678,8 @@ void publishValues(bool force) { // hc{1-4} char hc[10]; - strncpy(hc, THERMOSTAT_HC, sizeof(hc)); - strncat(hc, _int_to_char(s, thermostat->hc), sizeof(hc)); + strlcpy(hc, THERMOSTAT_HC, sizeof(hc)); + strlcat(hc, _int_to_char(s, thermostat->hc), sizeof(hc)); JsonObject dataThermostat = rootThermostat.createNestedObject(hc); // different logic depending on thermostat types @@ -909,7 +909,7 @@ char * _readWord() { // publish external dallas sensor temperature values to MQTT void do_publishSensorValues() { - if ((EMSESP_Settings.dallas_sensors != 0) && (EMSESP_Settings.publish_time != 0)) { + if ((EMSESP_Settings.dallas_sensors) && (EMSESP_Settings.publish_time)) { publishSensorValues(); } } @@ -917,7 +917,7 @@ void do_publishSensorValues() { // call PublishValues without forcing, so using CRC to see if we really need to publish void do_publishValues() { // don't publish if we're not connected to the EMS bus - if ((ems_getBusConnected()) && myESP.isMQTTConnected() && EMSESP_Settings.publish_time != 0) { + if ((ems_getBusConnected()) && myESP.isMQTTConnected() && EMSESP_Settings.publish_time) { publishValues(true); // force publish } } @@ -1542,7 +1542,6 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { if (strcmp(topic, TOPIC_GENERIC_CMD) == 0) { // convert JSON and get the command StaticJsonDocument<100> doc; - JsonObject root = doc.to(); // create empty object DeserializationError error = deserializeJson(doc, message); // Deserialize the JSON document if (error) { myDebug_P(PSTR("[MQTT] Invalid command from topic %s, payload %s, error %s"), topic, message, error.c_str()); @@ -1562,7 +1561,6 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { // check for shower commands if (strcmp(topic, TOPIC_SHOWER_DATA) == 0) { StaticJsonDocument<100> doc; - JsonObject root = doc.to(); // create empty object DeserializationError error = deserializeJson(doc, message); // Deserialize the JSON document if (error) { myDebug_P(PSTR("[MQTT] Invalid command from topic %s, payload %s, error %s"), topic, message, error.c_str()); @@ -1594,7 +1592,6 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { if (strcmp(topic, TOPIC_BOILER_CMD) == 0) { // convert JSON and get the command StaticJsonDocument<100> doc; - JsonObject root = doc.to(); // create empty object DeserializationError error = deserializeJson(doc, message); // Deserialize the JSON document if (error) { myDebug_P(PSTR("[MQTT] Invalid command from topic %s, payload %s, error %s"), topic, message, error.c_str()); @@ -1672,7 +1669,6 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { if (strcmp(topic, TOPIC_THERMOSTAT_CMD) == 0) { // convert JSON and get the command StaticJsonDocument<100> doc; - JsonObject root = doc.to(); // create empty object DeserializationError error = deserializeJson(doc, message); // Deserialize the JSON document if (error) { myDebug_P(PSTR("[MQTT] Invalid command from topic %s, payload %s, error %s"), topic, message, error.c_str()); @@ -1680,30 +1676,6 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { } const char * command = doc["cmd"]; - uint8_t hc; - // thermostat temp changes - hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_TEMP, command); - if (hc) { - float f = doc["data"]; - ems_setThermostatTemp(f, hc); - publishValues(true); // publish back immediately - return; - } - - // thermostat mode changes - hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_MODE, command); - if (hc) { - const char * data = doc["data"]; - if (strcmp(data, "auto") == 0) { - ems_setThermostatMode(2, hc); - } else if ((strcmp(data, "day") == 0) || (strcmp(data, "manual") == 0) || (strcmp(data, "heat") == 0)) { - ems_setThermostatMode(1, hc); - } else if ((strcmp(data, "night") == 0) || (strcmp(data, "off") == 0)) { - ems_setThermostatMode(0, hc); - } - return; - } - // set night temp value hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_NIGHTTEMP, command); if (hc) { @@ -1968,12 +1940,12 @@ void showerCheck() { } } else { // hot water is off // if it just turned off, record the time as it could be a short pause - if ((EMSESP_Shower.timerStart != 0) && (EMSESP_Shower.timerPause == 0)) { + if ((EMSESP_Shower.timerStart) && (EMSESP_Shower.timerPause == 0)) { EMSESP_Shower.timerPause = EMSESP_Settings.timestamp; } // if shower has been off for longer than the wait time - if ((EMSESP_Shower.timerPause != 0) && ((EMSESP_Settings.timestamp - EMSESP_Shower.timerPause) > SHOWER_PAUSE_TIME)) { + if ((EMSESP_Shower.timerPause) && ((EMSESP_Settings.timestamp - EMSESP_Shower.timerPause) > SHOWER_PAUSE_TIME)) { // it is over the wait period, so assume that the shower has finished and calculate the total time and publish // because its unsigned long, can't have negative so check if length is less than OFFSET_TIME if ((EMSESP_Shower.timerPause - EMSESP_Shower.timerStart) > SHOWER_OFFSET_TIME) { @@ -2047,7 +2019,7 @@ void setup() { } // set timers for MQTT publish - if (EMSESP_Settings.publish_time != 0) { + if (EMSESP_Settings.publish_time) { publishValuesTimer.attach(EMSESP_Settings.publish_time, do_publishValues); // post MQTT EMS values publishSensorValuesTimer.attach(EMSESP_Settings.publish_time, do_publishSensorValues); // post MQTT dallas sensor values } @@ -2076,7 +2048,7 @@ void loop() { // check Dallas sensors, using same schedule as publish_time (default 2 mins) // these values are published to MQTT separately via the timer publishSensorValuesTimer - if (EMSESP_Settings.dallas_sensors != 0) { + if (EMSESP_Settings.dallas_sensors) { ds18.loop(); } @@ -2085,6 +2057,7 @@ void loop() { if (ems_getEmsRefreshed() && (scanThermostat_count == 0)) { publishValues(false); do_publishSensorValues(); + do_publishShowerData(); ems_setEmsRefreshed(false); // reset } @@ -2093,7 +2066,7 @@ void loop() { showerCheck(); } - if (EMSESP_DELAY != 0) { + if (EMSESP_DELAY) { delay(EMSESP_DELAY); // some time to WiFi and everything else to catch up, and prevent overheating } } diff --git a/src/ems.cpp b/src/ems.cpp index b639fd965..a189226f9 100644 --- a/src/ems.cpp +++ b/src/ems.cpp @@ -7,9 +7,9 @@ */ #include "ems.h" -#include "ems_utils.h" #include "MyESP.h" #include "ems_devices.h" +#include "ems_utils.h" #include "emsuart.h" #include // https://github.com/rlogiacco/CircularBuffer @@ -1001,7 +1001,7 @@ void _printMessage(_EMS_RxTelegram * EMS_RxTelegram) { strlcpy(color_s, COLOR_MAGENTA, sizeof(color_s)); } - if (length != 0) { + if (length) { // type strlcat(output_str, ", type 0x", sizeof(output_str)); @@ -1691,7 +1691,7 @@ void _process_SM10Monitor(_EMS_RxTelegram * EMS_RxTelegram) { */ void _process_SM100Monitor(_EMS_RxTelegram * EMS_RxTelegram) { // only process the complete telegram, not partial - if (EMS_RxTelegram->offset != 0) { + if (EMS_RxTelegram->offset) { return; } @@ -1802,7 +1802,7 @@ void _process_ISM1Set(_EMS_RxTelegram * EMS_RxTelegram) { */ void _process_SetPoints(_EMS_RxTelegram * EMS_RxTelegram) { if (EMS_Sys_Status.emsLogging == EMS_SYS_LOGGING_VERBOSE) { - if (EMS_RxTelegram->data_length != 0) { + if (EMS_RxTelegram->data_length) { uint8_t setpoint = EMS_RxTelegram->data[0]; // flow temp //uint8_t ww_power = data[2]; // power in % diff --git a/src/emsuart.cpp b/src/emsuart.cpp index f40268f71..b9cb59906 100644 --- a/src/emsuart.cpp +++ b/src/emsuart.cpp @@ -178,7 +178,7 @@ void ICACHE_FLASH_ATTR emsuart_tx_brk() { uint32_t tmp; // must make sure Tx FIFO is empty - while (((USS(EMSUART_UART) >> USTXC) & 0xFF) != 0) + while (((USS(EMSUART_UART) >> USTXC) & 0xFF)) ; tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask @@ -227,7 +227,7 @@ _EMS_TX_STATUS ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) { USF(EMSUART_UART) = buf[i]; // just to be safe wait for tx fifo empty (needed?) - while (((USS(EMSUART_UART) >> USTXC) & 0xff) != 0) + while (((USS(EMSUART_UART) >> USTXC) & 0xff)) ; // wait until bits are sent on wire