Skip to content

Commit

Permalink
samples: sensor: update dht_polling to new API
Browse files Browse the repository at this point in the history
Updates dht_polling sample to new sensor API.

Depends on zephyrproject-rtos#71093 and zephyrproject-rtos#71160.

Signed-off-by: Lauren Murphy <[email protected]>
  • Loading branch information
laurenmurphyx64 committed May 28, 2024
1 parent 147a01b commit 1c496f6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
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
72 changes: 55 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, (;));

#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,49 @@ 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_sample_fetch(dev);
if (rc < 0) {
printk("%s: sensor_sample_fetch() failed: %d\n", dev->name, rc);
rc = sensor_read(dht_iodev[i], &dht_ctx, buf, 128);

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;
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 hum_fit = 0;
uint8_t hum_buf[64];

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

struct sensor_q31_data *hum_data =
(struct sensor_q31_data *) hum_buf;

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

0 comments on commit 1c496f6

Please sign in to comment.