From 492260cb7e393a16561892740872812d8b399e23 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 9 Nov 2019 18:34:22 +0100 Subject: [PATCH] Refactor I2C driver detection phase 4 --- tasmota/support.ino | 6 +++ tasmota/xdrv_15_pca9685.ino | 3 +- tasmota/xdrv_28_pcf8574.ino | 2 +- tasmota/xdsp_01_lcd.ino | 2 +- tasmota/xdsp_02_ssd1306.ino | 2 +- tasmota/xdsp_03_matrix.ino | 2 +- tasmota/xdsp_07_sh1106.ino | 2 +- tasmota/xnrg_07_ade7953.ino | 2 +- tasmota/xsns_08_htu21.ino | 3 +- tasmota/xsns_09_bmp.ino | 3 +- tasmota/xsns_10_bh1750.ino | 3 +- tasmota/xsns_11_veml6070.ino | 5 +-- tasmota/xsns_12_ads1115.ino | 3 +- tasmota/xsns_13_ina219.ino | 3 +- tasmota/xsns_14_sht3x.ino | 3 +- tasmota/xsns_16_tsl2561.ino | 2 +- tasmota/xsns_19_mgs.ino | 3 +- tasmota/xsns_21_sgp30.ino | 73 +++++++++++++++++----------------- tasmota/xsns_24_si1145.ino | 17 ++++---- tasmota/xsns_26_lm75ad.ino | 5 ++- tasmota/xsns_27_apds9960.ino | 37 ++++++----------- tasmota/xsns_29_mcp230xx.ino | 56 ++++++++++++++------------ tasmota/xsns_31_ccs811.ino | 2 +- tasmota/xsns_32_mpu6050.ino | 2 +- tasmota/xsns_36_mgc3130.ino | 2 +- tasmota/xsns_41_max44009.ino | 2 +- tasmota/xsns_45_vl53l0x.ino | 2 +- tasmota/xsns_46_MLX90614.ino | 2 +- tasmota/xsns_48_chirp.ino | 2 +- tasmota/xsns_54_ina226.ino | 7 +--- tasmota/xsns_55_hih_series.ino | 3 +- 31 files changed, 125 insertions(+), 136 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 16eaa9c79..0a07edf07 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1508,6 +1508,12 @@ void I2cSetActive(uint32_t addr, uint32_t count = 1) // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("I2C: Active %08X,%08X,%08X,%08X"), i2c_active[0], i2c_active[1], i2c_active[2], i2c_active[3]); } +void I2cSetActiveFound(uint32_t addr, const char *types) +{ + I2cSetActive(addr); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, types, addr); +} + bool I2cActive(uint32_t addr) { addr &= 0x7F; // Max I2C address is 127 diff --git a/tasmota/xdrv_15_pca9685.ino b/tasmota/xdrv_15_pca9685.ino index 0cf1c2fd3..00c2c4cdf 100644 --- a/tasmota/xdrv_15_pca9685.ino +++ b/tasmota/xdrv_15_pca9685.ino @@ -53,9 +53,8 @@ void PCA9685_Detect(void) I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, 0x20); if (I2cValidRead8(&buffer, USE_PCA9685_ADDR, PCA9685_REG_MODE1)) { if (0x20 == buffer) { - I2cSetActive(USE_PCA9685_ADDR); pca9685_detected = 1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "PCA9685", USE_PCA9685_ADDR); + I2cSetActiveFound(USE_PCA9685_ADDR, "PCA9685"); PCA9685_Reset(); // Reset the controller } } diff --git a/tasmota/xdrv_28_pcf8574.ino b/tasmota/xdrv_28_pcf8574.ino index d8e2daaef..7921b11fc 100644 --- a/tasmota/xdrv_28_pcf8574.ino +++ b/tasmota/xdrv_28_pcf8574.ino @@ -91,7 +91,7 @@ void Pcf8574Init() if (pcf8574_address >= PCF8574_ADDR2) { strcpy(Pcf8574.stype, "PCF8574A"); } - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, Pcf8574.stype, pcf8574_address); + I2cSetActiveFound(pcf8574_address, Pcf8574.stype); } pcf8574_address++; diff --git a/tasmota/xdsp_01_lcd.ino b/tasmota/xdsp_01_lcd.ino index 5b9e84c01..ce28201e9 100644 --- a/tasmota/xdsp_01_lcd.ino +++ b/tasmota/xdsp_01_lcd.ino @@ -69,7 +69,7 @@ void LcdInitDriver(void) } if (XDSP_01 == Settings.display_model) { - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "LCD", Settings.display_address[0]); + I2cSetActiveFound(Settings.display_address[0], "LCD"); Settings.display_width = Settings.display_cols[0]; Settings.display_height = Settings.display_rows; diff --git a/tasmota/xdsp_02_ssd1306.ino b/tasmota/xdsp_02_ssd1306.ino index d74e3827a..0b1fff3d0 100644 --- a/tasmota/xdsp_02_ssd1306.ino +++ b/tasmota/xdsp_02_ssd1306.ino @@ -61,7 +61,7 @@ void SSD1306InitDriver() } if (XDSP_02 == Settings.display_model) { - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "OLED", Settings.display_address[0]); + I2cSetActiveFound(Settings.display_address[0], "SSD1306"); if ((Settings.display_width != 64) && (Settings.display_width != 96) && (Settings.display_width != 128)) { Settings.display_width = 128; diff --git a/tasmota/xdsp_03_matrix.ino b/tasmota/xdsp_03_matrix.ino index f3e422be7..e5a613a29 100644 --- a/tasmota/xdsp_03_matrix.ino +++ b/tasmota/xdsp_03_matrix.ino @@ -208,7 +208,7 @@ void MatrixInitDriver(void) mtx_state = 1; for (mtx_matrices = 0; mtx_matrices < 8; mtx_matrices++) { if (Settings.display_address[mtx_matrices]) { - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "8x8Matrix", Settings.display_address[mtx_matrices]); + I2cSetActiveFound(Settings.display_address[mtx_matrices], "8x8Matrix"); matrix[mtx_matrices] = new Adafruit_8x8matrix(); matrix[mtx_matrices]->begin(Settings.display_address[mtx_matrices]); } else { diff --git a/tasmota/xdsp_07_sh1106.ino b/tasmota/xdsp_07_sh1106.ino index 13006090c..eba56a442 100644 --- a/tasmota/xdsp_07_sh1106.ino +++ b/tasmota/xdsp_07_sh1106.ino @@ -62,7 +62,7 @@ void SH1106InitDriver() } if (XDSP_07 == Settings.display_model) { - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "OLED", Settings.display_address[0]); + I2cSetActiveFound(Settings.display_address[0], "SH1106"); if (Settings.display_width != SH1106_LCDWIDTH) { Settings.display_width = SH1106_LCDWIDTH; diff --git a/tasmota/xnrg_07_ade7953.ino b/tasmota/xnrg_07_ade7953.ino index 6b587bc32..60174d0b6 100644 --- a/tasmota/xnrg_07_ade7953.ino +++ b/tasmota/xnrg_07_ade7953.ino @@ -207,7 +207,7 @@ void Ade7953DrvInit(void) Settings.energy_voltage_calibration = ADE7953_UREF; Settings.energy_current_calibration = ADE7953_IREF; } - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR); + I2cSetActiveFound(ADE7953_ADDR, "ADE7953"); Ade7953.init_step = 2; Energy.phase_count = 2; // Handle two channels as two phases diff --git a/tasmota/xsns_08_htu21.ino b/tasmota/xsns_08_htu21.ino index 63f5825a3..e10b79d94 100644 --- a/tasmota/xsns_08_htu21.ino +++ b/tasmota/xsns_08_htu21.ino @@ -204,7 +204,6 @@ void HtuDetect(void) htu_type = HtuReadDeviceId(); if (htu_type) { - I2cSetActive(htu_address); uint8_t index = 0; HtuInit(); switch (htu_type) { @@ -227,7 +226,7 @@ void HtuDetect(void) htu_delay_humidity = 23; } GetTextIndexed(htu_types, sizeof(htu_types), index, kHtuTypes); - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, htu_types, htu_address); + I2cSetActiveFound(htu_address, htu_types); } } diff --git a/tasmota/xsns_09_bmp.ino b/tasmota/xsns_09_bmp.ino index b88c6c130..60167b457 100644 --- a/tasmota/xsns_09_bmp.ino +++ b/tasmota/xsns_09_bmp.ino @@ -483,9 +483,8 @@ void BmpDetect(void) #endif // USE_BME680 } if (success) { - I2cSetActive(bmp_sensors[bmp_count].bmp_address); GetTextIndexed(bmp_sensors[bmp_count].bmp_name, sizeof(bmp_sensors[bmp_count].bmp_name), bmp_sensors[bmp_count].bmp_model, kBmpTypes); - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, bmp_sensors[bmp_count].bmp_name, bmp_sensors[bmp_count].bmp_address); + I2cSetActiveFound(bmp_sensors[bmp_count].bmp_address, bmp_sensors[bmp_count].bmp_name); bmp_count++; } } diff --git a/tasmota/xsns_10_bh1750.ino b/tasmota/xsns_10_bh1750.ino index 0dc79e5f6..50a0d1b03 100644 --- a/tasmota/xsns_10_bh1750.ino +++ b/tasmota/xsns_10_bh1750.ino @@ -64,9 +64,8 @@ void Bh1750Detect(void) Wire.beginTransmission(bh1750_address); Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); if (!Wire.endTransmission()) { - I2cSetActive(bh1750_address); bh1750_type = 1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, bh1750_types, bh1750_address); + I2cSetActiveFound(bh1750_address, bh1750_types); break; } } diff --git a/tasmota/xsns_11_veml6070.ino b/tasmota/xsns_11_veml6070.ino index eaf3bdb25..3573303b8 100644 --- a/tasmota/xsns_11_veml6070.ino +++ b/tasmota/xsns_11_veml6070.ino @@ -138,12 +138,11 @@ void Veml6070Detect(void) uint8_t status = Wire.endTransmission(); // action on status if (!status) { - I2cSetActive(VEML6070_ADDR_L); veml6070_type = 1; Veml6070UvTableInit(); // 1[ms], initalize the UV compare table only once uint8_t veml_model = 0; GetTextIndexed(veml6070_name, sizeof(veml6070_name), veml_model, kVemlTypes); - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "VEML6070", VEML6070_ADDR_L); + I2cSetActiveFound(VEML6070_ADDR_L, "VEML6070"); } } @@ -200,7 +199,7 @@ void Veml6070ModeCmd(bool mode_cmd) uint8_t status = Wire.endTransmission(); // action on status if (!status) { - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "VEML6070 mode_cmd", VEML6070_ADDR_L); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "VEML6070 mode_cmd")); } } diff --git a/tasmota/xsns_12_ads1115.ino b/tasmota/xsns_12_ads1115.ino index 66406cff0..289d79c0f 100644 --- a/tasmota/xsns_12_ads1115.ino +++ b/tasmota/xsns_12_ads1115.ino @@ -173,10 +173,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); - I2cSetActive(Ads1115.address); Ads1115.count++; Ads1115.found[i] = 1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "ADS1115", Ads1115.address); + I2cSetActiveFound(Ads1115.address, "ADS1115"); } } } diff --git a/tasmota/xsns_13_ina219.ino b/tasmota/xsns_13_ina219.ino index 03df212c0..cffe6e3f8 100644 --- a/tasmota/xsns_13_ina219.ino +++ b/tasmota/xsns_13_ina219.ino @@ -203,9 +203,8 @@ void Ina219Detect(void) uint16_t addr = ina219_addresses[i]; if (I2cActive(addr)) { continue; } if (Ina219SetCalibration(Settings.ina219_mode, addr)) { - I2cSetActive(addr); ina219_type[i] = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, ina219_types, addr); + I2cSetActiveFound(addr, ina219_types); } } } diff --git a/tasmota/xsns_14_sht3x.ino b/tasmota/xsns_14_sht3x.ino index e0d531ec8..bafaf84c1 100644 --- a/tasmota/xsns_14_sht3x.ino +++ b/tasmota/xsns_14_sht3x.ino @@ -86,10 +86,9 @@ void Sht3xDetect(void) for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) { if (I2cActive(sht3x_addresses[i])) { continue; } if (Sht3xRead(t, h, sht3x_addresses[i])) { - I2cSetActive(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); - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, sht3x_sensors[sht3x_count].types, sht3x_sensors[sht3x_count].address); + I2cSetActiveFound(sht3x_sensors[sht3x_count].address, sht3x_sensors[sht3x_count].types); sht3x_count++; } } diff --git a/tasmota/xsns_16_tsl2561.ino b/tasmota/xsns_16_tsl2561.ino index 2dccb05ac..f1967cda1 100644 --- a/tasmota/xsns_16_tsl2561.ino +++ b/tasmota/xsns_16_tsl2561.ino @@ -72,7 +72,7 @@ void Tsl2561Detect(void) if (!Tsl.id(id)) return; if (Tsl.on()) { tsl2561_type = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address()); + I2cSetActiveFound(Tsl.address(), tsl2561_types); } } } diff --git a/tasmota/xsns_19_mgs.ino b/tasmota/xsns_19_mgs.ino index 4375e54ba..303e7a93d 100644 --- a/tasmota/xsns_19_mgs.ino +++ b/tasmota/xsns_19_mgs.ino @@ -49,8 +49,7 @@ void MGSPrepare(void) 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); + I2cSetActiveFound(MGS_SENSOR_ADDR, "MultiGas"); mgs_detected = true; } } diff --git a/tasmota/xsns_21_sgp30.ino b/tasmota/xsns_21_sgp30.ino index b2821f986..bd44986f1 100644 --- a/tasmota/xsns_21_sgp30.ino +++ b/tasmota/xsns_21_sgp30.ino @@ -30,22 +30,25 @@ #define XSNS_21 21 #define XI2C_18 18 // See I2CDEVICES.md +#define SGP30_ADDRESS 0x58 + #include "Adafruit_SGP30.h" Adafruit_SGP30 sgp; -uint8_t sgp30_type = 0; -uint8_t sgp30_ready = 0; +bool sgp30_type = false; +bool sgp30_ready = false; float sgp30_abshum; /********************************************************************************************/ -void sgp30_Init(void) { +void sgp30_Init(void) +{ + if (sgp30_type || I2cActive(SGP30_ADDRESS)) { return; } + if (sgp.begin()) { - sgp30_type = 1; -// snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]); -// AddLog(LOG_LEVEL_DEBUG); - snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SGP30", 0x58); - AddLog(LOG_LEVEL_DEBUG); + sgp30_type = true; +// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]); + I2cSetActiveFound(SGP30_ADDRESS, "SGP30"); } } @@ -67,13 +70,11 @@ float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) { } if (tempUnit != 'C') { - temperature = (temperature - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/ + temperature = (temperature - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/ } temp = POW_FUNC(2.718281828, (17.67 * temperature) / (temperature + 243.5)); - - //return (6.112 * temp * humidity * 2.1674) / (273.15 + temperature); //simplified version return (6.112 * temp * humidity * mw) / ((273.15 + temperature) * r); //long version } @@ -82,40 +83,39 @@ float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) { void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm { - sgp30_ready = 0; - if (!sgp.IAQmeasure() || !sgp30_type) { - // retry to init every 100 seconds + if (!sgp30_type) { if (21 == (uptime %100)) { sgp30_Init(); } - 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 = 1; + } 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; - // 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 -// snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); -// AddLog(LOG_LEVEL_DEBUG); + // 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 +// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); + } } } - #ifdef USE_WEBSERVER const char HTTP_SNS_SGP30[] PROGMEM = "{s}SGP30 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = , {m} = , {e} = "{s}SGP30 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}"; -const char HTTP_SNS_AHUM[] PROGMEM = "{s}SGP30 " "Abs Humidity" "{m}%s g/m3{e}"; +const char HTTP_SNS_AHUM[] PROGMEM = "{s}SGP30 Abs Humidity{m}%s g/m3{e}"; #endif #define D_JSON_AHUM "aHumidity" @@ -147,7 +147,6 @@ void Sgp30Show(bool json) } } - /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -159,9 +158,6 @@ bool Xsns21(uint8_t function) bool result = false; switch (function) { - case FUNC_INIT: - sgp30_Init(); - break; case FUNC_EVERY_SECOND: Sgp30Update(); break; @@ -173,6 +169,9 @@ bool Xsns21(uint8_t function) Sgp30Show(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + sgp30_Init(); + break; } return result; } diff --git a/tasmota/xsns_24_si1145.ino b/tasmota/xsns_24_si1145.ino index 2dfe17967..58605cb3b 100644 --- a/tasmota/xsns_24_si1145.ino +++ b/tasmota/xsns_24_si1145.ino @@ -183,7 +183,7 @@ #define SI114X_IRQEN_PS2 0x08 #define SI114X_IRQEN_PS3 0x10 -uint8_t si1145_type = 0; +bool si1145_type = false; /********************************************************************************************/ @@ -308,11 +308,11 @@ uint16_t Si1145ReadIR(void) void Si1145Update(void) { - if (!si1145_type) { - if (Si1145Begin()) { - si1145_type = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "SI1145", SI114X_ADDR); - } + if (si1145_type || I2cActive(SI114X_ADDR)) { return; } + + if (Si1145Begin()) { + si1145_type = true; + I2cSetActiveFound(SI114X_ADDR, "SI1145"); } } @@ -341,7 +341,10 @@ void Si1145Show(bool json) #endif } } else { - si1145_type = 0; + if (si1145_type) { + I2cResetActive(SI114X_ADDR); + } + si1145_type = false; } } diff --git a/tasmota/xsns_26_lm75ad.ino b/tasmota/xsns_26_lm75ad.ino index 4d99c286e..ea5f656eb 100644 --- a/tasmota/xsns_26_lm75ad.ino +++ b/tasmota/xsns_26_lm75ad.ino @@ -53,13 +53,14 @@ void LM75ADDetect(void) { if (lm75ad_type) { return; } - uint16_t buffer; for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) { lm75ad_address = lm75ad_addresses[i]; + if (I2cActive(lm75ad_address)) { continue; } + uint16_t buffer; if (I2cValidRead16(&buffer, lm75ad_address, LM75_THYST_REGISTER)) { if (buffer == 0x4B00) { lm75ad_type = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "LM75AD", lm75ad_address); + I2cSetActiveFound(lm75ad_address, "LM75AD"); break; } } diff --git a/tasmota/xsns_27_apds9960.ino b/tasmota/xsns_27_apds9960.ino index eab4e9a5c..1d0cd623a 100644 --- a/tasmota/xsns_27_apds9960.ino +++ b/tasmota/xsns_27_apds9960.ino @@ -67,7 +67,7 @@ uint8_t APDS9960addr; uint8_t APDS9960type = 0; -char APDS9960stype[9]; +char APDS9960stype[] = "APDS9960"; char currentGesture[6]; uint8_t gesture_mode = 1; @@ -1886,35 +1886,24 @@ void APDS9960_loop(void) } } -bool APDS9960_detect(void) +void APDS9960_detect(void) { - if (APDS9960type) { - return true; - } + if (APDS9960type || I2cActive(APDS9960_I2C_ADDR)) { return; } - bool success = false; APDS9960type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID); - if (APDS9960type == APDS9960_CHIPID_1 || APDS9960type == APDS9960_CHIPID_2) { - strcpy_P(APDS9960stype, PSTR("APDS9960")); - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, APDS9960stype, APDS9960_I2C_ADDR); if (APDS9960_init()) { - success = true; - AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "APDS9960 initialized")); + I2cSetActiveFound(APDS9960_I2C_ADDR, APDS9960stype); + enableProximitySensor(); enableGestureSensor(); + } else { + APDS9960type = 0; } - } - else { - if (APDS9960type == APDS9930_CHIPID_1 || APDS9960type == APDS9930_CHIPID_2) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("APDS9930 found at address 0x%x, unsupported chip"), APDS9960_I2C_ADDR); - } - else{ - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("APDS9960 not found at address 0x%x"), APDS9960_I2C_ADDR); - } + } else { + APDS9960type = 0; } currentGesture[0] = '\0'; - return success; } /*********************************************************************************************\ @@ -1923,9 +1912,8 @@ bool APDS9960_detect(void) void APDS9960_show(bool json) { - if (!APDS9960type) { - return; - } + if (!APDS9960type) { return; } + if (!gesture_mode && !APDS9960_overload) { char red_chr[10]; char green_chr[10]; @@ -2033,7 +2021,8 @@ bool Xsns27(uint8_t function) if (FUNC_INIT == function) { APDS9960_detect(); - } else if (APDS9960type) { + } + else if (APDS9960type) { switch (function) { case FUNC_EVERY_50_MSECOND: APDS9960_loop(); diff --git a/tasmota/xsns_29_mcp230xx.ino b/tasmota/xsns_29_mcp230xx.ino index d74fdd5a4..c7313fcd0 100644 --- a/tasmota/xsns_29_mcp230xx.ino +++ b/tasmota/xsns_29_mcp230xx.ino @@ -201,9 +201,7 @@ void MCP230xx_ApplySettings(void) { void MCP230xx_Detect(void) { - if (mcp230xx_type) { - return; - } + if (mcp230xx_type || I2cActive(USE_MCP230xx_ADDR)) { return; } uint8_t buffer; @@ -211,13 +209,13 @@ void MCP230xx_Detect(void) if (I2cValidRead8(&buffer, USE_MCP230xx_ADDR, MCP230xx_IOCON)) { if (0x00 == buffer) { mcp230xx_type = 1; // We have a MCP23008 - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MCP23008", USE_MCP230xx_ADDR); + I2cSetActiveFound(USE_MCP230xx_ADDR, "MCP23008"); mcp230xx_pincount = 8; MCP230xx_ApplySettings(); } else { if (0x80 == buffer) { mcp230xx_type = 2; // We have a MCP23017 - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MCP23017", USE_MCP230xx_ADDR); + I2cSetActiveFound(USE_MCP230xx_ADDR, "MCP23017"); mcp230xx_pincount = 16; // Reset bank mode to 0 I2cWrite8(USE_MCP230xx_ADDR, MCP230xx_IOCON, 0x00); @@ -423,7 +421,10 @@ void MCP230xx_Reset(uint8_t pinmode) { Response_P(MCP230XX_SENSOR_RESPONSE,99,pinmode,pulluptxt,intmodetxt,""); } -bool MCP230xx_Command(void) { +bool MCP230xx_Command(void) +{ + if (!mcp230xx_type) { return false; } + bool serviced = true; bool validpin = false; uint8_t paramcount = 0; @@ -696,7 +697,10 @@ bool MCP230xx_Command(void) { const char HTTP_SNS_MCP230xx_OUTPUT[] PROGMEM = "{s}MCP230XX D%d{m}%s{e}"; // {s} = , {m} = , {e} = -void MCP230xx_UpdateWebData(void) { +void MCP230xx_UpdateWebData(void) +{ + if (!mcp230xx_type) { return; } + uint8_t gpio1 = MCP230xx_readGPIO(0); uint8_t gpio2 = 0; if (2 == mcp230xx_type) { @@ -782,25 +786,6 @@ bool Xsns29(uint8_t function) bool result = false; switch (function) { - case FUNC_EVERY_SECOND: - MCP230xx_Detect(); - if (mcp230xx_int_counter_en) { - mcp230xx_int_sec_counter++; - if (mcp230xx_int_sec_counter >= Settings.mcp230xx_int_timer) { // Interrupt counter interval reached, lets report - MCP230xx_Interrupt_Counter_Report(); - } - } - if (tele_period == 0) { - if (mcp230xx_int_retainer_en) { // We have pins configured for interrupt retain reporting - MCP230xx_Interrupt_Retain_Report(); - } - } -#ifdef USE_MCP230xx_OUTPUT - if (tele_period == 0) { - MCP230xx_OutputTelemetry(); - } -#endif // USE_MCP230xx_OUTPUT - break; 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++; @@ -810,6 +795,25 @@ bool Xsns29(uint8_t function) } } break; + case FUNC_EVERY_SECOND: + MCP230xx_Detect(); + if (mcp230xx_type) { + if (mcp230xx_int_counter_en) { + mcp230xx_int_sec_counter++; + if (mcp230xx_int_sec_counter >= Settings.mcp230xx_int_timer) { // Interrupt counter interval reached, lets report + MCP230xx_Interrupt_Counter_Report(); + } + } + if (tele_period == 0) { + if (mcp230xx_int_retainer_en) { // We have pins configured for interrupt retain reporting + MCP230xx_Interrupt_Retain_Report(); + } +#ifdef USE_MCP230xx_OUTPUT + MCP230xx_OutputTelemetry(); +#endif // USE_MCP230xx_OUTPUT + } + } + break; case FUNC_JSON_APPEND: MCP230xx_Show(1); break; diff --git a/tasmota/xsns_31_ccs811.ino b/tasmota/xsns_31_ccs811.ino index 34c4d3a7a..f310f01cc 100644 --- a/tasmota/xsns_31_ccs811.ino +++ b/tasmota/xsns_31_ccs811.ino @@ -53,7 +53,7 @@ void CCS811Update(void) // Perform every n second sint8_t res = ccs.begin(CCS811_ADDRESS); if (!res) { CCS811_type = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "CCS811", 0x5A); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CCS811", 0x5A); } else { //AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res); } diff --git a/tasmota/xsns_32_mpu6050.ino b/tasmota/xsns_32_mpu6050.ino index 399b8433a..cb3782167 100644 --- a/tasmota/xsns_32_mpu6050.ino +++ b/tasmota/xsns_32_mpu6050.ino @@ -145,7 +145,7 @@ void MPU_6050Detect(void) if (MPU_6050_found) { - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, D_SENSOR_MPU6050, MPU_6050_address); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, D_SENSOR_MPU6050, MPU_6050_address); } } diff --git a/tasmota/xsns_36_mgc3130.ino b/tasmota/xsns_36_mgc3130.ino index 07969f894..c3915d437 100644 --- a/tasmota/xsns_36_mgc3130.ino +++ b/tasmota/xsns_36_mgc3130.ino @@ -502,7 +502,7 @@ bool MGC3130_detect(void) success = MGC3130_receiveMessage(); // This should read the firmware info if (success) { strcpy_P(MGC3130stype, PSTR("MGC3130")); - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, MGC3130stype, MGC3130_I2C_ADDR); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, MGC3130stype, MGC3130_I2C_ADDR); MGC3130_currentGesture[0] = '\0'; MGC3130_type = true; } else { diff --git a/tasmota/xsns_41_max44009.ino b/tasmota/xsns_41_max44009.ino index a3dc2d731..c59e4fc09 100644 --- a/tasmota/xsns_41_max44009.ino +++ b/tasmota/xsns_41_max44009.ino @@ -95,7 +95,7 @@ void Max4409Detect(void) Wire.write(MAX44009_CONTINUOUS_AUTO_MODE); if (0 == Wire.endTransmission()) { max44009_found = 1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, max44009_types, max44009_address); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, max44009_types, max44009_address); break; } } diff --git a/tasmota/xsns_45_vl53l0x.ino b/tasmota/xsns_45_vl53l0x.ino index acfe165f8..b97df790b 100644 --- a/tasmota/xsns_45_vl53l0x.ino +++ b/tasmota/xsns_45_vl53l0x.ino @@ -42,7 +42,7 @@ void Vl53l0Detect(void) if (!sensor.init()) { return; } - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress()); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress()); sensor.setTimeout(500); diff --git a/tasmota/xsns_46_MLX90614.ino b/tasmota/xsns_46_MLX90614.ino index 943d32a65..f4be8afb1 100644 --- a/tasmota/xsns_46_MLX90614.ino +++ b/tasmota/xsns_46_MLX90614.ino @@ -34,7 +34,7 @@ void MLX90614_Init(void) if (!I2cSetDevice(I2_ADR_IRT)) { return; } mlx_ready=1; - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT); // not needed on tasmota //Wire.begin(); diff --git a/tasmota/xsns_48_chirp.ino b/tasmota/xsns_48_chirp.ino index 8a1d366c7..dce2550ff 100644 --- a/tasmota/xsns_48_chirp.ino +++ b/tasmota/xsns_48_chirp.ino @@ -233,7 +233,7 @@ bool ChirpScan() { delay(2); chirp_sensor[chirp_found_sensors].version = ChirpReadVersion(address); if(chirp_sensor[chirp_found_sensors].version > 0) { - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "CHIRP:", address); + AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CHIRP", address); if(chirp_found_sensorsaddress = addr; @@ -282,9 +279,9 @@ void Ina226Init() Ina226SetCalibration(i); - //AddLog_P2( LOG_LEVEL_NONE, S_LOG_I2C_FOUND_AT, Ina226Str, addr ); - slavesFound++; + I2cSetActiveFound(addr, Ina226Str); + slavesFound++; } } diff --git a/tasmota/xsns_55_hih_series.ino b/tasmota/xsns_55_hih_series.ino index 298bb54d2..5ac484e91 100644 --- a/tasmota/xsns_55_hih_series.ino +++ b/tasmota/xsns_55_hih_series.ino @@ -80,8 +80,7 @@ void Hih6Detect(void) if (uptime < 2) { delay(20); } // Skip entering power on comand mode Hih6.type = Hih6Read(); if (Hih6.type) { - I2cSetActive(HIH6_ADDR); - AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, Hih6.types, HIH6_ADDR); + I2cSetActiveFound(HIH6_ADDR, Hih6.types); } }