Refactor I2C driver detection phase 6

This commit is contained in:
Theo Arends 2019-11-11 17:32:44 +01:00
parent 83d28c163f
commit 24b71c5ba3
17 changed files with 432 additions and 475 deletions

View File

@ -45,7 +45,6 @@ uint16_t pca9685_pin_pwm_value[16];
void PCA9685_Detect(void) void PCA9685_Detect(void)
{ {
if (pca9685_detected) { return; }
if (I2cActive(USE_PCA9685_ADDR)) { return; } if (I2cActive(USE_PCA9685_ADDR)) { return; }
uint8_t buffer; uint8_t buffer;
@ -175,8 +174,6 @@ bool PCA9685_Command(void)
void PCA9685_OutputTelemetry(bool telemetry) 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); ResponseTime_P(PSTR(",\"PCA9685\":{\"PWM_FREQ\":%i,"),pca9685_freq);
for (uint32_t pin=0;pin<16;pin++) { for (uint32_t pin=0;pin<16;pin++) {
ResponseAppend_P(PSTR("\"PWM%i\":%i,"),pin,pca9685_pin_pwm_value[pin]); ResponseAppend_P(PSTR("\"PWM%i\":%i,"),pin,pca9685_pin_pwm_value[pin]);
@ -193,6 +190,10 @@ bool Xdrv15(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
PCA9685_Detect();
}
else if (pca9685_detected) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
if (tele_period == 0) { if (tele_period == 0) {
@ -204,9 +205,7 @@ bool Xdrv15(uint8_t function)
result = PCA9685_Command(); result = PCA9685_Command();
} }
break; break;
case FUNC_INIT: }
PCA9685_Detect();
break;
} }
return result; return result;
} }

View File

@ -40,7 +40,7 @@ struct PCF8574 {
uint8_t max_connected_ports = 0; // Max numbers of devices comming from PCF8574 modules uint8_t max_connected_ports = 0; // Max numbers of devices comming from PCF8574 modules
uint8_t max_devices = 0; // Max numbers of PCF8574 modules uint8_t max_devices = 0; // Max numbers of PCF8574 modules
char stype[9]; char stype[9];
bool type = true; bool type = false;
} Pcf8574; } Pcf8574;
void Pcf8574SwitchRelay(void) void Pcf8574SwitchRelay(void)
@ -74,8 +74,6 @@ void Pcf8574SwitchRelay(void)
void Pcf8574Init() void Pcf8574Init()
{ {
Pcf8574.type = false;
uint8_t pcf8574_address = PCF8574_ADDR1; uint8_t pcf8574_address = PCF8574_ADDR1;
while ((Pcf8574.max_devices < MAX_PCF8574) && (pcf8574_address < PCF8574_ADDR2 +8)) { while ((Pcf8574.max_devices < MAX_PCF8574) && (pcf8574_address < PCF8574_ADDR2 +8)) {
@ -223,10 +221,14 @@ void Pcf8574SaveSettings()
bool Xdrv28(uint8_t function) bool Xdrv28(uint8_t function)
{ {
if (!I2cEnabled(XI2C_02) || !Pcf8574.type) { return false; } if (!I2cEnabled(XI2C_02)) { return false; }
bool result = false; bool result = false;
if (FUNC_PRE_INIT == function) {
Pcf8574Init();
}
else if (Pcf8574.type) {
switch (function) { switch (function) {
case FUNC_SET_POWER: case FUNC_SET_POWER:
Pcf8574SwitchRelay(); Pcf8574SwitchRelay();
@ -239,9 +241,7 @@ bool Xdrv28(uint8_t function)
WebServer->on("/" WEB_HANDLE_PCF8574, HandlePcf8574); WebServer->on("/" WEB_HANDLE_PCF8574, HandlePcf8574);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_PRE_INIT: }
Pcf8574Init();
break;
} }
return result; return result;
} }

View File

@ -85,7 +85,7 @@ bool ShtSendCommand(const uint8_t cmd)
ackerror = true; ackerror = true;
} }
if (ackerror) { if (ackerror) {
sht_type = 0; // sht_type = 0;
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_DID_NOT_ACK_COMMAND)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_DID_NOT_ACK_COMMAND));
} }
return (!ackerror); return (!ackerror);
@ -101,7 +101,7 @@ bool ShtAwaitResult(void)
delay(20); delay(20);
} }
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_BUSY)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_SHT1 D_SENSOR_BUSY));
sht_type = 0; // sht_type = 0;
return false; return false;
} }
@ -159,10 +159,6 @@ bool ShtRead(void)
void ShtDetect(void) void ShtDetect(void)
{ {
if (sht_type) {
return;
}
sht_sda_pin = pin[GPIO_I2C_SDA]; sht_sda_pin = pin[GPIO_I2C_SDA];
sht_scl_pin = pin[GPIO_I2C_SCL]; sht_scl_pin = pin[GPIO_I2C_SCL];
if (ShtRead()) { if (ShtRead()) {
@ -176,11 +172,10 @@ void ShtDetect(void)
void ShtEverySecond(void) void ShtEverySecond(void)
{ {
if (sht_type && !(uptime %4)) { // Update every 4 seconds if (!(uptime %4)) { // Every 4 seconds
// 344mS // 344mS
if (!ShtRead()) { if (!ShtRead()) {
AddLogMissed(sht_types, sht_valid); AddLogMissed(sht_types, sht_valid);
// if (!sht_valid) { sht_type = 0; }
} }
} }
} }
@ -225,6 +220,10 @@ bool Xsns07(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
ShtDetect();
}
else if (sht_type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
ShtEverySecond(); ShtEverySecond();
@ -237,9 +236,7 @@ bool Xsns07(uint8_t function)
ShtShow(0); ShtShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: // Move detection to restart only removing interference }
ShtDetect();
break;
} }
return result; return result;
} }

View File

@ -197,8 +197,6 @@ bool HtuRead(void)
void HtuDetect(void) void HtuDetect(void)
{ {
if (htu_type) { return; }
htu_address = HTU21_ADDR; htu_address = HTU21_ADDR;
if (I2cActive(htu_address)) { return; } if (I2cActive(htu_address)) { return; }
@ -232,17 +230,10 @@ void HtuDetect(void)
void HtuEverySecond(void) void HtuEverySecond(void)
{ {
if (92 == (uptime %100)) { if (uptime &1) { // Every 2 seconds
// 1mS
HtuDetect();
}
else if (uptime &1) {
// HTU21: 68mS, SI70xx: 37mS // HTU21: 68mS, SI70xx: 37mS
if (htu_type) {
if (!HtuRead()) { if (!HtuRead()) {
AddLogMissed(htu_types, htu_valid); AddLogMissed(htu_types, htu_valid);
// if (!htu_valid) { htu_type = 0; }
}
} }
} }
} }
@ -287,10 +278,11 @@ bool Xsns08(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_INIT:
HtuDetect(); HtuDetect();
break; }
else if (htu_type) {
switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
HtuEverySecond(); HtuEverySecond();
break; break;
@ -303,6 +295,7 @@ bool Xsns08(uint8_t function)
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
}
return result; return result;
} }

View File

@ -447,8 +447,6 @@ void Bme680Read(uint8_t bmp_idx)
void BmpDetect(void) void BmpDetect(void)
{ {
if (bmp_count) { return; }
int bmp_sensor_size = BMP_MAX_SENSORS * sizeof(bmp_sensors_t); int bmp_sensor_size = BMP_MAX_SENSORS * sizeof(bmp_sensors_t);
if (!bmp_sensors) { if (!bmp_sensors) {
bmp_sensors = (bmp_sensors_t*)malloc(bmp_sensor_size); bmp_sensors = (bmp_sensors_t*)malloc(bmp_sensor_size);
@ -493,8 +491,6 @@ void BmpDetect(void)
void BmpRead(void) void BmpRead(void)
{ {
if (!bmp_sensors) { return; }
for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) {
switch (bmp_sensors[bmp_idx].bmp_type) { switch (bmp_sensors[bmp_idx].bmp_type) {
case BMP180_CHIPID: case BMP180_CHIPID:
@ -515,16 +511,8 @@ void BmpRead(void)
ConvertHumidity(bmp_sensors[0].bmp_humidity); // Set global humidity ConvertHumidity(bmp_sensors[0].bmp_humidity); // Set global humidity
} }
void BmpEverySecond(void)
{
// 2mS
BmpRead();
}
void BmpShow(bool json) void BmpShow(bool json)
{ {
if (!bmp_sensors) { return; }
for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) {
if (bmp_sensors[bmp_idx].bmp_type) { if (bmp_sensors[bmp_idx].bmp_type) {
float bmp_sealevel = 0.0; float bmp_sealevel = 0.0;
@ -623,9 +611,13 @@ bool Xsns09(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
BmpDetect();
}
else if (bmp_count) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
BmpEverySecond(); BmpRead();
break; break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
BmpShow(1); BmpShow(1);
@ -635,9 +627,7 @@ bool Xsns09(uint8_t function)
BmpShow(0); BmpShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
BmpDetect();
break;
} }
return result; return result;
} }

View File

@ -56,16 +56,14 @@ bool Bh1750Read(void)
void Bh1750Detect(void) void Bh1750Detect(void)
{ {
if (bh1750_type) { return; }
for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) { for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) {
bh1750_address = bh1750_addresses[i]; bh1750_address = bh1750_addresses[i];
if (I2cActive(bh1750_address)) { continue; } if (I2cActive(bh1750_address)) { continue; }
Wire.beginTransmission(bh1750_address); Wire.beginTransmission(bh1750_address);
Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE);
if (!Wire.endTransmission()) { if (!Wire.endTransmission()) {
bh1750_type = 1;
I2cSetActiveFound(bh1750_address, bh1750_types); I2cSetActiveFound(bh1750_address, bh1750_types);
bh1750_type = 1;
break; break;
} }
} }
@ -73,18 +71,9 @@ void Bh1750Detect(void)
void Bh1750EverySecond(void) void Bh1750EverySecond(void)
{ {
if (90 == (uptime %100)) {
// 1mS // 1mS
Bh1750Detect();
}
else {
// 1mS
if (bh1750_type) {
if (!Bh1750Read()) { if (!Bh1750Read()) {
AddLogMissed(bh1750_types, bh1750_valid); AddLogMissed(bh1750_types, bh1750_valid);
// if (!bh1750_valid) { bh1750_type = 0; }
}
}
} }
} }
@ -116,10 +105,11 @@ bool Xsns10(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_INIT:
Bh1750Detect(); Bh1750Detect();
break; }
else if (bh1750_type) {
switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Bh1750EverySecond(); Bh1750EverySecond();
break; break;
@ -132,6 +122,7 @@ bool Xsns10(uint8_t function)
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
}
return result; return result;
} }

View File

@ -121,7 +121,6 @@ uint8_t veml6070_addr_low = VEML6070_ADDR_L;
uint8_t veml6070_addr_high = VEML6070_ADDR_H; uint8_t veml6070_addr_high = VEML6070_ADDR_H;
uint8_t itime = VEML6070_INTEGRATION_TIME; uint8_t itime = VEML6070_INTEGRATION_TIME;
uint8_t veml6070_type = 0; uint8_t veml6070_type = 0;
uint8_t veml6070_valid = 0;
char veml6070_name[9]; char veml6070_name[9];
char str_uvrisk_text[10]; char str_uvrisk_text[10];
@ -129,7 +128,6 @@ char str_uvrisk_text[10];
void Veml6070Detect(void) void Veml6070Detect(void)
{ {
if (veml6070_type) { return; }
if (I2cActive(VEML6070_ADDR_L)) { return; } if (I2cActive(VEML6070_ADDR_L)) { return; }
// init the UV sensor // init the UV sensor
@ -142,7 +140,7 @@ void Veml6070Detect(void)
Veml6070UvTableInit(); // 1[ms], initalize the UV compare table only once Veml6070UvTableInit(); // 1[ms], initalize the UV compare table only once
uint8_t veml_model = 0; uint8_t veml_model = 0;
GetTextIndexed(veml6070_name, sizeof(veml6070_name), veml_model, kVemlTypes); GetTextIndexed(veml6070_name, sizeof(veml6070_name), veml_model, kVemlTypes);
I2cSetActiveFound(VEML6070_ADDR_L, "VEML6070"); I2cSetActiveFound(VEML6070_ADDR_L, veml6070_name);
} }
} }
@ -171,22 +169,12 @@ void Veml6070UvTableInit(void)
void Veml6070EverySecond(void) void Veml6070EverySecond(void)
{ {
// all = 10..15[ms] // 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 Veml6070ModeCmd(1); // 1[ms], wakeup the UV sensor
uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values
uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level
uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2 uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2
Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor
} }
}
}
/********************************************************************************************/ /********************************************************************************************/
@ -273,7 +261,6 @@ double Veml6070UvPower(double uvrisk)
void Veml6070Show(bool json) void Veml6070Show(bool json)
{ {
if (veml6070_type) {
// convert double values to string // convert double values to string
char str_uvlevel[33]; // e.g. 99999 inc = UVLevel char str_uvlevel[33]; // e.g. 99999 inc = UVLevel
dtostrfd((double)uvlevel, 0, str_uvlevel); dtostrfd((double)uvlevel, 0, str_uvlevel);
@ -302,7 +289,6 @@ void Veml6070Show(bool json)
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
@ -314,6 +300,10 @@ bool Xsns11(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Veml6070Detect();
}
else if (veml6070_type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values
@ -326,9 +316,7 @@ bool Xsns11(uint8_t function)
Veml6070Show(0); Veml6070Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
Veml6070Detect(); // 1[ms], detect and init the sensor
break;
} }
return result; return result;
} }

View File

@ -163,8 +163,6 @@ int16_t Ads1115GetConversion(uint8_t channel)
void Ads1115Detect(void) void Ads1115Detect(void)
{ {
if (Ads1115.count) { return; }
for (uint32_t i = 0; i < sizeof(Ads1115.addresses); i++) { for (uint32_t i = 0; i < sizeof(Ads1115.addresses); i++) {
if (!Ads1115.found[i]) { if (!Ads1115.found[i]) {
Ads1115.address = Ads1115.addresses[i]; Ads1115.address = Ads1115.addresses[i];
@ -173,9 +171,9 @@ void Ads1115Detect(void)
if (I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONVERT) && if (I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONVERT) &&
I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONFIG)) { I2cValidRead16(&buffer, Ads1115.address, ADS1115_REG_POINTER_CONFIG)) {
Ads1115StartComparator(i, ADS1115_REG_CONFIG_MODE_CONTIN); Ads1115StartComparator(i, ADS1115_REG_CONFIG_MODE_CONTIN);
Ads1115.count++;
Ads1115.found[i] = 1;
I2cSetActiveFound(Ads1115.address, "ADS1115"); I2cSetActiveFound(Ads1115.address, "ADS1115");
Ads1115.found[i] = 1;
Ads1115.count++;
} }
} }
} }
@ -183,8 +181,6 @@ void Ads1115Detect(void)
void Ads1115Show(bool json) void Ads1115Show(bool json)
{ {
if (!Ads1115.count) { return; }
int16_t values[4]; int16_t values[4];
for (uint32_t t = 0; t < sizeof(Ads1115.addresses); t++) { for (uint32_t t = 0; t < sizeof(Ads1115.addresses); t++) {
@ -236,6 +232,10 @@ bool Xsns12(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Ads1115Detect();
}
else if (Ads1115.count) {
switch (function) { switch (function) {
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
Ads1115Show(1); Ads1115Show(1);
@ -245,9 +245,7 @@ bool Xsns12(uint8_t function)
Ads1115Show(0); Ads1115Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
Ads1115Detect();
break;
} }
return result; return result;
} }

View File

@ -96,6 +96,7 @@ uint8_t ina219_valid[4] = {0,0,0,0};
float ina219_voltage[4] = {0,0,0,0}; float ina219_voltage[4] = {0,0,0,0};
float ina219_current[4] = {0,0,0,0}; float ina219_current[4] = {0,0,0,0};
char ina219_types[] = "INA219"; char ina219_types[] = "INA219";
uint8_t ina219_count = 0;
bool Ina219SetCalibration(uint8_t mode, uint16_t addr) bool Ina219SetCalibration(uint8_t mode, uint16_t addr)
{ {
@ -162,8 +163,7 @@ float Ina219GetCurrent_mA(uint16_t addr)
bool Ina219Read(void) bool Ina219Read(void)
{ {
for (int i=0; i<sizeof(ina219_type); i++) { for (int i=0; i<sizeof(ina219_type); i++) {
if (!ina219_type[i]) if (!ina219_type[i]) { continue; }
continue;
uint16_t addr = ina219_addresses[i]; uint16_t addr = ina219_addresses[i];
ina219_voltage[i] = Ina219GetBusVoltage_V(addr) + (Ina219GetShuntVoltage_mV(addr) / 1000); ina219_voltage[i] = Ina219GetBusVoltage_V(addr) + (Ina219GetShuntVoltage_mV(addr) / 1000);
ina219_current[i] = Ina219GetCurrent_mA(addr) / 1000; ina219_current[i] = Ina219GetCurrent_mA(addr) / 1000;
@ -183,15 +183,13 @@ bool Ina219Read(void)
bool Ina219CommandSensor(void) bool Ina219CommandSensor(void)
{ {
bool serviced = true;
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
Settings.ina219_mode = XdrvMailbox.payload; Settings.ina219_mode = XdrvMailbox.payload;
restart_flag = 2; restart_flag = 2;
} }
Response_P(S_JSON_SENSOR_INDEX_NVALUE, XSNS_13, Settings.ina219_mode); Response_P(S_JSON_SENSOR_INDEX_NVALUE, XSNS_13, Settings.ina219_mode);
return serviced; return true;
} }
/********************************************************************************************/ /********************************************************************************************/
@ -199,27 +197,21 @@ bool Ina219CommandSensor(void)
void Ina219Detect(void) void Ina219Detect(void)
{ {
for (uint32_t i = 0; i < sizeof(ina219_type); i++) { for (uint32_t i = 0; i < sizeof(ina219_type); i++) {
if (ina219_type[i]) { continue; }
uint16_t addr = ina219_addresses[i]; uint16_t addr = ina219_addresses[i];
if (I2cActive(addr)) { continue; } if (I2cActive(addr)) { continue; }
if (Ina219SetCalibration(Settings.ina219_mode, addr)) { if (Ina219SetCalibration(Settings.ina219_mode, addr)) {
ina219_type[i] = 1;
I2cSetActiveFound(addr, ina219_types); I2cSetActiveFound(addr, ina219_types);
ina219_type[i] = 1;
ina219_count++;
} }
} }
} }
void Ina219EverySecond(void) void Ina219EverySecond(void)
{ {
if (87 == (uptime %100)) {
// 4 x 2mS
Ina219Detect();
}
else {
// 4 x 3mS // 4 x 3mS
Ina219Read(); Ina219Read();
} }
}
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
const char HTTP_SNS_INA219_DATA[] PROGMEM = const char HTTP_SNS_INA219_DATA[] PROGMEM =
@ -280,9 +272,13 @@ bool Xsns13(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Ina219Detect();
}
else if (ina219_count) {
switch (function) { switch (function) {
case FUNC_COMMAND_SENSOR: case FUNC_COMMAND_SENSOR:
if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) { if (XSNS_13 == XdrvMailbox.index) {
result = Ina219CommandSensor(); result = Ina219CommandSensor();
} }
break; break;
@ -297,9 +293,7 @@ bool Xsns13(uint8_t function)
Ina219Show(0); Ina219Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
Ina219Detect();
break;
} }
return result; return result;
} }

View File

@ -79,12 +79,10 @@ bool Sht3xRead(float &t, float &h, uint8_t sht3x_address)
void Sht3xDetect(void) void Sht3xDetect(void)
{ {
if (sht3x_count) return;
float t;
float h;
for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) { for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) {
if (I2cActive(sht3x_addresses[i])) { continue; } if (I2cActive(sht3x_addresses[i])) { continue; }
float t;
float h;
if (Sht3xRead(t, h, sht3x_addresses[i])) { if (Sht3xRead(t, h, sht3x_addresses[i])) {
sht3x_sensors[sht3x_count].address = 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); GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), i, kShtTypes);
@ -96,16 +94,15 @@ void Sht3xDetect(void)
void Sht3xShow(bool json) void Sht3xShow(bool json)
{ {
if (sht3x_count) { for (uint32_t i = 0; i < sht3x_count; i++) {
float t; float t;
float h; float h;
char types[11];
for (uint32_t i = 0; i < sht3x_count; i++) {
if (Sht3xRead(t, h, sht3x_sensors[i].address)) { if (Sht3xRead(t, h, sht3x_sensors[i].address)) {
char temperature[33]; char temperature[33];
dtostrfd(t, Settings.flag2.temperature_resolution, temperature); dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
char humidity[33]; char humidity[33];
dtostrfd(h, Settings.flag2.humidity_resolution, humidity); 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" snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX"
if (json) { if (json) {
@ -132,7 +129,6 @@ void Sht3xShow(bool json)
} }
} }
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
@ -144,6 +140,10 @@ bool Xsns14(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Sht3xDetect();
}
else if (sht3x_count) {
switch (function) { switch (function) {
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
Sht3xShow(1); Sht3xShow(1);
@ -153,9 +153,7 @@ bool Xsns14(uint8_t function)
Sht3xShow(0); Sht3xShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
Sht3xDetect();
break;
} }
return result; return result;
} }

View File

@ -64,10 +64,8 @@ bool Tsl2561Read(void)
void Tsl2561Detect(void) void Tsl2561Detect(void)
{ {
if (tsl2561_type) { return; }
uint8_t id;
if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) { if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) {
uint8_t id;
Tsl.begin(); Tsl.begin();
if (!Tsl.id(id)) return; if (!Tsl.id(id)) return;
if (Tsl.on()) { if (Tsl.on()) {
@ -79,17 +77,10 @@ void Tsl2561Detect(void)
void Tsl2561EverySecond(void) void Tsl2561EverySecond(void)
{ {
if (90 == (uptime %100)) { if (!(uptime %2)) { // Every 2 seconds
// 1mS
Tsl2561Detect();
}
else if (!(uptime %2)) { // Update every 2 seconds
// ?mS - 4Sec // ?mS - 4Sec
if (tsl2561_type) {
if (!Tsl2561Read()) { if (!Tsl2561Read()) {
AddLogMissed(tsl2561_types, tsl2561_valid); AddLogMissed(tsl2561_types, tsl2561_valid);
if (!tsl2561_valid) { tsl2561_type = 0; }
}
} }
} }
} }
@ -126,6 +117,10 @@ bool Xsns16(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Tsl2561Detect();
}
else if (tsl2561_type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Tsl2561EverySecond(); Tsl2561EverySecond();
@ -138,9 +133,7 @@ bool Xsns16(uint8_t function)
Tsl2561Show(0); Tsl2561Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
Tsl2561Detect();
break;
} }
return result; return result;
} }

View File

@ -43,8 +43,6 @@ void MGSInit(void) {
void MGSPrepare(void) void MGSPrepare(void)
{ {
if (mgs_detected) { return; }
if (I2cActive(MGS_SENSOR_ADDR)) { return; } if (I2cActive(MGS_SENSOR_ADDR)) { return; }
gas.begin(MGS_SENSOR_ADDR); 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) void MGSShow(bool json)
{ {
if (!mgs_detected) { return; }
char buffer[33]; char buffer[33];
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"MGS\":{\"NH3\":%s"), measure_gas(NH3, buffer)); ResponseAppend_P(PSTR(",\"MGS\":{\"NH3\":%s"), measure_gas(NH3, buffer));
@ -103,6 +99,10 @@ bool Xsns19(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
MGSPrepare();
}
else if (mgs_detected) {
switch (function) { switch (function) {
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
MGSShow(1); MGSShow(1);
@ -112,9 +112,7 @@ bool Xsns19(uint8_t function)
MGSShow(0); MGSShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
MGSPrepare();
break;
} }
return result; return result;
} }

View File

@ -43,7 +43,7 @@ float sgp30_abshum;
void sgp30_Init(void) void sgp30_Init(void)
{ {
if (sgp30_type || I2cActive(SGP30_ADDRESS)) { return; } if (I2cActive(SGP30_ADDRESS)) { return; }
if (sgp.begin()) { if (sgp.begin()) {
sgp30_type = true; sgp30_type = true;
@ -83,11 +83,6 @@ float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) {
void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm 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; sgp30_ready = false;
if (!sgp.IAQmeasure()) { if (!sgp.IAQmeasure()) {
return; // Measurement failed return; // Measurement failed
@ -109,7 +104,6 @@ void Sgp30Update(void) // Perform every second to ensure proper operation of th
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base);
} }
} }
}
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
const char HTTP_SNS_SGP30[] PROGMEM = const char HTTP_SNS_SGP30[] PROGMEM =
@ -157,6 +151,10 @@ bool Xsns21(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
sgp30_Init();
}
else if (sgp30_type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Sgp30Update(); Sgp30Update();
@ -169,9 +167,7 @@ bool Xsns21(uint8_t function)
Sgp30Show(0); Sgp30Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
sgp30_Init();
break;
} }
return result; return result;
} }

View File

@ -183,7 +183,12 @@
#define SI114X_IRQEN_PS2 0x08 #define SI114X_IRQEN_PS2 0x08
#define SI114X_IRQEN_PS3 0x10 #define SI114X_IRQEN_PS3 0x10
uint16_t si1145_visible;
uint16_t si1145_infrared;
uint16_t si1145_uvindex;
bool si1145_type = false; 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()) { if (Si1145Begin()) {
si1145_type = true; si1145_type = true;
@ -316,6 +334,13 @@ void Si1145Update(void)
} }
} }
void Si1145Update(void)
{
if (!Si1145Read()) {
AddLogMissed("SI1145", si1145_valid);
}
}
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
const char HTTP_SNS_SI1145[] PROGMEM = const char HTTP_SNS_SI1145[] PROGMEM =
"{s}SI1145 " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr> "{s}SI1145 " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
@ -325,26 +350,18 @@ const char HTTP_SNS_SI1145[] PROGMEM =
void Si1145Show(bool json) void Si1145Show(bool json)
{ {
if (si1145_type && Si1145Present()) { if (si1145_valid) {
uint16_t visible = Si1145ReadVisible();
uint16_t infrared = Si1145ReadIR();
uint16_t uvindex = Si1145ReadUV();
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"SI1145\":{\"" D_JSON_ILLUMINANCE "\":%d,\"" D_JSON_INFRARED "\":%d,\"" D_JSON_UV_INDEX "\":%d.%d}"), 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 #ifdef USE_DOMOTICZ
if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, visible); if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, si1145_visible);
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } 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 #endif
} }
} else {
if (si1145_type) {
I2cResetActive(SI114X_ADDR);
}
si1145_type = false;
} }
} }
@ -358,6 +375,10 @@ bool Xsns24(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
Si1145Detect();
}
else if (si1145_type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Si1145Update(); Si1145Update();
@ -371,6 +392,7 @@ bool Xsns24(uint8_t function)
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
}
return result; return result;
} }

View File

@ -51,8 +51,6 @@ uint8_t lm75ad_addresses[] = { LM75AD_ADDRESS1, LM75AD_ADDRESS2, LM75AD_ADDRESS3
void LM75ADDetect(void) void LM75ADDetect(void)
{ {
if (lm75ad_type) { return; }
for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) { for (uint32_t i = 0; i < sizeof(lm75ad_addresses); i++) {
lm75ad_address = lm75ad_addresses[i]; lm75ad_address = lm75ad_addresses[i];
if (I2cActive(lm75ad_address)) { continue; } if (I2cActive(lm75ad_address)) { continue; }
@ -67,7 +65,8 @@ void LM75ADDetect(void)
} }
} }
float LM75ADGetTemp(void) { float LM75ADGetTemp(void)
{
int16_t sign = 1; int16_t sign = 1;
uint16_t t = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER); uint16_t t = I2cRead16(lm75ad_address, LM75_TEMP_REGISTER);
@ -81,7 +80,6 @@ float LM75ADGetTemp(void) {
void LM75ADShow(bool json) void LM75ADShow(bool json)
{ {
if (lm75ad_type) {
float t = LM75ADGetTemp(); float t = LM75ADGetTemp();
char temperature[33]; char temperature[33];
dtostrfd(t, Settings.flag2.temperature_resolution, temperature); dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
@ -97,7 +95,6 @@ void LM75ADShow(bool json)
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
@ -109,6 +106,10 @@ bool Xsns26(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
LM75ADDetect();
}
else if (lm75ad_type) {
switch (function) { switch (function) {
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
LM75ADShow(1); LM75ADShow(1);
@ -118,9 +119,7 @@ bool Xsns26(uint8_t function)
LM75ADShow(0); LM75ADShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT: }
LM75ADDetect();
break;
} }
return result; return result;
} }

View File

@ -140,7 +140,8 @@ uint8_t MCP230xx_readGPIO(uint8_t port) {
return I2cRead8(USE_MCP230xx_ADDR, MCP230xx_GPIO + port); return I2cRead8(USE_MCP230xx_ADDR, MCP230xx_GPIO + port);
} }
void MCP230xx_ApplySettings(void) { void MCP230xx_ApplySettings(void)
{
uint8_t int_en = 0; uint8_t int_en = 0;
for (uint32_t mcp230xx_port = 0; mcp230xx_port < mcp230xx_type; mcp230xx_port++) { for (uint32_t mcp230xx_port = 0; mcp230xx_port < mcp230xx_type; mcp230xx_port++) {
uint8_t reg_gppu = 0; uint8_t reg_gppu = 0;
@ -201,7 +202,7 @@ void MCP230xx_ApplySettings(void) {
void MCP230xx_Detect(void) void MCP230xx_Detect(void)
{ {
if (mcp230xx_type || I2cActive(USE_MCP230xx_ADDR)) { return; } if (I2cActive(USE_MCP230xx_ADDR)) { return; }
uint8_t buffer; uint8_t buffer;
@ -323,9 +324,7 @@ void MCP230xx_CheckForInterrupt(void) {
void MCP230xx_Show(bool json) void MCP230xx_Show(bool json)
{ {
if (mcp230xx_type) {
if (json) { if (json) {
if (mcp230xx_type > 0) { // we have at least 8 pins
uint8_t gpio = MCP230xx_readGPIO(0); 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"), 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); (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1);
@ -337,8 +336,6 @@ void MCP230xx_Show(bool json)
ResponseJsonEnd(); ResponseJsonEnd();
} }
} }
}
}
#ifdef USE_MCP230xx_OUTPUT #ifdef USE_MCP230xx_OUTPUT
@ -423,8 +420,6 @@ void MCP230xx_Reset(uint8_t pinmode) {
bool MCP230xx_Command(void) bool MCP230xx_Command(void)
{ {
if (!mcp230xx_type) { return false; }
bool serviced = true; bool serviced = true;
bool validpin = false; bool validpin = false;
uint8_t paramcount = 0; 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) void MCP230xx_UpdateWebData(void)
{ {
if (!mcp230xx_type) { return; }
uint8_t gpio1 = MCP230xx_readGPIO(0); uint8_t gpio1 = MCP230xx_readGPIO(0);
uint8_t gpio2 = 0; uint8_t gpio2 = 0;
if (2 == mcp230xx_type) { if (2 == mcp230xx_type) {
@ -720,8 +713,8 @@ void MCP230xx_UpdateWebData(void)
#ifdef USE_MCP230xx_OUTPUT #ifdef USE_MCP230xx_OUTPUT
void MCP230xx_OutputTelemetry(void) { void MCP230xx_OutputTelemetry(void)
if (0 == mcp230xx_type) { return; } // We do not do this if the MCP has not been detected {
uint8_t outputcount = 0; uint8_t outputcount = 0;
uint16_t gpiototal = 0; uint16_t gpiototal = 0;
uint8_t gpioa = 0; uint8_t gpioa = 0;
@ -785,9 +778,13 @@ bool Xsns29(uint8_t function)
bool result = false; bool result = false;
if (FUNC_INIT == function) {
MCP230xx_Detect();
}
else if (mcp230xx_type) {
switch (function) { switch (function) {
case FUNC_EVERY_50_MSECOND: case FUNC_EVERY_50_MSECOND:
if ((mcp230xx_int_en) && (mcp230xx_type)) { // Only check for interrupts if its enabled on one of the pins if (mcp230xx_int_en) { // Only check for interrupts if its enabled on one of the pins
mcp230xx_int_prio_counter++; mcp230xx_int_prio_counter++;
if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) { if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) {
MCP230xx_CheckForInterrupt(); MCP230xx_CheckForInterrupt();
@ -796,8 +793,6 @@ bool Xsns29(uint8_t function)
} }
break; break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
MCP230xx_Detect();
if (mcp230xx_type) {
if (mcp230xx_int_counter_en) { if (mcp230xx_int_counter_en) {
mcp230xx_int_sec_counter++; mcp230xx_int_sec_counter++;
if (mcp230xx_int_sec_counter >= Settings.mcp230xx_int_timer) { // Interrupt counter interval reached, lets report if (mcp230xx_int_sec_counter >= Settings.mcp230xx_int_timer) { // Interrupt counter interval reached, lets report
@ -812,7 +807,6 @@ bool Xsns29(uint8_t function)
MCP230xx_OutputTelemetry(); MCP230xx_OutputTelemetry();
#endif // USE_MCP230xx_OUTPUT #endif // USE_MCP230xx_OUTPUT
} }
}
break; break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
MCP230xx_Show(1); MCP230xx_Show(1);
@ -832,6 +826,7 @@ bool Xsns29(uint8_t function)
#endif // USE_MCP230xx_OUTPUT #endif // USE_MCP230xx_OUTPUT
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
}
return result; return result;
} }

View File

@ -199,29 +199,35 @@ struct mpr121 {
uint16_t previous[4] = { 0x0000, 0x0000, 0x0000, 0x0000 }; /** Current values in electrode register of sensor */ 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. * The function Mpr121Init() soft-resets, detects and configures up to 4x MPR121 sensors.
* *
* @param struct *pS Struct with MPR121 status and data. * @param struct *pS Struct with MPR121 status and data.
* bool initial true - Initial call, false - next calls
* @return void * @return void
* @pre None. * @pre None.
* @post None. * @post None.
* *
*/ */
void Mpr121Init(struct mpr121 *pS) void Mpr121Init(struct mpr121 *pS, bool initial)
{ {
// Loop through I2C addresses // Loop through I2C addresses
for (uint32_t i = 0; i < sizeof(pS->i2c_addr[i]); i++) { 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 // Soft reset sensor and check if connected at I2C address
pS->connected[i] = (I2cWrite8(pS->i2c_addr[i], MPR121_SRST_REG, MPR121_SRST_VAL) pS->connected[i] = (I2cWrite8(pS->i2c_addr[i], MPR121_SRST_REG, MPR121_SRST_VAL)
&& (0x24 == I2cRead8(pS->i2c_addr[i], 0x5D))); && (0x24 == I2cRead8(pS->i2c_addr[i], 0x5D)));
if (pS->connected[i]) { if (pS->connected[i]) {
// Log sensor found // 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) // Set thresholds for registers 0x41 - 0x5A (ExTTH and ExRTH)
for (uint32_t j = 0; j < 13; j++) { for (uint32_t j = 0; j < 13; j++) {
@ -326,7 +332,7 @@ void Mpr121Show(struct mpr121 *pS, uint8_t function)
// Read data // Read data
if (!I2cValidRead16LE(&pS->current[i], pS->i2c_addr[i], MPR121_ELEX_REG)) { 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]); AddLog_P2(LOG_LEVEL_ERROR, PSTR(D_LOG_I2C "MPR121%c: ERROR: Cannot read data!"), pS->id[i]);
Mpr121Init(pS); Mpr121Init(pS, false);
return; return;
} }
// Check if OVCF bit is set // Check if OVCF bit is set
@ -335,7 +341,7 @@ void Mpr121Show(struct mpr121 *pS, uint8_t function)
// Clear OVCF bit // Clear OVCF bit
I2cWrite8(pS->i2c_addr[i], MPR121_ELEX_REG, 0x00); 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]); 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; return;
} }
} }
@ -402,19 +408,18 @@ bool Xsns30(uint8_t function)
{ {
if (!I2cEnabled(XI2C_23)) { return false; } if (!I2cEnabled(XI2C_23)) { return false; }
// ???
bool result = false; bool result = false;
// Sensor state/data struct // Sensor state/data struct
static struct mpr121 mpr121; static struct mpr121 mpr121;
// Check if I2C is enabled if (FUNC_INIT == function) {
switch (function) {
// Initialize Sensors // Initialize Sensors
case FUNC_INIT: Mpr121Init(&mpr121, true);
Mpr121Init(&mpr121); }
break; else if (mpr21_found) {
switch (function) {
// Run ever 50 milliseconds (near real-time functions) // Run ever 50 milliseconds (near real-time functions)
case FUNC_EVERY_50_MSECOND: case FUNC_EVERY_50_MSECOND:
@ -433,6 +438,7 @@ bool Xsns30(uint8_t function)
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
}
// Return bool result // Return bool result
return result; return result;
} }