mirror of
https://github.com/nodemcu/nodemcu-firmware.git
synced 2025-01-16 20:52:57 +08:00
Update dhtlib api, support both integer and float version.
This commit is contained in:
parent
899935e60b
commit
af56aea1e8
@ -110,8 +110,8 @@ int dht_read(uint8_t pin)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CONVERT AND STORE
|
// CONVERT AND STORE
|
||||||
dht_humidity = COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1;
|
dht_humidity = (double)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_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1;
|
||||||
if (dht_bits[2] & 0x80) // negative dht_temperature
|
if (dht_bits[2] & 0x80) // negative dht_temperature
|
||||||
{
|
{
|
||||||
dht_temperature = -dht_temperature;
|
dht_temperature = -dht_temperature;
|
||||||
|
@ -7,6 +7,6 @@
|
|||||||
#define NODE_VERSION_INTERNAL 0U
|
#define NODE_VERSION_INTERNAL 0U
|
||||||
|
|
||||||
#define NODE_VERSION "NodeMCU 0.9.6"
|
#define NODE_VERSION "NodeMCU 0.9.6"
|
||||||
#define BUILD_DATE "build 20150617"
|
#define BUILD_DATE "build 20150618"
|
||||||
|
|
||||||
#endif /* __USER_VERSION_H__ */
|
#endif /* __USER_VERSION_H__ */
|
||||||
|
@ -22,7 +22,9 @@ static int dht_lapi_read( lua_State *L )
|
|||||||
unsigned id = luaL_checkinteger( L, 1 );
|
unsigned id = luaL_checkinteger( L, 1 );
|
||||||
MOD_CHECK_ID( dht, id );
|
MOD_CHECK_ID( dht, id );
|
||||||
lua_pushinteger( L, dht_read(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 )
|
// Lua: result = dht.read11( id )
|
||||||
@ -31,23 +33,42 @@ static int dht_lapi_read11( lua_State *L )
|
|||||||
unsigned id = luaL_checkinteger( L, 1 );
|
unsigned id = luaL_checkinteger( L, 1 );
|
||||||
MOD_CHECK_ID( dht, id );
|
MOD_CHECK_ID( dht, id );
|
||||||
lua_pushinteger( L, dht_read11(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()
|
// Lua: result = dht.humidity()
|
||||||
static int dht_lapi_humidity( lua_State *L )
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lua: result = dht.temperature()
|
// Lua: result = dht.temperature()
|
||||||
static int dht_lapi_temperature( lua_State *L )
|
static int dht_lapi_temperature( lua_State *L )
|
||||||
{
|
{
|
||||||
lua_pushinteger( L, dht_getTemperature() );
|
lua_pushnumber( L, dht_getTemperature() );
|
||||||
return 1;
|
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
|
// Module function map
|
||||||
#define MIN_OPT_LEVEL 2
|
#define MIN_OPT_LEVEL 2
|
||||||
@ -58,6 +79,8 @@ const LUA_REG_TYPE dht_map[] =
|
|||||||
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
|
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
|
||||||
{ LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) },
|
{ LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) },
|
||||||
{ LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) },
|
{ LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) },
|
||||||
|
{ LSTRKEY( "humiditydecimal" ), LFUNCVAL( dht_lapi_humiditydecimal ) },
|
||||||
|
{ LSTRKEY( "temperaturedecimal" ), LFUNCVAL( dht_lapi_temperaturedecimal ) },
|
||||||
#if LUA_OPTIMIZE_MEMORY > 0
|
#if LUA_OPTIMIZE_MEMORY > 0
|
||||||
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
|
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
|
||||||
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
|
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user