Skip to content

Commit 1dd06c2

Browse files
author
Yuval Peress
committed
sensors: Update bme280 sample to async APIs
DNM, this change is only an example and should not yet be merged. Signed-off-by: Yuval Peress <[email protected]>
1 parent add879b commit 1dd06c2

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

samples/sensor/bme280/Kconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ config LOG
77
config LOG_PRINTK
88
default y
99

10-
config SENSOR_LOG_LEVEL
11-
default 4
12-
1310
# Enable SPI and I2C support by default so that the sample works with
1411
# the device connected either way. These defaults can be overridden if
1512
# needed.

samples/sensor/bme280/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_SENSOR=y
2+
CONFIG_SENSOR_LOG_LEVEL_OFF=y

samples/sensor/bme280/src/main.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include "zephyr/rtio/rtio.h"
89
#include <zephyr/kernel.h>
910
#include <zephyr/device.h>
1011
#include <zephyr/devicetree.h>
1112
#include <zephyr/drivers/sensor.h>
1213

14+
15+
#define BME280_DT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(bosch_bme280)
16+
17+
RTIO_DEFINE_WITH_MEMPOOL(r, 1, 1, 1, 32, 4);
18+
SENSOR_DT_READ_IODEV(bme280_io, BME280_DT_NODE,
19+
SENSOR_CHAN_AMBIENT_TEMP, SENSOR_CHAN_PRESS, SENSOR_CHAN_HUMIDITY);
20+
21+
static const struct sensor_decoder_api *decoder;
22+
1323
/*
1424
* Get a device structure from a devicetree node with compatible
1525
* "bosch,bme280". (If there are multiple, just pick one.)
@@ -35,25 +45,50 @@ static const struct device *get_bme280_device(void)
3545
return dev;
3646
}
3747

48+
#define B(x) (int32_t)FIELD_GET(GENMASK64(63, 31), x)
49+
#define L(x) (int32_t)FIELD_GET(GENMASK(30, 0), x)
50+
51+
void process(int result, uint8_t *buf, uint32_t buf_len, void *userdata)
52+
{
53+
sensor_frame_iterator_t fit = {0};
54+
sensor_channel_iterator_t cit = {0};
55+
enum sensor_channel channels[3];
56+
q31_t values[3];
57+
int8_t shift[3];
58+
59+
decoder->decode(buf, &fit, &cit, channels, values, 3);
60+
61+
decoder->get_shift(buf, channels[0], &shift[0]);
62+
decoder->get_shift(buf, channels[0], &shift[1]);
63+
decoder->get_shift(buf, channels[0], &shift[2]);
64+
65+
int64_t scaled[] = {
66+
values[0] << shift[0],
67+
values[1] << shift[1],
68+
values[2] << shift[2]
69+
};
70+
71+
printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
72+
B(scaled[0]), L(scaled[0]),
73+
B(scaled[1]), L(scaled[1]),
74+
B(scaled[2]), L(scaled[2]));
75+
76+
}
77+
3878
int main(void)
3979
{
4080
const struct device *dev = get_bme280_device();
4181

82+
sensor_get_decoder(dev, &decoder);
83+
4284
if (dev == NULL) {
4385
return 0;
4486
}
4587

4688
while (1) {
47-
struct sensor_value temp, press, humidity;
48-
49-
sensor_sample_fetch(dev);
50-
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
51-
sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
52-
sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
89+
sensor_read(&bme280_io, &r, NULL);
5390

54-
printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
55-
temp.val1, temp.val2, press.val1, press.val2,
56-
humidity.val1, humidity.val2);
91+
sensor_processing_with_callback(&r, process);
5792

5893
k_sleep(K_MSEC(1000));
5994
}

0 commit comments

Comments
 (0)