-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsec_bme68x.c
341 lines (300 loc) · 9.55 KB
/
bsec_bme68x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**********************************************************************************************************************/
/* header files */
/**********************************************************************************************************************/
#define _POSIX_C_SOURCE 199309L
#include "bsec_integration.h"
#include <fcntl.h>
#include <inttypes.h>
#include <linux/i2c-dev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
/**********************************************************************************************************************/
/* globals */
/**********************************************************************************************************************/
int g_i2c_fd;
int i2c_address = 0x77;
char *filename_state = "bsec_iaq.state";
char *filename_config = "bsec_iaq.config";
uint32_t overflowCounter;
uint32_t lastTimeMS;
/**********************************************************************************************************************/
/* functions */
/**********************************************************************************************************************/
// open the Linux device
void i2cOpen()
{
g_i2c_fd = open("/dev/i2c-1", O_RDWR);
if (g_i2c_fd < 0)
{
perror("i2cOpen");
exit(1);
}
}
// close the Linux device
void i2cClose()
{
close(g_i2c_fd);
}
// set the I2C slave address for all subsequent I2C device transfers
void i2cSetAddress(int address)
{
if (ioctl(g_i2c_fd, I2C_SLAVE, address) < 0)
{
perror("i2cSetAddress");
exit(1);
}
}
/*!
* @brief Write operation in either Wire or SPI
*
* param[in] reg_addr register address
* param[in] reg_data_ptr pointer to the data to be written
* param[in] data_len number of bytes to be written
* param[in] intf_ptr interface pointer
*
* @return result of the bus communication function
*/
int8_t bus_write(uint8_t reg_addr, const uint8_t *reg_data_ptr, uint32_t data_len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
uint8_t reg[16];
reg[0] = reg_addr;
int i;
for (i = 1; i < data_len + 1; i++)
reg[i] = reg_data_ptr[i - 1];
if (write(g_i2c_fd, reg, data_len + 1) != data_len + 1)
{
perror("user_i2c_write");
rslt = 1;
exit(1);
}
return rslt;
}
/*!
* @brief Read operation in either Wire or SPI
*
* param[in] reg_addr register address
* param[out] reg_data_ptr pointer to the memory to be used to store the read data
* param[in] data_len number of bytes to be read
* param[in] intf_ptr interface pointer
*
* @return result of the bus communication function
*/
int8_t bus_read(uint8_t reg_addr, uint8_t *reg_data_ptr, uint32_t data_len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
uint8_t reg[1];
reg[0] = reg_addr;
if (write(g_i2c_fd, reg, 1) != 1)
{
perror("user_i2c_read_reg");
rslt = 1;
}
if (read(g_i2c_fd, reg_data_ptr, data_len) != data_len)
{
perror("user_i2c_read_data");
rslt = 1;
}
return rslt;
}
/*!
* @brief System specific implementation of sleep function
*
* @param[in] t_us Time in microseconds
* @param[in] intf_ptr Pointer to the interface descriptor
*
* @return none
*/
void sleep_n(uint32_t t_us, void *intf_ptr)
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = t_us * 1000;
nanosleep(&ts, NULL);
}
/*!
* @brief Capture the system time in microseconds
*
* @return system_current_time current system timestamp in microseconds
*/
int64_t get_timestamp_us()
{
struct timeval tp;
gettimeofday(&tp, NULL);
int64_t timeMs = tp.tv_sec * 1000000 + tp.tv_usec;
if (lastTimeMS > timeMs) /* An overflow occurred */
{
overflowCounter++;
}
lastTimeMS = timeMs;
return timeMs + (overflowCounter * INT64_C(0xFFFFFFFF));
}
/*!
* @brief Handling of the ready outputs
*
* @param[in] outputs output_t structure
* @param[in] bsec_status value returned by the bsec_do_steps() call
*
* @return none
*/
void output_ready(output_t *outputs, bsec_library_return_t bsec_status)
{
printf("{\"IAQ_Accuracy\": \"%d\",\"IAQ\":\"%.2f\"", outputs->iaq_accuracy, outputs->iaq);
printf(",\"Temperature\": \"%.2f\",\"Humidity\": \"%.2f\",\"Pressure\": \"%.2f\"", outputs->temperature,
outputs->humidity, outputs->raw_pressure);
printf(",\"Gas\": \"%.2f\"", outputs->gas_percentage);
printf(",\"Status\": \"%d\"", bsec_status);
printf(",\"Static_IAQ\": \"%.2f\"", outputs->static_iaq);
printf(",\"eCO2\": \"%.15f\"", outputs->co2_equivalent);
printf(",\"bVOCe\": \"%.25f\"}", outputs->breath_voc_equivalent);
printf("\r\n");
fflush(stdout);
}
uint32_t binary_load(uint8_t *b_buffer, uint32_t n_buffer, char *filename, uint32_t offset)
{
int32_t copied_bytes = 0;
int8_t rslt = 0;
struct stat fileinfo;
rslt = stat(filename, &fileinfo);
if (rslt != 0)
{
fprintf(stderr, "stat'ing binary file %s: ", filename);
perror("");
return 0;
}
uint32_t filesize = fileinfo.st_size - offset;
if (filesize > n_buffer)
{
fprintf(stderr, "%s: %d > %d\n", "binary data bigger than buffer", filesize, n_buffer);
return 0;
}
else
{
FILE *file_ptr;
file_ptr = fopen(filename, "rb");
if (!file_ptr)
{
perror("fopen");
return 0;
}
fseek(file_ptr, offset, SEEK_SET);
copied_bytes = fread(b_buffer, sizeof(char), filesize, file_ptr);
if (copied_bytes == 0)
{
fprintf(stderr, "%s empty\n", filename);
}
fclose(file_ptr);
return copied_bytes;
}
}
/*!
* @brief Load previous library state from non-volatile memory
*
* @param[in,out] state_buffer buffer to hold the loaded state string
* @param[in] n_buffer size of the allocated state buffer
*
* @return number of bytes copied to state_buffer
*/
uint32_t state_load(uint8_t *state_buffer, uint32_t n_buffer)
{
int32_t rslt = 0;
rslt = binary_load(state_buffer, n_buffer, filename_state, 0);
return rslt;
}
/*!
* @brief Save library state to non-volatile memory
*
* @param[in] state_buffer buffer holding the state to be stored
* @param[in] length length of the state string to be stored
*
* @return none
*/
void state_save(const uint8_t *state_buffer, uint32_t length)
{
FILE *state_w_ptr;
state_w_ptr = fopen(filename_state, "wb");
fwrite(state_buffer, length, 1, state_w_ptr);
fclose(state_w_ptr);
}
/*!
* @brief Load library config from non-volatile memory
*
* @param[in,out] config_buffer buffer to hold the loaded state string
* @param[in] n_buffer size of the allocated state buffer
*
* @return number of bytes copied to config_buffer
*/
uint32_t config_load(uint8_t *config_buffer, uint32_t n_buffer)
{
int32_t rslt = 0;
/*
* Provided config file is 4 bytes larger than buffer.
* Apparently skipping the first 4 bytes works fine.
*
*/
rslt = binary_load(config_buffer, n_buffer, filename_config, 4);
return rslt;
}
/*!
* @brief Main function which configures BSEC library and then reads and processes the data from sensor based
* on timer ticks
*
* @return result of the processing
*/
void setup()
{
i2cOpen();
i2cSetAddress(i2c_address);
struct bme68x_dev bme_dev[NUM_OF_SENS];
return_values_init ret;
for (uint8_t i = 0; i < NUM_OF_SENS; i++)
{
memset(&bme_dev[i], 0, sizeof(bme_dev[i]));
bme_dev[i].intf = BME68X_I2C_INTF;
bme_dev[i].read = bus_read;
bme_dev[i].write = bus_write;
bme_dev[i].delay_us = sleep_n;
/* Assigning a chunk of memory block to the bsecInstance */
allocateMemory(bsec_mem_block[i], i);
/* Call to the function which initializes the BSEC library
* Switch on low-power mode and provide no temperature offset */
ret = bsec_iot_init(BSEC_SAMPLE_RATE_LP, 0.0f, bus_write, bus_read, sleep_n, state_load, config_load,
bme_dev[i], i);
if (ret.bme68x_status)
{
/* Could not initialize BME68x */
printf("ERROR while initializing BME68x: %i", +ret.bme68x_status);
return;
}
else if (ret.bsec_status < BSEC_OK)
{
printf("\nERROR while initializing BSEC library: %d\n", ret.bsec_status);
return;
}
else if (ret.bsec_status > BSEC_OK)
{
printf("\nWARNING while initializing BSEC library: %d\n", ret.bsec_status);
}
}
bsec_version_t version;
bsec_get_version_m(bsecInstance, &version);
printf("BSEC version: %d.%d.%d.%d\n", version.major, version.minor, version.major_bugfix, version.minor_bugfix);
}
void loop()
{
/* Call to endless loop function which reads and processes data based on sensor settings */
/* State is saved every 10.000 samples, which means every 10.000 * 3 secs = 500 minutes */
bsec_iot_loop(sleep_n, get_timestamp_us, output_ready, state_save, 10000);
}
int main()
{
setup();
loop();
}