Skip to content

Commit

Permalink
drivers/dht: updated auto-init + some enhancements
Browse files Browse the repository at this point in the history
- unified auto-init to use same style as other SAUl devices
- make use of named return values
- added some asserts
  • Loading branch information
haukepetersen committed Feb 7, 2017
1 parent 8edb69a commit 3afc3eb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 43 deletions.
41 changes: 13 additions & 28 deletions drivers/dht/dht.c
@@ -1,7 +1,7 @@
/*
* Copyright 2015 Ludwig Knüpfer
* 2015 Christian Mehlis
* 2016 Freie Universität Berlin
* 2016-2017 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
Expand All @@ -27,6 +27,7 @@
#include <string.h>

#include "log.h"
#include "assert.h"
#include "xtimer.h"
#include "timex.h"
#include "periph/gpio.h"
Expand All @@ -39,11 +40,6 @@

#define PULSE_WIDTH_THRESHOLD (40U)

/**
* @brief Allocation of memory for device descriptors
*/
dht_t dht_devs[DHT_NUMOF];

static uint16_t read(gpio_t pin, int bits)
{
uint16_t res = 0;
Expand All @@ -65,43 +61,32 @@ static uint16_t read(gpio_t pin, int bits)
return res;
}

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);
}
#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
}
}

int dht_init(dht_t *dev, const dht_params_t *params)
{
DEBUG("dht_init\n");

/* check parameters and configuration */
assert(dev && params &&
((dev->type == DHT11) || (dev->type == DHT22) || (dev->type == DHT21)));

memcpy(dev, params, sizeof(dht_t));

if (gpio_init(dev->pin, GPIO_OUT) == -1) {
return -1;
}
gpio_init(dev->pin, GPIO_OUT);
gpio_set(dev->pin);

xtimer_usleep(2000 * US_PER_MS);

DEBUG("dht_init: success\n");
return 0;
return DHT_OK;
}

int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
{
uint8_t csum, sum;
uint16_t raw_hum, raw_temp;

assert(dev && temp && hum);

/* send init signal to device */
gpio_clear(dev->pin);
xtimer_usleep(20 * US_PER_MS);
Expand Down Expand Up @@ -133,7 +118,7 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
sum = (raw_temp >> 8) + (raw_temp & 0xff) + (raw_hum >> 8) + (raw_hum & 0xff);
if ((sum != csum) || (csum == 0)) {
DEBUG("error: checksum invalid\n");
return -1;
return DHT_NOCSUM;
}

/* parse the RAW values */
Expand All @@ -154,8 +139,8 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
}
break;
default:
return -2;
return DHT_NODEV; /* this should never be reached */
}

return 0;
return DHT_OK;
}
12 changes: 3 additions & 9 deletions drivers/dht/include/dht_params.h
Expand Up @@ -67,17 +67,11 @@ static const dht_params_t dht_params[] =
/**
* @brief Allocate and configure entries to the SAUL registry
*/
saul_reg_t dht_saul_reg[][2] =
static const saul_reg_info_t dht_saul_info[][2] =
{
{
{
.name = "dht-temp",
.driver = &dht_temp_saul_driver
},
{
.name = "dht-hum",
.driver = &dht_hum_saul_driver
}
{ .name = "dht-temp" },
{ .name = "dht-hum" }
}
};
#endif
Expand Down
11 changes: 10 additions & 1 deletion drivers/include/dht.h
@@ -1,7 +1,7 @@
/*
* Copyright 2015 Ludwig Knüpfer,
* 2015 Christian Mehlis
* 2016 Freie Universität Berlin
* 2016-2017 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
Expand Down Expand Up @@ -37,6 +37,15 @@
extern "C" {
#endif

/**
* @brief possible return codes
*/
enum {
DHT_OK = 0, /**< all good */
DHT_NOCSUM = -1, /**< checksum error */
DHT_NODEV = -2 /**< device type not defined */
};

/**
* @brief data type for storing DHT sensor readings
*/
Expand Down
9 changes: 4 additions & 5 deletions sys/auto_init/auto_init.c
Expand Up @@ -160,11 +160,6 @@ void auto_init(void)
DEBUG("Auto init TCP module\n");
gnrc_tcp_init();
#endif
#ifdef MODULE_DHT
DEBUG("Auto init DHT devices.\n");
extern void dht_auto_init(void);
dht_auto_init();
#endif
#ifdef MODULE_LWIP
DEBUG("Bootstraping lwIP.\n");
lwip_bootstrap();
Expand Down Expand Up @@ -319,6 +314,10 @@ void auto_init(void)
extern void auto_init_hdc1000(void);
auto_init_hdc1000();
#endif
#ifdef MODULE_DHT
extern void auto_init_dht(void);
auto_init_dht();
#endif

#endif /* MODULE_AUTO_INIT_SAUL */

Expand Down
67 changes: 67 additions & 0 deletions sys/auto_init/saul/auto_init_dht.c
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2017 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 auto_init_saul
* @{
*
* @file
* @brief Auto initialization for DHT temperature/humidity sensors
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/

#ifdef MODULE_DHT

#include "log.h"
#include "saul_reg.h"
#include "dht.h"
#include "dht_params.h"

/**
* @brief Define the number of configured sensors
*/
#define DHT_NUM (sizeof(dht_params) / sizeof(dht_params[0]))

/**
* @brief Allocate memory for the device descriptors
*/
static dht_t dht_devs[DHT_NUM];

/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[DHT_NUM * 2];

void auto_init_dht(void)
{
for (unsigned int i = 0; i < DHT_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing dht #%u\n", i);

if (dht_init(&dht_devs[i], &dht_params[i]) != DHT_OK) {
LOG_ERROR("[auto_init_saul] error initializing dht #%u\n", i);
}
else {
saul_entries[(i * 2)].dev = &(dht_devs[i]);
saul_entries[(i * 2)].name = dht_saul_info[i][0].name;
saul_entries[(i * 2)].driver = &dht_temp_saul_driver;
saul_entries[(i * 2) + 1].dev = &(dht_devs[i]);
saul_entries[(i * 2) + 1].name = dht_saul_info[i][1].name;
saul_entries[(i * 2) + 1].driver = &dht_hum_saul_driver;
saul_reg_add(&(saul_entries[(i * 2)]));
saul_reg_add(&(saul_entries[(i * 2) + 1]));
}
}
}

#else
typedef int dont_be_pedantic;
#endif /* MODULE_DHT */

0 comments on commit 3afc3eb

Please sign in to comment.