commit
234885e365
|
@ -42,6 +42,7 @@ INCLUDES += -I ./
|
|||
INCLUDES += -I ./include
|
||||
INCLUDES += -I ../include
|
||||
INCLUDES += -I ../../include
|
||||
INCLUDES += -I ../libc
|
||||
INCLUDES += -I ../platform
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
|
|
117
app/dhtlib/dht.c
117
app/dhtlib/dht.c
|
@ -29,6 +29,7 @@
|
|||
|
||||
#include "user_interface.h"
|
||||
#include "platform.h"
|
||||
#include "c_stdio.h"
|
||||
#include "dht.h"
|
||||
|
||||
#ifndef LOW
|
||||
|
@ -39,12 +40,12 @@
|
|||
#define HIGH 1
|
||||
#endif /* ifndef HIGH */
|
||||
|
||||
#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((byte_high << 8) | (byte_low))
|
||||
#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) (((byte_high) << 8) | (byte_low))
|
||||
|
||||
static double dht_humidity;
|
||||
static double dht_temperature;
|
||||
|
||||
static uint8_t dht_bits[5]; // buffer to receive data
|
||||
static uint8_t dht_bytes[5]; // buffer to receive data
|
||||
static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay);
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
|
@ -66,6 +67,74 @@ double dht_getTemperature(void)
|
|||
return dht_temperature;
|
||||
}
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht_read_universal(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP);
|
||||
if (rv != DHTLIB_OK)
|
||||
{
|
||||
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv; // propagate error value
|
||||
}
|
||||
|
||||
#if defined(DHT_DEBUG_BYTES)
|
||||
int i;
|
||||
for (i = 0; i < 5; i++)
|
||||
{
|
||||
DHT_DEBUG("%02X\n", dht_bytes[i]);
|
||||
}
|
||||
#endif // defined(DHT_DEBUG_BYTES)
|
||||
|
||||
// Assume it is DHT11
|
||||
// If it is DHT11, both bit[1] and bit[3] is 0
|
||||
if ((dht_bytes[1] == 0) && (dht_bytes[3] == 0))
|
||||
{
|
||||
// It may DHT11
|
||||
// CONVERT AND STORE
|
||||
DHT_DEBUG("DHT11 method\n");
|
||||
dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0;
|
||||
dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0;
|
||||
|
||||
// TEST CHECKSUM
|
||||
// dht_bytes[1] && dht_bytes[3] both 0
|
||||
uint8_t sum = dht_bytes[0] + dht_bytes[2];
|
||||
if (dht_bytes[4] != sum)
|
||||
{
|
||||
// It may not DHT11
|
||||
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
// Do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
}
|
||||
|
||||
// Assume it is not DHT11
|
||||
// CONVERT AND STORE
|
||||
DHT_DEBUG("DHTxx method\n");
|
||||
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1;
|
||||
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1;
|
||||
if (dht_bytes[2] & 0x80) // negative dht_temperature
|
||||
{
|
||||
dht_temperature = -dht_temperature;
|
||||
}
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
|
||||
if (dht_bytes[4] != sum)
|
||||
{
|
||||
return DHTLIB_ERROR_CHECKSUM;
|
||||
}
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
|
@ -82,13 +151,13 @@ int dht_read11(uint8_t pin)
|
|||
}
|
||||
|
||||
// CONVERT AND STORE
|
||||
dht_humidity = dht_bits[0]; // dht_bits[1] == 0;
|
||||
dht_temperature = dht_bits[2]; // dht_bits[3] == 0;
|
||||
dht_humidity = dht_bytes[0]; // dht_bytes[1] == 0;
|
||||
dht_temperature = dht_bytes[2]; // dht_bytes[3] == 0;
|
||||
|
||||
// TEST CHECKSUM
|
||||
// dht_bits[1] && dht_bits[3] both 0
|
||||
uint8_t sum = dht_bits[0] + dht_bits[2];
|
||||
if (dht_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
// dht_bytes[1] && dht_bytes[3] both 0
|
||||
uint8_t sum = dht_bytes[0] + dht_bytes[2];
|
||||
if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
|
@ -110,16 +179,16 @@ int dht_read(uint8_t pin)
|
|||
}
|
||||
|
||||
// CONVERT AND STORE
|
||||
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1;
|
||||
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1;
|
||||
if (dht_bits[2] & 0x80) // negative dht_temperature
|
||||
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1;
|
||||
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1;
|
||||
if (dht_bytes[2] & 0x80) // negative dht_temperature
|
||||
{
|
||||
dht_temperature = -dht_temperature;
|
||||
}
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3];
|
||||
if (dht_bits[4] != sum)
|
||||
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
|
||||
if (dht_bytes[4] != sum)
|
||||
{
|
||||
return DHTLIB_ERROR_CHECKSUM;
|
||||
}
|
||||
|
@ -130,37 +199,25 @@ int dht_read(uint8_t pin)
|
|||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
inline int dht_read21(uint8_t pin)
|
||||
{
|
||||
return dht_read(pin);
|
||||
}
|
||||
int dht_read21(uint8_t pin) __attribute__((alias("dht_read")));
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
inline int dht_read22(uint8_t pin)
|
||||
{
|
||||
return dht_read(pin);
|
||||
}
|
||||
int dht_read22(uint8_t pin) __attribute__((alias("dht_read")));
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
inline int dht_read33(uint8_t pin)
|
||||
{
|
||||
return dht_read(pin);
|
||||
}
|
||||
int dht_read33(uint8_t pin) __attribute__((alias("dht_read")));
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
inline int dht_read44(uint8_t pin)
|
||||
{
|
||||
return dht_read(pin);
|
||||
}
|
||||
int dht_read44(uint8_t pin) __attribute__((alias("dht_read")));
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
|
@ -185,7 +242,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
|||
// volatile uint8_t *PIR = portInputRegister(port);
|
||||
|
||||
// EMPTY BUFFER
|
||||
for (i = 0; i < 5; i++) dht_bits[i] = 0;
|
||||
for (i = 0; i < 5; i++) dht_bytes[i] = 0;
|
||||
|
||||
// REQUEST SAMPLE
|
||||
// pinMode(pin, OUTPUT);
|
||||
|
@ -237,7 +294,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
|||
|
||||
if ((system_get_time() - t) > 40)
|
||||
{
|
||||
dht_bits[idx] |= mask;
|
||||
dht_bytes[idx] |= mask;
|
||||
}
|
||||
mask >>= 1;
|
||||
if (mask == 0) // next byte?
|
||||
|
|
|
@ -28,6 +28,9 @@
|
|||
|
||||
#define DHTLIB_DHT11_WAKEUP 18
|
||||
#define DHTLIB_DHT_WAKEUP 1
|
||||
#define DHTLIB_DHT_UNI_WAKEUP 18
|
||||
|
||||
#define DHT_DEBUG
|
||||
|
||||
// max timeout is 100 usec.
|
||||
// For a 16 Mhz proc 100 usec is 1600 clock cycles
|
||||
|
@ -49,13 +52,14 @@
|
|||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht_read_universal(uint8_t pin);
|
||||
int dht_read11(uint8_t pin);
|
||||
int dht_read(uint8_t pin);
|
||||
|
||||
inline int dht_read21(uint8_t pin);
|
||||
inline int dht_read22(uint8_t pin);
|
||||
inline int dht_read33(uint8_t pin);
|
||||
inline int dht_read44(uint8_t pin);
|
||||
int dht_read21(uint8_t pin);
|
||||
int dht_read22(uint8_t pin);
|
||||
int dht_read33(uint8_t pin);
|
||||
int dht_read44(uint8_t pin);
|
||||
|
||||
double dht_getHumidity(void);
|
||||
double dht_getTemperature(void);
|
||||
|
|
|
@ -16,59 +16,88 @@ int platform_dht_exists( unsigned id )
|
|||
return ((id < NUM_DHT) && (id > 0));
|
||||
}
|
||||
|
||||
// Lua: result = dht.read( id )
|
||||
// Lua: status, temp, humi, tempdec, humidec = dht.read( id )
|
||||
static int dht_lapi_read( lua_State *L )
|
||||
{
|
||||
unsigned id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( dht, id );
|
||||
lua_pushinteger( L, dht_read(id) );
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 3;
|
||||
lua_pushinteger( L, dht_read_universal(id) );
|
||||
double temp = dht_getTemperature();
|
||||
double humi = dht_getHumidity();
|
||||
int tempdec = (int)((temp - (int)temp) * 1000);
|
||||
int humidec = (int)((humi - (int)humi) * 1000);
|
||||
lua_pushnumber( L, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.read11( id )
|
||||
// Lua: status, temp, humi, tempdec, humidec = dht.read11( id ))
|
||||
static int dht_lapi_read11( lua_State *L )
|
||||
{
|
||||
unsigned id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( dht, id );
|
||||
lua_pushinteger( L, dht_read11(id) );
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 3;
|
||||
double temp = dht_getTemperature();
|
||||
double humi = dht_getHumidity();
|
||||
int tempdec = (int)((temp - (int)temp) * 1000);
|
||||
int humidec = (int)((humi - (int)humi) * 1000);
|
||||
lua_pushnumber( L, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.humidity()
|
||||
static int dht_lapi_humidity( lua_State *L )
|
||||
// Lua: status, temp, humi, tempdec, humidec = dht.readxx( id ))
|
||||
static int dht_lapi_readxx( lua_State *L )
|
||||
{
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 1;
|
||||
unsigned id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( dht, id );
|
||||
lua_pushinteger( L, dht_read(id) );
|
||||
double temp = dht_getTemperature();
|
||||
double humi = dht_getHumidity();
|
||||
int tempdec = (int)((temp - (int)temp) * 1000);
|
||||
int humidec = (int)((humi - (int)humi) * 1000);
|
||||
lua_pushnumber( L, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.humiditydecimal()
|
||||
static int dht_lapi_humiditydecimal( lua_State *L )
|
||||
{
|
||||
double value = dht_getHumidity();
|
||||
int result = (int)((value - (int)value) * 1000);
|
||||
lua_pushnumber( L, result );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.humidity()
|
||||
// static int dht_lapi_humidity( lua_State *L )
|
||||
// {
|
||||
// lua_pushnumber( L, dht_getHumidity() );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Lua: result = dht.temperature()
|
||||
static int dht_lapi_temperature( lua_State *L )
|
||||
{
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.humiditydecimal()
|
||||
// static int dht_lapi_humiditydecimal( lua_State *L )
|
||||
// {
|
||||
// double value = dht_getHumidity();
|
||||
// int result = (int)((value - (int)value) * 1000);
|
||||
// lua_pushnumber( L, result );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Lua: result = dht.temperaturedecimal()
|
||||
static int dht_lapi_temperaturedecimal( lua_State *L )
|
||||
{
|
||||
double value = dht_getTemperature();
|
||||
int result = (int)((value - (int)value) * 1000);
|
||||
lua_pushnumber( L, result );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.temperature()
|
||||
// static int dht_lapi_temperature( lua_State *L )
|
||||
// {
|
||||
// lua_pushnumber( L, dht_getTemperature() );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// // Lua: result = dht.temperaturedecimal()
|
||||
// static int dht_lapi_temperaturedecimal( lua_State *L )
|
||||
// {
|
||||
// double value = dht_getTemperature();
|
||||
// int result = (int)((value - (int)value) * 1000);
|
||||
// lua_pushnumber( L, result );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
|
@ -77,10 +106,7 @@ const LUA_REG_TYPE dht_map[] =
|
|||
{
|
||||
{ LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) },
|
||||
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
|
||||
{ LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) },
|
||||
{ LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) },
|
||||
{ LSTRKEY( "humiditydecimal" ), LFUNCVAL( dht_lapi_humiditydecimal ) },
|
||||
{ LSTRKEY( "temperaturedecimal" ), LFUNCVAL( dht_lapi_temperaturedecimal ) },
|
||||
{ LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) },
|
||||
#if LUA_OPTIMIZE_MEMORY > 0
|
||||
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
|
||||
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
|
||||
|
|
|
@ -16,13 +16,13 @@
|
|||
// -- This WS2812 code must be compiled with -O2 to get the timing right. Read this:
|
||||
// -- http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
|
||||
// -- The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct.
|
||||
static void ICACHE_FLASH_ATTR send_ws_0(uint8_t gpio) {
|
||||
static void ICACHE_FLASH_ATTR __attribute__((optimize("O2"))) send_ws_0(uint8_t gpio){
|
||||
uint8_t i;
|
||||
i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
||||
}
|
||||
|
||||
static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) {
|
||||
static void ICACHE_FLASH_ATTR __attribute__((optimize("O2"))) send_ws_1(uint8_t gpio){
|
||||
uint8_t i;
|
||||
i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
||||
i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
||||
|
|
Loading…
Reference in New Issue