Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: sensor: update dht_polling to new API #72176

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/zephyr/drivers/sensor_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct sensor_q31_data {
q31_t power; /**< Unit: watts */
q31_t angle; /**< Unit: degrees */
q31_t electric_charge; /**< Unit: mAh */
q31_t humidity; /**< Unit: RH */
};
} readings[1];
};
Expand Down
1 change: 1 addition & 0 deletions samples/sensor/dht_polling/prj.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SENSOR=y
CONFIG_SENSOR_ASYNC_API=y
66 changes: 49 additions & 17 deletions samples/sensor/dht_polling/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@
#include <stdlib.h>

#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/sensor_data_types.h>
#include <zephyr/rtio/rtio.h>
#include <zephyr/dsp/print_format.h>

#define DHT_ALIAS(i) DT_ALIAS(_CONCAT(dht, i))
#define DHT_DEVICE(i, _) \
#define DHT_DEVICE(i, _) \
IF_ENABLED(DT_NODE_EXISTS(DHT_ALIAS(i)), (DEVICE_DT_GET(DHT_ALIAS(i)),))

/* Support up to 10 temperature/humidity sensors */
static const struct device *const sensors[] = {LISTIFY(10, DHT_DEVICE, ())};

#define DHT_IODEV(i, _) \
IF_ENABLED(DT_NODE_EXISTS(DHT_ALIAS(i)), \
(SENSOR_DT_READ_IODEV(_CONCAT(dht_iodev, i), DHT_ALIAS(i), \
{SENSOR_CHAN_AMBIENT_TEMP, 0}, \
{SENSOR_CHAN_HUMIDITY, 0})))

LISTIFY(10, DHT_IODEV, (;));
ubieda marked this conversation as resolved.
Show resolved Hide resolved

#define DHT_IODEV_REF(i, _) \
COND_CODE_1(DT_NODE_EXISTS(DHT_ALIAS(i)), (CONCAT(&dht_iodev, i)), (NULL))

static struct rtio_iodev *dht_iodev[] = { LISTIFY(10, DHT_IODEV_REF, (,)) };

RTIO_DEFINE(dht_ctx, 1, 1);

int main(void)
{
int rc;
Expand All @@ -32,29 +50,43 @@ int main(void)

while (1) {
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) {
struct device *dev = (struct device *)sensors[i];
struct device *dev = (struct device *) sensors[i];

uint8_t buf[128];

rc = sensor_read(dht_iodev[i], &dht_ctx, buf, 128);

rc = sensor_sample_fetch(dev);
if (rc < 0) {
printk("%s: sensor_sample_fetch() failed: %d\n", dev->name, rc);
if (rc != 0) {
printk("%s: sensor_read() failed: %d\n", dev->name, rc);
return rc;
}

struct sensor_value temp;
struct sensor_value hum;
const struct sensor_decoder_api *decoder;

rc = sensor_get_decoder(dev, &decoder);

rc = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &hum);
}
if (rc != 0) {
printf("get failed: %d\n", rc);
break;
printk("%s: sensor_get_decode() failed: %d\n", dev->name, rc);
return rc;
}

printk("%16s: temp is %d.%02d °C humidity is %d.%02d %%RH\n",
dev->name, temp.val1, temp.val2 / 10000,
hum.val1, hum.val2 / 10000);
uint32_t temp_fit = 0;
struct sensor_q31_data temp_data = {0};

decoder->decode(buf,
(struct sensor_chan_spec) {SENSOR_CHAN_AMBIENT_TEMP, 0},
&temp_fit, 1, &temp_data);

uint32_t hum_fit = 0;
struct sensor_q31_data hum_data = {0};

decoder->decode(buf,
(struct sensor_chan_spec) {SENSOR_CHAN_HUMIDITY, 0},
&hum_fit, 1, &hum_data);

printk("%16s: temp is %s%d.%d °C humidity is %s%d.%d RH\n", dev->name,
PRIq_arg(temp_data.readings[0].temperature, 2, temp_data.shift),
PRIq_arg(hum_data.readings[0].humidity, 2, hum_data.shift));
}
k_msleep(1000);
}
Expand Down
Loading