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
5 Use like: ./bme280 /dev/i2c-0
12 #include <linux/i2c-dev.h>
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
19 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
21 write(fd, ®_addr,1);
26 void user_delay_ms(uint32_t period)
31 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
36 memcpy(buf +1, data, len);
37 write(fd, buf, len +1);
42 void print_sensor_data(struct bme280_data *comp_data)
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);
47 printf("temp %ld, p %ld, hum %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
51 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
55 struct bme280_data comp_data;
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;
63 settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
65 rslt = bme280_set_sensor_settings(settings_sel, dev);
67 printf("Temperature, Pressure, Humidity\r\n");
68 /* Continuously stream sensor data */
71 rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
72 if (rslt != BME280_OK)
74 /* Wait for the measurement to complete and print data @25Hz */
76 rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
77 if (rslt != BME280_OK)
79 print_sensor_data(&comp_data);
84 int main(int argc, char* argv[])
86 struct bme280_dev dev;
87 int8_t rslt = BME280_OK;
91 printf("Missing argument for I2C device.\n");
95 // make sure to select BME280_I2C_ADDR_PRIM
96 // or BME280_I2C_ADDR_SEC as needed
105 dev.intf = BME280_I2C_INTF;
106 dev.read = user_i2c_read;
107 dev.write = user_i2c_write;
108 dev.delay_ms = user_delay_ms;
110 if ((fd = open(argv[1], O_RDWR)) < 0) {
111 printf("Failed to open the i2c bus %s\n", argv[1]);
114 if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0) {
115 printf("Failed to acquire bus access and/or talk to slave.\n");
119 rslt = bme280_init(&dev);
120 if (rslt != BME280_OK)
122 printf("Failed to initialize the device.\n");
125 rslt = stream_sensor_data_forced_mode(&dev);
126 if (rslt != BME280_OK)
128 printf("Failed to stream sensor data.\n");