]> git.itanic.dy.fi Git - BME280_driver/blob - examples/linux_userspace.c
added unit conversion to sensor data output
[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
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 #include "bme280.h"
17
18 int fd;
19
20 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
21 {
22         if (write(fd, &reg_addr, sizeof(reg_addr)) < sizeof(reg_addr))
23                 return BME280_E_COMM_FAIL;
24         if (read(fd, data, len) < len)
25                 return BME280_E_COMM_FAIL;
26         return BME280_OK;
27 }
28
29 void user_delay_ms(uint32_t period)
30 {
31         usleep(period * 1000);
32 }
33
34 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
35 {
36         int8_t *buf;
37         buf = malloc(len + 1);
38         buf[0] = reg_addr;
39         memcpy(buf + 1, data, len);
40         if (write(fd, buf, len + 1) < len)
41                 return BME280_E_COMM_FAIL;
42         free(buf);
43         return BME280_OK;
44 }
45
46 void print_sensor_data(struct bme280_data *comp_data)
47 {
48         float temp, press, hum;
49 #ifdef BME280_FLOAT_ENABLE
50         temp = comp_data->temperature;
51         press = 0.01 * comp_data->pressure;
52         hum = comp_data->humidity;
53 #else
54 #ifdef BME280_64BIT_ENABLE
55         temp = 0.01f * comp_data->temperature;
56         press = 0.0001f * comp_data->pressure;
57         hum = 1.0f / 1024.0f * comp_data->humidity;
58 #else
59         temp = 0.01f * comp_data->temperature;
60         press = 0.01f * comp_data->pressure;
61         hum = 1.0f / 1024.0f * comp_data->humidity;
62 #endif
63 #endif
64         printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
65 }
66
67 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
68 {
69         int8_t rslt;
70         uint8_t settings_sel;
71         struct bme280_data comp_data;
72
73         /* Recommended mode of operation: Indoor navigation */
74         dev->settings.osr_h = BME280_OVERSAMPLING_1X;
75         dev->settings.osr_p = BME280_OVERSAMPLING_16X;
76         dev->settings.osr_t = BME280_OVERSAMPLING_2X;
77         dev->settings.filter = BME280_FILTER_COEFF_16;
78
79         settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
80
81         rslt = bme280_set_sensor_settings(settings_sel, dev);
82         if (rslt != BME280_OK)
83         {
84                 fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
85                 return rslt;
86         }
87
88         printf("Temperature, Pressure, Humidity\n");
89         /* Continuously stream sensor data */
90         while (1)
91         {
92                 rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
93                 if (rslt != BME280_OK)
94                 {
95                         fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
96                         break;
97                 }
98                 /* Wait for the measurement to complete and print data @25Hz */
99                 dev->delay_ms(40);
100                 rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
101                 if (rslt != BME280_OK)
102                 {
103                         fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
104                         break;
105                 }
106                 print_sensor_data(&comp_data);
107         }
108         return rslt;
109 }
110
111 int main(int argc, char* argv[])
112 {
113         struct bme280_dev dev;
114         int8_t rslt = BME280_OK;
115
116         if (argc < 2)
117         {
118                 fprintf(stderr, "Missing argument for i2c bus.\n");
119                 exit(1);
120         }
121         
122         // make sure to select BME280_I2C_ADDR_PRIM
123         // or BME280_I2C_ADDR_SEC as needed
124         dev.dev_id =
125 #if 1
126                 BME280_I2C_ADDR_PRIM
127 #else
128                 BME280_I2C_ADDR_SEC
129 #endif
130                 ;
131
132         dev.intf = BME280_I2C_INTF;
133         dev.read = user_i2c_read;
134         dev.write = user_i2c_write;
135         dev.delay_ms = user_delay_ms;
136
137         if ((fd = open(argv[1], O_RDWR)) < 0)
138         {
139                 fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
140                 exit(1);
141         }
142         if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
143         {
144                 fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
145                 exit(1);
146         }
147         
148         rslt = bme280_init(&dev);
149         if (rslt != BME280_OK)
150         {
151                 fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
152                 exit(1);
153         }
154         rslt = stream_sensor_data_forced_mode(&dev);
155         if (rslt != BME280_OK)
156         {
157                 fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
158                 exit(1);
159         }
160         return 0;
161 }