From 1f521ed74841fbda1523925da1ce2df678479672 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 7 Nov 2019 17:08:58 +0100 Subject: [PATCH] Refactor I2C driver detection phase 3 --- tasmota/tasmota.h | 2 +- tasmota/tasmota.ino | 8 +------- tasmota/xdrv_29_deepsleep.ino | 2 +- tasmota/xsns_19_mgs.ino | 29 ++++++++++++++++------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 0412f6bc2..607afefd1 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -264,7 +264,7 @@ enum LightTypes { LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT enum XsnsFunctions {FUNC_SETTINGS_OVERRIDE, FUNC_PIN_STATE, FUNC_MODULE_INIT, FUNC_PRE_INIT, FUNC_INIT, FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_200_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_300_MSECOND, FUNC_EVERY_SECOND, FUNC_SAVE_AT_MIDNIGHT, FUNC_SAVE_BEFORE_RESTART, - FUNC_PREP_BEFORE_TELEPERIOD, FUNC_AFTER_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_SENSOR, FUNC_COMMAND, FUNC_COMMAND_SENSOR, FUNC_COMMAND_DRIVER, + FUNC_AFTER_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_SENSOR, FUNC_COMMAND, FUNC_COMMAND_SENSOR, FUNC_COMMAND_DRIVER, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_SET_POWER, FUNC_SET_DEVICE_POWER, FUNC_SHOW_SENSOR, FUNC_ANY_KEY, FUNC_ENERGY_EVERY_SECOND, FUNC_ENERGY_RESET, diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index b295cf664..8ac67d918 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -839,13 +839,7 @@ void PerformEverySecond(void) if (Settings.tele_period) { tele_period++; - // increase time for prepare and document state to ensure TELEPERIOD deliver results - if (tele_period == Settings.tele_period -3) { - // sensors must be called later if driver switch on e.g. power on deepsleep - XdrvCall(FUNC_PREP_BEFORE_TELEPERIOD); - XsnsCall(FUNC_PREP_BEFORE_TELEPERIOD); - } - if (tele_period >= Settings.tele_period) { + if (tele_period >= Settings.tele_period) { tele_period = 0; MqttPublishTeleState(); diff --git a/tasmota/xdrv_29_deepsleep.ino b/tasmota/xdrv_29_deepsleep.ino index 455df9f40..2cbfc0d91 100644 --- a/tasmota/xdrv_29_deepsleep.ino +++ b/tasmota/xdrv_29_deepsleep.ino @@ -141,7 +141,7 @@ void CmndDeepsleepTime(void) if ((XdrvMailbox.payload == 0) || ((XdrvMailbox.payload > 10) && (XdrvMailbox.payload < (24 * 60 * 60)))) { // Allow max 24 hours sleep Settings.deepsleep = XdrvMailbox.payload; RtcSettings.nextwakeup = 0; - tele_period = Settings.tele_period -3; // Initiate start DeepSleep on next finish of forced TelePeriod + tele_period = Settings.tele_period -1; // Initiate start DeepSleep on next finish of forced TelePeriod } Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.deepsleep); } diff --git a/tasmota/xsns_19_mgs.ino b/tasmota/xsns_19_mgs.ino index 50123d6a1..4375e54ba 100644 --- a/tasmota/xsns_19_mgs.ino +++ b/tasmota/xsns_19_mgs.ino @@ -35,18 +35,23 @@ #include "MutichannelGasSensor.h" +bool mgs_detected = false; + void MGSInit(void) { gas.begin(MGS_SENSOR_ADDR); } -bool MGSPrepare(void) +void MGSPrepare(void) { + if (mgs_detected) { return; } + + if (I2cActive(MGS_SENSOR_ADDR)) { return; } + gas.begin(MGS_SENSOR_ADDR); if (!gas.isError()) { + I2cSetActive(MGS_SENSOR_ADDR); AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MultiGasSensor", MGS_SENSOR_ADDR); - return true; - } else { - return false; + mgs_detected = true; } } @@ -63,6 +68,8 @@ const char HTTP_MGS_GAS[] PROGMEM = "{s}MGS %s{m}%s " D_UNIT_PARTS_PER_MILLION " void MGSShow(bool json) { + if (!mgs_detected) { return; } + char buffer[33]; if (json) { ResponseAppend_P(PSTR(",\"MGS\":{\"NH3\":%s"), measure_gas(NH3, buffer)); @@ -96,23 +103,19 @@ bool Xsns19(uint8_t function) if (!I2cEnabled(XI2C_17)) { return false; } bool result = false; - static int detected = false; switch (function) { - case FUNC_INIT: -// MGSInit(); - break; - case FUNC_PREP_BEFORE_TELEPERIOD: - detected = MGSPrepare(); - break; case FUNC_JSON_APPEND: - if (detected) MGSShow(1); + MGSShow(1); break; #ifdef USE_WEBSERVER case FUNC_WEB_SENSOR: - if (detected) MGSShow(0); + MGSShow(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + MGSPrepare(); + break; } return result; }