diff --git a/examples/basic_config_state/basic_config_state.ino b/examples/basic_config_state/basic_config_state.ino index 838b883..edc5ef5 100644 --- a/examples/basic_config_state/basic_config_state.ino +++ b/examples/basic_config_state/basic_config_state.ino @@ -1,20 +1,20 @@ #include #include "bsec.h" /* Configure the BSEC library with information about the sensor - * 18v/33v = Voltage at Vdd. 1.8V or 3.3V - * 3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP - * 4d/28d = Operating age of the sensor in days - * generic_18v_3s_4d - * generic_18v_3s_28d - * generic_18v_300s_4d - * generic_18v_300s_28d - * generic_33v_3s_4d - * generic_33v_3s_28d - * generic_33v_300s_4d - * generic_33v_300s_28d - */ + 18v/33v = Voltage at Vdd. 1.8V or 3.3V + 3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP + 4d/28d = Operating age of the sensor in days + generic_18v_3s_4d + generic_18v_3s_28d + generic_18v_300s_4d + generic_18v_300s_28d + generic_33v_3s_4d + generic_33v_3s_28d + generic_33v_300s_4d + generic_33v_300s_28d +*/ const uint8_t bsec_config_iaq[] = { - #include "config/generic_33v_3s_4d/bsec_iaq.txt" +#include "config/generic_33v_3s_4d/bsec_iaq.txt" }; #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day @@ -181,4 +181,4 @@ void updateState(void) EEPROM.write(0, BSEC_MAX_STATE_BLOB_SIZE); EEPROM.commit(); } -} +} \ No newline at end of file diff --git a/examples/basic_config_state_ulp_plus/basic_config_state_ulp_plus.ino b/examples/basic_config_state_ulp_plus/basic_config_state_ulp_plus.ino index e03da7f..2e891d9 100644 --- a/examples/basic_config_state_ulp_plus/basic_config_state_ulp_plus.ino +++ b/examples/basic_config_state_ulp_plus/basic_config_state_ulp_plus.ino @@ -1,20 +1,20 @@ #include #include "bsec.h" /* Configure the BSEC library with information about the sensor - * 18v/33v = Voltage at Vdd. 1.8V or 3.3V - * 3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP - * 4d/28d = Operating age of the sensor in days - * generic_18v_3s_4d - * generic_18v_3s_28d - * generic_18v_300s_4d - * generic_18v_300s_28d - * generic_33v_3s_4d - * generic_33v_3s_28d - * generic_33v_300s_4d - * generic_33v_300s_28d - */ + 18v/33v = Voltage at Vdd. 1.8V or 3.3V + 3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP + 4d/28d = Operating age of the sensor in days + generic_18v_3s_4d + generic_18v_3s_28d + generic_18v_300s_4d + generic_18v_300s_28d + generic_33v_3s_4d + generic_33v_3s_28d + generic_33v_300s_4d + generic_33v_300s_28d +*/ const uint8_t bsec_config_iaq[] = { - #include "config/generic_33v_3s_4d/bsec_iaq.txt" +#include "config/generic_33v_3s_4d/bsec_iaq.txt" }; #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day @@ -159,7 +159,7 @@ void updateState(void) { bool update = false; if (stateUpdateCounter == 0) { - /* Set a trigger to save the state. Here, the state is saved every STATE_SAVE_PERIOD with the first state being saved once the algorithm achieves full calibration, i.e. iaqAccuracy = 3 */ + /* Set a trigger to save the state. Here, the state is saved every STATE_SAVE_PERIOD with the first state being saved once the algorithm achieves full calibration, i.e. iaqAccuracy = 3 */ if (iaqSensor.iaqAccuracy >= 3) { update = true; stateUpdateCounter++; diff --git a/examples/esp32DeepSleep/esp32DeepSleep.ino b/examples/esp32DeepSleep/esp32DeepSleep.ino index 8131ead..8a4a6d4 100644 --- a/examples/esp32DeepSleep/esp32DeepSleep.ino +++ b/examples/esp32DeepSleep/esp32DeepSleep.ino @@ -1,4 +1,4 @@ -/* Thanks to Dmitry for the sketch(https://community.bosch-sensortec.com/t5/user/viewprofilepage/user-id/4285) */ +/* Thanks to Dmitry for the sketch */ #include #include /* Configure the BSEC library with information about the sensor @@ -133,4 +133,4 @@ void loop() { esp_sleep_enable_timer_wakeup(time_us); esp_deep_sleep_start(); } -} +} \ No newline at end of file diff --git a/src/bsec.cpp b/src/bsec.cpp index 5d25573..75366de 100644 --- a/src/bsec.cpp +++ b/src/bsec.cpp @@ -30,9 +30,9 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @file bsec.cpp - * @date 10 Jan 2020 - * @version 1.5.1474 + * @file bsec.cpp + * @date 10 Jan 2020 + * @version 1.5.1474 * */ @@ -46,36 +46,40 @@ SPIClass* Bsec::spiObj = NULL; */ Bsec::Bsec() { - nextCall = 0; - version.major = 0; - version.minor = 0; - version.major_bugfix = 0; - version.minor_bugfix = 0; - millisOverflowCounter = 0; - lastTime = 0; - bme680Status = BME680_OK; - outputTimestamp = 0; - _tempOffset = 0.0f; - status = BSEC_OK; - bsecConfig = NULL; - nSensorSettings = BSEC_MAX_PHYSICAL_SENSOR; - zeroOutputs(); + nextCall = 0; + version.major = 0; + version.minor = 0; + version.major_bugfix = 0; + version.minor_bugfix = 0; + millisOverflowCounter = 0; + lastTime = 0; + bme680Status = BME680_OK; + outputTimestamp = 0; + _tempOffset = 0.0f; + status = BSEC_OK; + bsecConfig = NULL; + nSensorSettings = BSEC_MAX_PHYSICAL_SENSOR; + zeroOutputs(); } /** * @brief Function to initialize the BSEC library and the BME680 sensor */ -void Bsec::begin(uint8_t devId, enum bme680_intf intf, bme680_com_fptr_t read, bme680_com_fptr_t write, bme680_delay_fptr_t idleTask) +void Bsec::begin(uint8_t devId, + enum bme680_intf intf, + bme680_com_fptr_t read, + bme680_com_fptr_t write, + bme680_delay_fptr_t idleTask) { - _bme680.dev_id = devId; - _bme680.intf = intf; - _bme680.read = read; - _bme680.write = write; - _bme680.delay_ms = idleTask; - _bme680.amb_temp = 25; - _bme680.power_mode = BME680_FORCED_MODE; - - beginCommon(); + _bme680.dev_id = devId; + _bme680.intf = intf; + _bme680.read = read; + _bme680.write = write; + _bme680.delay_ms = idleTask; + _bme680.amb_temp = 25; + _bme680.power_mode = BME680_FORCED_MODE; + + beginCommon(); } /** @@ -83,17 +87,17 @@ void Bsec::begin(uint8_t devId, enum bme680_intf intf, bme680_com_fptr_t read, b */ void Bsec::begin(uint8_t i2cAddr, TwoWire &i2c, bme680_delay_fptr_t idleTask) { - _bme680.dev_id = i2cAddr; - _bme680.intf = BME680_I2C_INTF; - _bme680.read = Bsec::i2cRead; - _bme680.write = Bsec::i2cWrite; - _bme680.delay_ms = idleTask; - _bme680.amb_temp = 25; - _bme680.power_mode = BME680_FORCED_MODE; + _bme680.dev_id = i2cAddr; + _bme680.intf = BME680_I2C_INTF; + _bme680.read = Bsec::i2cRead; + _bme680.write = Bsec::i2cWrite; + _bme680.delay_ms = idleTask; + _bme680.amb_temp = 25; + _bme680.power_mode = BME680_FORCED_MODE; - Bsec::wireObj = &i2c; + Bsec::wireObj = &i2c; - beginCommon(); + beginCommon(); } /** @@ -101,19 +105,19 @@ void Bsec::begin(uint8_t i2cAddr, TwoWire &i2c, bme680_delay_fptr_t idleTask) */ void Bsec::begin(uint8_t chipSelect, SPIClass &spi, bme680_delay_fptr_t idleTask) { - _bme680.dev_id = chipSelect; - _bme680.intf = BME680_SPI_INTF; - _bme680.read = Bsec::spiTransfer; - _bme680.write = Bsec::spiTransfer; - _bme680.delay_ms = idleTask; - _bme680.amb_temp = 25; - _bme680.power_mode = BME680_FORCED_MODE; - - pinMode(chipSelect, OUTPUT); - digitalWrite(chipSelect, HIGH); - Bsec::spiObj = &spi; - - beginCommon(); + _bme680.dev_id = chipSelect; + _bme680.intf = BME680_SPI_INTF; + _bme680.read = Bsec::spiTransfer; + _bme680.write = Bsec::spiTransfer; + _bme680.delay_ms = idleTask; + _bme680.amb_temp = 25; + _bme680.power_mode = BME680_FORCED_MODE; + + pinMode(chipSelect, OUTPUT); + digitalWrite(chipSelect, HIGH); + Bsec::spiObj = &spi; + + beginCommon(); } /** @@ -121,41 +125,41 @@ void Bsec::begin(uint8_t chipSelect, SPIClass &spi, bme680_delay_fptr_t idleTask */ void Bsec::beginCommon(void) { - virtualSensors[0].sensor_id = BSEC_OUTPUT_IAQ; - virtualSensors[1].sensor_id = BSEC_OUTPUT_STATIC_IAQ; - virtualSensors[2].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT; - virtualSensors[3].sensor_id = BSEC_OUTPUT_BREATH_VOC_EQUIVALENT; - virtualSensors[4].sensor_id = BSEC_OUTPUT_RAW_TEMPERATURE; - virtualSensors[5].sensor_id = BSEC_OUTPUT_RAW_PRESSURE; - virtualSensors[6].sensor_id = BSEC_OUTPUT_RAW_HUMIDITY; - virtualSensors[7].sensor_id = BSEC_OUTPUT_RAW_GAS; - virtualSensors[8].sensor_id = BSEC_OUTPUT_STABILIZATION_STATUS; - virtualSensors[9].sensor_id = BSEC_OUTPUT_RUN_IN_STATUS; - virtualSensors[10].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE; - virtualSensors[11].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY; - virtualSensors[12].sensor_id = BSEC_OUTPUT_COMPENSATED_GAS; - virtualSensors[13].sensor_id = BSEC_OUTPUT_GAS_PERCENTAGE; - - virtualSensors[0].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[1].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[2].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[3].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[4].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[5].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[6].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[7].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[8].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[9].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[10].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[11].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[12].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - virtualSensors[13].sample_rate = BSEC_SAMPLE_RATE_DISABLED; - - status = bsec_init(); - - getVersion(); - - bme680Status = bme680_init(&_bme680); + virtualSensors[0].sensor_id = BSEC_OUTPUT_IAQ; + virtualSensors[1].sensor_id = BSEC_OUTPUT_STATIC_IAQ; + virtualSensors[2].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT; + virtualSensors[3].sensor_id = BSEC_OUTPUT_BREATH_VOC_EQUIVALENT; + virtualSensors[4].sensor_id = BSEC_OUTPUT_RAW_TEMPERATURE; + virtualSensors[5].sensor_id = BSEC_OUTPUT_RAW_PRESSURE; + virtualSensors[6].sensor_id = BSEC_OUTPUT_RAW_HUMIDITY; + virtualSensors[7].sensor_id = BSEC_OUTPUT_RAW_GAS; + virtualSensors[8].sensor_id = BSEC_OUTPUT_STABILIZATION_STATUS; + virtualSensors[9].sensor_id = BSEC_OUTPUT_RUN_IN_STATUS; + virtualSensors[10].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE; + virtualSensors[11].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY; + virtualSensors[12].sensor_id = BSEC_OUTPUT_COMPENSATED_GAS; + virtualSensors[13].sensor_id = BSEC_OUTPUT_GAS_PERCENTAGE; + + virtualSensors[0].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[1].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[2].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[3].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[4].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[5].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[6].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[7].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[8].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[9].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[10].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[11].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[12].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + virtualSensors[13].sample_rate = BSEC_SAMPLE_RATE_DISABLED; + + status = bsec_init(); + + getVersion(); + + bme680Status = bme680_init(&_bme680); } /** @@ -163,17 +167,20 @@ void Bsec::beginCommon(void) */ void Bsec::updateSubscription(bsec_virtual_sensor_t sensorList[], uint8_t nSensors, float sampleRate) { - for(uint8_t i = 0; i < nSensors; i++) - { - for(uint8_t j = 0; j < BSEC_NUMBER_OUTPUTS; j++) { - if(virtualSensors[j].sensor_id == sensorList[i]) { - virtualSensors[j].sample_rate = sampleRate; - } - } - } - - status = bsec_update_subscription(virtualSensors, BSEC_NUMBER_OUTPUTS, sensorSettings, &nSensorSettings); - return; + for (uint8_t i = 0; i < nSensors; i++) + { + for (uint8_t j = 0; j < BSEC_NUMBER_OUTPUTS; j++) + { + if (virtualSensors[j].sensor_id == sensorList[i]) + { + virtualSensors[j].sample_rate = sampleRate; + } + } + } + + status = bsec_update_subscription(virtualSensors, BSEC_NUMBER_OUTPUTS, sensorSettings, &nSensorSettings); + + return; } /** @@ -181,57 +188,72 @@ void Bsec::updateSubscription(bsec_virtual_sensor_t sensorList[], uint8_t nSenso */ bool Bsec::run(int64_t timeMilliseconds) { - bool newData = false; - /* Check if the time has arrived to call do_steps() */ - int64_t callTimeMs = timeMilliseconds; - - if(callTimeMs < 0) // Use millis - callTimeMs = getTimeMs(); - - if (callTimeMs >= nextCall) { - bsec_init(); - - if(validBsecState) - setState(bsecState); - - nSensorSettings = BSEC_MAX_PHYSICAL_SENSOR; - status = bsec_update_subscription(virtualSensors, BSEC_NUMBER_OUTPUTS, sensorSettings, &nSensorSettings); - - bsec_bme_settings_t bme680Settings; - - int64_t callTimeNs = callTimeMs * INT64_C(1000000); - - status = bsec_sensor_control(callTimeNs, &bme680Settings); - if (status < BSEC_OK) - return false; - - nextCall = bme680Settings.next_call / INT64_C(1000000); // Convert from ns to ms - - bme680Status = setBme680Config(bme680Settings); - if (bme680Status != BME680_OK) { - return false; - } - - bme680Status = bme680_set_sensor_mode(&_bme680); - if (bme680Status != BME680_OK) { - return false; - } - - /* Wait for measurement to complete */ - uint16_t meas_dur = 0; - - bme680_get_profile_dur(&meas_dur, &_bme680); - _bme680.delay_ms(meas_dur); - - newData = readProcessData(callTimeNs + (meas_dur * INT64_C(1000000)), bme680Settings); - - uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; - uint32_t n_serialized_state = BSEC_MAX_STATE_BLOB_SIZE; - status = bsec_get_state(0, bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, BSEC_MAX_STATE_BLOB_SIZE, &n_serialized_state); - validBsecState = true; - } - - return newData; + bool newData = false; + + /* Check if the time has arrived to call do_steps() */ + int64_t callTimeMs = timeMilliseconds; + + if (callTimeMs < 0) /* Use millis */ + { + callTimeMs = getTimeMs(); + } + + if (callTimeMs >= nextCall) + { + bsec_init(); + + if (validBsecState) + { + setState(bsecState); + } + + nSensorSettings = BSEC_MAX_PHYSICAL_SENSOR; + status = bsec_update_subscription(virtualSensors, BSEC_NUMBER_OUTPUTS, sensorSettings, &nSensorSettings); + + bsec_bme_settings_t bme680Settings; + + int64_t callTimeNs = callTimeMs * INT64_C(1000000); + + status = bsec_sensor_control(callTimeNs, &bme680Settings); + if (status < BSEC_OK) + { + return false; + } + + nextCall = bme680Settings.next_call / INT64_C(1000000); /* Convert from ns to ms */ + + bme680Status = setBme680Config(bme680Settings); + if (bme680Status != BME680_OK) + { + return false; + } + + bme680Status = bme680_set_sensor_mode(&_bme680); + if (bme680Status != BME680_OK) + { + return false; + } + + /* Wait for measurement to complete */ + uint16_t meas_dur = 0; + + bme680_get_profile_dur(&meas_dur, &_bme680); + _bme680.delay_ms(meas_dur); + + newData = readProcessData(callTimeNs + (meas_dur * INT64_C(1000000)), bme680Settings); + + uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; + uint32_t n_serialized_state = BSEC_MAX_STATE_BLOB_SIZE; + status = bsec_get_state(0, + bsecState, + BSEC_MAX_STATE_BLOB_SIZE, + workBuffer, + BSEC_MAX_STATE_BLOB_SIZE, + &n_serialized_state); + validBsecState = true; + } + + return newData; } /** @@ -239,13 +261,20 @@ bool Bsec::run(int64_t timeMilliseconds) */ void Bsec::getState(uint8_t *state) { - uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; - uint32_t n_serialized_state = BSEC_MAX_STATE_BLOB_SIZE; - if(!validBsecState) { - status = bsec_get_state(0, bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, BSEC_MAX_STATE_BLOB_SIZE, &n_serialized_state); - validBsecState = true; - } - memcpy(state, bsecState, BSEC_MAX_STATE_BLOB_SIZE); + uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; + uint32_t n_serialized_state = BSEC_MAX_STATE_BLOB_SIZE; + + if (!validBsecState) + { + status = bsec_get_state(0, + bsecState, + BSEC_MAX_STATE_BLOB_SIZE, + workBuffer, + BSEC_MAX_STATE_BLOB_SIZE, + &n_serialized_state); + validBsecState = true; + } + memcpy(state, bsecState, BSEC_MAX_STATE_BLOB_SIZE); } /** @@ -253,15 +282,18 @@ void Bsec::getState(uint8_t *state) */ void Bsec::setState(uint8_t *state) { - uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; - - status = bsec_set_state(state, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, BSEC_MAX_STATE_BLOB_SIZE); - - if(status == BSEC_OK) { - for(uint32_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) - bsecState[i] = state[i]; - validBsecState = true; - } + uint8_t workBuffer[BSEC_MAX_STATE_BLOB_SIZE]; + + status = bsec_set_state(state, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, BSEC_MAX_STATE_BLOB_SIZE); + + if (status == BSEC_OK) + { + for (uint32_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) + { + bsecState[i] = state[i]; + } + validBsecState = true; + } } /** @@ -269,10 +301,11 @@ void Bsec::setState(uint8_t *state) */ void Bsec::setConfig(const uint8_t *config) { - uint8_t workBuffer[BSEC_MAX_PROPERTY_BLOB_SIZE]; - bsecConfig = (uint8_t *)config; + uint8_t workBuffer[BSEC_MAX_PROPERTY_BLOB_SIZE]; + + bsecConfig = (uint8_t *)config; - status = bsec_set_configuration(config, BSEC_MAX_PROPERTY_BLOB_SIZE, workBuffer, sizeof(workBuffer)); + status = bsec_set_configuration(config, BSEC_MAX_PROPERTY_BLOB_SIZE, workBuffer, sizeof(workBuffer)); } /* Private functions */ @@ -282,7 +315,7 @@ void Bsec::setConfig(const uint8_t *config) */ void Bsec::getVersion(void) { - bsec_get_version(&version); + bsec_get_version(&version); } /** @@ -290,126 +323,140 @@ void Bsec::getVersion(void) */ bool Bsec::readProcessData(int64_t currTimeNs, bsec_bme_settings_t bme680Settings) { - bme680Status = bme680_get_sensor_data(&_data, &_bme680); - if (bme680Status != BME680_OK) { - return false; - } - - bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR]; // Temperature, Pressure, Humidity & Gas Resistance - uint8_t nInputs = 0, nOutputs = 0; - - if (_data.status & BME680_NEW_DATA_MSK) { - if (bme680Settings.process_data & BSEC_PROCESS_TEMPERATURE) { - inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE; + bme680Status = bme680_get_sensor_data(&_data, &_bme680); + if (bme680Status != BME680_OK) + { + return false; + } + + bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR]; /* Temperature, Pressure, Humidity & Gas Resistance */ + uint8_t nInputs = 0, nOutputs = 0; + + if (_data.status & BME680_NEW_DATA_MSK) + { + if (bme680Settings.process_data & BSEC_PROCESS_TEMPERATURE) + { + inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE; #ifdef BME680_FLOAT_POINT_COMPENSATION - inputs[nInputs].signal = _data.temperature; + inputs[nInputs].signal = _data.temperature; #else - inputs[nInputs].signal = _data.temperature / 100.0f; + inputs[nInputs].signal = _data.temperature / 100.0f; #endif - inputs[nInputs].time_stamp = currTimeNs; - nInputs++; - /* Temperature offset from the real temperature due to external heat sources */ - inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE; - inputs[nInputs].signal = _tempOffset; - inputs[nInputs].time_stamp = currTimeNs; - nInputs++; - } - if (bme680Settings.process_data & BSEC_PROCESS_HUMIDITY) { - inputs[nInputs].sensor_id = BSEC_INPUT_HUMIDITY; + inputs[nInputs].time_stamp = currTimeNs; + nInputs++; + + /* Temperature offset from the real temperature due to external heat sources */ + inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE; + inputs[nInputs].signal = _tempOffset; + inputs[nInputs].time_stamp = currTimeNs; + nInputs++; + } + if (bme680Settings.process_data & BSEC_PROCESS_HUMIDITY) + { + inputs[nInputs].sensor_id = BSEC_INPUT_HUMIDITY; #ifdef BME680_FLOAT_POINT_COMPENSATION - inputs[nInputs].signal = _data.humidity; + inputs[nInputs].signal = _data.humidity; #else - inputs[nInputs].signal = _data.humidity / 1000.0f; + inputs[nInputs].signal = _data.humidity / 1000.0f; #endif - inputs[nInputs].time_stamp = currTimeNs; - nInputs++; - } - if (bme680Settings.process_data & BSEC_PROCESS_PRESSURE) { - inputs[nInputs].sensor_id = BSEC_INPUT_PRESSURE; - inputs[nInputs].signal = _data.pressure; - inputs[nInputs].time_stamp = currTimeNs; - nInputs++; - } - if (bme680Settings.process_data & BSEC_PROCESS_GAS) { - inputs[nInputs].sensor_id = BSEC_INPUT_GASRESISTOR; - inputs[nInputs].signal = _data.gas_resistance; - inputs[nInputs].time_stamp = currTimeNs; - nInputs++; - } - } - - if (nInputs > 0) { - nOutputs = BSEC_NUMBER_OUTPUTS; - bsec_output_t _outputs[BSEC_NUMBER_OUTPUTS]; - - status = bsec_do_steps(inputs, nInputs, _outputs, &nOutputs); - if (status != BSEC_OK) - return false; - - zeroOutputs(); - - if (nOutputs > 0) { - outputTimestamp = _outputs[0].time_stamp / 1000000; // Convert from ns to ms - - for (uint8_t i = 0; i < nOutputs; i++) { - switch (_outputs[i].sensor_id) { - case BSEC_OUTPUT_IAQ: - iaq = _outputs[i].signal; - iaqAccuracy = _outputs[i].accuracy; - break; - case BSEC_OUTPUT_STATIC_IAQ: - staticIaq = _outputs[i].signal; - staticIaqAccuracy = _outputs[i].accuracy; - break; - case BSEC_OUTPUT_CO2_EQUIVALENT: - co2Equivalent = _outputs[i].signal; - co2Accuracy = _outputs[i].accuracy; - break; - case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT: - breathVocEquivalent = _outputs[i].signal; - breathVocAccuracy = _outputs[i].accuracy; - break; - case BSEC_OUTPUT_RAW_TEMPERATURE: - rawTemperature = _outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_PRESSURE: - pressure = _outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_HUMIDITY: - rawHumidity = _outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_GAS: - gasResistance = _outputs[i].signal; - break; - case BSEC_OUTPUT_STABILIZATION_STATUS: - stabStatus = _outputs[i].signal; - break; - case BSEC_OUTPUT_RUN_IN_STATUS: - runInStatus = _outputs[i].signal; - break; - case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE: - temperature = _outputs[i].signal; - break; - case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY: - humidity = _outputs[i].signal; - break; - case BSEC_OUTPUT_COMPENSATED_GAS: - compGasValue = _outputs[i].signal; - compGasAccuracy = _outputs[i].accuracy; - break; - case BSEC_OUTPUT_GAS_PERCENTAGE: - gasPercentage = _outputs[i].signal; - gasPercentageAcccuracy = _outputs[i].accuracy; - break; - default: - break; - } - } - return true; - } - } - - return false; + inputs[nInputs].time_stamp = currTimeNs; + nInputs++; + } + if (bme680Settings.process_data & BSEC_PROCESS_PRESSURE) + { + inputs[nInputs].sensor_id = BSEC_INPUT_PRESSURE; + inputs[nInputs].signal = _data.pressure; + inputs[nInputs].time_stamp = currTimeNs; + nInputs++; + } + if (bme680Settings.process_data & BSEC_PROCESS_GAS) + { + inputs[nInputs].sensor_id = BSEC_INPUT_GASRESISTOR; + inputs[nInputs].signal = _data.gas_resistance; + inputs[nInputs].time_stamp = currTimeNs; + nInputs++; + } + } + + if (nInputs > 0) + { + nOutputs = BSEC_NUMBER_OUTPUTS; + bsec_output_t _outputs[BSEC_NUMBER_OUTPUTS]; + + status = bsec_do_steps(inputs, nInputs, _outputs, &nOutputs); + if (status != BSEC_OK) + { + return false; + } + + zeroOutputs(); + + if (nOutputs > 0) + { + outputTimestamp = _outputs[0].time_stamp / 1000000; /* Convert from ns to ms */ + + for (uint8_t i = 0; i < nOutputs; i++) + { + switch (_outputs[i].sensor_id) + { + case BSEC_OUTPUT_IAQ: + iaq = _outputs[i].signal; + iaqAccuracy = _outputs[i].accuracy; + break; + case BSEC_OUTPUT_STATIC_IAQ: + staticIaq = _outputs[i].signal; + staticIaqAccuracy = _outputs[i].accuracy; + break; + case BSEC_OUTPUT_CO2_EQUIVALENT: + co2Equivalent = _outputs[i].signal; + co2Accuracy = _outputs[i].accuracy; + break; + case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT: + breathVocEquivalent = _outputs[i].signal; + breathVocAccuracy = _outputs[i].accuracy; + break; + case BSEC_OUTPUT_RAW_TEMPERATURE: + rawTemperature = _outputs[i].signal; + break; + case BSEC_OUTPUT_RAW_PRESSURE: + pressure = _outputs[i].signal; + break; + case BSEC_OUTPUT_RAW_HUMIDITY: + rawHumidity = _outputs[i].signal; + break; + case BSEC_OUTPUT_RAW_GAS: + gasResistance = _outputs[i].signal; + break; + case BSEC_OUTPUT_STABILIZATION_STATUS: + stabStatus = _outputs[i].signal; + break; + case BSEC_OUTPUT_RUN_IN_STATUS: + runInStatus = _outputs[i].signal; + break; + case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE: + temperature = _outputs[i].signal; + break; + case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY: + humidity = _outputs[i].signal; + break; + case BSEC_OUTPUT_COMPENSATED_GAS: + compGasValue = _outputs[i].signal; + compGasAccuracy = _outputs[i].accuracy; + break; + case BSEC_OUTPUT_GAS_PERCENTAGE: + gasPercentage = _outputs[i].signal; + gasPercentageAcccuracy = _outputs[i].accuracy; + break; + default: + break; + } + } + + return true; + } + } + + return false; } /** @@ -417,15 +464,16 @@ bool Bsec::readProcessData(int64_t currTimeNs, bsec_bme_settings_t bme680Setting */ int8_t Bsec::setBme680Config(bsec_bme_settings_t bme680Settings) { - _bme680.gas_sett.run_gas = bme680Settings.run_gas; - _bme680.tph_sett.os_hum = bme680Settings.humidity_oversampling; - _bme680.tph_sett.os_temp = bme680Settings.temperature_oversampling; - _bme680.tph_sett.os_pres = bme680Settings.pressure_oversampling; - _bme680.gas_sett.heatr_temp = bme680Settings.heater_temperature; - _bme680.gas_sett.heatr_dur = bme680Settings.heating_duration; - uint16_t desired_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL - | BME680_GAS_SENSOR_SEL; - return bme680_set_sensor_settings(desired_settings, &_bme680); + _bme680.gas_sett.run_gas = bme680Settings.run_gas; + _bme680.tph_sett.os_hum = bme680Settings.humidity_oversampling; + _bme680.tph_sett.os_temp = bme680Settings.temperature_oversampling; + _bme680.tph_sett.os_pres = bme680Settings.pressure_oversampling; + _bme680.gas_sett.heatr_temp = bme680Settings.heater_temperature; + _bme680.gas_sett.heatr_dur = bme680Settings.heating_duration; + uint16_t desired_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | + BME680_GAS_SENSOR_SEL; + + return bme680_set_sensor_settings(desired_settings, &_bme680); } /** @@ -433,27 +481,27 @@ int8_t Bsec::setBme680Config(bsec_bme_settings_t bme680Settings) */ void Bsec::zeroOutputs(void) { - temperature = 0.0f; - pressure = 0.0f; - humidity = 0.0f; - gasResistance = 0.0f; - rawTemperature = 0.0f; - rawHumidity = 0.0f; - stabStatus = 0.0f; - runInStatus = 0.0f; - iaq = 0.0f; - iaqAccuracy = 0; - staticIaq = 0.0f; - staticIaqAccuracy = 0; - co2Equivalent = 0.0f; - co2Accuracy = 0; - breathVocEquivalent = 0.0f; - breathVocAccuracy = 0; - compGasValue = 0.0f; - compGasAccuracy = 0; - gasPercentage = 0.0f; - gasPercentageAcccuracy = 0; - validBsecState = false; + temperature = 0.0f; + pressure = 0.0f; + humidity = 0.0f; + gasResistance = 0.0f; + rawTemperature = 0.0f; + rawHumidity = 0.0f; + stabStatus = 0.0f; + runInStatus = 0.0f; + iaq = 0.0f; + iaqAccuracy = 0; + staticIaq = 0.0f; + staticIaqAccuracy = 0; + co2Equivalent = 0.0f; + co2Accuracy = 0; + breathVocEquivalent = 0.0f; + breathVocAccuracy = 0; + compGasValue = 0.0f; + compGasAccuracy = 0; + gasPercentage = 0.0f; + gasPercentageAcccuracy = 0; + validBsecState = false; } /** @@ -461,46 +509,55 @@ void Bsec::zeroOutputs(void) */ int64_t Bsec::getTimeMs(void) { - int64_t timeMs = millis(); + int64_t timeMs = millis(); - if (lastTime > timeMs) { // An overflow occurred - millisOverflowCounter++; - } + if (lastTime > timeMs) /* An overflow occurred */ + { + millisOverflowCounter++; + } - lastTime = timeMs; + lastTime = timeMs; - return timeMs + ((int64_t)millisOverflowCounter << 32); + return timeMs + ((int64_t)millisOverflowCounter << 32); } /** - @brief Task that delays for a ms period of time + * @brief Task that delays for a ms period of time */ void Bsec::delay_ms(uint32_t period) { - // Wait for a period amount of ms - // The system may simply idle, sleep or even perform background tasks - delay(period); + /* + * Wait for a period amount of ms + * The system may simply idle, sleep or even perform background tasks + */ + delay(period); } /** - @brief Callback function for reading registers over I2C + * @brief Callback function for reading registers over I2C */ int8_t Bsec::i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) { - uint16_t i; - int8_t rslt = 0; - if(Bsec::wireObj) { - Bsec::wireObj->beginTransmission(devId); - Bsec::wireObj->write(regAddr); - rslt = Bsec::wireObj->endTransmission(); - Bsec::wireObj->requestFrom((int) devId, (int) length); - for (i = 0; (i < length) && Bsec::wireObj->available(); i++) { - regData[i] = Bsec::wireObj->read(); - } - } else { - rslt = -1; - } - return rslt; + uint16_t i; + int8_t rslt = 0; + + if (Bsec::wireObj) + { + Bsec::wireObj->beginTransmission(devId); + Bsec::wireObj->write(regAddr); + rslt = Bsec::wireObj->endTransmission(); + Bsec::wireObj->requestFrom((int) devId, (int) length); + for (i = 0; (i < length) && Bsec::wireObj->available(); i++) + { + regData[i] = Bsec::wireObj->read(); + } + } + else + { + rslt = -1; + } + + return rslt; } /** @@ -508,20 +565,25 @@ int8_t Bsec::i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t */ int8_t Bsec::i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) { - uint16_t i; - int8_t rslt = 0; - if(Bsec::wireObj) { - Bsec::wireObj->beginTransmission(devId); - Bsec::wireObj->write(regAddr); - for (i = 0; i < length; i++) { - Bsec::wireObj->write(regData[i]); - } - rslt = Bsec::wireObj->endTransmission(); - } else { - rslt = -1; - } - - return rslt; + uint16_t i; + int8_t rslt = 0; + + if (Bsec::wireObj) + { + Bsec::wireObj->beginTransmission(devId); + Bsec::wireObj->write(regAddr); + for (i = 0; i < length; i++) + { + Bsec::wireObj->write(regData[i]); + } + rslt = Bsec::wireObj->endTransmission(); + } + else + { + rslt = -1; + } + + return rslt; } /** @@ -529,21 +591,27 @@ int8_t Bsec::i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t */ int8_t Bsec::spiTransfer(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) { - int8_t rslt = 0; - if(Bsec::spiObj) { - Bsec::spiObj->beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); // Can be up to 10MHz - - digitalWrite(devId, LOW); - - Bsec::spiObj->transfer(regAddr); // Write the register address, ignore the return - for (uint16_t i = 0; i < length; i++) - regData[i] = Bsec::spiObj->transfer(regData[i]); - - digitalWrite(devId, HIGH); - Bsec::spiObj->endTransaction(); - } else { - rslt = -1; - } - - return rslt; + int8_t rslt = 0; + + if (Bsec::spiObj) + { + Bsec::spiObj->beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); /* Can be up to 10MHz */ + + digitalWrite(devId, LOW); + + Bsec::spiObj->transfer(regAddr); /* Write the register address, ignore the return */ + for (uint16_t i = 0; i < length; i++) + { + regData[i] = Bsec::spiObj->transfer(regData[i]); + } + + digitalWrite(devId, HIGH); + Bsec::spiObj->endTransaction(); + } + else + { + rslt = -1; + } + + return rslt; } \ No newline at end of file diff --git a/src/bsec.h b/src/bsec.h index 5d8f66f..7351c23 100644 --- a/src/bsec.h +++ b/src/bsec.h @@ -30,9 +30,9 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * @file bsec.h - * @date 10 Jan 2020 - * @version 1.5.1474 + * @file bsec.h + * @date 10 Jan 2020 + * @version 1.5.1474 * */ @@ -48,183 +48,192 @@ #include "bme680/bme680.h" /* BSEC class definition */ -class Bsec -{ +class Bsec { public: - /* Public variables */ - bsec_version_t version; // Stores the version of the BSEC algorithm - int64_t nextCall; // Stores the time when the algorithm has to be called next in ms - int8_t bme680Status; // Placeholder for the BME680 driver's error codes - bsec_library_return_t status; - float iaq, rawTemperature, pressure, rawHumidity, gasResistance, stabStatus, runInStatus, temperature, humidity, - staticIaq, co2Equivalent, breathVocEquivalent, compGasValue, gasPercentage; - uint8_t iaqAccuracy, staticIaqAccuracy, co2Accuracy, breathVocAccuracy, compGasAccuracy, gasPercentageAcccuracy; - int64_t outputTimestamp; // Timestamp in ms of the output - static TwoWire *wireObj; - static SPIClass *spiObj; - - /* Public APIs */ - /** - * @brief Constructor - */ - Bsec(); - - /** - * @brief Function to initialize the BSEC library and the BME680 sensor - * @param devId : Device identifier parameter for the read/write interface functions - * @param intf : Physical communication interface - * @param read : Pointer to the read function - * @param write : Pointer to the write function - * @param idleTask : Pointer to the idling task - */ - void begin(uint8_t devId, enum bme680_intf intf, bme680_com_fptr_t read, bme680_com_fptr_t write, bme680_delay_fptr_t idleTask); - - /** - * @brief Function to initialize the BSEC library and the BME680 sensor - * @param i2cAddr : I2C address - * @param i2c : Pointer to the TwoWire object - * @param idleTask : Task to be called when idling - */ - void begin(uint8_t i2cAddr, TwoWire &i2c, bme680_delay_fptr_t idleTask = delay_ms); - - /** - * @brief Function to initialize the BSEC library and the BME680 sensor - * @param chipSelect : SPI chip select - * @param spi : Pointer to the SPIClass object - * @param idleTask : Task to be called when idling - */ - void begin(uint8_t chipSelect, SPIClass &spi, bme680_delay_fptr_t idleTask = delay_ms); - - /** - * @brief Function that sets the desired sensors and the sample rates - * @param sensorList : The list of output sensors - * @param nSensors : Number of outputs requested - * @param sampleRate : The sample rate of requested sensors - */ - void updateSubscription(bsec_virtual_sensor_t sensorList[], uint8_t nSensors, float sampleRate = BSEC_SAMPLE_RATE_ULP); - - /** - * @brief Callback from the user to trigger reading of data from the BME680, process and store outputs - * @param timeMilliseconds : Time in milliseconds. Defaults to -1 which means it takes the time from millis() - * @return true if there are new outputs. false otherwise - */ - bool run(int64_t timeMilliseconds = -1); - - /** - * @brief Function to get the state of the algorithm to save to non-volatile memory - * @param state : Pointer to a memory location that contains the state - */ - void getState(uint8_t *state); - - /** - * @brief Function to set the state of the algorithm from non-volatile memory - * @param state : Pointer to a memory location that contains the state - */ - void setState(uint8_t *state); - - /** - * @brief Function to set the configuration of the algorithm from memory - * @param state : Pointer to a memory location that contains the configuration - */ - void setConfig(const uint8_t *config); - - /** - * @brief Function to set the temperature offset - * @param tempOffset : Temperature offset in degree Celsius - */ - void setTemperatureOffset(float tempOffset) - { - _tempOffset = tempOffset; - } - - - /** - * @brief Function to calculate an int64_t timestamp in milliseconds - */ - int64_t getTimeMs(void); - - /** - * @brief Task that delays for a ms period of time - * @param period : Period of time in ms - */ - static void delay_ms(uint32_t period); - - /** - * @brief Callback function for reading registers over I2C - * @param devId : Library agnostic parameter to identify the device to communicate with - * @param regAddr : Register address - * @param regData : Pointer to the array containing the data to be read - * @param length : Length of the array of data - * @return Zero for success, non-zero otherwise - */ - static int8_t i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); - - /** - * @brief Callback function for writing registers over I2C - * @param devId : Library agnostic parameter to identify the device to communicate with - * @param regAddr : Register address - * @param regData : Pointer to the array containing the data to be written - * @param length : Length of the array of data - * @return Zero for success, non-zero otherwise - */ - static int8_t i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); - - /** - * @brief Callback function for reading and writing registers over SPI - * @param devId : Library agnostic parameter to identify the device to communicate with - * @param regAddr : Register address - * @param regData : Pointer to the array containing the data to be read or written - * @param length : Length of the array of data - * @return Zero for success, non-zero otherwise - */ - static int8_t spiTransfer(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); + +/* Public variables */ +bsec_version_t version; /* Stores the version of the BSEC algorithm */ +int64_t nextCall; /* Stores the time when the algorithm has to be called next in ms */ +int8_t bme680Status; /* Placeholder for the BME680 driver's error codes */ +bsec_library_return_t status; +float iaq, rawTemperature, pressure, rawHumidity, gasResistance, stabStatus, runInStatus, temperature, humidity, + staticIaq, co2Equivalent, breathVocEquivalent, compGasValue, gasPercentage; +uint8_t iaqAccuracy, staticIaqAccuracy, co2Accuracy, breathVocAccuracy, compGasAccuracy, gasPercentageAcccuracy; +int64_t outputTimestamp; /* Timestamp in ms of the output */ +static TwoWire *wireObj; +static SPIClass *spiObj; + +/* Public APIs */ + +/** + * @brief Constructor + */ +Bsec(); + +/** + * @brief Function to initialize the BSEC library and the BME680 sensor + * @param devId : Device identifier parameter for the read/write interface functions + * @param intf : Physical communication interface + * @param read : Pointer to the read function + * @param write : Pointer to the write function + * @param idleTask : Pointer to the idling task + */ +void begin(uint8_t devId, + enum bme680_intf intf, + bme680_com_fptr_t read, + bme680_com_fptr_t write, + bme680_delay_fptr_t idleTask); + +/** + * @brief Function to initialize the BSEC library and the BME680 sensor + * @param i2cAddr : I2C address + * @param i2c : Pointer to the TwoWire object + * @param idleTask : Task to be called when idling + */ +void begin(uint8_t i2cAddr, TwoWire &i2c, bme680_delay_fptr_t idleTask = delay_ms); + +/** + * @brief Function to initialize the BSEC library and the BME680 sensor + * @param chipSelect : SPI chip select + * @param spi : Pointer to the SPIClass object + * @param idleTask : Task to be called when idling + */ +void begin(uint8_t chipSelect, SPIClass &spi, bme680_delay_fptr_t idleTask = delay_ms); + +/** + * @brief Function that sets the desired sensors and the sample rates + * @param sensorList : The list of output sensors + * @param nSensors : Number of outputs requested + * @param sampleRate : The sample rate of requested sensors + */ +void updateSubscription(bsec_virtual_sensor_t sensorList[], uint8_t nSensors, float sampleRate = BSEC_SAMPLE_RATE_ULP); + +/** + * @brief Callback from the user to trigger reading of data from the BME680, process and store outputs + * @param timeMilliseconds : Time in milliseconds. Defaults to -1 which means it takes the time from millis() + * @return true if there are new outputs. false otherwise + */ +bool run(int64_t timeMilliseconds = -1); + +/** + * @brief Function to get the state of the algorithm to save to non-volatile memory + * @param state : Pointer to a memory location that contains the state + */ +void getState(uint8_t *state); + +/** + * @brief Function to set the state of the algorithm from non-volatile memory + * @param state : Pointer to a memory location that contains the state + */ +void setState(uint8_t *state); + +/** + * @brief Function to set the configuration of the algorithm from memory + * @param state : Pointer to a memory location that contains the configuration + */ +void setConfig(const uint8_t *config); + +/** + * @brief Function to set the temperature offset + * @param tempOffset : Temperature offset in degree Celsius + */ +void setTemperatureOffset(float tempOffset) +{ + _tempOffset = tempOffset; +} + +/** + * @brief Function to calculate an int64_t timestamp in milliseconds + */ +int64_t getTimeMs(void); + +/** + * @brief Task that delays for a ms period of time + * @param period : Period of time in ms + */ +static void delay_ms(uint32_t period); + +/** + * @brief Callback function for reading registers over I2C + * @param devId : Library agnostic parameter to identify the device to communicate with + * @param regAddr : Register address + * @param regData : Pointer to the array containing the data to be read + * @param length : Length of the array of data + * @return Zero for success, non-zero otherwise + */ +static int8_t i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); + +/** + * @brief Callback function for writing registers over I2C + * @param devId : Library agnostic parameter to identify the device to communicate with + * @param regAddr : Register address + * @param regData : Pointer to the array containing the data to be written + * @param length : Length of the array of data + * @return Zero for success, non-zero otherwise + */ +static int8_t i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); + +/** + * @brief Callback function for reading and writing registers over SPI + * @param devId : Library agnostic parameter to identify the device to communicate with + * @param regAddr : Register address + * @param regData : Pointer to the array containing the data to be read or written + * @param length : Length of the array of data + * @return Zero for success, non-zero otherwise + */ +static int8_t spiTransfer(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length); private: - /* Private variables */ - struct bme680_dev _bme680; - struct bme680_field_data _data; - float _tempOffset; - // Global variables to help create a millisecond timestamp that doesn't overflow every 51 days. - // If it overflows, it will have a negative value. Something that should never happen. - uint32_t millisOverflowCounter; - uint32_t lastTime; - - uint8_t *bsecConfig; - bool validBsecState; - uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE]; - bsec_sensor_configuration_t virtualSensors[BSEC_NUMBER_OUTPUTS], sensorSettings[BSEC_MAX_PHYSICAL_SENSOR]; - uint8_t nSensorSettings; - - /* Private APIs */ - /** - * @brief Get the version of the BSEC library - */ - void getVersion(void); - - /** - * @brief Read data from the BME680 and process it - * @param currTimeNs: Current time in ns - * @param bme680Settings: BME680 sensor's settings - * @return true if there are new outputs. false otherwise - */ - bool readProcessData(int64_t currTimeNs, bsec_bme_settings_t bme680Settings); - - /** - * @brief Set the BME680 sensor's configuration - * @param bme680Settings: Settings to configure the BME680 - * @return BME680 return code. BME680_OK for success, failure otherwise - */ - int8_t setBme680Config(bsec_bme_settings_t bme680Settings); - - /** - * @brief Common code for the begin function - */ - void beginCommon(void); - - /** - * @brief Function to zero the outputs - */ - void zeroOutputs(void); + +/* Private variables */ +struct bme680_dev _bme680; +struct bme680_field_data _data; +float _tempOffset; +/* + * Global variables to help create a millisecond timestamp that doesn't overflow every 51 days. + * If it overflows, it will have a negative value. Something that should never happen. + */ +uint32_t millisOverflowCounter; +uint32_t lastTime; + +uint8_t *bsecConfig; +bool validBsecState; +uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE]; +bsec_sensor_configuration_t virtualSensors[BSEC_NUMBER_OUTPUTS], sensorSettings[BSEC_MAX_PHYSICAL_SENSOR]; +uint8_t nSensorSettings; + +/* Private APIs */ + +/** + * @brief Get the version of the BSEC library + */ +void getVersion(void); + +/** + * @brief Read data from the BME680 and process it + * @param currTimeNs: Current time in ns + * @param bme680Settings: BME680 sensor's settings + * @return true if there are new outputs. false otherwise + */ +bool readProcessData(int64_t currTimeNs, bsec_bme_settings_t bme680Settings); + +/** + * @brief Set the BME680 sensor's configuration + * @param bme680Settings: Settings to configure the BME680 + * @return BME680 return code. BME680_OK for success, failure otherwise + */ +int8_t setBme680Config(bsec_bme_settings_t bme680Settings); + +/** + * @brief Common code for the begin function + */ +void beginCommon(void); + +/** + * @brief Function to zero the outputs + */ +void zeroOutputs(void); + }; #endif