]> git.itanic.dy.fi Git - BME280_driver/blob - examples/linux_userspace.c
d21a3ba7aca928dbe2d28089d9da8525ba656e4e
[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\n", comp_data->temperature, comp_data->pressure, comp_data->humidity);
46 #else
47         printf("temp %d, p %d, hum %d\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         if (rslt != BME280_OK)
67         {
68                 fprintf(stderr, "Failed to set sensor settings.");
69                 return rslt;
70         }
71
72         printf("Temperature, Pressure, Humidity\n");
73         /* Continuously stream sensor data */
74         while (1)
75         {
76                 rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
77                 if (rslt != BME280_OK)
78                         break;
79                 /* Wait for the measurement to complete and print data @25Hz */
80                 dev->delay_ms(40);
81                 rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
82                 if (rslt != BME280_OK)
83                         break;
84                 print_sensor_data(&comp_data);
85         }
86         return rslt;
87 }
88
89 int main(int argc, char* argv[])
90 {
91         struct bme280_dev dev;
92         int8_t rslt = BME280_OK;
93
94         if (argc < 2)
95         {
96                 fprintf(stderr, "Missing argument for i2c bus.\n");
97                 exit(1);
98         }
99         
100         // make sure to select BME280_I2C_ADDR_PRIM
101         // or BME280_I2C_ADDR_SEC as needed
102         dev.dev_id =
103 #if 1
104                 BME280_I2C_ADDR_PRIM
105 #else
106                 BME280_I2C_ADDR_SEC
107 #endif
108                 ;
109
110         dev.intf = BME280_I2C_INTF;
111         dev.read = user_i2c_read;
112         dev.write = user_i2c_write;
113         dev.delay_ms = user_delay_ms;
114
115         if ((fd = open(argv[1], O_RDWR)) < 0)
116         {
117                 fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
118                 exit(1);
119         }
120         if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
121         {
122                 fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
123                 exit(1);
124         }
125         
126         rslt = bme280_init(&dev);
127         if (rslt != BME280_OK)
128         {
129                 fprintf(stderr, "Failed to initialize the device.\n");
130                 exit(1);
131         }
132         rslt = stream_sensor_data_forced_mode(&dev);
133         if (rslt != BME280_OK)
134         {
135                 fprintf(stderr, "Failed to stream sensor data.\n");
136                 exit(1);
137         }
138         return 0;
139 }