Refactor I2c drivers

This commit is contained in:
Theo Arends 2019-11-04 10:38:05 +01:00
parent 93f0f9a6f0
commit b46c6ef3e5
39 changed files with 594 additions and 682 deletions

View File

@ -1547,7 +1547,7 @@ void CmndI2cDriver(void)
}
}
Response_P(PSTR("{\"" D_CMND_I2CDRIVER "\":"));
XI2cDriverState();
I2cDriverState();
ResponseJsonEnd();
}
#endif // USE_I2C

View File

@ -179,11 +179,10 @@ void PCA9685_OutputTelemetry(bool telemetry) {
bool Xdrv15(uint8_t function)
{
if (!XI2cEnabled(XI2C_01)) { return false; }
if (!I2cEnabled(XI2C_01)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
PCA9685_Detect();
@ -196,9 +195,6 @@ bool Xdrv15(uint8_t function)
result = PCA9685_Command();
}
break;
default:
break;
}
}
return result;
}

View File

@ -223,11 +223,10 @@ void Pcf8574SaveSettings()
bool Xdrv28(uint8_t function)
{
if (!XI2cEnabled(XI2C_02)) { return false; }
if (!I2cEnabled(XI2C_02) || !Pcf8574.type) { return false; }
bool result = false;
if (i2c_flg && Pcf8574.type) {
switch (function) {
case FUNC_SET_POWER:
Pcf8574SwitchRelay();
@ -244,7 +243,6 @@ bool Xdrv28(uint8_t function)
Pcf8574Init();
break;
}
}
return result;
}

View File

@ -190,11 +190,10 @@ void LcdRefresh(void) // Every second
bool Xdsp01(uint8_t function)
{
if (!XI2cEnabled(XI2C_03)) { return false; }
if (!I2cEnabled(XI2C_03)) { return false; }
bool result = false;
if (i2c_flg) {
if (FUNC_DISPLAY_INIT_DRIVER == function) {
LcdInitDriver();
}
@ -245,7 +244,6 @@ bool Xdsp01(uint8_t function)
#endif // USE_DISPLAY_MODES1TO5
}
}
}
return result;
}

View File

@ -174,11 +174,10 @@ void Ssd1306Refresh(void) // Every second
bool Xdsp02(byte function)
{
if (!XI2cEnabled(XI2C_04)) { return false; }
if (!I2cEnabled(XI2C_04)) { return false; }
bool result = false;
if (i2c_flg) {
if (FUNC_DISPLAY_INIT_DRIVER == function) {
SSD1306InitDriver();
}
@ -194,7 +193,6 @@ bool Xdsp02(byte function)
break;
}
}
}
return result;
}

View File

@ -331,11 +331,10 @@ void MatrixRefresh(void) // Every second
bool Xdsp03(uint8_t function)
{
if (!XI2cEnabled(XI2C_05)) { return false; }
if (!I2cEnabled(XI2C_05)) { return false; }
bool result = false;
if (i2c_flg) {
if (FUNC_DISPLAY_INIT_DRIVER == function) {
MatrixInitDriver();
}
@ -358,7 +357,6 @@ bool Xdsp03(uint8_t function)
break;
}
}
}
return result;
}

View File

@ -168,11 +168,10 @@ void SH1106Refresh(void) // Every second
bool Xdsp07(uint8_t function)
{
if (!XI2cEnabled(XI2C_06)) { return false; }
if (!I2cEnabled(XI2C_06)) { return false; }
bool result = false;
if (i2c_flg) {
if (FUNC_DISPLAY_INIT_DRIVER == function) {
SH1106InitDriver();
}
@ -189,7 +188,6 @@ bool Xdsp07(uint8_t function)
#endif // USE_DISPLAY_MODES1TO5
}
}
}
return result;
}

View File

@ -269,7 +269,7 @@ bool Ade7953Command(void)
bool Xnrg07(uint8_t function)
{
if (!XI2cEnabled(XI2C_07)) { return false; }
if (!I2cEnabled(XI2C_07)) { return false; }
bool result = false;

View File

@ -221,11 +221,10 @@ void ShtShow(bool json)
bool Xsns07(uint8_t function)
{
if (!XI2cEnabled(XI2C_08)) { return false; }
if (!I2cEnabled(XI2C_08)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
// case FUNC_PREP_BEFORE_TELEPERIOD: // As this is not a real I2C device it may interfere with other sensors
case FUNC_INIT: // Move detection to restart only removing interference
@ -243,7 +242,6 @@ bool Xsns07(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -281,11 +281,10 @@ void HtuShow(bool json)
bool Xsns08(uint8_t function)
{
if (!XI2cEnabled(XI2C_09)) { return false; }
if (!I2cEnabled(XI2C_09)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
HtuDetect();
@ -302,7 +301,6 @@ bool Xsns08(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -624,11 +624,10 @@ void BmpShow(bool json)
bool Xsns09(uint8_t function)
{
if (!XI2cEnabled(XI2C_10)) { return false; }
if (!I2cEnabled(XI2C_10)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
BmpDetect();
@ -645,7 +644,6 @@ bool Xsns09(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -113,11 +113,10 @@ void Bh1750Show(bool json)
bool Xsns10(uint8_t function)
{
if (!XI2cEnabled(XI2C_11)) { return false; }
if (!I2cEnabled(XI2C_11)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
Bh1750Detect();
@ -134,7 +133,6 @@ bool Xsns10(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -305,11 +305,11 @@ void Veml6070Show(bool json)
bool Xsns11(uint8_t function)
{
if (!XI2cEnabled(XI2C_12)) { return false; }
if (!I2cEnabled(XI2C_12) ||
(pin[GPIO_ADE7953_IRQ] < 99)) { return false; } // The ADE7953 uses I2C address 0x38 too but needs priority
bool result = false;
if (i2c_flg && !(pin[GPIO_ADE7953_IRQ] < 99)) { // The ADE7953 uses I2C address 0x38 too but needs priority
switch (function) {
case FUNC_INIT:
Veml6070Detect(); // 1[ms], detect and init the sensor
@ -327,7 +327,6 @@ bool Xsns11(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -242,11 +242,10 @@ void Ads1115Show(bool json)
bool Xsns12(uint8_t function)
{
if (!XI2cEnabled(XI2C_13)) { return false; }
if (!I2cEnabled(XI2C_13)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_PREP_BEFORE_TELEPERIOD:
Ads1115Detect();
@ -260,7 +259,6 @@ bool Xsns12(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -131,11 +131,10 @@ void Ads1115Show(bool json)
bool Xsns12(uint8_t function)
{
if (!XI2cEnabled(XI2C_13)) { return false; }
if (!I2cEnabled(XI2C_13)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_PREP_BEFORE_TELEPERIOD:
Ads1115Detect();
@ -149,7 +148,6 @@ bool Xsns12(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -276,11 +276,10 @@ void Ina219Show(bool json)
bool Xsns13(uint8_t function)
{
if (!XI2cEnabled(XI2C_14)) { return false; }
if (!I2cEnabled(XI2C_14)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_COMMAND_SENSOR:
if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) {
@ -302,7 +301,6 @@ bool Xsns13(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -139,11 +139,10 @@ void Sht3xShow(bool json)
bool Xsns14(uint8_t function)
{
if (!XI2cEnabled(XI2C_15)) { return false; }
if (!I2cEnabled(XI2C_15)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
Sht3xDetect();
@ -157,7 +156,6 @@ bool Xsns14(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -122,11 +122,10 @@ void Tsl2561Show(bool json)
bool Xsns16(uint8_t function)
{
if (!XI2cEnabled(XI2C_16)) { return false; }
if (!I2cEnabled(XI2C_16)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
Tsl2561Detect();
@ -143,7 +142,6 @@ bool Xsns16(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -93,12 +93,11 @@ void MGSShow(bool json)
bool Xsns19(uint8_t function)
{
if (!XI2cEnabled(XI2C_17)) { return false; }
if (!I2cEnabled(XI2C_17)) { return false; }
bool result = false;
static int detected = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
// MGSInit();
@ -115,7 +114,6 @@ bool Xsns19(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -154,11 +154,10 @@ void Sgp30Show(bool json)
bool Xsns21(uint8_t function)
{
if (!XI2cEnabled(XI2C_18)) { return false; }
if (!I2cEnabled(XI2C_18)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
sgp30_Init();
@ -175,7 +174,6 @@ bool Xsns21(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -351,11 +351,10 @@ void Si1145Show(bool json)
bool Xsns24(uint8_t function)
{
if (!XI2cEnabled(XI2C_19)) { return false; }
if (!I2cEnabled(XI2C_19)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
Si1145Update();
@ -369,7 +368,6 @@ bool Xsns24(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -104,11 +104,10 @@ void LM75ADShow(bool json)
bool Xsns26(uint8_t function)
{
if (!XI2cEnabled(XI2C_20)) { return false; }
if (!I2cEnabled(XI2C_20)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
LM75ADDetect();
@ -122,7 +121,6 @@ bool Xsns26(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -2027,11 +2027,10 @@ bool APDS9960CommandSensor(void)
bool Xsns27(uint8_t function)
{
if (!XI2cEnabled(XI2C_21)) { return false; }
if (!I2cEnabled(XI2C_21)) { return false; }
bool result = false;
if (i2c_flg) {
if (FUNC_INIT == function) {
APDS9960_detect();
} else if (APDS9960type) {
@ -2054,7 +2053,6 @@ bool Xsns27(uint8_t function)
#endif // USE_WEBSERVER
}
}
}
return result;
}
#endif // USE_APDS9960

View File

@ -777,11 +777,10 @@ void MCP230xx_Interrupt_Retain_Report(void) {
bool Xsns29(uint8_t function)
{
if (!XI2cEnabled(XI2C_22)) { return false; }
if (!I2cEnabled(XI2C_22)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
MCP230xx_Detect();
@ -828,9 +827,6 @@ bool Xsns29(uint8_t function)
#endif // USE_MCP230xx_DISPLAYOUTPUT
#endif // USE_MCP230xx_OUTPUT
#endif // USE_WEBSERVER
default:
break;
}
}
return result;
}

View File

@ -400,7 +400,7 @@ void Mpr121Show(struct mpr121 *pS, uint8_t function)
*/
bool Xsns30(uint8_t function)
{
if (!XI2cEnabled(XI2C_23)) { return false; }
if (!I2cEnabled(XI2C_23)) { return false; }
// ???
bool result = false;
@ -409,7 +409,6 @@ bool Xsns30(uint8_t function)
static struct mpr121 mpr121;
// Check if I2C is enabled
if (i2c_flg) {
switch (function) {
// Initialize Sensors
@ -434,7 +433,6 @@ bool Xsns30(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
// Return bool result
return result;
}

View File

@ -104,11 +104,10 @@ void CCS811Show(bool json)
bool Xsns31(uint8_t function)
{
if (!XI2cEnabled(XI2C_24)) { return false; }
if (!I2cEnabled(XI2C_24)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
CCS811Update();
@ -122,7 +121,6 @@ bool Xsns31(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -226,11 +226,10 @@ void MPU_6050Show(bool json)
bool Xsns32(uint8_t function)
{
if (!XI2cEnabled(XI2C_25)) { return false; }
if (!I2cEnabled(XI2C_25)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_PREP_BEFORE_TELEPERIOD:
MPU_6050Detect();
@ -250,7 +249,6 @@ bool Xsns32(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -134,11 +134,10 @@ void SetDS3231Time (uint32_t epoch_time) {
bool Xsns33(uint8_t function)
{
if (!XI2cEnabled(XI2C_26)) { return false; }
if (!I2cEnabled(XI2C_26)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
DS3231Detect();
@ -173,7 +172,6 @@ bool Xsns33(uint8_t function)
}
break;
}
}
return result;
}

View File

@ -603,11 +603,10 @@ bool MGC3130CommandSensor()
bool Xsns36(uint8_t function)
{
if (!XI2cEnabled(XI2C_27)) { return false; }
if (!I2cEnabled(XI2C_27)) { return false; }
bool result = false;
if (i2c_flg) {
if ((FUNC_INIT == function) && (pin[GPIO_MGC3130_XFER] < 99) && (pin[GPIO_MGC3130_RESET] < 99)) {
MGC3130_detect();
}
@ -631,7 +630,6 @@ bool Xsns36(uint8_t function)
#endif // USE_WEBSERVER
}
}
}
return result;
}
#endif // USE_MGC3130

View File

@ -150,11 +150,10 @@ void Max4409Show(bool json)
bool Xsns41(uint8_t function)
{
if (!XI2cEnabled(XI2C_28)) { return false; }
if (!I2cEnabled(XI2C_28)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
Max4409Detect();
@ -171,7 +170,6 @@ bool Xsns41(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -473,11 +473,10 @@ void Scd30Show(bool json)
bool Xsns42(byte function)
{
if (!XI2cEnabled(XI2C_29)) { return false; }
if (!I2cEnabled(XI2C_29)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
Scd30Update();
@ -494,7 +493,6 @@ bool Xsns42(byte function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -284,11 +284,10 @@ bool SPS30_cmd(void) {
bool Xsns44(byte function)
{
if (!XI2cEnabled(XI2C_30)) { return false; }
if (!I2cEnabled(XI2C_30)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
SPS30_Detect();
@ -310,7 +309,6 @@ bool Xsns44(byte function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -136,11 +136,10 @@ void Vl53l0Show(boolean json)
bool Xsns45(byte function)
{
if (!XI2cEnabled(XI2C_31)) { return false; }
if (!I2cEnabled(XI2C_31)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
Vl53l0Detect();
@ -157,7 +156,6 @@ bool Xsns45(byte function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -117,11 +117,10 @@ void MLX90614_Show(uint8_t json) {
bool Xsns46(byte function)
{
if (!XI2cEnabled(XI2C_32)) { return false; }
if (!I2cEnabled(XI2C_32)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
MLX90614_Init();
@ -138,7 +137,6 @@ bool Xsns46(byte function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -523,11 +523,10 @@ bool ChirpCmd(void) {
bool Xsns48(uint8_t function)
{
if (!XI2cEnabled(XI2C_33)) { return false; }
if (!I2cEnabled(XI2C_33)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
ChirpDetect(); // We can call CHIRPSCAN later to re-detect
@ -550,7 +549,6 @@ bool Xsns48(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -537,11 +537,10 @@ bool PAJ7620Cmd(void) {
bool Xsns50(uint8_t function)
{
if (!XI2cEnabled(XI2C_34)) { return false; }
if (!I2cEnabled(XI2C_34)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_INIT:
DEBUG_SENSOR_LOG(PSTR("PAJ7620: 1 second until init"));
@ -565,7 +564,6 @@ bool Xsns50(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -532,14 +532,11 @@ void Ina226Show(bool json)
*/
bool Xsns54(byte callback_id)
{
if (!XI2cEnabled(XI2C_35)) { return false; }
if (!I2cEnabled(XI2C_35)) { return false; }
// Set return value to `false`
bool result = false;
// Check if I2C interface mode is enabled
if(i2c_flg) {
// Check which callback ID is called by Tasmota
switch (callback_id) {
case FUNC_EVERY_SECOND:
@ -562,7 +559,6 @@ bool Xsns54(byte callback_id)
}
break;
}
} // if(i2c_flg)
// Return boolean result
return result;
}

View File

@ -135,11 +135,10 @@ void Hih6Show(bool json)
bool Xsns55(uint8_t function)
{
if (!XI2cEnabled(XI2C_36)) { return false; }
if (!I2cEnabled(XI2C_36)) { return false; }
bool result = false;
if (i2c_flg) {
switch (function) {
case FUNC_EVERY_SECOND:
Hih6EverySecond();
@ -156,7 +155,6 @@ bool Xsns55(uint8_t function)
Hih6Detect();
break;
}
}
return result;
}

View File

@ -412,23 +412,12 @@ const uint8_t kI2cList[] = {
/*********************************************************************************************/
bool XI2cEnabled(uint32_t i2c_index)
bool I2cEnabled(uint32_t i2c_index)
{
/*
if (i2c_index < sizeof(kI2cList)) {
#ifdef XFUNC_PTR_IN_ROM
uint32_t index = pgm_read_byte(kI2cList + i2c_index);
#else
uint32_t index = kI2cList[i2c_index];
#endif
return bitRead(Settings.i2c_drivers[index / 32], index % 32);
}
return true;
*/
return bitRead(Settings.i2c_drivers[i2c_index / 32], i2c_index % 32);
return (i2c_flg && bitRead(Settings.i2c_drivers[i2c_index / 32], i2c_index % 32));
}
void XI2cDriverState(void)
void I2cDriverState(void)
{
ResponseAppend_P(PSTR("\"")); // Use string for enable/disable signal
for (uint32_t i = 0; i < sizeof(kI2cList); i++) {