From 24b71c5ba334fb7d1baf5032fe52e8fda0b77675 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 11 Nov 2019 17:32:44 +0100 Subject: [PATCH] Refactor I2C driver detection phase 6 --- tasmota/xdrv_15_pca9685.ino | 33 +++++++------ tasmota/xdrv_28_pcf8574.ino | 34 +++++++------- tasmota/xsns_07_sht1x.ino | 39 +++++++--------- tasmota/xsns_08_htu21.ino | 43 +++++++---------- tasmota/xsns_09_bmp.ino | 40 ++++++---------- tasmota/xsns_10_bh1750.ino | 47 ++++++++----------- tasmota/xsns_11_veml6070.ino | 88 +++++++++++++++------------------- tasmota/xsns_12_ads1115.ino | 30 ++++++------ tasmota/xsns_13_ina219.ino | 64 ++++++++++++------------- tasmota/xsns_14_sht3x.ino | 74 ++++++++++++++--------------- tasmota/xsns_16_tsl2561.ino | 45 ++++++++---------- tasmota/xsns_19_mgs.ino | 30 ++++++------ tasmota/xsns_21_sgp30.ino | 72 ++++++++++++++-------------- tasmota/xsns_24_si1145.ino | 74 ++++++++++++++++++----------- tasmota/xsns_26_lm75ad.ino | 49 ++++++++++--------- tasmota/xsns_29_mcp230xx.ino | 91 +++++++++++++++++------------------- tasmota/xsns_30_mpr121.ino | 54 +++++++++++---------- 17 files changed, 432 insertions(+), 475 deletions(-) diff --git a/tasmota/xdrv_15_pca9685.ino b/tasmota/xdrv_15_pca9685.ino index 66bd9253b..b973d78eb 100644 --- a/tasmota/xdrv_15_pca9685.ino +++ b/tasmota/xdrv_15_pca9685.ino @@ -45,7 +45,6 @@ uint16_t pca9685_pin_pwm_value[16]; void PCA9685_Detect(void) { - if (pca9685_detected) { return; } if (I2cActive(USE_PCA9685_ADDR)) { return; } uint8_t buffer; @@ -175,8 +174,6 @@ bool PCA9685_Command(void) void PCA9685_OutputTelemetry(bool telemetry) { - if (!pca9685_detected) { return; } // We do not do this if the PCA9685 has not been detected - ResponseTime_P(PSTR(",\"PCA9685\":{\"PWM_FREQ\":%i,"),pca9685_freq); for (uint32_t pin=0;pin<16;pin++) { ResponseAppend_P(PSTR("\"PWM%i\":%i,"),pin,pca9685_pin_pwm_value[pin]); @@ -193,20 +190,22 @@ bool Xdrv15(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - if (tele_period == 0) { - PCA9685_OutputTelemetry(true); - } - break; - case FUNC_COMMAND_DRIVER: - if (XDRV_15 == XdrvMailbox.index) { - result = PCA9685_Command(); - } - break; - case FUNC_INIT: - PCA9685_Detect(); - break; + if (FUNC_INIT == function) { + PCA9685_Detect(); + } + else if (pca9685_detected) { + switch (function) { + case FUNC_EVERY_SECOND: + if (tele_period == 0) { + PCA9685_OutputTelemetry(true); + } + break; + case FUNC_COMMAND_DRIVER: + if (XDRV_15 == XdrvMailbox.index) { + result = PCA9685_Command(); + } + break; + } } return result; } diff --git a/tasmota/xdrv_28_pcf8574.ino b/tasmota/xdrv_28_pcf8574.ino index 7921b11fc..9448f6e6d 100644 --- a/tasmota/xdrv_28_pcf8574.ino +++ b/tasmota/xdrv_28_pcf8574.ino @@ -40,7 +40,7 @@ struct PCF8574 { uint8_t max_connected_ports = 0; // Max numbers of devices comming from PCF8574 modules uint8_t max_devices = 0; // Max numbers of PCF8574 modules char stype[9]; - bool type = true; + bool type = false; } Pcf8574; void Pcf8574SwitchRelay(void) @@ -74,8 +74,6 @@ void Pcf8574SwitchRelay(void) void Pcf8574Init() { - Pcf8574.type = false; - uint8_t pcf8574_address = PCF8574_ADDR1; while ((Pcf8574.max_devices < MAX_PCF8574) && (pcf8574_address < PCF8574_ADDR2 +8)) { @@ -223,25 +221,27 @@ void Pcf8574SaveSettings() bool Xdrv28(uint8_t function) { - if (!I2cEnabled(XI2C_02) || !Pcf8574.type) { return false; } + if (!I2cEnabled(XI2C_02)) { return false; } bool result = false; - switch (function) { - case FUNC_SET_POWER: - Pcf8574SwitchRelay(); - break; + if (FUNC_PRE_INIT == function) { + Pcf8574Init(); + } + else if (Pcf8574.type) { + switch (function) { + case FUNC_SET_POWER: + Pcf8574SwitchRelay(); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_ADD_BUTTON: - WSContentSend_P(HTTP_BTN_MENU_PCF8574); - break; - case FUNC_WEB_ADD_HANDLER: - WebServer->on("/" WEB_HANDLE_PCF8574, HandlePcf8574); - break; + case FUNC_WEB_ADD_BUTTON: + WSContentSend_P(HTTP_BTN_MENU_PCF8574); + break; + case FUNC_WEB_ADD_HANDLER: + WebServer->on("/" WEB_HANDLE_PCF8574, HandlePcf8574); + break; #endif // USE_WEBSERVER - case FUNC_PRE_INIT: - Pcf8574Init(); - break; + } } return result; } diff --git a/tasmota/xsns_07_sht1x.ino b/tasmota/xsns_07_sht1x.ino index 75df93671..eab315aa2 100644 --- a/tasmota/xsns_07_sht1x.ino +++ b/tasmota/xsns_07_sht1x.ino @@ -85,7 +85,7 @@ bool ShtSendCommand(const uint8_t cmd) ackerror = true; } if (ackerror) { - sht_type = 0; +// sht_type = 0; AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_DID_NOT_ACK_COMMAND)); } return (!ackerror); @@ -101,7 +101,7 @@ bool ShtAwaitResult(void) delay(20); } AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_BUSY)); - sht_type = 0; +// sht_type = 0; return false; } @@ -159,10 +159,6 @@ bool ShtRead(void) void ShtDetect(void) { - if (sht_type) { - return; - } - sht_sda_pin = pin[GPIO_I2C_SDA]; sht_scl_pin = pin[GPIO_I2C_SCL]; if (ShtRead()) { @@ -176,11 +172,10 @@ void ShtDetect(void) void ShtEverySecond(void) { - if (sht_type && !(uptime %4)) { // Update every 4 seconds + if (!(uptime %4)) { // Every 4 seconds // 344mS if (!ShtRead()) { AddLogMissed(sht_types, sht_valid); -// if (!sht_valid) { sht_type = 0; } } } } @@ -225,21 +220,23 @@ bool Xsns07(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - ShtEverySecond(); - break; - case FUNC_JSON_APPEND: - ShtShow(1); - break; + if (FUNC_INIT == function) { + ShtDetect(); + } + else if (sht_type) { + switch (function) { + case FUNC_EVERY_SECOND: + ShtEverySecond(); + break; + case FUNC_JSON_APPEND: + ShtShow(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - ShtShow(0); - break; + case FUNC_WEB_SENSOR: + ShtShow(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: // Move detection to restart only removing interference - ShtDetect(); - break; + } } return result; } diff --git a/tasmota/xsns_08_htu21.ino b/tasmota/xsns_08_htu21.ino index e10b79d94..ec5a5c1ac 100644 --- a/tasmota/xsns_08_htu21.ino +++ b/tasmota/xsns_08_htu21.ino @@ -197,8 +197,6 @@ bool HtuRead(void) void HtuDetect(void) { - if (htu_type) { return; } - htu_address = HTU21_ADDR; if (I2cActive(htu_address)) { return; } @@ -232,17 +230,10 @@ void HtuDetect(void) void HtuEverySecond(void) { - if (92 == (uptime %100)) { - // 1mS - HtuDetect(); - } - else if (uptime &1) { + if (uptime &1) { // Every 2 seconds // HTU21: 68mS, SI70xx: 37mS - if (htu_type) { - if (!HtuRead()) { - AddLogMissed(htu_types, htu_valid); -// if (!htu_valid) { htu_type = 0; } - } + if (!HtuRead()) { + AddLogMissed(htu_types, htu_valid); } } } @@ -287,21 +278,23 @@ bool Xsns08(uint8_t function) bool result = false; - switch (function) { - case FUNC_INIT: - HtuDetect(); - break; - case FUNC_EVERY_SECOND: - HtuEverySecond(); - break; - case FUNC_JSON_APPEND: - HtuShow(1); - break; + if (FUNC_INIT == function) { + HtuDetect(); + } + else if (htu_type) { + switch (function) { + case FUNC_EVERY_SECOND: + HtuEverySecond(); + break; + case FUNC_JSON_APPEND: + HtuShow(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - HtuShow(0); - break; + case FUNC_WEB_SENSOR: + HtuShow(0); + break; #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_09_bmp.ino b/tasmota/xsns_09_bmp.ino index 60fde6e03..9eeb70bd4 100644 --- a/tasmota/xsns_09_bmp.ino +++ b/tasmota/xsns_09_bmp.ino @@ -447,8 +447,6 @@ void Bme680Read(uint8_t bmp_idx) void BmpDetect(void) { - if (bmp_count) { return; } - int bmp_sensor_size = BMP_MAX_SENSORS * sizeof(bmp_sensors_t); if (!bmp_sensors) { bmp_sensors = (bmp_sensors_t*)malloc(bmp_sensor_size); @@ -493,8 +491,6 @@ void BmpDetect(void) void BmpRead(void) { - if (!bmp_sensors) { return; } - for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { switch (bmp_sensors[bmp_idx].bmp_type) { case BMP180_CHIPID: @@ -515,16 +511,8 @@ void BmpRead(void) ConvertHumidity(bmp_sensors[0].bmp_humidity); // Set global humidity } -void BmpEverySecond(void) -{ - // 2mS - BmpRead(); -} - void BmpShow(bool json) { - if (!bmp_sensors) { return; } - for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { if (bmp_sensors[bmp_idx].bmp_type) { float bmp_sealevel = 0.0; @@ -623,21 +611,23 @@ bool Xsns09(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - BmpEverySecond(); - break; - case FUNC_JSON_APPEND: - BmpShow(1); - break; + if (FUNC_INIT == function) { + BmpDetect(); + } + else if (bmp_count) { + switch (function) { + case FUNC_EVERY_SECOND: + BmpRead(); + break; + case FUNC_JSON_APPEND: + BmpShow(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - BmpShow(0); - break; + case FUNC_WEB_SENSOR: + BmpShow(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - BmpDetect(); - break; + } } return result; } diff --git a/tasmota/xsns_10_bh1750.ino b/tasmota/xsns_10_bh1750.ino index 50a0d1b03..cc6c9a45b 100644 --- a/tasmota/xsns_10_bh1750.ino +++ b/tasmota/xsns_10_bh1750.ino @@ -56,16 +56,14 @@ bool Bh1750Read(void) void Bh1750Detect(void) { - if (bh1750_type) { return; } - for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) { bh1750_address = bh1750_addresses[i]; if (I2cActive(bh1750_address)) { continue; } Wire.beginTransmission(bh1750_address); Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); if (!Wire.endTransmission()) { - bh1750_type = 1; I2cSetActiveFound(bh1750_address, bh1750_types); + bh1750_type = 1; break; } } @@ -73,18 +71,9 @@ void Bh1750Detect(void) void Bh1750EverySecond(void) { - if (90 == (uptime %100)) { - // 1mS - Bh1750Detect(); - } - else { - // 1mS - if (bh1750_type) { - if (!Bh1750Read()) { - AddLogMissed(bh1750_types, bh1750_valid); -// if (!bh1750_valid) { bh1750_type = 0; } - } - } + // 1mS + if (!Bh1750Read()) { + AddLogMissed(bh1750_types, bh1750_valid); } } @@ -116,21 +105,23 @@ bool Xsns10(uint8_t function) bool result = false; - switch (function) { - case FUNC_INIT: - Bh1750Detect(); - break; - case FUNC_EVERY_SECOND: - Bh1750EverySecond(); - break; - case FUNC_JSON_APPEND: - Bh1750Show(1); - break; + if (FUNC_INIT == function) { + Bh1750Detect(); + } + else if (bh1750_type) { + switch (function) { + case FUNC_EVERY_SECOND: + Bh1750EverySecond(); + break; + case FUNC_JSON_APPEND: + Bh1750Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Bh1750Show(0); - break; + case FUNC_WEB_SENSOR: + Bh1750Show(0); + break; #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_11_veml6070.ino b/tasmota/xsns_11_veml6070.ino index 3573303b8..c1ab7816e 100644 --- a/tasmota/xsns_11_veml6070.ino +++ b/tasmota/xsns_11_veml6070.ino @@ -121,7 +121,6 @@ uint8_t veml6070_addr_low = VEML6070_ADDR_L; uint8_t veml6070_addr_high = VEML6070_ADDR_H; uint8_t itime = VEML6070_INTEGRATION_TIME; uint8_t veml6070_type = 0; -uint8_t veml6070_valid = 0; char veml6070_name[9]; char str_uvrisk_text[10]; @@ -129,7 +128,6 @@ char str_uvrisk_text[10]; void Veml6070Detect(void) { - if (veml6070_type) { return; } if (I2cActive(VEML6070_ADDR_L)) { return; } // init the UV sensor @@ -142,7 +140,7 @@ void Veml6070Detect(void) Veml6070UvTableInit(); // 1[ms], initalize the UV compare table only once uint8_t veml_model = 0; GetTextIndexed(veml6070_name, sizeof(veml6070_name), veml_model, kVemlTypes); - I2cSetActiveFound(VEML6070_ADDR_L, "VEML6070"); + I2cSetActiveFound(VEML6070_ADDR_L, veml6070_name); } } @@ -171,21 +169,11 @@ void Veml6070UvTableInit(void) void Veml6070EverySecond(void) { // all = 10..15[ms] - if (11 == (uptime %100)) { - if (!veml6070_type) { -// Veml6070ModeCmd(1); // on = 1[ms], wakeup the UV sensor - THIS CORRUPTS OTHER I2C DEVICES - Veml6070Detect(); // 1[ms], check for sensor and init with IT time -// Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor - THIS CORRUPTS OTHER I2C DEVICES - } - } else { - if (veml6070_type) { - Veml6070ModeCmd(1); // 1[ms], wakeup the UV sensor - uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values - uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level - uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2 - Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor - } - } + Veml6070ModeCmd(1); // 1[ms], wakeup the UV sensor + uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values + uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level + uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2 + Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor } /********************************************************************************************/ @@ -273,34 +261,32 @@ double Veml6070UvPower(double uvrisk) void Veml6070Show(bool json) { - if (veml6070_type) { - // convert double values to string - char str_uvlevel[33]; // e.g. 99999 inc = UVLevel - dtostrfd((double)uvlevel, 0, str_uvlevel); - char str_uvrisk[33]; // e.g. 25.99 text = UvIndex - dtostrfd(uvrisk, 2, str_uvrisk); - char str_uvpower[33]; // e.g. 0.399 W/m² = UvPower - dtostrfd(uvpower, 3, str_uvpower); - if (json) { + // convert double values to string + char str_uvlevel[33]; // e.g. 99999 inc = UVLevel + dtostrfd((double)uvlevel, 0, str_uvlevel); + char str_uvrisk[33]; // e.g. 25.99 text = UvIndex + dtostrfd(uvrisk, 2, str_uvrisk); + char str_uvpower[33]; // e.g. 0.399 W/m² = UvPower + dtostrfd(uvpower, 3, str_uvpower); + if (json) { #ifdef USE_VEML6070_SHOW_RAW - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_LEVEL "\":%s,\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), - veml6070_name, str_uvlevel, str_uvrisk, str_uvrisk_text, str_uvpower); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_LEVEL "\":%s,\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), + veml6070_name, str_uvlevel, str_uvrisk, str_uvrisk_text, str_uvpower); #else - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), - veml6070_name, str_uvrisk, str_uvrisk_text, str_uvpower); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), + veml6070_name, str_uvrisk, str_uvrisk_text, str_uvpower); #endif // USE_VEML6070_SHOW_RAW #ifdef USE_DOMOTICZ - if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, uvlevel); } + if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, uvlevel); } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER - } else { + } else { #ifdef USE_VEML6070_SHOW_RAW - WSContentSend_PD(HTTP_SNS_UV_LEVEL, str_uvlevel); + WSContentSend_PD(HTTP_SNS_UV_LEVEL, str_uvlevel); #endif // USE_VEML6070_SHOW_RAW - WSContentSend_PD(HTTP_SNS_UV_INDEX, str_uvrisk, str_uvrisk_text); - WSContentSend_PD(HTTP_SNS_UV_POWER, str_uvpower); + WSContentSend_PD(HTTP_SNS_UV_INDEX, str_uvrisk, str_uvrisk_text); + WSContentSend_PD(HTTP_SNS_UV_POWER, str_uvpower); #endif // USE_WEBSERVER - } } } @@ -314,21 +300,23 @@ bool Xsns11(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values - break; - case FUNC_JSON_APPEND: - Veml6070Show(1); - break; + if (FUNC_INIT == function) { + Veml6070Detect(); + } + else if (veml6070_type) { + switch (function) { + case FUNC_EVERY_SECOND: + Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values + break; + case FUNC_JSON_APPEND: + Veml6070Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Veml6070Show(0); - break; + case FUNC_WEB_SENSOR: + Veml6070Show(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - Veml6070Detect(); // 1[ms], detect and init the sensor - break; + } } return result; } diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 289d79c0f..eb21d943f 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -163,8 +163,6 @@ int16_t Ads1115GetConversion(uint8_t channel) void Ads1115Detect(void) { - if (Ads1115.count) { return; } - for (uint32_t i = 0; i < sizeof(Ads1115.addresses); i++) { if (!Ads1115.found[i]) { Ads1115.address = Ads1115.addresses[i]; @@ -173,9 +171,9 @@ void Ads1115Detect(void) if (I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONVERT) && I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONFIG)) { Ads1115StartComparator(i, ADS1115_REG_CONFIG_MODE_CONTIN); - Ads1115.count++; - Ads1115.found[i] = 1; I2cSetActiveFound(Ads1115.address, "ADS1115"); + Ads1115.found[i] = 1; + Ads1115.count++; } } } @@ -183,8 +181,6 @@ void Ads1115Detect(void) void Ads1115Show(bool json) { - if (!Ads1115.count) { return; } - int16_t values[4]; for (uint32_t t = 0; t < sizeof(Ads1115.addresses); t++) { @@ -236,18 +232,20 @@ bool Xsns12(uint8_t function) bool result = false; - switch (function) { - case FUNC_JSON_APPEND: - Ads1115Show(1); - break; + if (FUNC_INIT == function) { + Ads1115Detect(); + } + else if (Ads1115.count) { + switch (function) { + case FUNC_JSON_APPEND: + Ads1115Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Ads1115Show(0); - break; + case FUNC_WEB_SENSOR: + Ads1115Show(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - Ads1115Detect(); - break; + } } return result; } diff --git a/tasmota/xsns_13_ina219.ino b/tasmota/xsns_13_ina219.ino index cffe6e3f8..c764c7535 100644 --- a/tasmota/xsns_13_ina219.ino +++ b/tasmota/xsns_13_ina219.ino @@ -96,6 +96,7 @@ uint8_t ina219_valid[4] = {0,0,0,0}; float ina219_voltage[4] = {0,0,0,0}; float ina219_current[4] = {0,0,0,0}; char ina219_types[] = "INA219"; +uint8_t ina219_count = 0; bool Ina219SetCalibration(uint8_t mode, uint16_t addr) { @@ -162,8 +163,7 @@ float Ina219GetCurrent_mA(uint16_t addr) bool Ina219Read(void) { for (int i=0; i= 0) && (XdrvMailbox.payload <= 2)) { Settings.ina219_mode = XdrvMailbox.payload; restart_flag = 2; } Response_P(S_JSON_SENSOR_INDEX_NVALUE, XSNS_13, Settings.ina219_mode); - return serviced; + return true; } /********************************************************************************************/ @@ -199,26 +197,20 @@ bool Ina219CommandSensor(void) void Ina219Detect(void) { for (uint32_t i = 0; i < sizeof(ina219_type); i++) { - if (ina219_type[i]) { continue; } uint16_t addr = ina219_addresses[i]; if (I2cActive(addr)) { continue; } if (Ina219SetCalibration(Settings.ina219_mode, addr)) { - ina219_type[i] = 1; I2cSetActiveFound(addr, ina219_types); + ina219_type[i] = 1; + ina219_count++; } } } void Ina219EverySecond(void) { - if (87 == (uptime %100)) { - // 4 x 2mS - Ina219Detect(); - } - else { - // 4 x 3mS - Ina219Read(); - } + // 4 x 3mS + Ina219Read(); } #ifdef USE_WEBSERVER @@ -280,26 +272,28 @@ bool Xsns13(uint8_t function) bool result = false; - switch (function) { - case FUNC_COMMAND_SENSOR: - if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) { - result = Ina219CommandSensor(); - } - break; - case FUNC_EVERY_SECOND: - Ina219EverySecond(); - break; - case FUNC_JSON_APPEND: - Ina219Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Ina219Show(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - Ina219Detect(); - break; + if (FUNC_INIT == function) { + Ina219Detect(); + } + else if (ina219_count) { + switch (function) { + case FUNC_COMMAND_SENSOR: + if (XSNS_13 == XdrvMailbox.index) { + result = Ina219CommandSensor(); + } + break; + case FUNC_EVERY_SECOND: + Ina219EverySecond(); + break; + case FUNC_JSON_APPEND: + Ina219Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Ina219Show(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_14_sht3x.ino b/tasmota/xsns_14_sht3x.ino index bafaf84c1..3f7c1ce9b 100644 --- a/tasmota/xsns_14_sht3x.ino +++ b/tasmota/xsns_14_sht3x.ino @@ -79,12 +79,10 @@ bool Sht3xRead(float &t, float &h, uint8_t sht3x_address) void Sht3xDetect(void) { - if (sht3x_count) return; - - float t; - float h; for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) { if (I2cActive(sht3x_addresses[i])) { continue; } + float t; + float h; if (Sht3xRead(t, h, sht3x_addresses[i])) { sht3x_sensors[sht3x_count].address = sht3x_addresses[i]; GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), i, kShtTypes); @@ -96,39 +94,37 @@ void Sht3xDetect(void) void Sht3xShow(bool json) { - if (sht3x_count) { + for (uint32_t i = 0; i < sht3x_count; i++) { float t; float h; - char types[11]; - for (uint32_t i = 0; i < sht3x_count; i++) { - if (Sht3xRead(t, h, sht3x_sensors[i].address)) { - char temperature[33]; - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); - char humidity[33]; - dtostrfd(h, Settings.flag2.humidity_resolution, humidity); - snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX" + if (Sht3xRead(t, h, sht3x_sensors[i].address)) { + char temperature[33]; + dtostrfd(t, Settings.flag2.temperature_resolution, temperature); + char humidity[33]; + dtostrfd(h, Settings.flag2.humidity_resolution, humidity); + char types[11]; + snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX" - if (json) { - ResponseAppend_P(JSON_SNS_TEMPHUM, types, temperature, humidity); + if (json) { + ResponseAppend_P(JSON_SNS_TEMPHUM, types, temperature, humidity); #ifdef USE_DOMOTICZ - if ((0 == tele_period) && (0 == i)) { // We want the same first sensor to report to Domoticz in case a read is missed - DomoticzTempHumSensor(temperature, humidity); - } + if ((0 == tele_period) && (0 == i)) { // We want the same first sensor to report to Domoticz in case a read is missed + DomoticzTempHumSensor(temperature, humidity); + } #endif // USE_DOMOTICZ #ifdef USE_KNX - if (0 == tele_period) { - KnxSensor(KNX_TEMPERATURE, t); - KnxSensor(KNX_HUMIDITY, h); - } + if (0 == tele_period) { + KnxSensor(KNX_TEMPERATURE, t); + KnxSensor(KNX_HUMIDITY, h); + } #endif // USE_KNX #ifdef USE_WEBSERVER - } else { - WSContentSend_PD(HTTP_SNS_TEMP, types, temperature, TempUnit()); - WSContentSend_PD(HTTP_SNS_HUM, types, humidity); + } else { + WSContentSend_PD(HTTP_SNS_TEMP, types, temperature, TempUnit()); + WSContentSend_PD(HTTP_SNS_HUM, types, humidity); #endif // USE_WEBSERVER - } } } } @@ -144,18 +140,20 @@ bool Xsns14(uint8_t function) bool result = false; - switch (function) { - case FUNC_JSON_APPEND: - Sht3xShow(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Sht3xShow(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - Sht3xDetect(); - break; + if (FUNC_INIT == function) { + Sht3xDetect(); + } + else if (sht3x_count) { + switch (function) { + case FUNC_JSON_APPEND: + Sht3xShow(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Sht3xShow(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_16_tsl2561.ino b/tasmota/xsns_16_tsl2561.ino index f1967cda1..2c17469d2 100644 --- a/tasmota/xsns_16_tsl2561.ino +++ b/tasmota/xsns_16_tsl2561.ino @@ -64,10 +64,8 @@ bool Tsl2561Read(void) void Tsl2561Detect(void) { - if (tsl2561_type) { return; } - uint8_t id; - if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) { + uint8_t id; Tsl.begin(); if (!Tsl.id(id)) return; if (Tsl.on()) { @@ -79,17 +77,10 @@ void Tsl2561Detect(void) void Tsl2561EverySecond(void) { - if (90 == (uptime %100)) { - // 1mS - Tsl2561Detect(); - } - else if (!(uptime %2)) { // Update every 2 seconds + if (!(uptime %2)) { // Every 2 seconds // ?mS - 4Sec - if (tsl2561_type) { - if (!Tsl2561Read()) { - AddLogMissed(tsl2561_types, tsl2561_valid); - if (!tsl2561_valid) { tsl2561_type = 0; } - } + if (!Tsl2561Read()) { + AddLogMissed(tsl2561_types, tsl2561_valid); } } } @@ -126,21 +117,23 @@ bool Xsns16(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Tsl2561EverySecond(); - break; - case FUNC_JSON_APPEND: - Tsl2561Show(1); - break; + if (FUNC_INIT == function) { + Tsl2561Detect(); + } + else if (tsl2561_type) { + switch (function) { + case FUNC_EVERY_SECOND: + Tsl2561EverySecond(); + break; + case FUNC_JSON_APPEND: + Tsl2561Show(1); + break; #ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Tsl2561Show(0); - break; + case FUNC_WEB_SENSOR: + Tsl2561Show(0); + break; #endif // USE_WEBSERVER - case FUNC_INIT: - Tsl2561Detect(); - break; + } } return result; } diff --git a/tasmota/xsns_19_mgs.ino b/tasmota/xsns_19_mgs.ino index 303e7a93d..3d289b99e 100644 --- a/tasmota/xsns_19_mgs.ino +++ b/tasmota/xsns_19_mgs.ino @@ -43,8 +43,6 @@ void MGSInit(void) { void MGSPrepare(void) { - if (mgs_detected) { return; } - if (I2cActive(MGS_SENSOR_ADDR)) { return; } gas.begin(MGS_SENSOR_ADDR); @@ -67,8 +65,6 @@ 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)); @@ -103,18 +99,20 @@ bool Xsns19(uint8_t function) bool result = false; - switch (function) { - case FUNC_JSON_APPEND: - MGSShow(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - MGSShow(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - MGSPrepare(); - break; + if (FUNC_INIT == function) { + MGSPrepare(); + } + else if (mgs_detected) { + switch (function) { + case FUNC_JSON_APPEND: + MGSShow(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + MGSShow(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_21_sgp30.ino b/tasmota/xsns_21_sgp30.ino index bd44986f1..721cd4300 100644 --- a/tasmota/xsns_21_sgp30.ino +++ b/tasmota/xsns_21_sgp30.ino @@ -43,7 +43,7 @@ float sgp30_abshum; void sgp30_Init(void) { - if (sgp30_type || I2cActive(SGP30_ADDRESS)) { return; } + if (I2cActive(SGP30_ADDRESS)) { return; } if (sgp.begin()) { sgp30_type = true; @@ -83,31 +83,25 @@ float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) { void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm { - if (!sgp30_type) { - if (21 == (uptime %100)) { - sgp30_Init(); - } - } else { - sgp30_ready = false; - if (!sgp.IAQmeasure()) { - return; // Measurement failed - } - if (global_update && (global_humidity > 0) && (global_temperature != 9999)) { - // abs hum in mg/m3 - sgp30_abshum=sgp30_AbsoluteHumidity(global_temperature,global_humidity,TempUnit()); - sgp.setHumidity(sgp30_abshum*1000); - } - sgp30_ready = true; + sgp30_ready = false; + if (!sgp.IAQmeasure()) { + return; // Measurement failed + } + if (global_update && (global_humidity > 0) && (global_temperature != 9999)) { + // abs hum in mg/m3 + sgp30_abshum=sgp30_AbsoluteHumidity(global_temperature,global_humidity,TempUnit()); + sgp.setHumidity(sgp30_abshum*1000); + } + sgp30_ready = true; - // these should normally be stored permanently and used for fast restart - if (!(uptime%SAVE_PERIOD)) { - // store settings every N seconds - uint16_t TVOC_base; - uint16_t eCO2_base; + // these should normally be stored permanently and used for fast restart + if (!(uptime%SAVE_PERIOD)) { + // store settings every N seconds + uint16_t TVOC_base; + uint16_t eCO2_base; - if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings + if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); - } } } @@ -157,21 +151,23 @@ bool Xsns21(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Sgp30Update(); - break; - case FUNC_JSON_APPEND: - Sgp30Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Sgp30Show(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - sgp30_Init(); - break; + if (FUNC_INIT == function) { + sgp30_Init(); + } + else if (sgp30_type) { + switch (function) { + case FUNC_EVERY_SECOND: + Sgp30Update(); + break; + case FUNC_JSON_APPEND: + Sgp30Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Sgp30Show(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_24_si1145.ino b/tasmota/xsns_24_si1145.ino index 58605cb3b..336cdbc8a 100644 --- a/tasmota/xsns_24_si1145.ino +++ b/tasmota/xsns_24_si1145.ino @@ -183,7 +183,12 @@ #define SI114X_IRQEN_PS2 0x08 #define SI114X_IRQEN_PS3 0x10 +uint16_t si1145_visible; +uint16_t si1145_infrared; +uint16_t si1145_uvindex; + bool si1145_type = false; +uint8_t si1145_valid = 0; /********************************************************************************************/ @@ -306,9 +311,22 @@ uint16_t Si1145ReadIR(void) /********************************************************************************************/ -void Si1145Update(void) +bool Si1145Read(void) { - if (si1145_type || I2cActive(SI114X_ADDR)) { return; } + if (si1145_valid) { si1145_valid--; } + + if (!Si1145Present()) { return false; } + + si1145_visible = Si1145ReadVisible(); + si1145_infrared = Si1145ReadIR(); + si1145_uvindex = Si1145ReadUV(); + si1145_valid = SENSOR_MAX_MISS; + return true; +} + +void Si1145Detect(void) +{ + if (I2cActive(SI114X_ADDR)) { return; } if (Si1145Begin()) { si1145_type = true; @@ -316,6 +334,13 @@ void Si1145Update(void) } } +void Si1145Update(void) +{ + if (!Si1145Read()) { + AddLogMissed("SI1145", si1145_valid); + } +} + #ifdef USE_WEBSERVER const char HTTP_SNS_SI1145[] PROGMEM = "{s}SI1145 " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}" // {s} = , {m} = , {e} = @@ -325,26 +350,18 @@ const char HTTP_SNS_SI1145[] PROGMEM = void Si1145Show(bool json) { - if (si1145_type && Si1145Present()) { - uint16_t visible = Si1145ReadVisible(); - uint16_t infrared = Si1145ReadIR(); - uint16_t uvindex = Si1145ReadUV(); + if (si1145_valid) { if (json) { ResponseAppend_P(PSTR(",\"SI1145\":{\"" D_JSON_ILLUMINANCE "\":%d,\"" D_JSON_INFRARED "\":%d,\"" D_JSON_UV_INDEX "\":%d.%d}"), - visible, infrared, uvindex /100, uvindex %100); + si1145_visible, si1145_infrared, si1145_uvindex /100, si1145_uvindex %100); #ifdef USE_DOMOTICZ - if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, visible); + if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, si1145_visible); #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_SNS_SI1145, visible, infrared, uvindex /100, uvindex %100); + WSContentSend_PD(HTTP_SNS_SI1145, si1145_visible, si1145_infrared, si1145_uvindex /100, si1145_uvindex %100); #endif } - } else { - if (si1145_type) { - I2cResetActive(SI114X_ADDR); - } - si1145_type = false; } } @@ -358,18 +375,23 @@ bool Xsns24(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_SECOND: - Si1145Update(); - break; - case FUNC_JSON_APPEND: - Si1145Show(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - Si1145Show(0); - break; -#endif // USE_WEBSERVER + if (FUNC_INIT == function) { + Si1145Detect(); + } + else if (si1145_type) { + switch (function) { + case FUNC_EVERY_SECOND: + Si1145Update(); + break; + case FUNC_JSON_APPEND: + Si1145Show(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Si1145Show(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_26_lm75ad.ino b/tasmota/xsns_26_lm75ad.ino index da66a2445..d1dd7bb3b 100644 --- a/tasmota/xsns_26_lm75ad.ino +++ b/tasmota/xsns_26_lm75ad.ino @@ -51,8 +51,6 @@ uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3 void LM75ADDetect(void) { - if (lm75ad_type) { return; } - for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) { lm75ad_address = lm75ad_addresses[i]; if (I2cActive(lm75ad_address)) { continue; } @@ -67,7 +65,8 @@ void LM75ADDetect(void) } } -float LM75ADGetTemp(void) { +float LM75ADGetTemp(void) +{ int16_t sign = 1; uint16_t t = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER); @@ -81,21 +80,19 @@ float LM75ADGetTemp(void) { void LM75ADShow(bool json) { - if (lm75ad_type) { - float t = LM75ADGetTemp(); - char temperature[33]; - dtostrfd(t, Settings.flag2.temperature_resolution, temperature); + float t = LM75ADGetTemp(); + char temperature[33]; + dtostrfd(t, Settings.flag2.temperature_resolution, temperature); - if (json) { - ResponseAppend_P(PSTR(",\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), temperature); + if (json) { + ResponseAppend_P(PSTR(",\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), temperature); #ifdef USE_DOMOTICZ - if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature); + if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature); #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER - } else { - WSContentSend_PD(HTTP_SNS_TEMP, "LM75AD", temperature, TempUnit()); + } else { + WSContentSend_PD(HTTP_SNS_TEMP, "LM75AD", temperature, TempUnit()); #endif // USE_WEBSERVER - } } } @@ -109,18 +106,20 @@ bool Xsns26(uint8_t function) bool result = false; - switch (function) { - case FUNC_JSON_APPEND: - LM75ADShow(1); - break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - LM75ADShow(0); - break; -#endif // USE_WEBSERVER - case FUNC_INIT: - LM75ADDetect(); - break; + if (FUNC_INIT == function) { + LM75ADDetect(); + } + else if (lm75ad_type) { + switch (function) { + case FUNC_JSON_APPEND: + LM75ADShow(1); + break; + #ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + LM75ADShow(0); + break; + #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_29_mcp230xx.ino b/tasmota/xsns_29_mcp230xx.ino index 693336dcf..37776b626 100644 --- a/tasmota/xsns_29_mcp230xx.ino +++ b/tasmota/xsns_29_mcp230xx.ino @@ -140,9 +140,10 @@ uint8_t MCP230xx_readGPIO(uint8_t port) { return I2cRead8(USE_MCP230xx_ADDR, MCP230xx_GPIO + port); } -void MCP230xx_ApplySettings(void) { +void MCP230xx_ApplySettings(void) +{ uint8_t int_en = 0; - for (uint32_t mcp230xx_port=0;mcp230xx_port 0) { if (I2cValidRead8(&mcp230xx_intcap, USE_MCP230xx_ADDR, MCP230xx_INTCAP+mcp230xx_port)) { @@ -323,20 +324,16 @@ void MCP230xx_CheckForInterrupt(void) { void MCP230xx_Show(bool json) { - if (mcp230xx_type) { - if (json) { - if (mcp230xx_type > 0) { // we have at least 8 pins - uint8_t gpio = MCP230xx_readGPIO(0); - ResponseAppend_P(PSTR(",\"MCP230XX\":{\"D0\":%i,\"D1\":%i,\"D2\":%i,\"D3\":%i,\"D4\":%i,\"D5\":%i,\"D6\":%i,\"D7\":%i"), - (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1); - if (2 == mcp230xx_type) { - gpio = MCP230xx_readGPIO(1); - ResponseAppend_P(PSTR(",\"D8\":%i,\"D9\":%i,\"D10\":%i,\"D11\":%i,\"D12\":%i,\"D13\":%i,\"D14\":%i,\"D15\":%i"), - (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1); - } - ResponseJsonEnd(); - } + if (json) { + uint8_t gpio = MCP230xx_readGPIO(0); + ResponseAppend_P(PSTR(",\"MCP230XX\":{\"D0\":%i,\"D1\":%i,\"D2\":%i,\"D3\":%i,\"D4\":%i,\"D5\":%i,\"D6\":%i,\"D7\":%i"), + (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1); + if (2 == mcp230xx_type) { + gpio = MCP230xx_readGPIO(1); + ResponseAppend_P(PSTR(",\"D8\":%i,\"D9\":%i,\"D10\":%i,\"D11\":%i,\"D12\":%i,\"D13\":%i,\"D14\":%i,\"D15\":%i"), + (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1); } + ResponseJsonEnd(); } } @@ -423,8 +420,6 @@ void MCP230xx_Reset(uint8_t pinmode) { bool MCP230xx_Command(void) { - if (!mcp230xx_type) { return false; } - bool serviced = true; bool validpin = false; uint8_t paramcount = 0; @@ -699,8 +694,6 @@ const char HTTP_SNS_MCP230xx_OUTPUT[] PROGMEM = "{s}MCP230XX D%d{m}%s{e}"; // {s void MCP230xx_UpdateWebData(void) { - if (!mcp230xx_type) { return; } - uint8_t gpio1 = MCP230xx_readGPIO(0); uint8_t gpio2 = 0; if (2 == mcp230xx_type) { @@ -720,8 +713,8 @@ void MCP230xx_UpdateWebData(void) #ifdef USE_MCP230xx_OUTPUT -void MCP230xx_OutputTelemetry(void) { - if (0 == mcp230xx_type) { return; } // We do not do this if the MCP has not been detected +void MCP230xx_OutputTelemetry(void) +{ uint8_t outputcount = 0; uint16_t gpiototal = 0; uint8_t gpioa = 0; @@ -785,19 +778,21 @@ bool Xsns29(uint8_t function) bool result = false; - switch (function) { - case FUNC_EVERY_50_MSECOND: - if ((mcp230xx_int_en) && (mcp230xx_type)) { // Only check for interrupts if its enabled on one of the pins - mcp230xx_int_prio_counter++; - if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) { - MCP230xx_CheckForInterrupt(); - mcp230xx_int_prio_counter=0; - } - } - break; - case FUNC_EVERY_SECOND: + if (FUNC_INIT == function) { MCP230xx_Detect(); - if (mcp230xx_type) { + } + else if (mcp230xx_type) { + switch (function) { + case FUNC_EVERY_50_MSECOND: + if (mcp230xx_int_en) { // Only check for interrupts if its enabled on one of the pins + mcp230xx_int_prio_counter++; + if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) { + MCP230xx_CheckForInterrupt(); + mcp230xx_int_prio_counter=0; + } + } + break; + case FUNC_EVERY_SECOND: if (mcp230xx_int_counter_en) { mcp230xx_int_sec_counter++; if (mcp230xx_int_sec_counter >= Settings.mcp230xx_int_timer) { // Interrupt counter interval reached, lets report @@ -812,25 +807,25 @@ bool Xsns29(uint8_t function) MCP230xx_OutputTelemetry(); #endif // USE_MCP230xx_OUTPUT } - } - break; - case FUNC_JSON_APPEND: - MCP230xx_Show(1); - break; - case FUNC_COMMAND_SENSOR: - if (XSNS_29 == XdrvMailbox.index) { - result = MCP230xx_Command(); - } - break; + break; + case FUNC_JSON_APPEND: + MCP230xx_Show(1); + break; + case FUNC_COMMAND_SENSOR: + if (XSNS_29 == XdrvMailbox.index) { + result = MCP230xx_Command(); + } + break; #ifdef USE_WEBSERVER #ifdef USE_MCP230xx_OUTPUT #ifdef USE_MCP230xx_DISPLAYOUTPUT - case FUNC_WEB_SENSOR: - MCP230xx_UpdateWebData(); - break; + case FUNC_WEB_SENSOR: + MCP230xx_UpdateWebData(); + break; #endif // USE_MCP230xx_DISPLAYOUTPUT #endif // USE_MCP230xx_OUTPUT #endif // USE_WEBSERVER + } } return result; } diff --git a/tasmota/xsns_30_mpr121.ino b/tasmota/xsns_30_mpr121.ino index 12df09909..eb6b82993 100644 --- a/tasmota/xsns_30_mpr121.ino +++ b/tasmota/xsns_30_mpr121.ino @@ -199,29 +199,35 @@ struct mpr121 { uint16_t previous[4] = { 0x0000, 0x0000, 0x0000, 0x0000 }; /** Current values in electrode register of sensor */ }; +bool mpr21_found = false; /** * The function Mpr121Init() soft-resets, detects and configures up to 4x MPR121 sensors. * * @param struct *pS Struct with MPR121 status and data. + * bool initial true - Initial call, false - next calls * @return void * @pre None. * @post None. * */ -void Mpr121Init(struct mpr121 *pS) +void Mpr121Init(struct mpr121 *pS, bool initial) { - // Loop through I2C addresses for (uint32_t i = 0; i < sizeof(pS->i2c_addr[i]); i++) { + if (initial && I2cActive(pS->i2c_addr[i])) { continue; } + // Soft reset sensor and check if connected at I2C address pS->connected[i] = (I2cWrite8(pS->i2c_addr[i], MPR121_SRST_REG, MPR121_SRST_VAL) && (0x24 == I2cRead8(pS->i2c_addr[i], 0x5D))); if (pS->connected[i]) { // Log sensor found - AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_I2C "MPR121(%c) " D_FOUND_AT " 0x%X"), pS->id[i], pS->i2c_addr[i]); + mpr21_found = true; + char device_name[16]; + snprintf_P(device_name, sizeof(device_name), PSTR("MPR121(%c)"), pS->id[i]); + I2cSetActiveFound(pS->i2c_addr[i], device_name); // Set thresholds for registers 0x41 - 0x5A (ExTTH and ExRTH) for (uint32_t j = 0; j < 13; j++) { @@ -326,7 +332,7 @@ void Mpr121Show(struct mpr121 *pS, uint8_t function) // Read data if (!I2cValidRead16LE(&pS->current[i], pS->i2c_addr[i], MPR121_ELEX_REG)) { AddLog_P2(LOG_LEVEL_ERROR, PSTR(D_LOG_I2C "MPR121%c: ERROR: Cannot read data!"), pS->id[i]); - Mpr121Init(pS); + Mpr121Init(pS, false); return; } // Check if OVCF bit is set @@ -335,7 +341,7 @@ void Mpr121Show(struct mpr121 *pS, uint8_t function) // Clear OVCF bit I2cWrite8(pS->i2c_addr[i], MPR121_ELEX_REG, 0x00); AddLog_P2(LOG_LEVEL_ERROR, PSTR(D_LOG_I2C "MPR121%c: ERROR: Excess current detected! Fix circuits if it happens repeatedly! Soft-resetting MPR121 ..."), pS->id[i]); - Mpr121Init(pS); + Mpr121Init(pS, false); return; } } @@ -402,36 +408,36 @@ bool Xsns30(uint8_t function) { if (!I2cEnabled(XI2C_23)) { return false; } - // ??? bool result = false; // Sensor state/data struct static struct mpr121 mpr121; - // Check if I2C is enabled - switch (function) { - + if (FUNC_INIT == function) { // Initialize Sensors - case FUNC_INIT: - Mpr121Init(&mpr121); - break; + Mpr121Init(&mpr121, true); + } + else if (mpr21_found) { - // Run ever 50 milliseconds (near real-time functions) - case FUNC_EVERY_50_MSECOND: - Mpr121Show(&mpr121, FUNC_EVERY_50_MSECOND); - break; + switch (function) { - // Generate JSON telemetry string - case FUNC_JSON_APPEND: - Mpr121Show(&mpr121, FUNC_JSON_APPEND); - break; + // Run ever 50 milliseconds (near real-time functions) + case FUNC_EVERY_50_MSECOND: + Mpr121Show(&mpr121, FUNC_EVERY_50_MSECOND); + break; + + // Generate JSON telemetry string + case FUNC_JSON_APPEND: + Mpr121Show(&mpr121, FUNC_JSON_APPEND); + break; #ifdef USE_WEBSERVER - // Show sensor data on main web page - case FUNC_WEB_SENSOR: - Mpr121Show(&mpr121, FUNC_WEB_SENSOR); - break; + // Show sensor data on main web page + case FUNC_WEB_SENSOR: + Mpr121Show(&mpr121, FUNC_WEB_SENSOR); + break; #endif // USE_WEBSERVER + } } // Return bool result return result;