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} =