/* Linux userspace test code, simple and mose code directy from the doco. compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280 tested: Raspberry Pi. Use like: ./bme280 /dev/i2c-0 */ #include "bme280.h" #include #include #include #include #include #include #include #include int fd; int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) { write(fd, ®_addr,1); read(fd, data, len); return 0; } void user_delay_ms(uint32_t period) { usleep(period*1000); } int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) { int8_t *buf; buf = malloc(len +1); buf[0] = reg_addr; memcpy(buf +1, data, len); write(fd, buf, len +1); free(buf); return 0; } void print_sensor_data(struct bme280_data *comp_data) { #ifdef BME280_FLOAT_ENABLE printf("temp %0.2f, p %0.2f, hum %0.2f\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity); #else printf("temp %ld, p %ld, hum %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity); #endif } int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev) { int8_t rslt; uint8_t settings_sel; struct bme280_data comp_data; /* Recommended mode of operation: Indoor navigation */ dev->settings.osr_h = BME280_OVERSAMPLING_1X; dev->settings.osr_p = BME280_OVERSAMPLING_16X; dev->settings.osr_t = BME280_OVERSAMPLING_2X; dev->settings.filter = BME280_FILTER_COEFF_16; settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL; rslt = bme280_set_sensor_settings(settings_sel, dev); printf("Temperature, Pressure, Humidity\r\n"); /* Continuously stream sensor data */ while (1) { rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); /* Wait for the measurement to complete and print data @25Hz */ dev->delay_ms(40); rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); print_sensor_data(&comp_data); } return rslt; } int main(int argc, char* argv[]) { struct bme280_dev dev; int8_t rslt = BME280_OK; if ((fd = open(argv[1], O_RDWR)) < 0) { printf("Failed to open the i2c bus %s", argv[1]); exit(1); } if (ioctl(fd, I2C_SLAVE, 0x76) < 0) { printf("Failed to acquire bus access and/or talk to slave.\n"); exit(1); } dev.dev_id = BME280_I2C_ADDR_PRIM; dev.intf = BME280_I2C_INTF; dev.read = user_i2c_read; dev.write = user_i2c_write; dev.delay_ms = user_delay_ms; rslt = bme280_init(&dev); stream_sensor_data_forced_mode(&dev); }