Skip to content

Commit

Permalink
sensors: Update bme280 sample to async APIs
Browse files Browse the repository at this point in the history
DNM, this change is only an example and should not yet be merged.

Signed-off-by: Yuval Peress <peress@google.com>
  • Loading branch information
yperess committed May 18, 2023
1 parent add879b commit 1dd06c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
3 changes: 0 additions & 3 deletions samples/sensor/bme280/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ config LOG
config LOG_PRINTK
default y

config SENSOR_LOG_LEVEL
default 4

# Enable SPI and I2C support by default so that the sample works with
# the device connected either way. These defaults can be overridden if
# needed.
Expand Down
1 change: 1 addition & 0 deletions samples/sensor/bme280/prj.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CONFIG_SENSOR=y
CONFIG_SENSOR_LOG_LEVEL_OFF=y
53 changes: 44 additions & 9 deletions samples/sensor/bme280/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "zephyr/rtio/rtio.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>


#define BME280_DT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(bosch_bme280)

RTIO_DEFINE_WITH_MEMPOOL(r, 1, 1, 1, 32, 4);
SENSOR_DT_READ_IODEV(bme280_io, BME280_DT_NODE,
SENSOR_CHAN_AMBIENT_TEMP, SENSOR_CHAN_PRESS, SENSOR_CHAN_HUMIDITY);

static const struct sensor_decoder_api *decoder;

/*
* Get a device structure from a devicetree node with compatible
* "bosch,bme280". (If there are multiple, just pick one.)
Expand All @@ -35,25 +45,50 @@ static const struct device *get_bme280_device(void)
return dev;
}

#define B(x) (int32_t)FIELD_GET(GENMASK64(63, 31), x)
#define L(x) (int32_t)FIELD_GET(GENMASK(30, 0), x)

void process(int result, uint8_t *buf, uint32_t buf_len, void *userdata)
{
sensor_frame_iterator_t fit = {0};
sensor_channel_iterator_t cit = {0};
enum sensor_channel channels[3];
q31_t values[3];
int8_t shift[3];

decoder->decode(buf, &fit, &cit, channels, values, 3);

decoder->get_shift(buf, channels[0], &shift[0]);
decoder->get_shift(buf, channels[0], &shift[1]);
decoder->get_shift(buf, channels[0], &shift[2]);

int64_t scaled[] = {
values[0] << shift[0],
values[1] << shift[1],
values[2] << shift[2]
};

printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
B(scaled[0]), L(scaled[0]),
B(scaled[1]), L(scaled[1]),
B(scaled[2]), L(scaled[2]));

}

int main(void)
{
const struct device *dev = get_bme280_device();

sensor_get_decoder(dev, &decoder);

if (dev == NULL) {
return 0;
}

while (1) {
struct sensor_value temp, press, humidity;

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);
sensor_read(&bme280_io, &r, NULL);

printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
temp.val1, temp.val2, press.val1, press.val2,
humidity.val1, humidity.val2);
sensor_processing_with_callback(&r, process);

k_sleep(K_MSEC(1000));
}
Expand Down

0 comments on commit 1dd06c2

Please sign in to comment.