Extras/DHT improvements: makefile fix, replace DHT_TYPE macro by param (#252)

This commit is contained in:
Ruslan V. Uss 2016-10-26 19:41:08 +06:00 committed by sheinz
parent 574f944fbb
commit 2ab9beb946
5 changed files with 38 additions and 36 deletions

View file

@ -1,4 +1,4 @@
PROGRAM=dht_sensor
PROGRAM = dht_sensor
EXTRA_COMPONENTS = extras/dht
include ../../common.mk

View file

@ -8,7 +8,7 @@
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "dht.h"
#include <dht/dht.h>
#include "esp8266.h"
/* An example using the ubiquitous DHT** humidity sensors
@ -16,6 +16,7 @@
* from a sensor attached to GPIO pin 4.
*/
uint8_t const dht_gpio = 4;
const dht_sensor_type_t sensor_type = DHT_TYPE_DHT22;
void dhtMeasurementTask(void *pvParameters)
{
@ -28,7 +29,7 @@ void dhtMeasurementTask(void *pvParameters)
gpio_set_pullup(dht_gpio, false, false);
while(1) {
if (dht_read_data(dht_gpio, &humidity, &temperature)) {
if (dht_read_data(sensor_type, dht_gpio, &humidity, &temperature)) {
printf("Humidity: %d%% Temp: %dC\n",
humidity / 10,
temperature / 10);

View file

@ -1,10 +1,10 @@
# Component makefile for extras/dht
INC_DIRS += $(ROOT)extras/dht
# include it as 'dht/dht.h'
INC_DIRS += $(dht_ROOT)..
# args for passing into compile rule generation
extras/dht_INC_DIR = $(ROOT)extras/dht
extras/dht_SRC_DIR = $(ROOT)extras/dht
dht_SRC_DIR = $(dht_ROOT)
$(eval $(call component_compile_rules,extras/dht))
$(eval $(call component_compile_rules,dht))

View file

@ -18,7 +18,6 @@
#define DHT_DATA_BITS 40
// #define DEBUG_DHT
#ifdef DEBUG_DHT
#define debug(fmt, ...) printf("%s" fmt "\n", "dht: ", ## __VA_ARGS__);
#else
@ -116,7 +115,7 @@ static inline bool dht_fetch_data(uint8_t pin, bool bits[DHT_DATA_BITS])
debug("LOW bit timeout\n");
return false;
}
if (!dht_await_pin_state(pin, 75, false, &high_duration)){
if (!dht_await_pin_state(pin, 75, false, &high_duration)) {
debug("HIGHT bit timeout\n");
return false;
}
@ -128,27 +127,26 @@ static inline bool dht_fetch_data(uint8_t pin, bool bits[DHT_DATA_BITS])
/**
* Pack two data bytes into single value and take into account sign bit.
*/
static inline int16_t dht_convert_data(uint8_t msb, uint8_t lsb)
static inline int16_t dht_convert_data(dht_sensor_type_t sensor_type, uint8_t msb, uint8_t lsb)
{
int16_t data;
#if DHT_TYPE == DHT22
if (sensor_type == DHT_TYPE_DHT22) {
data = msb & 0x7F;
data <<= 8;
data |= lsb;
if (msb & BIT(7)) {
data = 0 - data; // convert it to negative
}
#elif DHT_TYPE == DHT11
}
else {
data = msb * 10;
#else
#error "Unsupported DHT type"
#endif
}
return data;
}
bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature)
bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity, int16_t *temperature)
{
bool bits[DHT_DATA_BITS];
uint8_t data[DHT_DATA_BITS/8] = {0};
@ -175,19 +173,19 @@ bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature)
return false;
}
*humidity = dht_convert_data(data[0], data[1]);
*temperature = dht_convert_data(data[2], data[3]);
*humidity = dht_convert_data(sensor_type, data[0], data[1]);
*temperature = dht_convert_data(sensor_type, data[2], data[3]);
debug("Sensor data: humidity=%d, temp=%d\n", *humidity, *temperature);
return true;
}
bool dht_read_float_data(uint8_t pin, float *humidity, float *temperature)
bool dht_read_float_data(dht_sensor_type_t sensor_type, uint8_t pin, float *humidity, float *temperature)
{
int16_t i_humidity, i_temp;
if (dht_read_data(pin, &i_humidity, &i_temp)) {
if (dht_read_data(sensor_type, pin, &i_humidity, &i_temp)) {
*humidity = (float)i_humidity / 10;
*temperature = (float)i_temp / 10;
return true;

View file

@ -11,16 +11,19 @@
#include <stdint.h>
#include <stdbool.h>
#define DHT11 11
#define DHT22 22
// Type of sensor to use
#define DHT_TYPE DHT22
#ifdef __cplusplus
extern "C" {
#endif
/**
* Sensor type
*/
typedef enum
{
DHT_TYPE_DHT11 = 0, //!< DHT11
DHT_TYPE_DHT22 //!< DHT22
} dht_sensor_type_t;
/**
* Read data from sensor on specified pin.
*
@ -29,7 +32,7 @@ extern "C" {
* temperature=24.4 is 24.4 degrees Celsius
*
*/
bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature);
bool dht_read_data(dht_sensor_type_t sensor_type, uint8_t pin, int16_t *humidity, int16_t *temperature);
/**
@ -37,7 +40,7 @@ bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature);
*
* Return values as floating point values.
*/
bool dht_read_float_data(uint8_t pin, float *humidity, float *temperature);
bool dht_read_float_data(dht_sensor_type_t sensor_type, uint8_t pin, float *humidity, float *temperature);
#ifdef __cplusplus
}