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:
parent
4ea1a56f10
commit
c46bc68ac5
@ -22,6 +22,7 @@ endif
|
||||
|
||||
ifneq (,$(filter dht,$(USEMODULE)))
|
||||
USEMODULE += xtimer
|
||||
FEATURES_REQUIRED = periph_gpio
|
||||
endif
|
||||
|
||||
ifneq (,$(filter enc28j60,$(USEMODULE)))
|
||||
|
||||
@ -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 <ludwig.knuepfer@fu-berlin.de>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
65
drivers/dht/include/dht_params.h
Normal file
65
drivers/dht/include/dht_params.h
Normal 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 */
|
||||
/** @} */
|
||||
@ -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 <ludwig.knuepfer@fu-berlin.de
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user