Merge pull request #4666 from haukepetersen/opt_driver_dhtsaul
drivers/dht: added support for SAUL
This commit is contained in:
commit
b68b14b49b
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
80
drivers/dht/dht_saul.c
Normal file
80
drivers/dht/dht_saul.c
Normal file
@ -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 <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#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
|
||||
};
|
||||
@ -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
|
||||
|
||||
@ -30,6 +30,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
*/
|
||||
|
||||
@ -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
|
||||
*
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
SRC = saul_str.c
|
||||
SRC = saul.c saul_str.c
|
||||
|
||||
ifneq (,$(filter saul_gpio,$(USEMODULE)))
|
||||
SRC += gpio_saul.c
|
||||
|
||||
30
drivers/saul/saul.c
Normal file
30
drivers/saul/saul.c
Normal file
@ -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 <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include "saul.h"
|
||||
|
||||
int saul_notsup(void *dev, phydat_t *dat)
|
||||
{
|
||||
(void)dev;
|
||||
(void)dat;
|
||||
return -ENOTSUP;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user