Refactor I2C driver detection phase 7

This commit is contained in:
Theo Arends 2019-11-11 18:36:13 +01:00
parent 3c89dedb92
commit 9c9d177ad5
7 changed files with 278 additions and 358 deletions

View File

@ -30,6 +30,8 @@
#define XSNS_31 31
#define XI2C_24 24 // See I2CDEVICES.md
#define EVERYNSECONDS 5
#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs;
@ -41,25 +43,23 @@ uint8_t tcnt = 0;
uint8_t ecnt = 0;
/********************************************************************************************/
#define EVERYNSECONDS 5
void CCS811Update(void) // Perform every n second
void CCS811Detect(void)
{
if (I2cActive(CCS811_ADDRESS)) { return; }
if (!ccs.begin(CCS811_ADDRESS)) {
CCS811_type = 1;
I2cSetActiveFound(CCS811_ADDRESS, "CCS811");
}
}
void CCS811Update(void) // Perform every n second
{
tcnt++;
if (tcnt >= EVERYNSECONDS) {
tcnt = 0;
CCS811_ready = 0;
if (!CCS811_type) {
sint8_t res = ccs.begin(CCS811_ADDRESS);
if (!res) {
CCS811_type = 1;
I2cSetActiveFound(CCS811_ADDRESS, "CCS811");
} else {
//AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res);
}
} else {
if (ccs.available()) {
if (!ccs.readData()){
TVOC = ccs.getTVOC();
@ -77,7 +77,6 @@ void CCS811Update(void) // Perform every n second
}
}
}
}
}
const char HTTP_SNS_CCS811[] PROGMEM =
@ -110,6 +109,10 @@ bool Xsns31(uint8_t function)
bool result = false;
if (FUNC_INIT == function) {
CCS811Detect();
}
else if (CCS811_type) {
switch (function) {
case FUNC_EVERY_SECOND:
CCS811Update();
@ -123,6 +126,7 @@ bool Xsns31(uint8_t function)
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -67,8 +67,6 @@ MPU6050 mpu6050;
void MPU_6050PerformReading(void)
{
if (!MPU_6050_found) { return; }
#ifdef USE_MPU6050_DMP
mpu6050.resetFIFO(); // with a default dampling rate of 200Hz, we create a delay of approx. 5ms with a complete read cycle
MPU6050_dmp.fifoCount = mpu6050.getFIFOCount();
@ -118,8 +116,6 @@ void MPU_6050SetAccelOffsets(int x, int y, int z)
void MPU_6050Detect(void)
{
if (MPU_6050_found) { return; }
for (uint32_t i = 0; i < sizeof(MPU_6050_addresses); i++)
{
MPU_6050_address = MPU_6050_addresses[i];
@ -144,8 +140,7 @@ void MPU_6050Detect(void)
Settings.flag2.axis_resolution = 2; // Need to be services by command Sensor32
}
if (MPU_6050_found)
{
if (MPU_6050_found) {
I2cSetActiveFound(MPU_6050_address, D_SENSOR_MPU6050);
}
}
@ -169,8 +164,6 @@ const char HTTP_SNS_AXIS[] PROGMEM =
void MPU_6050Show(bool json)
{
if (!MPU_6050_found) { return; }
MPU_6050PerformReading();
double tempConv = (MPU_6050_temperature / 340.0 + 35.53);
@ -225,6 +218,10 @@ bool Xsns32(uint8_t function)
bool result = false;
if (FUNC_INIT == function) {
MPU_6050Detect();
}
else if (MPU_6050_found) {
switch (function) {
case FUNC_EVERY_SECOND:
if (tele_period == Settings.tele_period -3) {
@ -240,9 +237,7 @@ bool Xsns32(uint8_t function)
MPU_6050PerformReading();
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
MPU_6050Detect();
break;
}
}
return result;
}

View File

@ -72,7 +72,7 @@ bool DS3231chipDetected = false;
----------------------------------------------------------------------*/
void DS3231Detect(void)
{
if (DS3231chipDetected || I2cActive(USE_RTC_ADDR)) { return; }
if (I2cActive(USE_RTC_ADDR)) { return; }
if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) {
I2cSetActiveFound(USE_RTC_ADDR, "DS3231");
@ -129,8 +129,6 @@ void SetDS3231Time (uint32_t epoch_time) {
void DS3231EverySecond(void)
{
if (!DS3231chipDetected) { return; }
TIME_T tmpTime;
if (!ds3231ReadStatus && Rtc.utc_time < START_VALID_TIME ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231
ntp_force_sync = true; //force to sync with ntp
@ -170,13 +168,15 @@ bool Xsns33(uint8_t function)
bool result = false;
if (FUNC_INIT == function) {
DS3231Detect();
}
else if (DS3231chipDetected) {
switch (function) {
case FUNC_EVERY_SECOND:
DS3231EverySecond();
break;
case FUNC_INIT:
DS3231Detect();
break;
}
}
return result;
}

View File

@ -66,8 +66,6 @@ bool Max4409Read_lum(void)
void Max4409Detect(void)
{
if (max44009_found) { return; }
uint8_t buffer1;
uint8_t buffer2;
for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) {
@ -101,9 +99,7 @@ void Max4409Detect(void)
void Max4409EverySecond(void)
{
if (max44009_found) {
Max4409Read_lum();
}
}
void Max4409Show(bool json)
@ -150,6 +146,10 @@ bool Xsns41(uint8_t function)
bool result = false;
if (FUNC_INIT == function) {
Max4409Detect();
}
else if (max44009_found) {
switch (function) {
case FUNC_EVERY_SECOND:
Max4409EverySecond();
@ -157,14 +157,12 @@ bool Xsns41(uint8_t function)
case FUNC_JSON_APPEND:
Max4409Show(1);
break;
#ifdef USE_WEBSERVER
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Max4409Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Max4409Detect();
break;
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -23,6 +23,8 @@
#define XSNS_42 42
#define XI2C_29 29 // See I2CDEVICES.md
#define SCD30_ADDRESS 0x61
#define SCD30_MAX_MISSED_READS 3
#define SCD30_STATE_NO_ERROR 0
#define SCD30_STATE_ERROR_DATA_CRC 1
@ -55,8 +57,6 @@ enum SCD30_Commands { // commands useable in console or rules
CMND_SCD30_TEMPOFFSET
};
FrogmoreScd30 scd30;
bool scd30Found = false;
@ -75,91 +75,35 @@ uint16_t scd30_CO2EAvg = 0;
float scd30_Humid = 0.0;
float scd30_Temp = 0.0;
bool Scd30Init()
void Scd30Detect(void)
{
int error;
bool i2c_flg = ((pin[GPIO_I2C_SCL] < 99) && (pin[GPIO_I2C_SDA] < 99));
if (i2c_flg)
{
if (I2cActive(SCD30_ADDRESS)) { return; }
scd30.begin();
uint8_t major = 0;
uint8_t minor = 0;
if (scd30.getFirmwareVersion(&major, &minor)) { return; }
uint16_t interval_sec;
scd30.begin();
error = scd30.getFirmwareVersion(&major, &minor);
if (error)
{
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: did not find an SCD30: 0x%lX", error);
AddLog(LOG_LEVEL_DEBUG);
#endif
return false;
}
else
{
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: found an SCD30: FW v%d.%d", major, minor);
AddLog(LOG_LEVEL_INFO);
#endif
}
if (scd30.getMeasurementInterval(&scd30Interval_sec)) { return; }
if (scd30.beginMeasuring()) { return; }
error = scd30.getMeasurementInterval(&scd30Interval_sec);
if (error)
{
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: error getMeasurementInterval: 0x%lX", error);
AddLog(LOG_LEVEL_ERROR);
#endif
return false;
}
I2cSetActiveFound(SCD30_ADDRESS, "SCD30");
scd30Found = true;
error = scd30.beginMeasuring();
if (error)
{
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "Error: Scd30BeginMeasuring: 0x%lX", error);
AddLog(LOG_LEVEL_ERROR);
#endif
return false;
}
return true;
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SCD: FW v%d.%d"), major, minor);
}
// gets data from the sensor every 3 seconds or so to give the sensor time to gather new data
int Scd30Update()
void Scd30Update(void)
{
int error = 0;
int16_t delta = 0;
scd30Loop_count++;
if (!scd30Found)
{
scd30Found = Scd30Init();
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "Scd30Update: found: %d ", scd30Found);
AddLog(LOG_LEVEL_INFO);
#endif
if (!scd30Found)
{
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "Scd30Update: found: %d ", scd30Found);
AddLog(LOG_LEVEL_INFO);
#endif
return (ERROR_SCD30_NOT_FOUND_ERROR);
}
}
else
{
if (scd30Loop_count > (scd30Interval_sec - 1))
{
switch (scd30ErrorState)
{
case SCD30_STATE_NO_ERROR:
{
if (scd30Loop_count > (scd30Interval_sec - 1)) {
int error = 0;
switch (scd30ErrorState) {
case SCD30_STATE_NO_ERROR: {
error = scd30.readMeasurement(&scd30_CO2, &scd30_CO2EAvg, &scd30_Temp, &scd30_Humid);
switch (error)
{
switch (error) {
case ERROR_SCD30_NO_ERROR:
scd30Loop_count = 0;
scd30IsDataValid = true;
@ -187,22 +131,20 @@ int Scd30Update()
#endif
break;
default:
{
default: {
scd30ErrorState = SCD30_STATE_ERROR_READ_MEAS;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: Update: ReadMeasurement error: 0x%lX, counter: %ld", error, scd30Loop_count);
AddLog(LOG_LEVEL_ERROR);
#endif
return (error);
return;
}
break;
}
}
break;
case SCD30_STATE_ERROR_DATA_CRC:
{
case SCD30_STATE_ERROR_DATA_CRC: {
//scd30IsDataValid = false;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count);
@ -214,8 +156,7 @@ int Scd30Update()
}
break;
case SCD30_STATE_ERROR_READ_MEAS:
{
case SCD30_STATE_ERROR_READ_MEAS: {
//scd30IsDataValid = false;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count);
@ -225,31 +166,24 @@ int Scd30Update()
#endif
scd30Reset_count++;
error = scd30.softReset();
if (error)
{
if (error) {
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: resetting got error: 0x%lX", error);
AddLog(LOG_LEVEL_ERROR);
#endif
error >>= 8;
if (error == 4)
{
if (error == 4) {
scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET;
}
else
{
} else {
scd30ErrorState = SCD30_STATE_ERROR_UNKNOWN;
}
}
else
{
} else {
scd30ErrorState = ERROR_SCD30_NO_ERROR;
}
}
break;
case SCD30_STATE_ERROR_SOFT_RESET:
{
case SCD30_STATE_ERROR_SOFT_RESET: {
//scd30IsDataValid = false;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: in error state: %d, good: %ld, no data: %ld, sc30 reset: %ld, i2c reset: %ld", scd30ErrorState, scd30GoodMeas_count, scd30DataNotAvailable_count, scd30Reset_count, i2cReset_count);
@ -259,39 +193,31 @@ int Scd30Update()
#endif
i2cReset_count++;
error = scd30.clearI2CBus();
if (error)
{
if (error) {
scd30ErrorState = SCD30_STATE_ERROR_I2C_RESET;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: error clearing i2c bus: 0x%lX", error);
AddLog(LOG_LEVEL_ERROR);
#endif
}
else
{
} else {
scd30ErrorState = ERROR_SCD30_NO_ERROR;
}
}
break;
default:
{
default: {
//scd30IsDataValid = false;
#ifdef SCD30_DEBUG
snprintf_P(log_data, sizeof(log_data), "SCD30: unknown error state: 0x%lX", scd30ErrorState);
AddLog(LOG_LEVEL_ERROR);
AddLog_P2(LOG_LEVEL_ERROR, PSTR("SCD30: unknown error state: 0x%lX"), scd30ErrorState);
#endif
scd30ErrorState = SCD30_STATE_ERROR_SOFT_RESET; // try again
}
}
if (scd30Loop_count > (SCD30_MAX_MISSED_READS * scd30Interval_sec))
{
if (scd30Loop_count > (SCD30_MAX_MISSED_READS * scd30Interval_sec)) {
scd30IsDataValid = false;
}
}
}
return (ERROR_SCD30_NO_ERROR);
}
@ -370,8 +296,9 @@ int Scd30SetCommand(int command_code, uint16_t value)
break;
}
}
/*********************************************************************************************\
* Command Sensor92
* Command Sensor42
\*********************************************************************************************/
bool Scd30CommandSensor()
@ -438,13 +365,13 @@ bool Scd30CommandSensor()
void Scd30Show(bool json)
{
char humidity[10];
char temperature[10];
if (scd30Found && scd30IsDataValid)
if (scd30IsDataValid)
{
char humidity[10];
dtostrfd(ConvertHumidity(scd30_Humid), Settings.flag2.humidity_resolution, humidity);
char temperature[10];
dtostrfd(ConvertTemp(scd30_Temp), Settings.flag2.temperature_resolution, temperature);
if (json) {
//ResponseAppend_P(PSTR(",\"SCD30\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s}"), scd30_CO2, temperature, humidity);
ResponseAppend_P(PSTR(",\"SCD30\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_HUMIDITY "\":%s}"),
@ -477,6 +404,10 @@ bool Xsns42(byte function)
bool result = false;
if (FUNC_INIT == function) {
Scd30Detect();
}
else if (scd30Found) {
switch (function) {
case FUNC_EVERY_SECOND:
Scd30Update();
@ -487,11 +418,12 @@ bool Xsns42(byte function)
case FUNC_JSON_APPEND:
Scd30Show(1);
break;
#ifdef USE_WEBSERVER
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Scd30Show(0);
break;
#endif // USE_WEBSERVER
#endif // USE_WEBSERVER
}
}
return result;
}

View File

@ -130,6 +130,7 @@ unsigned char cmdb[6];
void SPS30_Detect(void)
{
if (!I2cSetDevice(SPS30_ADDR)) { return; }
I2cSetActiveFound(SPS30_ADDR, "SPS30");
uint8_t dcode[32];
sps30_get_data(SPS_CMD_GET_SERIAL,dcode,sizeof(dcode));
@ -155,11 +156,8 @@ const char HTTP_SNS_SPS30_c[] PROGMEM ="{s}SPS30 " "TYPSIZ" "{m}%s " "um" "{e}";
//uint8_t sps30_inuse_hours;
void SPS30_Every_Second() {
if (!sps30_ready) return;
if (!sps30_running) return;
if (uptime%10==0) {
uint8_t vars[sizeof(float)*10];
sps30_get_data(SPS_CMD_READ_MEASUREMENT,vars,sizeof(vars));
@ -192,16 +190,11 @@ void SPS30_Every_Second() {
}
void SPS30_Show(bool json) {
void SPS30_Show(bool json)
{
if (!sps30_running) { return; }
char str[64];
if (!sps30_ready) {
return;
}
if (!sps30_running) {
return;
}
if (json) {
dtostrfd(sps30_result.PM1_0,PMDP,str);
ResponseAppend_P(PSTR(",\"SPS30\":{\"" "PM1_0" "\":%s"), str);
@ -248,16 +241,17 @@ void SPS30_Show(bool json) {
WSContentSend_PD(HTTP_SNS_SPS30_c,str);
#endif
}
}
void CmdClean(void) {
void CmdClean(void)
{
sps30_cmd(SPS_CMD_CLEAN);
ResponseTime_P(PSTR(",\"SPS30\":{\"CFAN\":\"true\"}}"));
MqttPublishTeleSensor();
}
bool SPS30_cmd(void) {
bool SPS30_cmd(void)
{
bool serviced = true;
if (XdrvMailbox.data_len > 0) {
char *cp=XdrvMailbox.data;
@ -280,13 +274,16 @@ bool SPS30_cmd(void) {
* Interface
\*********************************************************************************************/
bool Xsns44(byte function)
{
if (!I2cEnabled(XI2C_30)) { return false; }
bool result = false;
if (FUNC_INIT == function) {
SPS30_Detect();
}
else if (sps30_ready) {
switch (function) {
case FUNC_EVERY_SECOND:
SPS30_Every_Second();
@ -294,19 +291,17 @@ bool Xsns44(byte function)
case FUNC_JSON_APPEND:
SPS30_Show(1);
break;
#ifdef USE_WEBSERVER
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
SPS30_Show(0);
break;
#endif // USE_WEBSERVER
#endif // USE_WEBSERVER
case FUNC_COMMAND_SENSOR:
if (XSNS_44 == XdrvMailbox.index) {
result = SPS30_cmd();
}
break;
case FUNC_INIT:
SPS30_Detect();
break;
}
}
return result;
}

View File

@ -36,8 +36,6 @@ uint8_t Vl53l0_index;
void Vl53l0Detect(void)
{
if (vl53l0x_ready) { return; }
if (!I2cSetDevice(0x29)) { return; }
if (!sensor.init()) { return; }
@ -65,8 +63,6 @@ const char HTTP_SNS_VL53L0X[] PROGMEM =
void Vl53l0Every_250MSecond(void)
{
if (!vl53l0x_ready) { return; }
uint16_t tbuff[5],tmp;
uint8_t flag;
@ -104,8 +100,6 @@ void Vl53l0Every_250MSecond(void)
void Vl53l0Show(boolean json)
{
if (!vl53l0x_ready) { return; }
if (json) {
ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l0x_distance);
#ifdef USE_WEBSERVER
@ -125,6 +119,10 @@ bool Xsns45(byte function)
bool result = false;
if (FUNC_INIT == function) {
Vl53l0Detect();
}
else if (vl53l0x_ready) {
switch (function) {
case FUNC_EVERY_250_MSECOND:
Vl53l0Every_250MSecond();
@ -137,9 +135,7 @@ bool Xsns45(byte function)
Vl53l0Show(0);
break;
#endif // USE_WEBSERVER
case FUNC_INIT:
Vl53l0Detect();
break;
}
}
return result;
}