]> git.itanic.dy.fi Git - BME280_driver/blobdiff - examples/linux_userspace.c
Added a wait until the NVM copy was complete.
[BME280_driver] / examples / linux_userspace.c
index 649609bc9d4033e354c8bc4901aab21ffe98cea1..6ce36618cad0918a980ad81dffd2165b7eafbfff 100644 (file)
-/*
-       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 <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <linux/i2c-dev.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include "bme280.h"
-
-int fd;
-
-int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
-{
-       if (write(fd, &reg_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)
-{
-       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);
-       if (write(fd, buf, len + 1) < len)
-               return BME280_E_COMM_FAIL;
-       free(buf);
-       return BME280_OK;
-}
-
-void print_sensor_data(struct bme280_data *comp_data)
-{
-       float temp, press, hum;
-#ifdef BME280_FLOAT_ENABLE
-       temp = comp_data->temperature;
-       press = 0.01 * comp_data->pressure;
-       hum = comp_data->humidity;
-#else
-#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)
-{
-       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);
-       if (rslt != BME280_OK)
-       {
-               fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
-               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)
-               {
-                       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;
-}
-
-int main(int argc, char* argv[])
-{
-       struct bme280_dev dev;
-       int8_t rslt = BME280_OK;
-
-       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
-#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)
-       {
-               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 (code %+d).\n", rslt);
-               exit(1);
-       }
-       rslt = stream_sensor_data_forced_mode(&dev);
-       if (rslt != BME280_OK)
-       {
-               fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
-               exit(1);
-       }
-       return 0;
-}
+/*\r
+ * Linux userspace test code, simple and mose code directy from the doco.\r
+ * compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280\r
+ * tested: Raspberry Pi.\r
+ * Use like: ./bme280 /dev/i2c-0\r
+ */\r
+#include "bme280.h"\r
+#include <string.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
+#ifdef __KERNEL__\r
+#include <linux/i2c-dev.h>\r
+#include <sys/ioctl.h>\r
+#endif\r
+#include <sys/types.h>\r
+#include <fcntl.h>\r
+\r
+int fd;\r
+\r
+int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+void user_delay_ms(uint32_t period);\r
+int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+void print_sensor_data(struct bme280_data *comp_data);\r
+int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\r
+\r
+int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
+{\r
+    write(fd, &reg_addr, 1);\r
+    read(fd, data, len);\r
+\r
+    return 0;\r
+}\r
+\r
+void user_delay_ms(uint32_t period)\r
+{\r
+    usleep(period * 1000);\r
+}\r
+\r
+int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
+{\r
+    int8_t *buf;\r
+\r
+    buf = malloc(len + 1);\r
+    buf[0] = reg_addr;\r
+    memcpy(buf + 1, data, len);\r
+    if (write(fd, buf, len + 1) < len)\r
+        return BME280_E_COMM_FAIL;\r
+    free(buf);\r
+       return BME280_OK;\r
+}\r
+\r
+void print_sensor_data(struct bme280_data *comp_data)\r
+{\r
+    float temp, press, hum;\r
+#ifdef BME280_FLOAT_ENABLE\r
+    temp = comp_data->temperature;\r
+    press = 0.01 * comp_data->pressure;\r
+    hum = comp_data->humidity;\r
+#else\r
+#ifdef BME280_64BIT_ENABLE\r
+    temp = 0.01f * comp_data->temperature;\r
+    press = 0.0001f * comp_data->pressure;\r
+    hum = 1.0f / 1024.0f * comp_data->humidity;\r
+#else\r
+    temp = 0.01f * comp_data->temperature;\r
+    press = 0.01f * comp_data->pressure;\r
+    hum = 1.0f / 1024.0f * comp_data->humidity;\r
+#endif\r
+#endif\r
+    printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);\r
+}\r
+\r
+int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)\r
+{\r
+    int8_t rslt;\r
+    uint8_t settings_sel;\r
+    struct bme280_data comp_data;\r
+\r
+    /* Recommended mode of operation: Indoor navigation */\r
+    dev->settings.osr_h = BME280_OVERSAMPLING_1X;\r
+    dev->settings.osr_p = BME280_OVERSAMPLING_16X;\r
+    dev->settings.osr_t = BME280_OVERSAMPLING_2X;\r
+    dev->settings.filter = BME280_FILTER_COEFF_16;\r
+\r
+    settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;\r
+\r
+    rslt = bme280_set_sensor_settings(settings_sel, dev);\r
+       if (rslt != BME280_OK)\r
+       {\r
+               fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);\r
+               return rslt;\r
+       }\r
+\r
+    printf("Temperature, Pressure, Humidity\n");\r
+    /* Continuously stream sensor data */\r
+    while (1)\r
+    {\r
+        rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);\r
+        if (rslt != BME280_OK)\r
+        {\r
+            fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);\r
+            break;\r
+        }\r
+        /* Wait for the measurement to complete and print data @25Hz */\r
+        dev->delay_ms(40);\r
+        rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
+        if (rslt != BME280_OK)\r
+        {\r
+            fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);\r
+            break;\r
+        }\r
+        print_sensor_data(&comp_data);\r
+    }\r
+\r
+    return rslt;\r
+}\r
+\r
+int main(int argc, char* argv[])\r
+{\r
+    struct bme280_dev dev;\r
+    int8_t rslt = BME280_OK;\r
+\r
+    if (argc < 2)\r
+    {\r
+        fprintf(stderr, "Missing argument for i2c bus.\n");\r
+        exit(1);\r
+    }\r
+    \r
+    // make sure to select BME280_I2C_ADDR_PRIM\r
+    // or BME280_I2C_ADDR_SEC as needed\r
+    dev.dev_id =\r
+#if 1\r
+        BME280_I2C_ADDR_PRIM\r
+#else\r
+        BME280_I2C_ADDR_SEC\r
+#endif\r
+;\r
+\r
+    dev.intf = BME280_I2C_INTF;\r
+    dev.read = user_i2c_read;\r
+    dev.write = user_i2c_write;\r
+    dev.delay_ms = user_delay_ms;\r
+\r
+    if ((fd = open(argv[1], O_RDWR)) < 0)\r
+    {\r
+        fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);\r
+    exit(1);\r
+    }\r
+#ifdef __KERNEL__\r
+    if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)\r
+    {\r
+        fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");\r
+        exit(1);\r
+    }\r
+#endif\r
+\r
+    rslt = bme280_init(&dev);\r
+    if (rslt != BME280_OK)\r
+    {\r
+        fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);\r
+        exit(1);\r
+}\r
+    rslt = stream_sensor_data_forced_mode(&dev);\r
+    if (rslt != BME280_OK)\r
+    {\r
+        fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
+        exit(1);\r
+    }\r
+    return 0;\r
+}\r