]> git.itanic.dy.fi Git - BME280_driver/blob - examples/linux_userspace.c
Linux userspace example code
[BME280_driver] / examples / linux_userspace.c
1 /*
2   Linux userspace test code, simple and mose code directy from the doco.
3   compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280
4   tested: Raspberry Pi.
5   Use like: ./bme280 /dev/i2c-0
6 */
7 #include "bme280.h"
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <linux/i2c-dev.h>
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
15 #include <fcntl.h>
16
17 int fd;
18
19 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
20 {
21   write(fd, &reg_addr,1);
22   read(fd, data, len);
23   return 0;
24 }
25
26 void user_delay_ms(uint32_t period)
27 {
28   usleep(period*1000);
29 }
30
31 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
32 {
33   int8_t *buf;
34   buf = malloc(len +1);
35   buf[0] = reg_addr;
36   memcpy(buf +1, data, len);
37   write(fd, buf, len +1);
38   free(buf);
39   return 0;
40 }
41
42 void print_sensor_data(struct bme280_data *comp_data)
43 {
44 #ifdef BME280_FLOAT_ENABLE
45   printf("temp %0.2f, p %0.2f, hum %0.2f\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
46 #else
47   printf("temp %ld, p %ld, hum %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
48 #endif
49 }
50
51 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
52 {
53   int8_t rslt;
54   uint8_t settings_sel;
55   struct bme280_data comp_data;
56
57   /* Recommended mode of operation: Indoor navigation */
58   dev->settings.osr_h = BME280_OVERSAMPLING_1X;
59   dev->settings.osr_p = BME280_OVERSAMPLING_16X;
60   dev->settings.osr_t = BME280_OVERSAMPLING_2X;
61   dev->settings.filter = BME280_FILTER_COEFF_16;
62
63   settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
64
65   rslt = bme280_set_sensor_settings(settings_sel, dev);
66
67   printf("Temperature, Pressure, Humidity\r\n");
68   /* Continuously stream sensor data */
69   while (1) {
70     rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
71     /* Wait for the measurement to complete and print data @25Hz */
72     dev->delay_ms(40);
73     rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
74     print_sensor_data(&comp_data);
75   }
76   return rslt;
77 }
78
79 int main(int argc, char* argv[])
80 {
81   struct bme280_dev dev;
82   int8_t rslt = BME280_OK;
83
84   if ((fd = open(argv[1], O_RDWR)) < 0) {
85     printf("Failed to open the i2c bus %s", argv[1]);
86     exit(1);
87   }
88   if (ioctl(fd, I2C_SLAVE, 0x76) < 0) {
89     printf("Failed to acquire bus access and/or talk to slave.\n");
90     exit(1);
91   }
92   dev.dev_id = BME280_I2C_ADDR_PRIM;
93   dev.intf = BME280_I2C_INTF;
94   dev.read = user_i2c_read;
95   dev.write = user_i2c_write;
96   dev.delay_ms = user_delay_ms;
97
98   rslt = bme280_init(&dev);
99   stream_sensor_data_forced_mode(&dev);
100 }