mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-24 22:13:52 +01:00
drivers/dht: simplified interface
This commit is contained in:
parent
044cb3c2f9
commit
6fca96c668
@ -34,7 +34,7 @@
|
||||
#include "dht.h"
|
||||
#include "dht_params.h"
|
||||
|
||||
#define ENABLE_DEBUG (1)
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
#define PULSE_WIDTH_THRESHOLD (40U)
|
||||
@ -91,10 +91,10 @@ int dht_init(dht_t *dev, const dht_params_t *params)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dht_read_raw(dht_t *dev, dht_data_t *data)
|
||||
int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
|
||||
{
|
||||
uint8_t csum, sum;
|
||||
uint16_t hum, temp;
|
||||
uint16_t raw_hum, raw_temp;
|
||||
|
||||
/* send init signal to device */
|
||||
gpio_clear(dev->pin);
|
||||
@ -114,8 +114,8 @@ int dht_read_raw(dht_t *dev, dht_data_t *data)
|
||||
*/
|
||||
|
||||
/* read the humidity, temperature, and checksum bits */
|
||||
hum = read(dev->pin, 16);
|
||||
temp = read(dev->pin, 16);
|
||||
raw_hum = read(dev->pin, 16);
|
||||
raw_temp = read(dev->pin, 16);
|
||||
csum = (uint8_t)read(dev->pin, 8);
|
||||
|
||||
/* set pin high again - so we can trigger the next reading by pulling it low
|
||||
@ -123,54 +123,32 @@ int dht_read_raw(dht_t *dev, dht_data_t *data)
|
||||
gpio_init(dev->pin, GPIO_DIR_OUT, dev->pull);
|
||||
gpio_set(dev->pin);
|
||||
|
||||
/* parse the RAW values */
|
||||
DEBUG("RAW: temp: %7i hum: %7i\n", (int)temp, (int)hum);
|
||||
data->humidity = hum;
|
||||
data->temperature = temp;
|
||||
|
||||
/* and finally validate the checksum */
|
||||
sum = (temp >> 8) + (temp & 0xff) + (hum >> 8) + (hum & 0xff);
|
||||
/* validate the checksum */
|
||||
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 0;
|
||||
}
|
||||
|
||||
void dht_parse(dht_t *dev, dht_data_t *data, float *outrelhum, float *outtemp)
|
||||
{
|
||||
/* parse the RAW values */
|
||||
DEBUG("RAW values: temp: %7i hum: %7i\n", (int)raw_temp, (int)raw_hum);
|
||||
switch (dev->type) {
|
||||
case DHT11:
|
||||
*outrelhum = data->humidity >> 8;
|
||||
*outtemp = data->temperature >> 8;
|
||||
*temp = (int16_t)((raw_temp >> 8) * 10);
|
||||
*hum = (int16_t)((raw_hum >> 8) * 10);
|
||||
break;
|
||||
|
||||
case DHT22:
|
||||
*outrelhum = data->humidity / 10;
|
||||
|
||||
/* the highest bit indicates a negative value */
|
||||
if (data->temperature & 0x8000) {
|
||||
*outtemp = (data->temperature & 0x7FFF) / -10;
|
||||
*hum = (int16_t)raw_hum;
|
||||
/* if the high-bit is set, the value is negative */
|
||||
if (raw_temp & 0x8000) {
|
||||
*temp = (int16_t)((raw_temp & ~0x8000) * -1);
|
||||
}
|
||||
else {
|
||||
*outtemp = data->temperature / 10;
|
||||
*temp = (int16_t)raw_temp;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG("unknown DHT type\n");
|
||||
}
|
||||
}
|
||||
|
||||
int dht_read(dht_t *dev, float *outrelhum, float *outtemp)
|
||||
{
|
||||
/* read data, fail on error */
|
||||
dht_data_t data;
|
||||
if (dht_read_raw(dev, &data) == -1) {
|
||||
return -1;
|
||||
return -2;
|
||||
}
|
||||
|
||||
dht_parse(dev, &data, outrelhum, outtemp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -84,41 +84,21 @@ void dht_auto_init(void);
|
||||
*/
|
||||
int dht_init(dht_t *dev, const dht_params_t *params);
|
||||
|
||||
/**
|
||||
* @brief read sensor data from device
|
||||
/**
|
||||
* @brief get a new temperature and humidity value from the device
|
||||
*
|
||||
* @note if reading fails or checksum is invalid, no new values will be
|
||||
* written into the result values
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[out] relhum pointer to relative humidity in g/m^3
|
||||
* @param[out] temp pointer to temperature in degrees Celsius
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on error
|
||||
*/
|
||||
int dht_read(dht_t *dev, float *relhum, float *temp);
|
||||
|
||||
/**
|
||||
* @brief read sensor data from device
|
||||
*
|
||||
* @note When reading fails and the function indicates an error,
|
||||
* data will contain garbage.
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[in] data pointer to DHT data object
|
||||
* @param[out] temp temperature value [in °C * 10^-1]
|
||||
* @param[out] hum relative humidity value [in percent * 10^-1]
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on checksum error
|
||||
* @return -2 on parsing error
|
||||
*/
|
||||
int dht_read_raw(dht_t *dev, dht_data_t *data);
|
||||
|
||||
/**
|
||||
* @brief parse raw sensor data into relative humidity and Celsius
|
||||
*
|
||||
* @param[in] dev device descriptor of a DHT device
|
||||
* @param[in] data sensor data
|
||||
* @param[out] relhum pointer to relative humidity in g/m^3
|
||||
* @param[out] temp pointer to temperature in degrees Celsius
|
||||
*/
|
||||
void dht_parse(dht_t *dev, dht_data_t *data, float *relhum, float *temp);
|
||||
int dht_read(dht_t *dev, int16_t *temp, int16_t *hum);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user