diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 002a7a9df..9ab1c102c 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -9,7 +9,7 @@ */ /** - * @ingroup driver_dht + * @ingroup drivers_dht * @{ * * @file @@ -71,6 +71,12 @@ void dht_auto_init(void) if (dht_init(&dht_devs[i], &dht_params[i]) < 0) { LOG_ERROR("Unable to initialize DHT sensor #%i\n", i); } +#ifdef MODULE_SAUL_REG + for (int j = 0; j < 2; j++) { + dht_saul_reg[i][j].dev = &dht_devs[i]; + saul_reg_add(&dht_saul_reg[i][j]); + } +#endif } } diff --git a/drivers/dht/dht_saul.c b/drivers/dht/dht_saul.c new file mode 100644 index 000000000..2114c55a6 --- /dev/null +++ b/drivers/dht/dht_saul.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_dht + * @{ + * + * @file + * @brief SAUL adaption for DHT devices + * + * The values exported to SAUL for DHT devices are buffered, meaning that new + * values will only be read from the device, if the buffered values are older + * then 1 second (being exactly the maximum sampling time for DHT devices). + * + * This buffering does further introduce a coupling between both SAUL endpoints, + * meaning that if you read from both endpoints after each other, both values + * are from the same sensor reading. + * + * @author Hauke Petersen + * + * @} + */ + +#include + +#include "saul.h" +#include "dht.h" +#include "xtimer.h" + +#define DHT_SAUL_HOLD_TIME (1000 * 1000U) /* 1s */ + +static int16_t temp, hum; +static uint32_t last = 0; + +static int check_and_read(void *dev, phydat_t *res, int16_t *val, uint8_t unit) +{ + dht_t *d = (dht_t *)dev; + uint32_t now = xtimer_now(); + + if ((now - last) > DHT_SAUL_HOLD_TIME) { + dht_read(d, &temp, &hum); + last = now; + } + else { + (void)d; + } + + res->val[0] = *val; + memset(&res->val[1], 0, 2 * sizeof(int16_t)); + res->unit = unit; + res->scale = -1; + return 1; +} + +static int read_temp(void *dev, phydat_t *res) +{ + return check_and_read(dev, res, &temp, UNIT_TEMP_C); +} + +static int read_hum(void *dev, phydat_t *res) +{ + return check_and_read(dev, res, &hum, UNIT_PERCENT); +} + +const saul_driver_t dht_temp_saul_driver = { + .read = read_temp, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP +}; + +const saul_driver_t dht_hum_saul_driver = { + .read = read_hum, + .write = saul_notsup, + .type = SAUL_SENSE_HUM +}; diff --git a/drivers/dht/include/dht_params.h b/drivers/dht/include/dht_params.h index 0d68ccc51..0a4be851c 100644 --- a/drivers/dht/include/dht_params.h +++ b/drivers/dht/include/dht_params.h @@ -20,6 +20,8 @@ #define DHT_PARAMS_H #include "board.h" +#include "dht.h" +#include "saul_reg.h" #ifdef __cplusplus extern "C" { @@ -61,6 +63,25 @@ static const dht_params_t dht_params[] = */ #define DHT_NUMOF (sizeof(dht_params) / sizeof(dht_params[0])) +#ifdef MODULE_SAUL_REG +/** + * @brief Allocate and configure entries to the SAUL registry + */ +saul_reg_t dht_saul_reg[][2] = +{ + { + { + .name = "dht-temp", + .driver = &dht_temp_saul_driver + }, + { + .name = "dht-hum", + .driver = &dht_hum_saul_driver + } + } +}; +#endif + #ifdef __cplusplus } #endif diff --git a/drivers/include/dht.h b/drivers/include/dht.h index 054590b7e..f6b01f97f 100644 --- a/drivers/include/dht.h +++ b/drivers/include/dht.h @@ -30,6 +30,7 @@ #include +#include "saul.h" #include "periph/gpio.h" #ifdef __cplusplus @@ -68,6 +69,14 @@ typedef struct { */ typedef dht_t dht_params_t; +/** + * @brief export SAUL endpoints + * @{ + */ +extern const saul_driver_t dht_temp_saul_driver; +extern const saul_driver_t dht_hum_saul_driver; +/** @} */ + /** * @brief auto-initialize all configured DHT devices */ diff --git a/drivers/include/saul.h b/drivers/include/saul.h index f6a9dcc47..e1a72e2e0 100644 --- a/drivers/include/saul.h +++ b/drivers/include/saul.h @@ -139,6 +139,11 @@ typedef struct { uint8_t type; /**< device class the device belongs to */ } saul_driver_t; +/** + * @brief Default not supported function + */ +int saul_notsup(void *dev, phydat_t *dat); + /** * @brief Helper function converts a class ID to a string * diff --git a/drivers/saul/Makefile b/drivers/saul/Makefile index 3247d1d8a..cc5ba7234 100644 --- a/drivers/saul/Makefile +++ b/drivers/saul/Makefile @@ -1,4 +1,4 @@ -SRC = saul_str.c +SRC = saul.c saul_str.c ifneq (,$(filter saul_gpio,$(USEMODULE))) SRC += gpio_saul.c diff --git a/drivers/saul/saul.c b/drivers/saul/saul.c new file mode 100644 index 000000000..dbcb4f1b4 --- /dev/null +++ b/drivers/saul/saul.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2016 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_saul + * @{ + * + * @file + * @brief SAUL fallback functions + * + * @author Hauke Petersen + * + * @} + */ + +#include + +#include "saul.h" + +int saul_notsup(void *dev, phydat_t *dat) +{ + (void)dev; + (void)dat; + return -ENOTSUP; +}