From 0caf745d8e12adbbef1c8a02b329fbd1f6f57b0e Mon Sep 17 00:00:00 2001 From: aeprox Date: Sun, 23 Aug 2015 01:42:39 +0200 Subject: [PATCH] Improve lua API interface --- app/modules/tsl2561.c | 47 ++++++++++++++++++++++++++++++++----------- app/tsl2561/tsl2561.c | 2 +- app/tsl2561/tsl2561.h | 4 ++-- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index bd3c3ea4..18e39979 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -13,7 +13,9 @@ static uint16_t ch0; static uint16_t ch1; - +/* + * Lua: error = tsl2561.init(sdapin, sclpin) + */ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { uint32_t sda; uint32_t scl; @@ -33,23 +35,44 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { lua_pushnumber( L, error ); return 1; } - +/* + * Lua: error = tsl2561.settiming(integration, gain) + */ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { // check variables if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { return luaL_error(L, "wrong arg range"); } 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); + if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))){ + return luaL_error(L, "wrong range for arg gain"); + } lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); return 1; } +/* + * Lua: lux, error = tsl2561.getlux() + */ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { - lua_pushnumber( L, tsl2561CalculateLux(ch0,ch1) ); - return 1; + uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); + 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) { uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); lua_pushnumber( L, ch0 ); @@ -64,8 +87,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { const LUA_REG_TYPE tsl2561_map[] = { { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, - { LSTRKEY( "calclux" ), LFUNCVAL( tsl2561_lua_calclux )}, - { LSTRKEY( "channels_raw" ), LFUNCVAL( tsl2561_lua_getchannels )}, + { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, + { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, @@ -74,11 +97,11 @@ const LUA_REG_TYPE tsl2561_map[] = { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, - { LSTRKEY( "TSL2561_GAIN_0X" ), LNUMVAL( TSL2561_GAIN_0X ) }, - { LSTRKEY( "TSL2561_GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, + { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, + { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, + { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, + { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X ) }, + { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, { LNILKEY, LNILVAL} }; diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index c3a2a197..9dd3d744 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -71,7 +71,7 @@ static const uint32_t tsl2561_i2c_id = 0; static bool _tsl2561Initialised = 0; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; -static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_0X; +static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; /**************************************************************************/ diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 98cb9f6f..f4b5b203 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -7,7 +7,7 @@ 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. Redistribution and use in source and binary forms, with or without @@ -137,7 +137,7 @@ tsl2561IntegrationTime_t; typedef enum { - TSL2561_GAIN_0X = 0x00, // No gain + TSL2561_GAIN_1X = 0x00, // No gain TSL2561_GAIN_16X = 0x10, // 16x gain } tsl2561Gain_t;