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
This commit is contained in:
Hauke Petersen 2016-01-21 15:24:57 +01:00
parent 4ea1a56f10
commit c46bc68ac5
4 changed files with 106 additions and 14 deletions

View File

@ -22,6 +22,7 @@ endif
ifneq (,$(filter dht,$(USEMODULE))) ifneq (,$(filter dht,$(USEMODULE)))
USEMODULE += xtimer USEMODULE += xtimer
FEATURES_REQUIRED = periph_gpio
endif endif
ifneq (,$(filter enc28j60,$(USEMODULE))) ifneq (,$(filter enc28j60,$(USEMODULE)))

View File

@ -1,6 +1,7 @@
/* /*
* Copyright 2015 Ludwig Knüpfer * 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 * 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 * General Public License v2.1. See the file LICENSE in the top level
@ -17,17 +18,21 @@
* *
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de> * @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
* @author Christian Mehlis <mehlis@inf.fu-berlin.de> * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* *
* @} * @}
*/ */
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "log.h"
#include "xtimer.h" #include "xtimer.h"
#include "timex.h" #include "timex.h"
#include "periph/gpio.h" #include "periph/gpio.h"
#include "dht.h" #include "dht.h"
#include "dht_params.h"
#define ENABLE_DEBUG (0) #define ENABLE_DEBUG (0)
#include "debug.h" #include "debug.h"
@ -131,17 +136,27 @@ static void dht_read_data(gpio_t dev, uint32_t *data, uint8_t *checksum)
* public API implementation * 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"); DEBUG("dht_init\n");
dev->gpio = gpio; memcpy(dev, params, sizeof(dht_t));
dev->type = type;
if (gpio_init(gpio, GPIO_DIR_OUT, GPIO_PULLUP) == -1) { if (gpio_init(dev->pin, GPIO_DIR_OUT, GPIO_PULLUP) == -1) {
return -1; return -1;
} }
gpio_set(gpio); gpio_set(dev->pin);
xtimer_usleep(2000 * MS_IN_USEC); 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; return 0;
} }
int dht_read_raw(dht_t *dev, dht_data_t *outdata) int dht_read_raw(dht_t *dev, dht_data_t *outdata)
{ {
uint32_t data; uint32_t data;
uint8_t checksum; uint8_t checksum;
/* read raw data */ /* read raw data */
dht_read_data(dev->gpio, &data, &checksum); dht_read_data(dev->pin, &data, &checksum);
/* check checksum */ /* check checksum */
int ret = dht_test_checksum(data, checksum); int ret = dht_test_checksum(data, checksum);

View File

@ -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 <hauke.petersen@fu-berlin.de>
*/
#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 */
/** @} */

View File

@ -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 * 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 * General Public License v2.1. See the file LICENSE in the top level
@ -20,6 +22,7 @@
* *
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de * @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de
* @author Christian Mehlis <mehlis@inf.fu-berlin.de> * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/ */
#ifndef DHT_H #ifndef DHT_H
@ -54,21 +57,30 @@ typedef enum {
* @brief device descriptor for DHT sensor devices * @brief device descriptor for DHT sensor devices
*/ */
typedef struct { 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_type_t type; /**< type of the DHT device */
} dht_t; } 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 * @brief initialize a new DHT device
* *
* @param[in] dev device descriptor of a DHT device * @param[out] dev device descriptor of a DHT device
* @param[in] type type of the DHT device * @param[in] params configuration parameters
* @param[in] gpio GPIO pin the device's data pin is connected to
* *
* @return 0 on success * @return 0 on success
* @return -1 on error * @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 * @brief read sensor data from device