From 4c9406ecfdf27e07d6ae02473c3dc101ee5a5644 Mon Sep 17 00:00:00 2001 From: byteneumann Date: Wed, 31 Jul 2019 07:20:16 +0000 Subject: [PATCH 1/4] added basic error handling, replaced hardcoded dev_id with define --- examples/linux_userspace.c | 50 +++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/examples/linux_userspace.c b/examples/linux_userspace.c index 07b6e9b..c550d8f 100644 --- a/examples/linux_userspace.c +++ b/examples/linux_userspace.c @@ -66,11 +66,16 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev) printf("Temperature, Pressure, Humidity\r\n"); /* Continuously stream sensor data */ - while (1) { + while (1) + { rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); + if (rslt != BME280_OK) + break; /* Wait for the measurement to complete and print data @25Hz */ dev->delay_ms(40); rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); + if (rslt != BME280_OK) + break; print_sensor_data(&comp_data); } return rslt; @@ -81,20 +86,47 @@ 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]); + if (argc < 2) + { + printf("Missing argument for I2C device.\n"); 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; + + // make sure to select BME280_I2C_ADDR_PRIM + // or BME280_I2C_ADDR_SEC as needed + dev.dev_id = +#if 1 + BME280_I2C_ADDR_PRIM +#else + BME280_I2C_ADDR_SEC +#endif + ; + dev.intf = BME280_I2C_INTF; dev.read = user_i2c_read; dev.write = user_i2c_write; dev.delay_ms = user_delay_ms; + if ((fd = open(argv[1], O_RDWR)) < 0) { + printf("Failed to open the i2c bus %s\n", argv[1]); + exit(1); + } + if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0) { + printf("Failed to acquire bus access and/or talk to slave.\n"); + exit(1); + } + rslt = bme280_init(&dev); - stream_sensor_data_forced_mode(&dev); + if (rslt != BME280_OK) + { + printf("Failed to initialize the device.\n"); + exit(1); + } + rslt = stream_sensor_data_forced_mode(&dev); + if (rslt != BME280_OK) + { + printf("Failed to stream sensor data.\n"); + exit(1); + } + return 0; } From 64356f5f7ed91d88343f271b3b222dca91f0d54f Mon Sep 17 00:00:00 2001 From: byteneumann Date: Wed, 31 Jul 2019 09:48:00 +0000 Subject: [PATCH 2/4] fixed indentation --- examples/linux_userspace.c | 171 +++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 82 deletions(-) diff --git a/examples/linux_userspace.c b/examples/linux_userspace.c index c550d8f..d21a3ba 100644 --- a/examples/linux_userspace.c +++ b/examples/linux_userspace.c @@ -1,8 +1,8 @@ /* - 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 + 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 @@ -18,115 +18,122 @@ 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; + write(fd, ®_addr, 1); + read(fd, data, len); + return 0; } void user_delay_ms(uint32_t period) { - usleep(period*1000); + 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; + 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); + printf("temp %0.2f, p %0.2f, hum %0.2f\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); + printf("temp %d, p %d, hum %d\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; + 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; + /* 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; + 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); + rslt = bme280_set_sensor_settings(settings_sel, dev); + if (rslt != BME280_OK) + { + fprintf(stderr, "Failed to set sensor settings."); + return rslt; + } - printf("Temperature, Pressure, Humidity\r\n"); - /* Continuously stream sensor data */ - while (1) - { - rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); - if (rslt != BME280_OK) - break; - /* Wait for the measurement to complete and print data @25Hz */ - dev->delay_ms(40); - rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); - if (rslt != BME280_OK) - break; - print_sensor_data(&comp_data); - } - return rslt; + printf("Temperature, Pressure, Humidity\n"); + /* Continuously stream sensor data */ + while (1) + { + rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); + if (rslt != BME280_OK) + break; + /* Wait for the measurement to complete and print data @25Hz */ + dev->delay_ms(40); + rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); + if (rslt != BME280_OK) + break; + print_sensor_data(&comp_data); + } + return rslt; } int main(int argc, char* argv[]) { - struct bme280_dev dev; - int8_t rslt = BME280_OK; + struct bme280_dev dev; + int8_t rslt = BME280_OK; - if (argc < 2) - { - printf("Missing argument for I2C device.\n"); - exit(1); - } - - // make sure to select BME280_I2C_ADDR_PRIM - // or BME280_I2C_ADDR_SEC as needed - dev.dev_id = + if (argc < 2) + { + fprintf(stderr, "Missing argument for i2c bus.\n"); + exit(1); + } + + // make sure to select BME280_I2C_ADDR_PRIM + // or BME280_I2C_ADDR_SEC as needed + dev.dev_id = #if 1 - BME280_I2C_ADDR_PRIM + BME280_I2C_ADDR_PRIM #else - BME280_I2C_ADDR_SEC + BME280_I2C_ADDR_SEC #endif - ; + ; - dev.intf = BME280_I2C_INTF; - dev.read = user_i2c_read; - dev.write = user_i2c_write; - dev.delay_ms = user_delay_ms; + dev.intf = BME280_I2C_INTF; + dev.read = user_i2c_read; + dev.write = user_i2c_write; + dev.delay_ms = user_delay_ms; - if ((fd = open(argv[1], O_RDWR)) < 0) { - printf("Failed to open the i2c bus %s\n", argv[1]); - exit(1); - } - if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0) { - printf("Failed to acquire bus access and/or talk to slave.\n"); - exit(1); - } - - rslt = bme280_init(&dev); - if (rslt != BME280_OK) - { - printf("Failed to initialize the device.\n"); - exit(1); - } - rslt = stream_sensor_data_forced_mode(&dev); - if (rslt != BME280_OK) - { - printf("Failed to stream sensor data.\n"); - exit(1); - } - return 0; + if ((fd = open(argv[1], O_RDWR)) < 0) + { + fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]); + exit(1); + } + if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0) + { + fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n"); + exit(1); + } + + rslt = bme280_init(&dev); + if (rslt != BME280_OK) + { + fprintf(stderr, "Failed to initialize the device.\n"); + exit(1); + } + rslt = stream_sensor_data_forced_mode(&dev); + if (rslt != BME280_OK) + { + fprintf(stderr, "Failed to stream sensor data.\n"); + exit(1); + } + return 0; } From 47256731aa8346309db1a8e0a19b01cf51b99c31 Mon Sep 17 00:00:00 2001 From: byteneumann Date: Wed, 31 Jul 2019 19:01:48 +0000 Subject: [PATCH 3/4] improved error output --- examples/linux_userspace.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/linux_userspace.c b/examples/linux_userspace.c index d21a3ba..2954825 100644 --- a/examples/linux_userspace.c +++ b/examples/linux_userspace.c @@ -4,7 +4,7 @@ tested: Raspberry Pi. Use like: ./bme280 /dev/i2c-0 */ -#include "bme280.h" + #include #include #include @@ -13,14 +13,17 @@ #include #include #include +#include "bme280.h" 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; + if (write(fd, ®_addr, sizeof(reg_addr)) < sizeof(reg_addr)) + return BME280_E_COMM_FAIL; + if (read(fd, data, len) < len) + return BME280_E_COMM_FAIL; + return BME280_OK; } void user_delay_ms(uint32_t period) @@ -31,12 +34,13 @@ void user_delay_ms(uint32_t period) 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 = malloc(len + 1); buf[0] = reg_addr; memcpy(buf + 1, data, len); - write(fd, buf, len + 1); + if (write(fd, buf, len + 1) < len) + return BME280_E_COMM_FAIL; free(buf); - return 0; + return BME280_OK; } void print_sensor_data(struct bme280_data *comp_data) @@ -65,7 +69,7 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev) rslt = bme280_set_sensor_settings(settings_sel, dev); if (rslt != BME280_OK) { - fprintf(stderr, "Failed to set sensor settings."); + fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt); return rslt; } @@ -75,12 +79,18 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev) { rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev); if (rslt != BME280_OK) + { + fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt); break; + } /* Wait for the measurement to complete and print data @25Hz */ dev->delay_ms(40); rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev); if (rslt != BME280_OK) + { + fprintf(stderr, "Failed to get sensor data (code %+d).", rslt); break; + } print_sensor_data(&comp_data); } return rslt; @@ -126,13 +136,13 @@ int main(int argc, char* argv[]) rslt = bme280_init(&dev); if (rslt != BME280_OK) { - fprintf(stderr, "Failed to initialize the device.\n"); + fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt); exit(1); } rslt = stream_sensor_data_forced_mode(&dev); if (rslt != BME280_OK) { - fprintf(stderr, "Failed to stream sensor data.\n"); + fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt); exit(1); } return 0; From 64216feb552bbcf3db900f903d6916a2748b284d Mon Sep 17 00:00:00 2001 From: byteneumann Date: Thu, 1 Aug 2019 16:39:06 +0000 Subject: [PATCH 4/4] added unit conversion to sensor data output --- examples/linux_userspace.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/linux_userspace.c b/examples/linux_userspace.c index 2954825..649609b 100644 --- a/examples/linux_userspace.c +++ b/examples/linux_userspace.c @@ -45,11 +45,23 @@ int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) void print_sensor_data(struct bme280_data *comp_data) { + float temp, press, hum; #ifdef BME280_FLOAT_ENABLE - printf("temp %0.2f, p %0.2f, hum %0.2f\n", comp_data->temperature, comp_data->pressure, comp_data->humidity); + temp = comp_data->temperature; + press = 0.01 * comp_data->pressure; + hum = comp_data->humidity; #else - printf("temp %d, p %d, hum %d\n", comp_data->temperature, comp_data->pressure, comp_data->humidity); +#ifdef BME280_64BIT_ENABLE + temp = 0.01f * comp_data->temperature; + press = 0.0001f * comp_data->pressure; + hum = 1.0f / 1024.0f * comp_data->humidity; +#else + temp = 0.01f * comp_data->temperature; + press = 0.01f * comp_data->pressure; + hum = 1.0f / 1024.0f * comp_data->humidity; +#endif #endif + printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum); } int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)