From c46bc68ac59ec55e4f4303533b4abb2ceb51c16f Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 21 Jan 2016 15:24:57 +0100 Subject: [PATCH] drivers/dht: optimized DHT driver - added default dht_params.h file - added dht_auto_init(void) function - changed init function to init(dev, params) - s/gpio/pin/ in device descriptor - moved FEATURE_REQUIRED to drivers/Makefile.dep --- drivers/Makefile.dep | 1 + drivers/dht/dht.c | 30 +++++++++++---- drivers/dht/include/dht_params.h | 65 ++++++++++++++++++++++++++++++++ drivers/include/dht.h | 24 +++++++++--- 4 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 drivers/dht/include/dht_params.h diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index c7b1579ee6..778ba90343 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -22,6 +22,7 @@ endif ifneq (,$(filter dht,$(USEMODULE))) USEMODULE += xtimer + FEATURES_REQUIRED = periph_gpio endif ifneq (,$(filter enc28j60,$(USEMODULE))) diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index e9d2b7487a..a82fd2b183 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -1,6 +1,7 @@ /* * Copyright 2015 Ludwig Knüpfer - * Copyright 2015 Christian Mehlis + * 2015 Christian Mehlis + * 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 @@ -17,17 +18,21 @@ * * @author Ludwig Knüpfer * @author Christian Mehlis + * @author Hauke Petersen * * @} */ #include +#include +#include "log.h" #include "xtimer.h" #include "timex.h" #include "periph/gpio.h" #include "dht.h" +#include "dht_params.h" #define ENABLE_DEBUG (0) #include "debug.h" @@ -131,17 +136,27 @@ static void dht_read_data(gpio_t dev, uint32_t *data, uint8_t *checksum) * public API implementation **********************************************************************/ -int dht_init(dht_t *dev, dht_type_t type, gpio_t gpio) +dht_t dht_devs[DHT_NUMOF]; + +void dht_auto_init(void) +{ + for (unsigned i = 0; i < DHT_NUMOF; i++) { + if (dht_init(&dht_devs[i], &dht_params[i]) < 0) { + LOG_ERROR("Unable to initialize DHT sensor #%i\n", i); + } + } +} + +int dht_init(dht_t *dev, const dht_params_t *params) { DEBUG("dht_init\n"); - dev->gpio = gpio; - dev->type = type; + memcpy(dev, params, sizeof(dht_t)); - if (gpio_init(gpio, GPIO_DIR_OUT, GPIO_PULLUP) == -1) { + if (gpio_init(dev->pin, GPIO_DIR_OUT, GPIO_PULLUP) == -1) { return -1; } - gpio_set(gpio); + gpio_set(dev->pin); xtimer_usleep(2000 * MS_IN_USEC); @@ -149,14 +164,13 @@ int dht_init(dht_t *dev, dht_type_t type, gpio_t gpio) return 0; } - int dht_read_raw(dht_t *dev, dht_data_t *outdata) { uint32_t data; uint8_t checksum; /* read raw data */ - dht_read_data(dev->gpio, &data, &checksum); + dht_read_data(dev->pin, &data, &checksum); /* check checksum */ int ret = dht_test_checksum(data, checksum); diff --git a/drivers/dht/include/dht_params.h b/drivers/dht/include/dht_params.h new file mode 100644 index 0000000000..3beaf12215 --- /dev/null +++ b/drivers/dht/include/dht_params.h @@ -0,0 +1,65 @@ +/* + * 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 Default configuration for DHT devices + * + * @author Hauke Petersen + */ + +#ifndef DHT_PARAMS_H +#define DHT_PARAMS_H + +#include "board.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Set default configuration parameters for the DHT devices + * @{ + */ +#ifndef DHT_PARAM_PIN +#define DHT_PARAM_PIN (GPIO_PIN(0, 0)) +#endif +#ifndef DHT_PARAM_TYPE +#define DHT_PARAM_TYPE (DHT11) +#endif + +#define DHT_PARAMS_DEFAULT {.pin = DHT_PARAM_PIN, \ + .type = DHT_PARAM_TYPE} +/**@}*/ + +/** + * @brief Configure DHT devices + */ +static const dht_params_t dht_params[] = +{ +#ifdef DHT_PARAMS_BOARD + DHT_PARAMS_BOARD, +#else + DHT_PARAMS_DEFAULT, +#endif +}; + +/** + * @brief Get the number of configured DHT devices + */ +#define DHT_NUMOF (sizeof(dht_params) / sizeof(dht_params[0])) + +#ifdef __cplusplus +} +#endif + +#endif /* DHT_PARAMS_H */ +/** @} */ diff --git a/drivers/include/dht.h b/drivers/include/dht.h index 375fb95ed5..fc4852c4c4 100644 --- a/drivers/include/dht.h +++ b/drivers/include/dht.h @@ -1,5 +1,7 @@ /* - * Copyright 2015 Ludwig Knüpfer, Christian Mehlis + * Copyright 2015 Ludwig Knüpfer, + * 2015 Christian Mehlis + * 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 @@ -20,6 +22,7 @@ * * @author Ludwig Knüpfer + * @author Hauke Petersen */ #ifndef DHT_H @@ -54,21 +57,30 @@ typedef enum { * @brief device descriptor for DHT sensor devices */ typedef struct { - gpio_t gpio; /**< GPIO pin of the device's data pin */ + gpio_t pin; /**< GPIO pin of the device's data pin */ dht_type_t type; /**< type of the DHT device */ } dht_t; +/** + * @brief configuration parameters for DHT devices + */ +typedef dht_t dht_params_t; + +/** + * @brief auto-initialize all configured DHT devices + */ +void dht_auto_init(void); + /** * @brief initialize a new DHT device * - * @param[in] dev device descriptor of a DHT device - * @param[in] type type of the DHT device - * @param[in] gpio GPIO pin the device's data pin is connected to + * @param[out] dev device descriptor of a DHT device + * @param[in] params configuration parameters * * @return 0 on success * @return -1 on error */ -int dht_init(dht_t *dev, dht_type_t type, gpio_t gpio); +int dht_init(dht_t *dev, const dht_params_t *params); /** * @brief read sensor data from device