Last active
April 25, 2024 20:27
-
-
Save teburd/0ed506fa72bd0a43cb631eb6b4dcac31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2023 Ian Morris | |
* | |
* SPDX-License-Identifier: Apache-2.0 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <zephyr/device.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, _) \ | |
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) \ | |
// SENSOR_DT_READ_IODEV(_CONCAT(dht_iodev, i), DHT_ALIAS(i), \ | |
// {SENSOR_CHAN_AMBIENT_TEMP, 0}, \ | |
// {SENSOR_CHAN_HUMIDITY, 0}) | |
#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}))) | |
/* Define our iodevs */ | |
LISTIFY(DHT_IODEV, 10); | |
#define DHT_IODEV_REF(i) IF_ENABLED(DT_NODE_EXISTS(DHT_ALIAS(i), (CONCAT(&dht_iodev, i)), (NULL))) | |
static struct rtio_iodev *dht_iodev = {LISTIFY(DHT_IODEV_REF, 10, (,))}; | |
// static struct rtio_iodev *const dht_iodev = {LISTIFY(10, DHT_IODEV, ())}; | |
RTIO_DEFINE(dht_ctx, 1, 1); | |
int main(void) | |
{ | |
int rc; | |
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) { | |
if (!device_is_ready(sensors[i])) { | |
printk("sensor: device %s not ready.\n", sensors[i]->name); | |
return 0; | |
} | |
} | |
while (1) { | |
for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) { | |
struct device *dev = (struct device *)sensors[i]; | |
uint8_t buf[128]; | |
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; | |
} | |
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 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); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment