mirror of
https://github.com/boschsensortec/BME280_SensorAPI.git
synced 2024-10-05 20:47:48 -04:00
303 lines
8.4 KiB
C
303 lines
8.4 KiB
C
/*
|
|
* Copyright (C) 2020 Bosch Sensortec GmbH
|
|
*
|
|
* The license is available at root folder
|
|
*
|
|
*/
|
|
|
|
/*!
|
|
* @ingroup bme280GroupExample
|
|
* @defgroup bme280GroupExample linux_userspace
|
|
* @brief Linux userspace test code, simple and mose code directly from the doco.
|
|
* compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280
|
|
* tested: Raspberry Pi.
|
|
* Use like: ./bme280 /dev/i2c-0
|
|
* \include linux_userspace.c
|
|
*/
|
|
|
|
#ifdef __KERNEL__
|
|
#include <linux/i2c-dev.h>
|
|
#include <sys/ioctl.h>
|
|
#endif
|
|
|
|
/******************************************************************************/
|
|
/*! System header files */
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <fcntl.h>
|
|
|
|
/******************************************************************************/
|
|
/*! Own header files */
|
|
#include "bme280.h"
|
|
|
|
/*****************************************************************************/
|
|
/*! Global variables */
|
|
int fd;
|
|
|
|
/****************************************************************************/
|
|
/*! Functions */
|
|
|
|
/*!
|
|
* @brief Function that creates a mandatory delay required in some of the APIs.
|
|
*
|
|
* @param[in] period : The required wait time in microseconds.
|
|
* @return void.
|
|
*
|
|
*/
|
|
void user_delay_ms(uint32_t period);
|
|
|
|
/*!
|
|
* @brief Function for print the temperature, humidity and pressure data.
|
|
*
|
|
* @param[out] comp_data : Structure instance of bme280_data
|
|
*
|
|
* @note Sensor data whose can be read
|
|
*
|
|
* sens_list
|
|
* --------------
|
|
* Pressure
|
|
* Temperature
|
|
* Humidity
|
|
*
|
|
*/
|
|
void print_sensor_data(struct bme280_data *comp_data);
|
|
|
|
/*!
|
|
* @brief Function for reading the sensor's registers through I2C bus.
|
|
*
|
|
* @param[in] id : Sensor I2C address.
|
|
* @param[in] reg_addr : Register address.
|
|
* @param[out] data : Pointer to the data buffer to store the read data.
|
|
* @param[in] len : No of bytes to read.
|
|
*
|
|
* @return Status of execution
|
|
*
|
|
* @retval 0 -> Success
|
|
* @retval > 0 -> Failure Info
|
|
*
|
|
*/
|
|
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
|
|
|
|
/*!
|
|
* @brief Function for writing the sensor's registers through I2C bus.
|
|
*
|
|
* @param[in] id : Sensor I2C address.
|
|
* @param[in] reg_addr : Register address.
|
|
* @param[in] data : Pointer to the data buffer whose value is to be written.
|
|
* @param[in] len : No of bytes to write.
|
|
*
|
|
* @return Status of execution
|
|
*
|
|
* @retval BME280_OK -> Success
|
|
* @retval BME280_E_COMM_FAIL -> Communication failure.
|
|
*
|
|
*/
|
|
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
|
|
|
|
/*!
|
|
* @brief Function reads temperature, humidity and pressure data in forced mode.
|
|
*
|
|
* @param[in] dev : Structure instance of bme280_dev.
|
|
*
|
|
* @return Result of API execution status
|
|
*
|
|
* @retval BME280_OK - Success.
|
|
* @retval BME280_E_NULL_PTR - Error: Null pointer error
|
|
* @retval BME280_E_COMM_FAIL - Error: Communication fail error
|
|
* @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed
|
|
*
|
|
*/
|
|
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
|
|
|
|
/*!
|
|
* @brief This function starts execution of the program.
|
|
*/
|
|
int main(int argc, char* argv[])
|
|
{
|
|
struct bme280_dev dev;
|
|
|
|
/* Variable to define the result */
|
|
int8_t rslt = BME280_OK;
|
|
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Missing argument for i2c bus.\n");
|
|
exit(1);
|
|
}
|
|
|
|
/* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */
|
|
dev.dev_id = BME280_I2C_ADDR_PRIM;
|
|
|
|
/* dev.dev_id = BME280_I2C_ADDR_SEC; */
|
|
dev.intf = BME280_I2C_INTF;
|
|
dev.read = user_i2c_read;
|
|
dev.write = user_i2c_write;
|
|
dev.delay_ms = user_delay_ms;
|
|
|
|
if ((fd = open(argv[1], O_RDWR)) < 0)
|
|
{
|
|
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
|
|
exit(1);
|
|
}
|
|
|
|
#ifdef __KERNEL__
|
|
if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
|
|
{
|
|
fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
|
|
exit(1);
|
|
}
|
|
#endif
|
|
|
|
/* Initialize the bme280 */
|
|
rslt = bme280_init(&dev);
|
|
if (rslt != BME280_OK)
|
|
{
|
|
fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
|
|
exit(1);
|
|
}
|
|
|
|
rslt = stream_sensor_data_forced_mode(&dev);
|
|
if (rslt != BME280_OK)
|
|
{
|
|
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
|
|
exit(1);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*!
|
|
* @brief This function reading the sensor's registers through I2C bus.
|
|
*/
|
|
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
|
|
{
|
|
write(fd, ®_addr, 1);
|
|
read(fd, data, len);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*!
|
|
* @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
|
|
* APIs
|
|
*/
|
|
void user_delay_ms(uint32_t period)
|
|
{
|
|
/* Milliseconds convert to microseconds */
|
|
usleep(period * 1000);
|
|
}
|
|
|
|
/*!
|
|
* @brief This function for writing the sensor's registers through I2C bus.
|
|
*/
|
|
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
|
|
{
|
|
int8_t *buf;
|
|
|
|
buf = malloc(len + 1);
|
|
buf[0] = reg_addr;
|
|
memcpy(buf + 1, data, len);
|
|
if (write(fd, buf, len + 1) < len)
|
|
{
|
|
return BME280_E_COMM_FAIL;
|
|
}
|
|
|
|
free(buf);
|
|
|
|
return BME280_OK;
|
|
}
|
|
|
|
/*!
|
|
* @brief This API used to print the sensor temperature, pressure and humidity data.
|
|
*/
|
|
void print_sensor_data(struct bme280_data *comp_data)
|
|
{
|
|
float temp, press, hum;
|
|
|
|
#ifdef BME280_FLOAT_ENABLE
|
|
temp = comp_data->temperature;
|
|
press = 0.01 * comp_data->pressure;
|
|
hum = comp_data->humidity;
|
|
#else
|
|
#ifdef BME280_64BIT_ENABLE
|
|
temp = 0.01f * comp_data->temperature;
|
|
press = 0.0001f * comp_data->pressure;
|
|
hum = 1.0f / 1024.0f * comp_data->humidity;
|
|
#else
|
|
temp = 0.01f * comp_data->temperature;
|
|
press = 0.01f * comp_data->pressure;
|
|
hum = 1.0f / 1024.0f * comp_data->humidity;
|
|
#endif
|
|
#endif
|
|
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
|
|
}
|
|
|
|
/*!
|
|
* @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
|
|
*/
|
|
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
|
|
{
|
|
/* Variable to define the result */
|
|
int8_t rslt = BME280_OK;
|
|
|
|
/* Variable to define the selecting sensors */
|
|
uint8_t settings_sel = 0;
|
|
|
|
/* Variable to store minimum wait time between consecutive measurement in force mode */
|
|
uint32_t req_delay;
|
|
|
|
/* Structure to get the pressure, temperature and humidity values */
|
|
struct bme280_data comp_data;
|
|
|
|
/* Recommended mode of operation: Indoor navigation */
|
|
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
|
|
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
|
|
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
|
|
dev->settings.filter = BME280_FILTER_COEFF_16;
|
|
|
|
settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
|
|
|
|
/* Set the sensor settings */
|
|
rslt = bme280_set_sensor_settings(settings_sel, dev);
|
|
if (rslt != BME280_OK)
|
|
{
|
|
fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
|
|
|
|
return rslt;
|
|
}
|
|
|
|
printf("Temperature, Pressure, Humidity\n");
|
|
|
|
/*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled
|
|
* and the oversampling configuration. */
|
|
req_delay = bme280_cal_meas_delay(&dev->settings);
|
|
|
|
/* Continuously stream sensor data */
|
|
while (1)
|
|
{
|
|
/* Set the sensor to forced mode */
|
|
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
|
|
if (rslt != BME280_OK)
|
|
{
|
|
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
|
|
break;
|
|
}
|
|
|
|
/* Wait for the measurement to complete and print data */
|
|
dev->delay_ms(req_delay);
|
|
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
|
|
if (rslt != BME280_OK)
|
|
{
|
|
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
|
|
break;
|
|
}
|
|
|
|
print_sensor_data(&comp_data);
|
|
}
|
|
|
|
return rslt;
|
|
}
|