Refactor I2C driver detection phase 3

This commit is contained in:
Theo Arends 2019-11-07 17:08:58 +01:00
parent f32b64d5de
commit 1f521ed748
4 changed files with 19 additions and 22 deletions

View File

@ -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,

View File

@ -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();

View File

@ -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);
}

View File

@ -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;
}