summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/i2cdetect/main.cc39
-rw-r--r--src/driver/bme680.cc714
2 files changed, 261 insertions, 492 deletions
diff --git a/src/app/i2cdetect/main.cc b/src/app/i2cdetect/main.cc
index 2b30b47..acf5ef1 100644
--- a/src/app/i2cdetect/main.cc
+++ b/src/app/i2cdetect/main.cc
@@ -47,35 +47,32 @@ void loop(void)
}
#endif
#ifdef DRIVER_BME680
- struct bme680_dev gas_sensor;
-
- gas_sensor.dev_id = BME680_I2C_ADDR_SECONDARY;
- gas_sensor.intf = BME680_I2C_INTF;
- gas_sensor.read = bme680_i2c_read;
- gas_sensor.write = bme680_i2c_write;
- gas_sensor.delay_ms = bme680_delay_ms;
+ bme680.intf = BME680_I2C_INTF;
+ bme680.read = bme680_i2c_read;
+ bme680.write = bme680_i2c_write;
+ bme680.delay_ms = bme680_delay_ms;
/* amb_temp can be set to 25 prior to configuring the gas sensor
* or by performing a few temperature readings without operating the gas sensor.
*/
- gas_sensor.amb_temp = 25;
+ bme680.amb_temp = 25;
int8_t rslt = BME680_OK;
- rslt = bme680_init(&gas_sensor);
+ rslt = bme680.init();
kout << "BME680 init " << rslt << endl;
- gas_sensor.power_mode = BME680_FORCED_MODE;
- gas_sensor.tph_sett.os_hum = BME680_OS_1X;
- gas_sensor.tph_sett.os_pres = BME680_OS_16X;
- gas_sensor.tph_sett.os_temp = BME680_OS_2X;
-
- gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
- gas_sensor.gas_sett.heatr_dur = 150;
- gas_sensor.gas_sett.heatr_temp = 300;
- bme680_set_sensor_settings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL, &gas_sensor);
- bme680_set_sensor_mode(&gas_sensor);
- arch.delay_ms(500);
+ bme680.power_mode = BME680_FORCED_MODE;
+ bme680.tph_sett.os_hum = BME680_OS_1X;
+ bme680.tph_sett.os_pres = BME680_OS_16X;
+ bme680.tph_sett.os_temp = BME680_OS_2X;
+
+ bme680.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
+ bme680.gas_sett.heatr_dur = 30;
+ bme680.gas_sett.heatr_temp = 300;
+ bme680.setSensorSettings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL);
+ bme680.setSensorMode();
+ arch.delay_ms(200);
struct bme680_field_data data;
- bme680_get_sensor_data(&data, &gas_sensor);
+ bme680.getSensorData(&data);
kout << "BME680 temperature " << (float)data.temperature / 100 << " degC" << endl;
kout << "BME680 humidity " << (float)data.humidity / 1000 << " %" << endl;
kout << "BME680 pressure " << (float)data.pressure / 100 << " hPa" << endl;
diff --git a/src/driver/bme680.cc b/src/driver/bme680.cc
index 9469c8f..52f94bf 100644
--- a/src/driver/bme680.cc
+++ b/src/driver/bme680.cc
@@ -49,256 +49,27 @@
@brief Sensor driver for BME680 sensor */
#include "driver/bme680.h"
-/*!
- * @brief This internal API is used to read the calibrated data from the sensor.
- *
- * This function is used to retrieve the calibration
- * data from the image registers of the sensor.
- *
- * @note Registers 89h to A1h for calibration data 1 to 24
- * from bit 0 to 7
- * @note Registers E1h to F0h for calibration data 25 to 40
- * from bit 0 to 7
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status.
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t get_calib_data(struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to set the gas configuration of the sensor.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status.
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t set_gas_config(struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to get the gas configuration of the sensor.
- * @note heatr_temp and heatr_dur values are currently register data
- * and not the actual values set
- *
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status.
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t get_gas_config(struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the Heat duration value.
- *
- * @param[in] dur :Value of the duration to be shared.
- *
- * @return uint8_t threshold duration after calculation.
- */
-static uint8_t calc_heater_dur(uint16_t dur);
-
-#ifndef BME680_FLOAT_POINT_COMPENSATION
-
-/*!
- * @brief This internal API is used to calculate the temperature value.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] temp_adc :Contains the temperature ADC value .
- *
- * @return uint32_t calculated temperature.
- */
-static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the pressure value.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] pres_adc :Contains the pressure ADC value .
- *
- * @return uint32_t calculated pressure.
- */
-static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the humidity value.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] hum_adc :Contains the humidity ADC value.
- *
- * @return uint32_t calculated humidity.
- */
-static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the Gas Resistance value.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] gas_res_adc :Contains the Gas Resistance ADC value.
- * @param[in] gas_range :Contains the range of gas values.
- *
- * @return uint32_t calculated gas resistance.
- */
-static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the Heat Resistance value.
- *
- * @param[in] dev : Structure instance of bme680_dev
- * @param[in] temp : Contains the target temperature value.
- *
- * @return uint8_t calculated heater resistance.
- */
-static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev);
-
-#else
-/*!
- * @brief This internal API is used to calculate the
- * temperature value value in float format
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] temp_adc :Contains the temperature ADC value .
- *
- * @return Calculated temperature in float
- */
-static float calc_temperature(uint32_t temp_adc, struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the
- * pressure value value in float format
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] pres_adc :Contains the pressure ADC value .
- *
- * @return Calculated pressure in float.
- */
-static float calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the
- * humidity value value in float format
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] hum_adc :Contains the humidity ADC value.
- *
- * @return Calculated humidity in float.
- */
-static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the
- * gas resistance value value in float format
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] gas_res_adc :Contains the Gas Resistance ADC value.
- * @param[in] gas_range :Contains the range of gas values.
- *
- * @return Calculated gas resistance in float.
- */
-static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to calculate the
- * heater resistance value in float format
- *
- * @param[in] temp : Contains the target temperature value.
- * @param[in] dev : Structure instance of bme680_dev.
- *
- * @return Calculated heater resistance in float.
- */
-static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev);
-
-#endif
-
-/*!
- * @brief This internal API is used to calculate the field data of sensor.
- *
- * @param[out] data :Structure instance to hold the data
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return int8_t result of the field data from sensor.
- */
-static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to set the memory page
- * based on register address.
- *
- * The value of memory page
- * value | Description
- * --------|--------------
- * 0 | BME680_PAGE0_SPI
- * 1 | BME680_PAGE1_SPI
- *
- * @param[in] dev :Structure instance of bme680_dev.
- * @param[in] reg_addr :Contains the register address array.
- *
- * @return Result of API execution status
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to get the memory page based
- * on register address.
- *
- * The value of memory page
- * value | Description
- * --------|--------------
- * 0 | BME680_PAGE0_SPI
- * 1 | BME680_PAGE1_SPI
- *
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t get_mem_page(struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to validate the device pointer for
- * null conditions.
- *
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t null_ptr_check(const struct bme680_dev *dev);
-
-/*!
- * @brief This internal API is used to check the boundary
- * conditions.
- *
- * @param[in] value :pointer to the value.
- * @param[in] min :minimum value.
- * @param[in] max :maximum value.
- * @param[in] dev :Structure instance of bme680_dev.
- *
- * @return Result of API execution status
- * @retval zero -> Success / +ve value -> Warning / -ve value -> Error
- */
-static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bme680_dev *dev);
/****************** Global Function Definitions *******************************/
/*!
*@brief This API is the entry point.
*It reads the chip-id and calibration data from the sensor.
*/
-int8_t bme680_init(struct bme680_dev *dev)
+int8_t BME680::init()
{
int8_t rslt;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
/* Soft reset to restore it to default values*/
- rslt = bme680_soft_reset(dev);
+ rslt = softReset();
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(BME680_CHIP_ID_ADDR, &dev->chip_id, 1, dev);
+ rslt = getRegs(BME680_CHIP_ID_ADDR, &chip_id, 1);
if (rslt == BME680_OK) {
- if (dev->chip_id == BME680_CHIP_ID) {
+ if (chip_id == BME680_CHIP_ID) {
/* Get the Calibration data */
- rslt = get_calib_data(dev);
+ rslt = getCalibData();
} else {
rslt = BME680_E_DEV_NOT_FOUND;
}
@@ -312,21 +83,21 @@ int8_t bme680_init(struct bme680_dev *dev)
/*!
* @brief This API reads the data from the given register address of the sensor.
*/
-int8_t bme680_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bme680_dev *dev)
+int8_t BME680::getRegs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- if (dev->intf == BME680_SPI_INTF) {
+ if (intf == BME680_SPI_INTF) {
/* Set the memory page */
- rslt = set_mem_page(reg_addr, dev);
+ rslt = setMemPage(reg_addr);
if (rslt == BME680_OK)
reg_addr = reg_addr | BME680_SPI_RD_MSK;
}
- dev->com_rslt = dev->read(dev->dev_id, reg_addr, reg_data, len);
- if (dev->com_rslt != 0)
+ com_rslt = read(dev_id, reg_addr, reg_data, len);
+ if (com_rslt != 0)
rslt = BME680_E_COM_FAIL;
}
@@ -337,7 +108,7 @@ int8_t bme680_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct
* @brief This API writes the given data to the register address
* of the sensor.
*/
-int8_t bme680_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, struct bme680_dev *dev)
+int8_t BME680::setRegs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len)
{
int8_t rslt;
/* Length of the temporary buffer is 2*(length of register)*/
@@ -345,14 +116,14 @@ int8_t bme680_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t
uint16_t index;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
if ((len > 0) && (len < BME680_TMP_BUFFER_LENGTH / 2)) {
/* Interleave the 2 arrays */
for (index = 0; index < len; index++) {
- if (dev->intf == BME680_SPI_INTF) {
+ if (intf == BME680_SPI_INTF) {
/* Set the memory page */
- rslt = set_mem_page(reg_addr[index], dev);
+ rslt = setMemPage(reg_addr[index]);
tmp_buff[(2 * index)] = reg_addr[index] & BME680_SPI_WR_MSK;
} else {
tmp_buff[(2 * index)] = reg_addr[index];
@@ -361,8 +132,8 @@ int8_t bme680_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t
}
/* Write the interleaved array */
if (rslt == BME680_OK) {
- dev->com_rslt = dev->write(dev->dev_id, tmp_buff[0], &tmp_buff[1], (2 * len) - 1);
- if (dev->com_rslt != 0)
+ com_rslt = write(dev_id, tmp_buff[0], &tmp_buff[1], (2 * len) - 1);
+ if (com_rslt != 0)
rslt = BME680_E_COM_FAIL;
}
} else {
@@ -376,7 +147,7 @@ int8_t bme680_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint8_t
/*!
* @brief This API performs the soft reset of the sensor.
*/
-int8_t bme680_soft_reset(struct bme680_dev *dev)
+int8_t BME680::softReset()
{
int8_t rslt;
uint8_t reg_addr = BME680_SOFT_RESET_ADDR;
@@ -384,21 +155,21 @@ int8_t bme680_soft_reset(struct bme680_dev *dev)
uint8_t soft_rst_cmd = BME680_SOFT_RESET_CMD;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- if (dev->intf == BME680_SPI_INTF)
- rslt = get_mem_page(dev);
+ if (intf == BME680_SPI_INTF)
+ rslt = getMemPage();
/* Reset the device */
if (rslt == BME680_OK) {
- rslt = bme680_set_regs(&reg_addr, &soft_rst_cmd, 1, dev);
+ rslt = setRegs(&reg_addr, &soft_rst_cmd, 1);
/* Wait for 5ms */
- dev->delay_ms(BME680_RESET_PERIOD);
+ delay_ms(BME680_RESET_PERIOD);
if (rslt == BME680_OK) {
/* After reset get the memory page */
- if (dev->intf == BME680_SPI_INTF)
- rslt = get_mem_page(dev);
+ if (intf == BME680_SPI_INTF)
+ rslt = getMemPage();
}
}
}
@@ -410,7 +181,7 @@ int8_t bme680_soft_reset(struct bme680_dev *dev)
* @brief This API is used to set the oversampling, filter and T,P,H, gas selection
* settings in the sensor.
*/
-int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *dev)
+int8_t BME680::setSensorSettings(uint16_t desired_settings)
{
int8_t rslt;
uint8_t reg_addr;
@@ -418,28 +189,28 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
uint8_t count = 0;
uint8_t reg_array[BME680_REG_BUFFER_LENGTH] = { 0 };
uint8_t data_array[BME680_REG_BUFFER_LENGTH] = { 0 };
- uint8_t intended_power_mode = dev->power_mode; /* Save intended power mode */
+ uint8_t intended_power_mode = power_mode; /* Save intended power mode */
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
if (desired_settings & BME680_GAS_MEAS_SEL)
- rslt = set_gas_config(dev);
+ rslt = setGasConfig();
- dev->power_mode = BME680_SLEEP_MODE;
+ power_mode = BME680_SLEEP_MODE;
if (rslt == BME680_OK)
- rslt = bme680_set_sensor_mode(dev);
+ rslt = getSensorMode();
/* Selecting the filter */
if (desired_settings & BME680_FILTER_SEL) {
- rslt = boundary_check(&dev->tph_sett.filter, BME680_FILTER_SIZE_0, BME680_FILTER_SIZE_127, dev);
+ rslt = boundaryCheck(&tph_sett.filter, BME680_FILTER_SIZE_0, BME680_FILTER_SIZE_127);
reg_addr = BME680_CONF_ODR_FILT_ADDR;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(reg_addr, &data, 1, dev);
+ rslt = getRegs(reg_addr, &data, 1);
if (desired_settings & BME680_FILTER_SEL)
- data = BME680_SET_BITS(data, BME680_FILTER, dev->tph_sett.filter);
+ data = BME680_SET_BITS(data, BME680_FILTER, tph_sett.filter);
reg_array[count] = reg_addr; /* Append configuration */
data_array[count] = data;
@@ -448,13 +219,13 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
/* Selecting heater control for the sensor */
if (desired_settings & BME680_HCNTRL_SEL) {
- rslt = boundary_check(&dev->gas_sett.heatr_ctrl, BME680_ENABLE_HEATER,
- BME680_DISABLE_HEATER, dev);
+ rslt = boundaryCheck(&gas_sett.heatr_ctrl, BME680_ENABLE_HEATER,
+ BME680_DISABLE_HEATER);
reg_addr = BME680_CONF_HEAT_CTRL_ADDR;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(reg_addr, &data, 1, dev);
- data = BME680_SET_BITS_POS_0(data, BME680_HCTRL, dev->gas_sett.heatr_ctrl);
+ rslt = getRegs(reg_addr, &data, 1);
+ data = BME680_SET_BITS_POS_0(data, BME680_HCTRL, gas_sett.heatr_ctrl);
reg_array[count] = reg_addr; /* Append configuration */
data_array[count] = data;
@@ -463,17 +234,17 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
/* Selecting heater T,P oversampling for the sensor */
if (desired_settings & (BME680_OST_SEL | BME680_OSP_SEL)) {
- rslt = boundary_check(&dev->tph_sett.os_temp, BME680_OS_NONE, BME680_OS_16X, dev);
+ rslt = boundaryCheck(&tph_sett.os_temp, BME680_OS_NONE, BME680_OS_16X);
reg_addr = BME680_CONF_T_P_MODE_ADDR;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(reg_addr, &data, 1, dev);
+ rslt = getRegs(reg_addr, &data, 1);
if (desired_settings & BME680_OST_SEL)
- data = BME680_SET_BITS(data, BME680_OST, dev->tph_sett.os_temp);
+ data = BME680_SET_BITS(data, BME680_OST, tph_sett.os_temp);
if (desired_settings & BME680_OSP_SEL)
- data = BME680_SET_BITS(data, BME680_OSP, dev->tph_sett.os_pres);
+ data = BME680_SET_BITS(data, BME680_OSP, tph_sett.os_pres);
reg_array[count] = reg_addr;
data_array[count] = data;
@@ -482,12 +253,12 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
/* Selecting humidity oversampling for the sensor */
if (desired_settings & BME680_OSH_SEL) {
- rslt = boundary_check(&dev->tph_sett.os_hum, BME680_OS_NONE, BME680_OS_16X, dev);
+ rslt = boundaryCheck(&tph_sett.os_hum, BME680_OS_NONE, BME680_OS_16X);
reg_addr = BME680_CONF_OS_H_ADDR;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(reg_addr, &data, 1, dev);
- data = BME680_SET_BITS_POS_0(data, BME680_OSH, dev->tph_sett.os_hum);
+ rslt = getRegs(reg_addr, &data, 1);
+ data = BME680_SET_BITS_POS_0(data, BME680_OSH, tph_sett.os_hum);
reg_array[count] = reg_addr; /* Append configuration */
data_array[count] = data;
@@ -496,24 +267,24 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
/* Selecting the runGas and NB conversion settings for the sensor */
if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) {
- rslt = boundary_check(&dev->gas_sett.run_gas, BME680_RUN_GAS_DISABLE,
- BME680_RUN_GAS_ENABLE, dev);
+ rslt = boundaryCheck(&gas_sett.run_gas, BME680_RUN_GAS_DISABLE,
+ BME680_RUN_GAS_ENABLE);
if (rslt == BME680_OK) {
/* Validate boundary conditions */
- rslt = boundary_check(&dev->gas_sett.nb_conv, BME680_NBCONV_MIN,
- BME680_NBCONV_MAX, dev);
+ rslt = boundaryCheck(&gas_sett.nb_conv, BME680_NBCONV_MIN,
+ BME680_NBCONV_MAX);
}
reg_addr = BME680_CONF_ODR_RUN_GAS_NBC_ADDR;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(reg_addr, &data, 1, dev);
+ rslt = getRegs(reg_addr, &data, 1);
if (desired_settings & BME680_RUN_GAS_SEL)
- data = BME680_SET_BITS(data, BME680_RUN_GAS, dev->gas_sett.run_gas);
+ data = BME680_SET_BITS(data, BME680_RUN_GAS, gas_sett.run_gas);
if (desired_settings & BME680_NBCONV_SEL)
- data = BME680_SET_BITS_POS_0(data, BME680_NBCONV, dev->gas_sett.nb_conv);
+ data = BME680_SET_BITS_POS_0(data, BME680_NBCONV, gas_sett.nb_conv);
reg_array[count] = reg_addr; /* Append configuration */
data_array[count] = data;
@@ -521,10 +292,10 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
}
if (rslt == BME680_OK)
- rslt = bme680_set_regs(reg_array, data_array, count, dev);
+ rslt = setRegs(reg_array, data_array, count);
/* Restore previous intended power mode */
- dev->power_mode = intended_power_mode;
+ power_mode = intended_power_mode;
}
return rslt;
@@ -534,7 +305,7 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev *
* @brief This API is used to get the oversampling, filter and T,P,H, gas selection
* settings in the sensor.
*/
-int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev *dev)
+int8_t BME680::getSensorSettings(uint16_t desired_settings)
{
int8_t rslt;
/* starting address of the register array for burst read*/
@@ -542,37 +313,37 @@ int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev *
uint8_t data_array[BME680_REG_BUFFER_LENGTH] = { 0 };
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(reg_addr, data_array, BME680_REG_BUFFER_LENGTH, dev);
+ rslt = getRegs(reg_addr, data_array, BME680_REG_BUFFER_LENGTH);
if (rslt == BME680_OK) {
if (desired_settings & BME680_GAS_MEAS_SEL)
- rslt = get_gas_config(dev);
+ rslt = getGasConfig();
/* get the T,P,H ,Filter,ODR settings here */
if (desired_settings & BME680_FILTER_SEL)
- dev->tph_sett.filter = BME680_GET_BITS(data_array[BME680_REG_FILTER_INDEX],
+ tph_sett.filter = BME680_GET_BITS(data_array[BME680_REG_FILTER_INDEX],
BME680_FILTER);
if (desired_settings & (BME680_OST_SEL | BME680_OSP_SEL)) {
- dev->tph_sett.os_temp = BME680_GET_BITS(data_array[BME680_REG_TEMP_INDEX], BME680_OST);
- dev->tph_sett.os_pres = BME680_GET_BITS(data_array[BME680_REG_PRES_INDEX], BME680_OSP);
+ tph_sett.os_temp = BME680_GET_BITS(data_array[BME680_REG_TEMP_INDEX], BME680_OST);
+ tph_sett.os_pres = BME680_GET_BITS(data_array[BME680_REG_PRES_INDEX], BME680_OSP);
}
if (desired_settings & BME680_OSH_SEL)
- dev->tph_sett.os_hum = BME680_GET_BITS_POS_0(data_array[BME680_REG_HUM_INDEX],
+ tph_sett.os_hum = BME680_GET_BITS_POS_0(data_array[BME680_REG_HUM_INDEX],
BME680_OSH);
/* get the gas related settings */
if (desired_settings & BME680_HCNTRL_SEL)
- dev->gas_sett.heatr_ctrl = BME680_GET_BITS_POS_0(data_array[BME680_REG_HCTRL_INDEX],
+ gas_sett.heatr_ctrl = BME680_GET_BITS_POS_0(data_array[BME680_REG_HCTRL_INDEX],
BME680_HCTRL);
if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) {
- dev->gas_sett.nb_conv = BME680_GET_BITS_POS_0(data_array[BME680_REG_NBCONV_INDEX],
+ gas_sett.nb_conv = BME680_GET_BITS_POS_0(data_array[BME680_REG_NBCONV_INDEX],
BME680_NBCONV);
- dev->gas_sett.run_gas = BME680_GET_BITS(data_array[BME680_REG_RUN_GAS_INDEX],
+ gas_sett.run_gas = BME680_GET_BITS(data_array[BME680_REG_RUN_GAS_INDEX],
BME680_RUN_GAS);
}
}
@@ -586,7 +357,7 @@ int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev *
/*!
* @brief This API is used to set the power mode of the sensor.
*/
-int8_t bme680_set_sensor_mode(struct bme680_dev *dev)
+int8_t BME680::setSensorMode()
{
int8_t rslt;
uint8_t tmp_pow_mode;
@@ -594,28 +365,28 @@ int8_t bme680_set_sensor_mode(struct bme680_dev *dev)
uint8_t reg_addr = BME680_CONF_T_P_MODE_ADDR;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
/* Call repeatedly until in sleep */
do {
- rslt = bme680_get_regs(BME680_CONF_T_P_MODE_ADDR, &tmp_pow_mode, 1, dev);
+ rslt = getRegs(BME680_CONF_T_P_MODE_ADDR, &tmp_pow_mode, 1);
if (rslt == BME680_OK) {
/* Put to sleep before changing mode */
pow_mode = (tmp_pow_mode & BME680_MODE_MSK);
if (pow_mode != BME680_SLEEP_MODE) {
tmp_pow_mode = tmp_pow_mode & (~BME680_MODE_MSK); /* Set to sleep */
- rslt = bme680_set_regs(&reg_addr, &tmp_pow_mode, 1, dev);
- dev->delay_ms(BME680_POLL_PERIOD_MS);
+ rslt = setRegs(&reg_addr, &tmp_pow_mode, 1);
+ delay_ms(BME680_POLL_PERIOD_MS);
}
}
} while (pow_mode != BME680_SLEEP_MODE);
/* Already in sleep */
- if (dev->power_mode != BME680_SLEEP_MODE) {
- tmp_pow_mode = (tmp_pow_mode & ~BME680_MODE_MSK) | (dev->power_mode & BME680_MODE_MSK);
+ if (power_mode != BME680_SLEEP_MODE) {
+ tmp_pow_mode = (tmp_pow_mode & ~BME680_MODE_MSK) | (power_mode & BME680_MODE_MSK);
if (rslt == BME680_OK)
- rslt = bme680_set_regs(&reg_addr, &tmp_pow_mode, 1, dev);
+ rslt = setRegs(&reg_addr, &tmp_pow_mode, 1);
}
}
@@ -625,17 +396,17 @@ int8_t bme680_set_sensor_mode(struct bme680_dev *dev)
/*!
* @brief This API is used to get the power mode of the sensor.
*/
-int8_t bme680_get_sensor_mode(struct bme680_dev *dev)
+int8_t BME680::getSensorMode()
{
int8_t rslt;
uint8_t mode;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(BME680_CONF_T_P_MODE_ADDR, &mode, 1, dev);
+ rslt = getRegs(BME680_CONF_T_P_MODE_ADDR, &mode, 1);
/* Masking the other register bit info*/
- dev->power_mode = mode & BME680_MODE_MSK;
+ power_mode = mode & BME680_MODE_MSK;
}
return rslt;
@@ -644,15 +415,15 @@ int8_t bme680_get_sensor_mode(struct bme680_dev *dev)
/*!
* @brief This API is used to set the profile duration of the sensor.
*/
-void bme680_set_profile_dur(uint16_t duration, struct bme680_dev *dev)
+void BME680::setProfileDur(uint16_t duration)
{
uint32_t tph_dur; /* Calculate in us */
uint32_t meas_cycles;
uint8_t os_to_meas_cycles[6] = {0, 1, 2, 4, 8, 16};
- meas_cycles = os_to_meas_cycles[dev->tph_sett.os_temp];
- meas_cycles += os_to_meas_cycles[dev->tph_sett.os_pres];
- meas_cycles += os_to_meas_cycles[dev->tph_sett.os_hum];
+ meas_cycles = os_to_meas_cycles[tph_sett.os_temp];
+ meas_cycles += os_to_meas_cycles[tph_sett.os_pres];
+ meas_cycles += os_to_meas_cycles[tph_sett.os_hum];
/* TPH measurement duration */
tph_dur = meas_cycles * UINT32_C(1963);
@@ -663,21 +434,21 @@ void bme680_set_profile_dur(uint16_t duration, struct bme680_dev *dev)
tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
/* The remaining time should be used for heating */
- dev->gas_sett.heatr_dur = duration - (uint16_t) tph_dur;
+ gas_sett.heatr_dur = duration - (uint16_t) tph_dur;
}
/*!
* @brief This API is used to get the profile duration of the sensor.
*/
-void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev)
+void BME680::getProfileDur(uint16_t *duration)
{
uint32_t tph_dur; /* Calculate in us */
uint32_t meas_cycles;
uint8_t os_to_meas_cycles[6] = {0, 1, 2, 4, 8, 16};
- meas_cycles = os_to_meas_cycles[dev->tph_sett.os_temp];
- meas_cycles += os_to_meas_cycles[dev->tph_sett.os_pres];
- meas_cycles += os_to_meas_cycles[dev->tph_sett.os_hum];
+ meas_cycles = os_to_meas_cycles[tph_sett.os_temp];
+ meas_cycles += os_to_meas_cycles[tph_sett.os_pres];
+ meas_cycles += os_to_meas_cycles[tph_sett.os_hum];
/* TPH measurement duration */
tph_dur = meas_cycles * UINT32_C(1963);
@@ -691,9 +462,9 @@ void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev)
*duration = (uint16_t) tph_dur;
/* Get the gas duration only when the run gas is enabled */
- if (dev->gas_sett.run_gas) {
+ if (gas_sett.run_gas) {
/* The remaining time should be used for heating */
- *duration += dev->gas_sett.heatr_dur;
+ *duration += gas_sett.heatr_dur;
}
}
@@ -702,20 +473,20 @@ void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev)
* from the sensor, compensates the data and store it in the bme680_data
* structure instance passed by the user.
*/
-int8_t bme680_get_sensor_data(struct bme680_field_data *data, struct bme680_dev *dev)
+int8_t BME680::getSensorData(struct bme680_field_data *data)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
/* Reading the sensor data in forced mode only */
- rslt = read_field_data(data, dev);
+ rslt = readFieldData(data);
if (rslt == BME680_OK) {
if (data->status & BME680_NEW_DATA_MSK)
- dev->new_fields = 1;
+ new_fields = 1;
else
- dev->new_fields = 0;
+ new_fields = 0;
}
}
@@ -725,77 +496,77 @@ int8_t bme680_get_sensor_data(struct bme680_field_data *data, struct bme680_dev
/*!
* @brief This internal API is used to read the calibrated data from the sensor.
*/
-static int8_t get_calib_data(struct bme680_dev *dev)
+int8_t BME680::getCalibData()
{
int8_t rslt;
uint8_t coeff_array[BME680_COEFF_SIZE] = { 0 };
uint8_t temp_var = 0; /* Temporary variable */
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(BME680_COEFF_ADDR1, coeff_array, BME680_COEFF_ADDR1_LEN, dev);
+ rslt = getRegs(BME680_COEFF_ADDR1, coeff_array, BME680_COEFF_ADDR1_LEN);
/* Append the second half in the same array */
if (rslt == BME680_OK)
- rslt = bme680_get_regs(BME680_COEFF_ADDR2, &coeff_array[BME680_COEFF_ADDR1_LEN]
- , BME680_COEFF_ADDR2_LEN, dev);
+ rslt = getRegs(BME680_COEFF_ADDR2, &coeff_array[BME680_COEFF_ADDR1_LEN]
+ , BME680_COEFF_ADDR2_LEN);
/* Temperature related coefficients */
- dev->calib.par_t1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T1_MSB_REG],
+ calib.par_t1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T1_MSB_REG],
coeff_array[BME680_T1_LSB_REG]));
- dev->calib.par_t2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T2_MSB_REG],
+ calib.par_t2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T2_MSB_REG],
coeff_array[BME680_T2_LSB_REG]));
- dev->calib.par_t3 = (int8_t) (coeff_array[BME680_T3_REG]);
+ calib.par_t3 = (int8_t) (coeff_array[BME680_T3_REG]);
/* Pressure related coefficients */
- dev->calib.par_p1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P1_MSB_REG],
+ calib.par_p1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P1_MSB_REG],
coeff_array[BME680_P1_LSB_REG]));
- dev->calib.par_p2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P2_MSB_REG],
+ calib.par_p2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P2_MSB_REG],
coeff_array[BME680_P2_LSB_REG]));
- dev->calib.par_p3 = (int8_t) coeff_array[BME680_P3_REG];
- dev->calib.par_p4 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P4_MSB_REG],
+ calib.par_p3 = (int8_t) coeff_array[BME680_P3_REG];
+ calib.par_p4 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P4_MSB_REG],
coeff_array[BME680_P4_LSB_REG]));
- dev->calib.par_p5 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P5_MSB_REG],
+ calib.par_p5 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P5_MSB_REG],
coeff_array[BME680_P5_LSB_REG]));
- dev->calib.par_p6 = (int8_t) (coeff_array[BME680_P6_REG]);
- dev->calib.par_p7 = (int8_t) (coeff_array[BME680_P7_REG]);
- dev->calib.par_p8 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P8_MSB_REG],
+ calib.par_p6 = (int8_t) (coeff_array[BME680_P6_REG]);
+ calib.par_p7 = (int8_t) (coeff_array[BME680_P7_REG]);
+ calib.par_p8 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P8_MSB_REG],
coeff_array[BME680_P8_LSB_REG]));
- dev->calib.par_p9 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P9_MSB_REG],
+ calib.par_p9 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P9_MSB_REG],
coeff_array[BME680_P9_LSB_REG]));
- dev->calib.par_p10 = (uint8_t) (coeff_array[BME680_P10_REG]);
+ calib.par_p10 = (uint8_t) (coeff_array[BME680_P10_REG]);
/* Humidity related coefficients */
- dev->calib.par_h1 = (uint16_t) (((uint16_t) coeff_array[BME680_H1_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
+ calib.par_h1 = (uint16_t) (((uint16_t) coeff_array[BME680_H1_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
| (coeff_array[BME680_H1_LSB_REG] & BME680_BIT_H1_DATA_MSK));
- dev->calib.par_h2 = (uint16_t) (((uint16_t) coeff_array[BME680_H2_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
+ calib.par_h2 = (uint16_t) (((uint16_t) coeff_array[BME680_H2_MSB_REG] << BME680_HUM_REG_SHIFT_VAL)
| ((coeff_array[BME680_H2_LSB_REG]) >> BME680_HUM_REG_SHIFT_VAL));
- dev->calib.par_h3 = (int8_t) coeff_array[BME680_H3_REG];
- dev->calib.par_h4 = (int8_t) coeff_array[BME680_H4_REG];
- dev->calib.par_h5 = (int8_t) coeff_array[BME680_H5_REG];
- dev->calib.par_h6 = (uint8_t) coeff_array[BME680_H6_REG];
- dev->calib.par_h7 = (int8_t) coeff_array[BME680_H7_REG];
+ calib.par_h3 = (int8_t) coeff_array[BME680_H3_REG];
+ calib.par_h4 = (int8_t) coeff_array[BME680_H4_REG];
+ calib.par_h5 = (int8_t) coeff_array[BME680_H5_REG];
+ calib.par_h6 = (uint8_t) coeff_array[BME680_H6_REG];
+ calib.par_h7 = (int8_t) coeff_array[BME680_H7_REG];
/* Gas heater related coefficients */
- dev->calib.par_gh1 = (int8_t) coeff_array[BME680_GH1_REG];
- dev->calib.par_gh2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_GH2_MSB_REG],
+ calib.par_gh1 = (int8_t) coeff_array[BME680_GH1_REG];
+ calib.par_gh2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_GH2_MSB_REG],
coeff_array[BME680_GH2_LSB_REG]));
- dev->calib.par_gh3 = (int8_t) coeff_array[BME680_GH3_REG];
+ calib.par_gh3 = (int8_t) coeff_array[BME680_GH3_REG];
/* Other coefficients */
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(BME680_ADDR_RES_HEAT_RANGE_ADDR, &temp_var, 1, dev);
+ rslt = getRegs(BME680_ADDR_RES_HEAT_RANGE_ADDR, &temp_var, 1);
- dev->calib.res_heat_range = ((temp_var & BME680_RHRANGE_MSK) / 16);
+ calib.res_heat_range = ((temp_var & BME680_RHRANGE_MSK) / 16);
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(BME680_ADDR_RES_HEAT_VAL_ADDR, &temp_var, 1, dev);
+ rslt = getRegs(BME680_ADDR_RES_HEAT_VAL_ADDR, &temp_var, 1);
- dev->calib.res_heat_val = (int8_t) temp_var;
+ calib.res_heat_val = (int8_t) temp_var;
if (rslt == BME680_OK)
- rslt = bme680_get_regs(BME680_ADDR_RANGE_SW_ERR_ADDR, &temp_var, 1, dev);
+ rslt = getRegs(BME680_ADDR_RANGE_SW_ERR_ADDR, &temp_var, 1);
}
}
- dev->calib.range_sw_err = ((int8_t) temp_var & (int8_t) BME680_RSERROR_MSK) / 16;
+ calib.range_sw_err = ((int8_t) temp_var & (int8_t) BME680_RSERROR_MSK) / 16;
}
return rslt;
@@ -804,28 +575,28 @@ static int8_t get_calib_data(struct bme680_dev *dev)
/*!
* @brief This internal API is used to set the gas configuration of the sensor.
*/
-static int8_t set_gas_config(struct bme680_dev *dev)
+int8_t BME680::setGasConfig()
{
int8_t rslt;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
uint8_t reg_addr[2] = {0};
uint8_t reg_data[2] = {0};
- if (dev->power_mode == BME680_FORCED_MODE) {
+ if (power_mode == BME680_FORCED_MODE) {
reg_addr[0] = BME680_RES_HEAT0_ADDR;
- reg_data[0] = calc_heater_res(dev->gas_sett.heatr_temp, dev);
+ reg_data[0] = calcHeaterRes(gas_sett.heatr_temp);
reg_addr[1] = BME680_GAS_WAIT0_ADDR;
- reg_data[1] = calc_heater_dur(dev->gas_sett.heatr_dur);
- dev->gas_sett.nb_conv = 0;
+ reg_data[1] = calcHeaterDur(gas_sett.heatr_dur);
+ gas_sett.nb_conv = 0;
} else {
rslt = BME680_W_DEFINE_PWR_MODE;
}
if (rslt == BME680_OK)
- rslt = bme680_set_regs(reg_addr, reg_data, 2, dev);
+ rslt = setRegs(reg_addr, reg_data, 2);
}
return rslt;
@@ -836,7 +607,7 @@ static int8_t set_gas_config(struct bme680_dev *dev)
* @note heatr_temp and heatr_dur values are currently register data
* and not the actual values set
*/
-static int8_t get_gas_config(struct bme680_dev *dev)
+int8_t BME680::getGasConfig()
{
int8_t rslt;
/* starting address of the register array for burst read*/
@@ -845,21 +616,21 @@ static int8_t get_gas_config(struct bme680_dev *dev)
uint8_t reg_data = 0;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- if (BME680_SPI_INTF == dev->intf) {
+ if (BME680_SPI_INTF == intf) {
/* Memory page switch the SPI address*/
- rslt = set_mem_page(reg_addr1, dev);
+ rslt = setMemPage(reg_addr1);
}
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(reg_addr1, &reg_data, 1, dev);
+ rslt = getRegs(reg_addr1, &reg_data, 1);
if (rslt == BME680_OK) {
- dev->gas_sett.heatr_temp = reg_data;
- rslt = bme680_get_regs(reg_addr2, &reg_data, 1, dev);
+ gas_sett.heatr_temp = reg_data;
+ rslt = getRegs(reg_addr2, &reg_data, 1);
if (rslt == BME680_OK) {
/* Heating duration register value */
- dev->gas_sett.heatr_dur = reg_data;
+ gas_sett.heatr_dur = reg_data;
}
}
}
@@ -873,19 +644,19 @@ static int8_t get_gas_config(struct bme680_dev *dev)
/*!
* @brief This internal API is used to calculate the temperature value.
*/
-static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
+int16_t BME680::calcTemperature(uint32_t temp_adc)
{
int64_t var1;
int64_t var2;
int64_t var3;
int16_t calc_temp;
- var1 = ((int32_t) temp_adc >> 3) - ((int32_t) dev->calib.par_t1 << 1);
- var2 = (var1 * (int32_t) dev->calib.par_t2) >> 11;
+ var1 = ((int32_t) temp_adc >> 3) - ((int32_t) calib.par_t1 << 1);
+ var2 = (var1 * (int32_t) calib.par_t2) >> 11;
var3 = ((var1 >> 1) * (var1 >> 1)) >> 12;
- var3 = ((var3) * ((int32_t) dev->calib.par_t3 << 4)) >> 14;
- dev->calib.t_fine = (int32_t) (var2 + var3);
- calc_temp = (int16_t) (((dev->calib.t_fine * 5) + 128) >> 8);
+ var3 = ((var3) * ((int32_t) calib.par_t3 << 4)) >> 14;
+ calib.t_fine = (int32_t) (var2 + var3);
+ calc_temp = (int16_t) (((calib.t_fine * 5) + 128) >> 8);
return calc_temp;
}
@@ -893,39 +664,39 @@ static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
/*!
* @brief This internal API is used to calculate the pressure value.
*/
-static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
+uint32_t BME680::calcPressure(uint32_t pres_adc)
{
int32_t var1;
int32_t var2;
int32_t var3;
int32_t pressure_comp;
- var1 = (((int32_t)dev->calib.t_fine) >> 1) - 64000;
+ var1 = (((int32_t)calib.t_fine) >> 1) - 64000;
var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
- (int32_t)dev->calib.par_p6) >> 2;
- var2 = var2 + ((var1 * (int32_t)dev->calib.par_p5) << 1);
- var2 = (var2 >> 2) + ((int32_t)dev->calib.par_p4 << 16);
+ (int32_t)calib.par_p6) >> 2;
+ var2 = var2 + ((var1 * (int32_t)calib.par_p5) << 1);
+ var2 = (var2 >> 2) + ((int32_t)calib.par_p4 << 16);
var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
- ((int32_t)dev->calib.par_p3 << 5)) >> 3) +
- (((int32_t)dev->calib.par_p2 * var1) >> 1);
+ ((int32_t)calib.par_p3 << 5)) >> 3) +
+ (((int32_t)calib.par_p2 * var1) >> 1);
var1 = var1 >> 18;
- var1 = ((32768 + var1) * (int32_t)dev->calib.par_p1) >> 15;
+ var1 = ((32768 + var1) * (int32_t)calib.par_p1) >> 15;
pressure_comp = 1048576 - pres_adc;
pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));
if (pressure_comp >= BME680_MAX_OVERFLOW_VAL)
pressure_comp = ((pressure_comp / var1) << 1);
else
pressure_comp = ((pressure_comp << 1) / var1);
- var1 = ((int32_t)dev->calib.par_p9 * (int32_t)(((pressure_comp >> 3) *
+ var1 = ((int32_t)calib.par_p9 * (int32_t)(((pressure_comp >> 3) *
(pressure_comp >> 3)) >> 13)) >> 12;
var2 = ((int32_t)(pressure_comp >> 2) *
- (int32_t)dev->calib.par_p8) >> 13;
+ (int32_t)calib.par_p8) >> 13;
var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *
(int32_t)(pressure_comp >> 8) *
- (int32_t)dev->calib.par_p10) >> 17;
+ (int32_t)calib.par_p10) >> 17;
pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +
- ((int32_t)dev->calib.par_p7 << 7)) >> 4);
+ ((int32_t)calib.par_p7 << 7)) >> 4);
return (uint32_t)pressure_comp;
@@ -934,7 +705,7 @@ static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
/*!
* @brief This internal API is used to calculate the humidity value.
*/
-static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
+uint32_t BME680::calcHumidity(uint16_t hum_adc)
{
int32_t var1;
int32_t var2;
@@ -945,16 +716,16 @@ static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
int32_t temp_scaled;
int32_t calc_hum;
- temp_scaled = (((int32_t) dev->calib.t_fine * 5) + 128) >> 8;
- var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) dev->calib.par_h1 * 16)))
- - (((temp_scaled * (int32_t) dev->calib.par_h3) / ((int32_t) 100)) >> 1);
- var2 = ((int32_t) dev->calib.par_h2
- * (((temp_scaled * (int32_t) dev->calib.par_h4) / ((int32_t) 100))
- + (((temp_scaled * ((temp_scaled * (int32_t) dev->calib.par_h5) / ((int32_t) 100))) >> 6)
+ temp_scaled = (((int32_t) calib.t_fine * 5) + 128) >> 8;
+ var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) calib.par_h1 * 16)))
+ - (((temp_scaled * (int32_t) calib.par_h3) / ((int32_t) 100)) >> 1);
+ var2 = ((int32_t) calib.par_h2
+ * (((temp_scaled * (int32_t) calib.par_h4) / ((int32_t) 100))
+ + (((temp_scaled * ((temp_scaled * (int32_t) calib.par_h5) / ((int32_t) 100))) >> 6)
/ ((int32_t) 100)) + (int32_t) (1 << 14))) >> 10;
var3 = var1 * var2;
- var4 = (int32_t) dev->calib.par_h6 << 7;
- var4 = ((var4) + ((temp_scaled * (int32_t) dev->calib.par_h7) / ((int32_t) 100))) >> 4;
+ var4 = (int32_t) calib.par_h6 << 7;
+ var4 = ((var4) + ((temp_scaled * (int32_t) calib.par_h7) / ((int32_t) 100))) >> 4;
var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
var6 = (var4 * var5) >> 1;
calc_hum = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12;
@@ -970,7 +741,7 @@ static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
/*!
* @brief This internal API is used to calculate the Gas Resistance value.
*/
-static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev)
+uint32_t BME680::calcGasResistance(uint16_t gas_res_adc, uint8_t gas_range)
{
int64_t var1;
uint64_t var2;
@@ -987,7 +758,7 @@ static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, con
UINT32_C(8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000),
UINT32_C(250000), UINT32_C(125000) };
- var1 = (int64_t) ((1340 + (5 * (int64_t) dev->calib.range_sw_err)) *
+ var1 = (int64_t) ((1340 + (5 * (int64_t) calib.range_sw_err)) *
((int64_t) lookupTable1[gas_range])) >> 16;
var2 = (((int64_t) ((int64_t) gas_res_adc << 15) - (int64_t) (16777216)) + var1);
var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) >> 9);
@@ -999,7 +770,7 @@ static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, con
/*!
* @brief This internal API is used to calculate the Heat Resistance value.
*/
-static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
+uint8_t BME680::calcHeaterRes(uint16_t temp)
{
uint8_t heatr_res;
int32_t var1;
@@ -1012,11 +783,11 @@ static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
if (temp > 400) /* Cap temperature */
temp = 400;
- var1 = (((int32_t) dev->amb_temp * dev->calib.par_gh3) / 1000) * 256;
- var2 = (dev->calib.par_gh1 + 784) * (((((dev->calib.par_gh2 + 154009) * temp * 5) / 100) + 3276800) / 10);
+ var1 = (((int32_t) amb_temp * calib.par_gh3) / 1000) * 256;
+ var2 = (calib.par_gh1 + 784) * (((((calib.par_gh2 + 154009) * temp * 5) / 100) + 3276800) / 10);
var3 = var1 + (var2 / 2);
- var4 = (var3 / (dev->calib.res_heat_range + 4));
- var5 = (131 * dev->calib.res_heat_val) + 65536;
+ var4 = (var3 / (calib.res_heat_range + 4));
+ var5 = (131 * calib.res_heat_val) + 65536;
heatr_res_x100 = (int32_t) (((var4 / var5) - 250) * 34);
heatr_res = (uint8_t) ((heatr_res_x100 + 50) / 100);
@@ -1030,26 +801,26 @@ static uint8_t calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
* @brief This internal API is used to calculate the
* temperature value in float format
*/
-static float calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
+float BME680::calcTemperature(uint32_t temp_adc)
{
float var1 = 0;
float var2 = 0;
float calc_temp = 0;
/* calculate var1 data */
- var1 = ((((float)temp_adc / 16384.0f) - ((float)dev->calib.par_t1 / 1024.0f))
- * ((float)dev->calib.par_t2));
+ var1 = ((((float)temp_adc / 16384.0f) - ((float)calib.par_t1 / 1024.0f))
+ * ((float)calib.par_t2));
/* calculate var2 data */
- var2 = (((((float)temp_adc / 131072.0f) - ((float)dev->calib.par_t1 / 8192.0f)) *
- (((float)temp_adc / 131072.0f) - ((float)dev->calib.par_t1 / 8192.0f))) *
- ((float)dev->calib.par_t3 * 16.0f));
+ var2 = (((((float)temp_adc / 131072.0f) - ((float)calib.par_t1 / 8192.0f)) *
+ (((float)temp_adc / 131072.0f) - ((float)calib.par_t1 / 8192.0f))) *
+ ((float)calib.par_t3 * 16.0f));
/* t_fine value*/
- dev->calib.t_fine = (var1 + var2);
+ calib.t_fine = (var1 + var2);
/* compensated temperature data*/
- calc_temp = ((dev->calib.t_fine) / 5120.0f);
+ calc_temp = ((calib.t_fine) / 5120.0f);
return calc_temp;
}
@@ -1058,30 +829,30 @@ static float calc_temperature(uint32_t temp_adc, struct bme680_dev *dev)
* @brief This internal API is used to calculate the
* pressure value in float format
*/
-static float calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
+float BME680::calcPressure(uint32_t pres_adc)
{
float var1 = 0;
float var2 = 0;
float var3 = 0;
float calc_pres = 0;
- var1 = (((float)dev->calib.t_fine / 2.0f) - 64000.0f);
- var2 = var1 * var1 * (((float)dev->calib.par_p6) / (131072.0f));
- var2 = var2 + (var1 * ((float)dev->calib.par_p5) * 2.0f);
- var2 = (var2 / 4.0f) + (((float)dev->calib.par_p4) * 65536.0f);
- var1 = (((((float)dev->calib.par_p3 * var1 * var1) / 16384.0f)
- + ((float)dev->calib.par_p2 * var1)) / 524288.0f);
- var1 = ((1.0f + (var1 / 32768.0f)) * ((float)dev->calib.par_p1));
+ var1 = (((float)calib.t_fine / 2.0f) - 64000.0f);
+ var2 = var1 * var1 * (((float)calib.par_p6) / (131072.0f));
+ var2 = var2 + (var1 * ((float)calib.par_p5) * 2.0f);
+ var2 = (var2 / 4.0f) + (((float)calib.par_p4) * 65536.0f);
+ var1 = (((((float)calib.par_p3 * var1 * var1) / 16384.0f)
+ + ((float)calib.par_p2 * var1)) / 524288.0f);
+ var1 = ((1.0f + (var1 / 32768.0f)) * ((float)calib.par_p1));
calc_pres = (1048576.0f - ((float)pres_adc));
/* Avoid exception caused by division by zero */
if ((int)var1 != 0) {
calc_pres = (((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1);
- var1 = (((float)dev->calib.par_p9) * calc_pres * calc_pres) / 2147483648.0f;
- var2 = calc_pres * (((float)dev->calib.par_p8) / 32768.0f);
+ var1 = (((float)calib.par_p9) * calc_pres * calc_pres) / 2147483648.0f;
+ var2 = calc_pres * (((float)calib.par_p8) / 32768.0f);
var3 = ((calc_pres / 256.0f) * (calc_pres / 256.0f) * (calc_pres / 256.0f)
- * (dev->calib.par_p10 / 131072.0f));
- calc_pres = (calc_pres + (var1 + var2 + var3 + ((float)dev->calib.par_p7 * 128.0f)) / 16.0f);
+ * (calib.par_p10 / 131072.0f));
+ calc_pres = (calc_pres + (var1 + var2 + var3 + ((float)calib.par_p7 * 128.0f)) / 16.0f);
} else {
calc_pres = 0;
}
@@ -1093,7 +864,7 @@ static float calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev)
* @brief This internal API is used to calculate the
* humidity value in float format
*/
-static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
+float BME680::calcHumidity(uint16_t hum_adc)
{
float calc_hum = 0;
float var1 = 0;
@@ -1103,17 +874,17 @@ static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
float temp_comp;
/* compensated temperature data*/
- temp_comp = ((dev->calib.t_fine) / 5120.0f);
+ temp_comp = ((calib.t_fine) / 5120.0f);
- var1 = (float)((float)hum_adc) - (((float)dev->calib.par_h1 * 16.0f) + (((float)dev->calib.par_h3 / 2.0f)
+ var1 = (float)((float)hum_adc) - (((float)calib.par_h1 * 16.0f) + (((float)calib.par_h3 / 2.0f)
* temp_comp));
- var2 = var1 * ((float)(((float) dev->calib.par_h2 / 262144.0f) * (1.0f + (((float)dev->calib.par_h4 / 16384.0f)
- * temp_comp) + (((float)dev->calib.par_h5 / 1048576.0f) * temp_comp * temp_comp))));
+ var2 = var1 * ((float)(((float) calib.par_h2 / 262144.0f) * (1.0f + (((float)calib.par_h4 / 16384.0f)
+ * temp_comp) + (((float)calib.par_h5 / 1048576.0f) * temp_comp * temp_comp))));
- var3 = (float) dev->calib.par_h6 / 16384.0f;
+ var3 = (float) calib.par_h6 / 16384.0f;
- var4 = (float) dev->calib.par_h7 / 2097152.0f;
+ var4 = (float) calib.par_h7 / 2097152.0f;
calc_hum = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2);
@@ -1129,7 +900,7 @@ static float calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev)
* @brief This internal API is used to calculate the
* gas resistance value in float format
*/
-static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const struct bme680_dev *dev)
+float BME680::calcGasResistance(uint16_t gas_res_adc, uint8_t gas_range)
{
float calc_gas_res;
float var1 = 0;
@@ -1143,7 +914,7 @@ static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const
0.0, 0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8,
-0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
- var1 = (1340.0f + (5.0f * dev->calib.range_sw_err));
+ var1 = (1340.0f + (5.0f * calib.range_sw_err));
var2 = (var1) * (1.0f + lookup_k1_range[gas_range]/100.0f);
var3 = 1.0f + (lookup_k2_range[gas_range]/100.0f);
@@ -1157,7 +928,7 @@ static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, const
* @brief This internal API is used to calculate the
* heater resistance value in float format
*/
-static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
+float BME680::calcHeaterRes(uint16_t temp)
{
float var1 = 0;
float var2 = 0;
@@ -1169,13 +940,13 @@ static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
if (temp > 400) /* Cap temperature */
temp = 400;
- var1 = (((float)dev->calib.par_gh1 / (16.0f)) + 49.0f);
- var2 = ((((float)dev->calib.par_gh2 / (32768.0f)) * (0.0005f)) + 0.00235f);
- var3 = ((float)dev->calib.par_gh3 / (1024.0f));
+ var1 = (((float)calib.par_gh1 / (16.0f)) + 49.0f);
+ var2 = ((((float)calib.par_gh2 / (32768.0f)) * (0.0005f)) + 0.00235f);
+ var3 = ((float)calib.par_gh3 / (1024.0f));
var4 = (var1 * (1.0f + (var2 * (float)temp)));
- var5 = (var4 + (var3 * (float)dev->amb_temp));
- res_heat = (uint8_t)(3.4f * ((var5 * (4 / (4 + (float)dev->calib.res_heat_range)) *
- (1/(1 + ((float) dev->calib.res_heat_val * 0.002f)))) - 25));
+ var5 = (var4 + (var3 * (float)amb_temp));
+ res_heat = (uint8_t)(3.4f * ((var5 * (4 / (4 + (float)calib.res_heat_range)) *
+ (1/(1 + ((float) calib.res_heat_val * 0.002f)))) - 25));
return res_heat;
}
@@ -1185,7 +956,7 @@ static float calc_heater_res(uint16_t temp, const struct bme680_dev *dev)
/*!
* @brief This internal API is used to calculate the Heat duration value.
*/
-static uint8_t calc_heater_dur(uint16_t dur)
+uint8_t BME680::calcHeaterDur(uint16_t dur)
{
uint8_t factor = 0;
uint8_t durval;
@@ -1206,7 +977,7 @@ static uint8_t calc_heater_dur(uint16_t dur)
/*!
* @brief This internal API is used to calculate the field data of sensor.
*/
-static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev *dev)
+int8_t BME680::readFieldData(struct bme680_field_data *data)
{
int8_t rslt;
uint8_t buff[BME680_FIELD_LENGTH] = { 0 };
@@ -1218,11 +989,10 @@ static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev
uint8_t tries = 10;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
do {
if (rslt == BME680_OK) {
- rslt = bme680_get_regs(((uint8_t) (BME680_FIELD0_ADDR)), buff, (uint16_t) BME680_FIELD_LENGTH,
- dev);
+ rslt = getRegs(((uint8_t) (BME680_FIELD0_ADDR)), buff, (uint16_t) BME680_FIELD_LENGTH);
data->status = buff[0] & BME680_NEW_DATA_MSK;
data->gas_index = buff[0] & BME680_GAS_INDEX_MSK;
@@ -1241,14 +1011,14 @@ static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev
data->status |= buff[14] & BME680_HEAT_STAB_MSK;
if (data->status & BME680_NEW_DATA_MSK) {
- data->temperature = calc_temperature(adc_temp, dev);
- data->pressure = calc_pressure(adc_pres, dev);
- data->humidity = calc_humidity(adc_hum, dev);
- data->gas_resistance = calc_gas_resistance(adc_gas_res, gas_range, dev);
+ data->temperature = calcTemperature(adc_temp);
+ data->pressure = calcPressure(adc_pres);
+ data->humidity = calcHumidity(adc_hum);
+ data->gas_resistance = calcGasResistance(adc_gas_res, gas_range);
break;
}
/* Delay to poll the data */
- dev->delay_ms(BME680_POLL_PERIOD_MS);
+ delay_ms(BME680_POLL_PERIOD_MS);
}
tries--;
} while (tries);
@@ -1262,34 +1032,34 @@ static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev
/*!
* @brief This internal API is used to set the memory page based on register address.
*/
-static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev)
+int8_t BME680::setMemPage(uint8_t reg_addr)
{
int8_t rslt;
uint8_t reg;
uint8_t mem_page;
/* Check for null pointers in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
if (reg_addr > 0x7f)
mem_page = BME680_MEM_PAGE1;
else
mem_page = BME680_MEM_PAGE0;
- if (mem_page != dev->mem_page) {
- dev->mem_page = mem_page;
+ if (mem_page != mem_page) {
+ mem_page = mem_page;
- dev->com_rslt = dev->read(dev->dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
- if (dev->com_rslt != 0)
+ com_rslt = read(dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
+ if (com_rslt != 0)
rslt = BME680_E_COM_FAIL;
if (rslt == BME680_OK) {
reg = reg & (~BME680_MEM_PAGE_MSK);
- reg = reg | (dev->mem_page & BME680_MEM_PAGE_MSK);
+ reg = reg | (mem_page & BME680_MEM_PAGE_MSK);
- dev->com_rslt = dev->write(dev->dev_id, BME680_MEM_PAGE_ADDR & BME680_SPI_WR_MSK,
+ com_rslt = write(dev_id, BME680_MEM_PAGE_ADDR & BME680_SPI_WR_MSK,
&reg, 1);
- if (dev->com_rslt != 0)
+ if (com_rslt != 0)
rslt = BME680_E_COM_FAIL;
}
}
@@ -1301,19 +1071,19 @@ static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev)
/*!
* @brief This internal API is used to get the memory page based on register address.
*/
-static int8_t get_mem_page(struct bme680_dev *dev)
+int8_t BME680::getMemPage()
{
int8_t rslt;
uint8_t reg;
/* Check for null pointer in the device structure*/
- rslt = null_ptr_check(dev);
+ rslt = nullPtrCheck();
if (rslt == BME680_OK) {
- dev->com_rslt = dev->read(dev->dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
- if (dev->com_rslt != 0)
+ com_rslt = read(dev_id, BME680_MEM_PAGE_ADDR | BME680_SPI_RD_MSK, &reg, 1);
+ if (com_rslt != 0)
rslt = BME680_E_COM_FAIL;
else
- dev->mem_page = reg & BME680_MEM_PAGE_MSK;
+ mem_page = reg & BME680_MEM_PAGE_MSK;
}
return rslt;
@@ -1323,7 +1093,7 @@ static int8_t get_mem_page(struct bme680_dev *dev)
* @brief This internal API is used to validate the boundary
* conditions.
*/
-static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bme680_dev *dev)
+int8_t BME680::boundaryCheck(uint8_t *value, uint8_t min, uint8_t max)
{
int8_t rslt = BME680_OK;
@@ -1332,13 +1102,13 @@ static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bm
if (*value < min) {
/* Auto correct the invalid value to minimum value */
*value = min;
- dev->info_msg |= BME680_I_MIN_CORRECTION;
+ info_msg |= BME680_I_MIN_CORRECTION;
}
/* Check if value is above maximum value */
if (*value > max) {
/* Auto correct the invalid value to maximum value */
*value = max;
- dev->info_msg |= BME680_I_MAX_CORRECTION;
+ info_msg |= BME680_I_MAX_CORRECTION;
}
} else {
rslt = BME680_E_NULL_PTR;
@@ -1351,11 +1121,11 @@ static int8_t boundary_check(uint8_t *value, uint8_t min, uint8_t max, struct bm
* @brief This internal API is used to validate the device structure pointer for
* null conditions.
*/
-static int8_t null_ptr_check(const struct bme680_dev *dev)
+int8_t BME680::nullPtrCheck()
{
int8_t rslt;
- if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL)) {
+ if ((read == NULL) || (write == NULL) || (delay_ms == NULL)) {
/* Device structure pointer is not valid */
rslt = BME680_E_NULL_PTR;
} else {
@@ -1365,3 +1135,5 @@ static int8_t null_ptr_check(const struct bme680_dev *dev)
return rslt;
}
+
+BME680 bme680(BME680_I2C_ADDR_SECONDARY); \ No newline at end of file