ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
bme280_sensor.c
Go to the documentation of this file.
1#include "bme280_sensor.h"
2
3#include "../../proto/sensor.h"
4// #include "../../user_config.h"
5
6#include <bme280_common.h>
7
13static uint32_t period = 0;
14
20static struct bme280_dev dev;
21
27static struct bme280_settings settings;
28
30 int8_t rslt;
31
32 /* Interface selection is to be updated as parameter
33 * For I2C : BME280_I2C_INTF
34 * For SPI : BME280_SPI_INTF
35 */
36 rslt = bme280_interface_selection(&dev, BME280_I2C_INTF);
37 if (rslt != BME280_OK) {
38 return rslt;
39 }
40
41 rslt = bme280_init(&dev);
42 if (rslt != BME280_OK) {
43 return rslt;
44 }
45
46 /* Always read the current settings before writing, especially when all the
47 * configuration is not modified */
48 rslt = bme280_get_sensor_settings(&settings, &dev);
49 if (rslt != BME280_OK) {
50 return rslt;
51 }
52
53 /* Configuring the over-sampling rate, filter coefficient and standby time */
54 /* Overwrite the desired settings */
55 settings.filter = BME280_FILTER_COEFF_OFF;
56
57 /* Over-sampling rate for humidity, temperature and pressure */
58 settings.osr_h = BME280_OVERSAMPLING_1X;
59 settings.osr_p = BME280_OVERSAMPLING_1X;
60 settings.osr_t = BME280_OVERSAMPLING_1X;
61
62 /* Setting the standby time */
63 settings.standby_time = BME280_STANDBY_TIME_0_5_MS;
64
65 rslt = bme280_set_sensor_settings(BME280_SEL_ALL_SETTINGS, &settings, &dev);
66 if (rslt != BME280_OK) {
67 return rslt;
68 }
69
70 /* Always set the power mode after setting the configuration */
71 rslt = bme280_set_sensor_mode(BME280_POWERMODE_FORCED, &dev);
72 if (rslt != BME280_OK) {
73 return rslt;
74 }
75
76 /* Calculate measurement time in microseconds */
77 rslt = bme280_cal_meas_delay(&period, &settings);
78 if (rslt != BME280_OK) {
79 return rslt;
80 }
81
82 return rslt;
83}
84
86 int8_t rslt = BME280_E_NULL_PTR;
87 uint8_t status_reg;
88
89 // trigger measurement
90 rslt = bme280_set_sensor_mode(BME280_POWERMODE_FORCED, &dev);
91 if (rslt != BME280_OK) {
92 return rslt;
93 }
94
95 // check the status
96 rslt = bme280_get_regs(BME280_REG_STATUS, &status_reg, 1, &dev);
97 if (rslt != BME280_OK) {
98 return rslt;
99 }
100
101 if (status_reg & BME280_STATUS_MEAS_DONE) {
102 /* Measurement time delay given to read sample */
103 dev.delay_us(period, dev.intf_ptr);
104
105 /* Read compensated data */
106 rslt = bme280_get_sensor_data(BME280_ALL, data, &dev);
107 if (rslt != BME280_OK) {
108 return rslt;
109 }
110 }
111
112 // adjust based on defines
113 // #ifndef BME280_DOUBLE_ENABLE
115 // data->temperature = data->temperature / 100;
116 // data->humidity = data->humidity / 1000;
117 // */
118 // data->temperature = data->temperature;
119 // data->humidity = data->humidity;
120 // #endif
121 //
122 // #ifdef BME280_64BIT_ENABLE
123 // data->pressure = data->pressure / 100;
124 // #endif
125
126 return rslt;
127}
128
129uint8_t BME280MeasureTemperature(uint8_t* data, Metadata meta, uint32_t idx) {
130 (void)idx;
131
132 // read sensor
133 BME280Data sens_data;
134 BME280Status status = BME280MeasureAll(&sens_data);
135 if (status != BME280_STATUS_OK) {
136 return -1;
137 }
138
139 SensorStatus sen_status = SENSOR_OK;
140 size_t data_len = 0;
141
142 // temperature
143 sen_status = EncodeDoubleMeasurement(meta, sens_data.temperature,
144 SensorType_BME280_TEMP, data, &data_len);
145 if (sen_status != SENSOR_OK) {
146 return -1;
147 }
148
149 return data_len;
150}
151
152uint8_t BME280MeasurePressure(uint8_t* data, Metadata meta, uint32_t idx) {
153 (void)idx;
154
155 // read sensor
156 BME280Data sens_data;
157 BME280Status status = BME280MeasureAll(&sens_data);
158 if (status != BME280_STATUS_OK) {
159 return -1;
160 }
161
162 SensorStatus sen_status = SENSOR_OK;
163 size_t data_len = 0;
164
165 // pressure
166 sen_status = EncodeDoubleMeasurement(
167 meta, sens_data.pressure, SensorType_BME280_PRESSURE, data, &data_len);
168 if (sen_status != SENSOR_OK) {
169 return -1;
170 }
171
172 return data_len;
173}
174
175uint8_t BME280MeasureHumidity(uint8_t* data, Metadata meta, uint32_t idx) {
176 (void)idx;
177
178 // read sensor
179 BME280Data sens_data;
180 BME280Status status = BME280MeasureAll(&sens_data);
181 if (status != BME280_STATUS_OK) {
182 return -1;
183 }
184
185 SensorStatus sen_status = SENSOR_OK;
186 size_t data_len = 0;
187
188 // humidity
189 sen_status = EncodeDoubleMeasurement(
190 meta, sens_data.humidity, SensorType_BME280_HUMIDITY, data, &data_len);
191 if (sen_status != SENSOR_OK) {
192 return -1;
193 }
194
195 return data_len;
196}
BME280Status BME280Init(void)
BME280Status BME280MeasureAll(BME280Data *data)
Measures temperature, pressure, and humidity.
uint8_t BME280MeasureHumidity(uint8_t *data, Metadata meta, uint32_t idx)
uint8_t BME280MeasureTemperature(uint8_t *data, Metadata meta, uint32_t idx)
BME280 sensor library function.
uint8_t BME280MeasurePressure(uint8_t *data, Metadata meta, uint32_t idx)
Sensor interface for the BME280 sensor.
BME280Status
BME280 error codes.
@ BME280_STATUS_OK
struct bme280_data BME280Data
Struct to store data from sensor.
const size_t data_len
const char data[]
SensorStatus EncodeDoubleMeasurement(Metadata meta, double value, SensorType type, uint8_t *buffer, size_t *size)
Encodes a double sensor measurement into a buffer.
Definition sensor.c:244
enum _SensorStatus SensorStatus
@ SENSOR_OK
Definition sensor.h:41