sanitize BME sensor detection logic

This commit is contained in:
cyberman54 2020-12-28 18:32:47 +01:00
parent a3249203d0
commit a0f3d89295
2 changed files with 14 additions and 55 deletions

View File

@ -46,10 +46,10 @@ Adafruit_BMP085 bmp; // I2C
void setBMEIRQ() { xTaskNotify(irqHandlerTask, BME_IRQ, eSetBits); } void setBMEIRQ() { xTaskNotify(irqHandlerTask, BME_IRQ, eSetBits); }
// initialize MEMS sensor // initialize MEMS sensor
// return = 0 -> error / return = 1 -> success
int bme_init(void) { int bme_init(void) {
// return = 0 -> error / return = 1 -> success int rc = 0;
int rc = 1;
#ifdef HAS_BME680 #ifdef HAS_BME680
// block i2c bus access // block i2c bus access
@ -63,76 +63,33 @@ int bme_init(void) {
iaqSensor.version.minor_bugfix); iaqSensor.version.minor_bugfix);
iaqSensor.setConfig(bsec_config_iaq); iaqSensor.setConfig(bsec_config_iaq);
if (checkIaqSensorStatus())
ESP_LOGI(TAG, "BME680 sensor found and initialized");
else {
ESP_LOGE(TAG, "BME680 sensor not found");
rc = 0;
goto finish;
}
loadState(); loadState();
iaqSensor.setTemperatureOffset((float)BME_TEMP_OFFSET); iaqSensor.setTemperatureOffset((float)BME_TEMP_OFFSET);
iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP); iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
if (checkIaqSensorStatus()) rc = checkIaqSensorStatus();
ESP_LOGI(TAG, "BSEC subscription succesful");
else { } else
ESP_LOGE(TAG, "BSEC subscription error");
rc = 0;
goto finish;
}
} else {
ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error");
rc = 0;
goto finish;
}
#elif defined HAS_BME280 #elif defined HAS_BME280
bool status;
// block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
rc = bme.begin(BME280_ADDR);
status = bme.begin(BME280_ADDR); } else
if (!status) {
ESP_LOGE(TAG, "BME280 sensor not found");
rc = 0;
goto finish;
}
ESP_LOGI(TAG, "BME280 sensor found and initialized");
} else {
ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error");
rc = 0;
goto finish;
}
#elif defined HAS_BMP180 #elif defined HAS_BMP180
bool status;
// block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
// Wire.begin(21, 22); // Wire.begin(21, 22);
status = bmp.begin(); rc = bmp.begin();
if (!status) { } else
ESP_LOGE(TAG, "BMP180 sensor not found");
rc = 0;
goto finish;
}
ESP_LOGI(TAG, "BMP180 sensor found and initialized");
} else {
ESP_LOGE(TAG, "I2c bus busy - BMP180 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BMP180 initialization error");
rc = 0;
goto finish;
}
#endif #endif
finish:
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
if (rc) if (rc)
bmecycler.attach(BMECYCLE, setBMEIRQ); bmecycler.attach(BMECYCLE, setBMEIRQ); // start cyclic data transmit
return rc; return rc;
} // bme_init() } // bme_init()

View File

@ -451,8 +451,10 @@ void setup() {
#endif #endif
if (bme_init()) if (bme_init())
ESP_LOGI(TAG, "BME sensor initialized"); ESP_LOGI(TAG, "BME sensor initialized");
else else {
ESP_LOGE(TAG, "BME sensor could not be initialized"); ESP_LOGE(TAG, "BME sensor could not be initialized");
cfg.payloadmask &= ~MEMS_DATA; // switch off transmit of BME data
}
#endif #endif
// starting timers and interrupts // starting timers and interrupts