Update dhtlib api, support both integer and float version.

This commit is contained in:
vowstar 2015-06-18 17:19:55 +08:00
parent 899935e60b
commit af56aea1e8
3 changed files with 30 additions and 7 deletions

View File

@ -110,8 +110,8 @@ int dht_read(uint8_t pin)
}
// CONVERT AND STORE
dht_humidity = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1;
dht_temperature = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1;
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_temperature = -dht_temperature;

View File

@ -7,6 +7,6 @@
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.6"
#define BUILD_DATE "build 20150617"
#define BUILD_DATE "build 20150618"
#endif /* __USER_VERSION_H__ */

View File

@ -22,7 +22,9 @@ 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) );
return 1;
lua_pushnumber( L, dht_getTemperature() );
lua_pushnumber( L, dht_getHumidity() );
return 3;
}
// Lua: result = dht.read11( id )
@ -31,23 +33,42 @@ 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) );
return 1;
lua_pushnumber( L, dht_getTemperature() );
lua_pushnumber( L, dht_getHumidity() );
return 3;
}
// Lua: result = dht.humidity()
static int dht_lapi_humidity( lua_State *L )
{
lua_pushinteger( L, dht_getHumidity() );
lua_pushnumber( L, dht_getHumidity() );
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.temperature()
static int dht_lapi_temperature( lua_State *L )
{
lua_pushinteger( L, dht_getTemperature() );
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
@ -58,6 +79,8 @@ const LUA_REG_TYPE dht_map[] =
{ 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 ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },