Commit 0caf745d authored by aeprox's avatar aeprox
Browse files

Improve lua API interface

parent 29ee02f6
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
static uint16_t ch0; static uint16_t ch0;
static uint16_t ch1; static uint16_t ch1;
/*
* Lua: error = tsl2561.init(sdapin, sclpin)
*/
static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
uint32_t sda; uint32_t sda;
uint32_t scl; uint32_t scl;
...@@ -33,23 +35,44 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { ...@@ -33,23 +35,44 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
lua_pushnumber( L, error ); lua_pushnumber( L, error );
return 1; return 1;
} }
/*
* Lua: error = tsl2561.settiming(integration, gain)
*/
static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) {
// check variables // check variables
if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
return luaL_error(L, "wrong arg range"); return luaL_error(L, "wrong arg range");
} }
uint8_t integration = luaL_checkinteger(L, 1); uint8_t integration = luaL_checkinteger(L, 1);
if(!((integration == TSL2561_INTEGRATIONTIME_13MS) ||(integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))){
return luaL_error(L, "wrong range for arg integration");
}
uint8_t gain = luaL_checkinteger(L, 2); uint8_t gain = luaL_checkinteger(L, 2);
if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))){
return luaL_error(L, "wrong range for arg gain");
}
lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); lua_pushnumber( L, tsl2561SetTiming(integration, gain) );
return 1; return 1;
} }
/*
* Lua: lux, error = tsl2561.getlux()
*/
static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) {
lua_pushnumber( L, tsl2561CalculateLux(ch0,ch1) ); uint8_t error = tsl2561GetLuminosity(&ch0,&ch1);
return 1; if (error){
lua_pushnumber(L, 0);
lua_pushnumber(L, error);
}
else{
lua_pushnumber(L, tsl2561CalculateLux(ch0,ch1));
lua_pushnumber(L, error);
}
return 2;
} }
/*
* Lua: tsl2561.getrawchannels()
*/
static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) {
uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); uint8_t error = tsl2561GetLuminosity(&ch0,&ch1);
lua_pushnumber( L, ch0 ); lua_pushnumber( L, ch0 );
...@@ -64,8 +87,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { ...@@ -64,8 +87,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) {
const LUA_REG_TYPE tsl2561_map[] = const LUA_REG_TYPE tsl2561_map[] =
{ {
{ LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)},
{ LSTRKEY( "calclux" ), LFUNCVAL( tsl2561_lua_calclux )}, { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )},
{ LSTRKEY( "channels_raw" ), LFUNCVAL( tsl2561_lua_getchannels )}, { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )},
{ LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )},
{ LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) },
...@@ -74,11 +97,11 @@ const LUA_REG_TYPE tsl2561_map[] = ...@@ -74,11 +97,11 @@ const LUA_REG_TYPE tsl2561_map[] =
{ LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) },
{ LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) },
{ LSTRKEY( "TSL2561_INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) },
{ LSTRKEY( "TSL2561_INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) },
{ LSTRKEY( "TSL2561_INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) },
{ LSTRKEY( "TSL2561_GAIN_0X" ), LNUMVAL( TSL2561_GAIN_0X ) }, { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X ) },
{ LSTRKEY( "TSL2561_GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) },
{ LNILKEY, LNILVAL} { LNILKEY, LNILVAL}
}; };
......
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
static const uint32_t tsl2561_i2c_id = 0; static const uint32_t tsl2561_i2c_id = 0;
static bool _tsl2561Initialised = 0; static bool _tsl2561Initialised = 0;
static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS;
static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_0X; static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X;
/**************************************************************************/ /**************************************************************************/
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
Software License Agreement (BSD License) Software License Agreement (BSD License)
Copyright (c) 2010, microBuilder SARL/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) Copyright (c) 2010, microBuilder SARL
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
...@@ -137,7 +137,7 @@ tsl2561IntegrationTime_t; ...@@ -137,7 +137,7 @@ tsl2561IntegrationTime_t;
typedef enum typedef enum
{ {
TSL2561_GAIN_0X = 0x00, // No gain TSL2561_GAIN_1X = 0x00, // No gain
TSL2561_GAIN_16X = 0x10, // 16x gain TSL2561_GAIN_16X = 0x10, // 16x gain
} }
tsl2561Gain_t; tsl2561Gain_t;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment