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,20 +190,22 @@ bool Xdrv15(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: PCA9685_Detect();
if (tele_period == 0) { }
PCA9685_OutputTelemetry(true); else if (pca9685_detected) {
} switch (function) {
break; case FUNC_EVERY_SECOND:
case FUNC_COMMAND_DRIVER: if (tele_period == 0) {
if (XDRV_15 == XdrvMailbox.index) { PCA9685_OutputTelemetry(true);
result = PCA9685_Command(); }
} break;
break; case FUNC_COMMAND_DRIVER:
case FUNC_INIT: if (XDRV_15 == XdrvMailbox.index) {
PCA9685_Detect(); result = PCA9685_Command();
break; }
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,25 +221,27 @@ 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;
switch (function) { if (FUNC_PRE_INIT == function) {
case FUNC_SET_POWER: Pcf8574Init();
Pcf8574SwitchRelay(); }
break; else if (Pcf8574.type) {
switch (function) {
case FUNC_SET_POWER:
Pcf8574SwitchRelay();
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_ADD_BUTTON: case FUNC_WEB_ADD_BUTTON:
WSContentSend_P(HTTP_BTN_MENU_PCF8574); WSContentSend_P(HTTP_BTN_MENU_PCF8574);
break; break;
case FUNC_WEB_ADD_HANDLER: case FUNC_WEB_ADD_HANDLER:
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,21 +220,23 @@ bool Xsns07(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: ShtDetect();
ShtEverySecond(); }
break; else if (sht_type) {
case FUNC_JSON_APPEND: switch (function) {
ShtShow(1); case FUNC_EVERY_SECOND:
break; ShtEverySecond();
break;
case FUNC_JSON_APPEND:
ShtShow(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
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,21 +278,23 @@ 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) {
case FUNC_EVERY_SECOND: switch (function) {
HtuEverySecond(); case FUNC_EVERY_SECOND:
break; HtuEverySecond();
case FUNC_JSON_APPEND: break;
HtuShow(1); case FUNC_JSON_APPEND:
break; HtuShow(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
HtuShow(0); HtuShow(0);
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,21 +611,23 @@ bool Xsns09(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: BmpDetect();
BmpEverySecond(); }
break; else if (bmp_count) {
case FUNC_JSON_APPEND: switch (function) {
BmpShow(1); case FUNC_EVERY_SECOND:
break; BmpRead();
break;
case FUNC_JSON_APPEND:
BmpShow(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
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 if (!Bh1750Read()) {
Bh1750Detect(); AddLogMissed(bh1750_types, bh1750_valid);
}
else {
// 1mS
if (bh1750_type) {
if (!Bh1750Read()) {
AddLogMissed(bh1750_types, bh1750_valid);
// if (!bh1750_valid) { bh1750_type = 0; }
}
}
} }
} }
@ -116,21 +105,23 @@ 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) {
case FUNC_EVERY_SECOND: switch (function) {
Bh1750EverySecond(); case FUNC_EVERY_SECOND:
break; Bh1750EverySecond();
case FUNC_JSON_APPEND: break;
Bh1750Show(1); case FUNC_JSON_APPEND:
break; Bh1750Show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
Bh1750Show(0); Bh1750Show(0);
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,21 +169,11 @@ void Veml6070UvTableInit(void)
void Veml6070EverySecond(void) void Veml6070EverySecond(void)
{ {
// all = 10..15[ms] // all = 10..15[ms]
if (11 == (uptime %100)) { Veml6070ModeCmd(1); // 1[ms], wakeup the UV sensor
if (!veml6070_type) { uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values
// Veml6070ModeCmd(1); // on = 1[ms], wakeup the UV sensor - THIS CORRUPTS OTHER I2C DEVICES uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level
Veml6070Detect(); // 1[ms], check for sensor and init with IT time uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2
// Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor - THIS CORRUPTS OTHER I2C DEVICES Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor
}
} else {
if (veml6070_type) {
Veml6070ModeCmd(1); // 1[ms], wakeup the UV sensor
uvlevel = Veml6070ReadUv(); // 1..2[ms], get UV raw values
uvrisk = Veml6070UvRiskLevel(uvlevel); // 0..1[ms], get UV risk level
uvpower = Veml6070UvPower(uvrisk); // 2[ms], get UV power in W/m2
Veml6070ModeCmd(0); // off = 5[ms], suspend the UV sensor
}
}
} }
/********************************************************************************************/ /********************************************************************************************/
@ -273,34 +261,32 @@ 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); char str_uvrisk[33]; // e.g. 25.99 text = UvIndex
char str_uvrisk[33]; // e.g. 25.99 text = UvIndex dtostrfd(uvrisk, 2, str_uvrisk);
dtostrfd(uvrisk, 2, str_uvrisk); char str_uvpower[33]; // e.g. 0.399 W/m² = UvPower
char str_uvpower[33]; // e.g. 0.399 W/m² = UvPower dtostrfd(uvpower, 3, str_uvpower);
dtostrfd(uvpower, 3, str_uvpower); if (json) {
if (json) {
#ifdef USE_VEML6070_SHOW_RAW #ifdef USE_VEML6070_SHOW_RAW
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_LEVEL "\":%s,\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_LEVEL "\":%s,\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"),
veml6070_name, str_uvlevel, str_uvrisk, str_uvrisk_text, str_uvpower); veml6070_name, str_uvlevel, str_uvrisk, str_uvrisk_text, str_uvpower);
#else #else
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"), ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_UV_INDEX "\":%s,\"" D_JSON_UV_INDEX_TEXT "\":\"%s\",\"" D_JSON_UV_POWER "\":%s}"),
veml6070_name, str_uvrisk, str_uvrisk_text, str_uvpower); veml6070_name, str_uvrisk, str_uvrisk_text, str_uvpower);
#endif // USE_VEML6070_SHOW_RAW #endif // USE_VEML6070_SHOW_RAW
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, uvlevel); } if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, uvlevel); }
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
#ifdef USE_VEML6070_SHOW_RAW #ifdef USE_VEML6070_SHOW_RAW
WSContentSend_PD(HTTP_SNS_UV_LEVEL, str_uvlevel); WSContentSend_PD(HTTP_SNS_UV_LEVEL, str_uvlevel);
#endif // USE_VEML6070_SHOW_RAW #endif // USE_VEML6070_SHOW_RAW
WSContentSend_PD(HTTP_SNS_UV_INDEX, str_uvrisk, str_uvrisk_text); WSContentSend_PD(HTTP_SNS_UV_INDEX, str_uvrisk, str_uvrisk_text);
WSContentSend_PD(HTTP_SNS_UV_POWER, str_uvpower); WSContentSend_PD(HTTP_SNS_UV_POWER, str_uvpower);
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
} }
} }
@ -314,21 +300,23 @@ bool Xsns11(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: Veml6070Detect();
Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values }
break; else if (veml6070_type) {
case FUNC_JSON_APPEND: switch (function) {
Veml6070Show(1); case FUNC_EVERY_SECOND:
break; Veml6070EverySecond(); // 10..15[ms], tested with OLED display, do all the actions needed to get all sensor values
break;
case FUNC_JSON_APPEND:
Veml6070Show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
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,18 +232,20 @@ bool Xsns12(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_JSON_APPEND: Ads1115Detect();
Ads1115Show(1); }
break; else if (Ads1115.count) {
switch (function) {
case FUNC_JSON_APPEND:
Ads1115Show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
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,26 +197,20 @@ 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 3mS
// 4 x 2mS Ina219Read();
Ina219Detect();
}
else {
// 4 x 3mS
Ina219Read();
}
} }
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
@ -280,26 +272,28 @@ bool Xsns13(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_COMMAND_SENSOR: Ina219Detect();
if ((XSNS_13 == XdrvMailbox.index) && (ina219_type)) { }
result = Ina219CommandSensor(); else if (ina219_count) {
} switch (function) {
break; case FUNC_COMMAND_SENSOR:
case FUNC_EVERY_SECOND: if (XSNS_13 == XdrvMailbox.index) {
Ina219EverySecond(); result = Ina219CommandSensor();
break; }
case FUNC_JSON_APPEND: break;
Ina219Show(1); case FUNC_EVERY_SECOND:
break; Ina219EverySecond();
#ifdef USE_WEBSERVER break;
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
Ina219Show(0); Ina219Show(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_INIT: case FUNC_WEB_SENSOR:
Ina219Detect(); Ina219Show(0);
break; break;
#endif // USE_WEBSERVER
}
} }
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,39 +94,37 @@ 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]; if (Sht3xRead(t, h, sht3x_sensors[i].address)) {
for (uint32_t i = 0; i < sht3x_count; i++) { char temperature[33];
if (Sht3xRead(t, h, sht3x_sensors[i].address)) { dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
char temperature[33]; char humidity[33];
dtostrfd(t, Settings.flag2.temperature_resolution, temperature); dtostrfd(h, Settings.flag2.humidity_resolution, humidity);
char humidity[33]; char types[11];
dtostrfd(h, Settings.flag2.humidity_resolution, humidity); snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX"
snprintf_P(types, sizeof(types), PSTR("%s%c0x%02X"), sht3x_sensors[i].types, IndexSeparator(), sht3x_sensors[i].address); // "SHT3X-0xXX"
if (json) { if (json) {
ResponseAppend_P(JSON_SNS_TEMPHUM, types, temperature, humidity); ResponseAppend_P(JSON_SNS_TEMPHUM, types, temperature, humidity);
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if ((0 == tele_period) && (0 == i)) { // We want the same first sensor to report to Domoticz in case a read is missed if ((0 == tele_period) && (0 == i)) { // We want the same first sensor to report to Domoticz in case a read is missed
DomoticzTempHumSensor(temperature, humidity); DomoticzTempHumSensor(temperature, humidity);
} }
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_KNX #ifdef USE_KNX
if (0 == tele_period) { if (0 == tele_period) {
KnxSensor(KNX_TEMPERATURE, t); KnxSensor(KNX_TEMPERATURE, t);
KnxSensor(KNX_HUMIDITY, h); KnxSensor(KNX_HUMIDITY, h);
} }
#endif // USE_KNX #endif // USE_KNX
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
WSContentSend_PD(HTTP_SNS_TEMP, types, temperature, TempUnit()); WSContentSend_PD(HTTP_SNS_TEMP, types, temperature, TempUnit());
WSContentSend_PD(HTTP_SNS_HUM, types, humidity); WSContentSend_PD(HTTP_SNS_HUM, types, humidity);
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
} }
} }
} }
@ -144,18 +140,20 @@ bool Xsns14(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_JSON_APPEND: Sht3xDetect();
Sht3xShow(1); }
break; else if (sht3x_count) {
#ifdef USE_WEBSERVER switch (function) {
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
Sht3xShow(0); Sht3xShow(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_INIT: case FUNC_WEB_SENSOR:
Sht3xDetect(); Sht3xShow(0);
break; break;
#endif // USE_WEBSERVER
}
} }
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,21 +117,23 @@ bool Xsns16(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: Tsl2561Detect();
Tsl2561EverySecond(); }
break; else if (tsl2561_type) {
case FUNC_JSON_APPEND: switch (function) {
Tsl2561Show(1); case FUNC_EVERY_SECOND:
break; Tsl2561EverySecond();
break;
case FUNC_JSON_APPEND:
Tsl2561Show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
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,18 +99,20 @@ bool Xsns19(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_JSON_APPEND: MGSPrepare();
MGSShow(1); }
break; else if (mgs_detected) {
#ifdef USE_WEBSERVER switch (function) {
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
MGSShow(0); MGSShow(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_INIT: case FUNC_WEB_SENSOR:
MGSPrepare(); MGSShow(0);
break; break;
#endif // USE_WEBSERVER
}
} }
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,31 +83,25 @@ float sgp30_AbsoluteHumidity(float temperature, float humidity,char tempUnit) {
void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm
{ {
if (!sgp30_type) { sgp30_ready = false;
if (21 == (uptime %100)) { if (!sgp.IAQmeasure()) {
sgp30_Init(); return; // Measurement failed
} }
} else { if (global_update && (global_humidity > 0) && (global_temperature != 9999)) {
sgp30_ready = false; // abs hum in mg/m3
if (!sgp.IAQmeasure()) { sgp30_abshum=sgp30_AbsoluteHumidity(global_temperature,global_humidity,TempUnit());
return; // Measurement failed sgp.setHumidity(sgp30_abshum*1000);
} }
if (global_update && (global_humidity > 0) && (global_temperature != 9999)) { sgp30_ready = true;
// 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 // these should normally be stored permanently and used for fast restart
if (!(uptime%SAVE_PERIOD)) { if (!(uptime%SAVE_PERIOD)) {
// store settings every N seconds // store settings every N seconds
uint16_t TVOC_base; uint16_t TVOC_base;
uint16_t eCO2_base; uint16_t eCO2_base;
if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base); // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base);
}
} }
} }
@ -157,21 +151,23 @@ bool Xsns21(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: sgp30_Init();
Sgp30Update(); }
break; else if (sgp30_type) {
case FUNC_JSON_APPEND: switch (function) {
Sgp30Show(1); case FUNC_EVERY_SECOND:
break; Sgp30Update();
#ifdef USE_WEBSERVER break;
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
Sgp30Show(0); Sgp30Show(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_INIT: case FUNC_WEB_SENSOR:
sgp30_Init(); Sgp30Show(0);
break; break;
#endif // USE_WEBSERVER
}
} }
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,18 +375,23 @@ bool Xsns24(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_SECOND: Si1145Detect();
Si1145Update(); }
break; else if (si1145_type) {
case FUNC_JSON_APPEND: switch (function) {
Si1145Show(1); case FUNC_EVERY_SECOND:
break; Si1145Update();
#ifdef USE_WEBSERVER break;
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
Si1145Show(0); Si1145Show(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Si1145Show(0);
break;
#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,21 +80,19 @@ 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);
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), temperature); ResponseAppend_P(PSTR(",\"LM75AD\":{\"" D_JSON_TEMPERATURE "\":%s}"), temperature);
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature); if (0 == tele_period) DomoticzSensor(DZ_TEMP, temperature);
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
WSContentSend_PD(HTTP_SNS_TEMP, "LM75AD", temperature, TempUnit()); WSContentSend_PD(HTTP_SNS_TEMP, "LM75AD", temperature, TempUnit());
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
} }
} }
@ -109,18 +106,20 @@ bool Xsns26(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_JSON_APPEND: LM75ADDetect();
LM75ADShow(1); }
break; else if (lm75ad_type) {
#ifdef USE_WEBSERVER switch (function) {
case FUNC_WEB_SENSOR: case FUNC_JSON_APPEND:
LM75ADShow(0); LM75ADShow(1);
break; break;
#endif // USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_INIT: case FUNC_WEB_SENSOR:
LM75ADDetect(); LM75ADShow(0);
break; break;
#endif // USE_WEBSERVER
}
} }
return result; return result;
} }

View File

@ -140,9 +140,10 @@ 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;
uint8_t reg_gpinten = 0; uint8_t reg_gpinten = 0;
uint8_t reg_iodir = 0xFF; uint8_t reg_iodir = 0xFF;
@ -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;
@ -235,7 +236,7 @@ void MCP230xx_CheckForInterrupt(void) {
uint8_t intf; uint8_t intf;
uint8_t mcp230xx_intcap = 0; uint8_t mcp230xx_intcap = 0;
uint8_t report_int; uint8_t report_int;
for (uint32_t mcp230xx_port=0;mcp230xx_port<mcp230xx_type;mcp230xx_port++) { for (uint32_t mcp230xx_port = 0; mcp230xx_port < mcp230xx_type; mcp230xx_port++) {
if (I2cValidRead8(&intf,USE_MCP230xx_ADDR,MCP230xx_INTF+mcp230xx_port)) { if (I2cValidRead8(&intf,USE_MCP230xx_ADDR,MCP230xx_INTF+mcp230xx_port)) {
if (intf > 0) { if (intf > 0) {
if (I2cValidRead8(&mcp230xx_intcap, USE_MCP230xx_ADDR, MCP230xx_INTCAP+mcp230xx_port)) { if (I2cValidRead8(&mcp230xx_intcap, USE_MCP230xx_ADDR, MCP230xx_INTCAP+mcp230xx_port)) {
@ -323,20 +324,16 @@ void MCP230xx_CheckForInterrupt(void) {
void MCP230xx_Show(bool json) void MCP230xx_Show(bool json)
{ {
if (mcp230xx_type) { if (json) {
if (json) { uint8_t gpio = MCP230xx_readGPIO(0);
if (mcp230xx_type > 0) { // we have at least 8 pins ResponseAppend_P(PSTR(",\"MCP230XX\":{\"D0\":%i,\"D1\":%i,\"D2\":%i,\"D3\":%i,\"D4\":%i,\"D5\":%i,\"D6\":%i,\"D7\":%i"),
uint8_t gpio = MCP230xx_readGPIO(0); (gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1);
ResponseAppend_P(PSTR(",\"MCP230XX\":{\"D0\":%i,\"D1\":%i,\"D2\":%i,\"D3\":%i,\"D4\":%i,\"D5\":%i,\"D6\":%i,\"D7\":%i"), if (2 == mcp230xx_type) {
(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 = MCP230xx_readGPIO(1);
if (2 == mcp230xx_type) { ResponseAppend_P(PSTR(",\"D8\":%i,\"D9\":%i,\"D10\":%i,\"D11\":%i,\"D12\":%i,\"D13\":%i,\"D14\":%i,\"D15\":%i"),
gpio = MCP230xx_readGPIO(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);
ResponseAppend_P(PSTR(",\"D8\":%i,\"D9\":%i,\"D10\":%i,\"D11\":%i,\"D12\":%i,\"D13\":%i,\"D14\":%i,\"D15\":%i"),
(gpio>>0)&1,(gpio>>1)&1,(gpio>>2)&1,(gpio>>3)&1,(gpio>>4)&1,(gpio>>5)&1,(gpio>>6)&1,(gpio>>7)&1);
}
ResponseJsonEnd();
}
} }
ResponseJsonEnd();
} }
} }
@ -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,19 +778,21 @@ bool Xsns29(uint8_t function)
bool result = false; bool result = false;
switch (function) { if (FUNC_INIT == function) {
case FUNC_EVERY_50_MSECOND:
if ((mcp230xx_int_en) && (mcp230xx_type)) { // Only check for interrupts if its enabled on one of the pins
mcp230xx_int_prio_counter++;
if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) {
MCP230xx_CheckForInterrupt();
mcp230xx_int_prio_counter=0;
}
}
break;
case FUNC_EVERY_SECOND:
MCP230xx_Detect(); MCP230xx_Detect();
if (mcp230xx_type) { }
else if (mcp230xx_type) {
switch (function) {
case FUNC_EVERY_50_MSECOND:
if (mcp230xx_int_en) { // Only check for interrupts if its enabled on one of the pins
mcp230xx_int_prio_counter++;
if ((mcp230xx_int_prio_counter) >= (Settings.mcp230xx_int_prio)) {
MCP230xx_CheckForInterrupt();
mcp230xx_int_prio_counter=0;
}
}
break;
case FUNC_EVERY_SECOND:
if (mcp230xx_int_counter_en) { 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,25 +807,25 @@ 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); break;
break; case FUNC_COMMAND_SENSOR:
case FUNC_COMMAND_SENSOR: if (XSNS_29 == XdrvMailbox.index) {
if (XSNS_29 == XdrvMailbox.index) { result = MCP230xx_Command();
result = MCP230xx_Command(); }
} break;
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
#ifdef USE_MCP230xx_OUTPUT #ifdef USE_MCP230xx_OUTPUT
#ifdef USE_MCP230xx_DISPLAYOUTPUT #ifdef USE_MCP230xx_DISPLAYOUTPUT
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
MCP230xx_UpdateWebData(); MCP230xx_UpdateWebData();
break; break;
#endif // USE_MCP230xx_DISPLAYOUTPUT #endif // USE_MCP230xx_DISPLAYOUTPUT
#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,36 +408,36 @@ 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) {
// Run ever 50 milliseconds (near real-time functions) switch (function) {
case FUNC_EVERY_50_MSECOND:
Mpr121Show(&mpr121, FUNC_EVERY_50_MSECOND);
break;
// Generate JSON telemetry string // Run ever 50 milliseconds (near real-time functions)
case FUNC_JSON_APPEND: case FUNC_EVERY_50_MSECOND:
Mpr121Show(&mpr121, FUNC_JSON_APPEND); Mpr121Show(&mpr121, FUNC_EVERY_50_MSECOND);
break; break;
// Generate JSON telemetry string
case FUNC_JSON_APPEND:
Mpr121Show(&mpr121, FUNC_JSON_APPEND);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
// Show sensor data on main web page // Show sensor data on main web page
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
Mpr121Show(&mpr121, FUNC_WEB_SENSOR); Mpr121Show(&mpr121, FUNC_WEB_SENSOR);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
} }
// Return bool result // Return bool result
return result; return result;