From a0f3d8929550aa66764beb7cdfb0edefc624dd9a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 28 Dec 2020 18:32:47 +0100 Subject: [PATCH] sanitize BME sensor detection logic --- src/bmesensor.cpp | 65 ++++++++--------------------------------------- src/main.cpp | 4 ++- 2 files changed, 14 insertions(+), 55 deletions(-) diff --git a/src/bmesensor.cpp b/src/bmesensor.cpp index 9ba31fcf..6baa0f1c 100644 --- a/src/bmesensor.cpp +++ b/src/bmesensor.cpp @@ -46,10 +46,10 @@ Adafruit_BMP085 bmp; // I2C void setBMEIRQ() { xTaskNotify(irqHandlerTask, BME_IRQ, eSetBits); } // initialize MEMS sensor +// return = 0 -> error / return = 1 -> success int bme_init(void) { - // return = 0 -> error / return = 1 -> success - int rc = 1; + int rc = 0; #ifdef HAS_BME680 // block i2c bus access @@ -63,76 +63,33 @@ int bme_init(void) { iaqSensor.version.minor_bugfix); 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(); - iaqSensor.setTemperatureOffset((float)BME_TEMP_OFFSET); iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP); - if (checkIaqSensorStatus()) - ESP_LOGI(TAG, "BSEC subscription succesful"); - else { - ESP_LOGE(TAG, "BSEC subscription error"); - rc = 0; - goto finish; - } - } else { + rc = checkIaqSensorStatus(); + + } else ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error"); - rc = 0; - goto finish; - } #elif defined HAS_BME280 - - bool status; - - // block i2c bus access if (I2C_MUTEX_LOCK()) { - - status = bme.begin(BME280_ADDR); - if (!status) { - ESP_LOGE(TAG, "BME280 sensor not found"); - rc = 0; - goto finish; - } - ESP_LOGI(TAG, "BME280 sensor found and initialized"); - } else { + rc = bme.begin(BME280_ADDR); + } else ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error"); - rc = 0; - goto finish; - } #elif defined HAS_BMP180 - bool status; - // block i2c bus access if (I2C_MUTEX_LOCK()) { // Wire.begin(21, 22); - status = bmp.begin(); - if (!status) { - ESP_LOGE(TAG, "BMP180 sensor not found"); - rc = 0; - goto finish; - } - ESP_LOGI(TAG, "BMP180 sensor found and initialized"); - } else { + rc = bmp.begin(); + } else ESP_LOGE(TAG, "I2c bus busy - BMP180 initialization error"); - rc = 0; - goto finish; - } + #endif -finish: I2C_MUTEX_UNLOCK(); // release i2c bus access if (rc) - bmecycler.attach(BMECYCLE, setBMEIRQ); + bmecycler.attach(BMECYCLE, setBMEIRQ); // start cyclic data transmit return rc; } // bme_init() diff --git a/src/main.cpp b/src/main.cpp index 5d7bb62b..33fe1ae5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -451,8 +451,10 @@ void setup() { #endif if (bme_init()) ESP_LOGI(TAG, "BME sensor initialized"); - else + else { ESP_LOGE(TAG, "BME sensor could not be initialized"); + cfg.payloadmask &= ~MEMS_DATA; // switch off transmit of BME data + } #endif // starting timers and interrupts