From 62e066d130999c3c1f00affd0d6ff416bde991af Mon Sep 17 00:00:00 2001 From: Lauren Murphy Date: Wed, 5 Jun 2024 13:36:13 -0700 Subject: [PATCH] sensors: bme280: update sample to new async api Updates BME280 sample to new async sensor API. Signed-off-by: Lauren Murphy --- samples/sensor/bme280/prj.conf | 1 + samples/sensor/bme280/src/main.c | 78 +++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/samples/sensor/bme280/prj.conf b/samples/sensor/bme280/prj.conf index 42fcd3c973bcbe5..f143196e59a8ed2 100644 --- a/samples/sensor/bme280/prj.conf +++ b/samples/sensor/bme280/prj.conf @@ -1 +1,2 @@ CONFIG_SENSOR=y +CONFIG_SENSOR_ASYNC_API=y diff --git a/samples/sensor/bme280/src/main.c b/samples/sensor/bme280/src/main.c index 07fa9a60eb64b33..a604fe97f3f968b 100644 --- a/samples/sensor/bme280/src/main.c +++ b/samples/sensor/bme280/src/main.c @@ -9,15 +9,27 @@ #include #include #include +#include +#include +#include /* * Get a device structure from a devicetree node with compatible * "bosch,bme280". (If there are multiple, just pick one.) + * + * TODO: Won't work as expected with multiple BME280 devices. */ -static const struct device *get_bme280_device(void) -{ - const struct device *const dev = DEVICE_DT_GET_ANY(bosch_bme280); +const struct device *const dev = DEVICE_DT_GET_ANY(bosch_bme280); + +SENSOR_DT_READ_IODEV(iodev, DT_COMPAT_GET_ANY_STATUS_OKAY(bosch_bme280), + {SENSOR_CHAN_AMBIENT_TEMP, 0}, + {SENSOR_CHAN_HUMIDITY, 0}, + {SENSOR_CHAN_PRESS, 0}); +RTIO_DEFINE(ctx, 1, 1); + +static const struct device *check_bme280_device(void) +{ if (dev == NULL) { /* No such node, or the node does not have status "okay". */ printk("\nError: no device found.\n"); @@ -37,23 +49,65 @@ static const struct device *get_bme280_device(void) int main(void) { - const struct device *dev = get_bme280_device(); + const struct device *dev = check_bme280_device(); if (dev == NULL) { return 0; } while (1) { - struct sensor_value temp, press, humidity; + uint8_t buf[128]; + + int rc = sensor_read(&iodev, &ctx, buf, 128); + + if (rc != 0) { + printk("%s: sensor_read() failed: %d\n", dev->name, rc); + return rc; + } + + const struct sensor_decoder_api *decoder; + + rc = sensor_get_decoder(dev, &decoder); + + if (rc != 0) { + printk("%s: sensor_get_decode() failed: %d\n", dev->name, rc); + return rc; + } + + uint32_t temp_fit = 0; + uint8_t temp_buf[64]; + + decoder->decode(buf, + (struct sensor_chan_spec) {SENSOR_CHAN_AMBIENT_TEMP, 0}, + &temp_fit, 1, temp_buf); + + struct sensor_q31_data *temp_data = + (struct sensor_q31_data *) temp_buf; + + uint32_t press_fit = 0; + uint8_t press_buf[64]; + + decoder->decode(buf, + (struct sensor_chan_spec) {SENSOR_CHAN_PRESS, 0}, + &press_fit, 1, press_buf); + + struct sensor_q31_data *press_data = + (struct sensor_q31_data *) press_buf; + + uint32_t hum_fit = 0; + uint8_t hum_buf[64]; + + decoder->decode(buf, + (struct sensor_chan_spec) {SENSOR_CHAN_HUMIDITY, 0}, + &hum_fit, 1, hum_buf); - sensor_sample_fetch(dev); - sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp); - sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press); - sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity); + struct sensor_q31_data *hum_data = + (struct sensor_q31_data *) hum_buf; - printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n", - temp.val1, temp.val2, press.val1, press.val2, - humidity.val1, humidity.val2); + printk("temp: %s%d.%d; press: %s%d.%d; humidity: %s%d.%d\n", + PRIq_arg(temp_data->readings[0].temperature, 6, temp_data->shift), + PRIq_arg(press_data->readings[0].pressure, 6, press_data->shift), + PRIq_arg(hum_data->readings[0].humidity, 6, hum_data->shift)); k_sleep(K_MSEC(1000)); }