From 6b6456be478e8b72d7a5af2ae1f375ae8d85151e Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Sat, 12 Dec 2015 14:27:31 +1100 Subject: [PATCH 1/5] Major cleanup of module registration. As per #810 & #796, only LUA_OPTIMIZE_MEMORY=2 & MIN_OPT_LEVEL=2 are supported when building. This commit effects that limitation. With this change modules/auxmods.h no longer needs to be updated for every new module, nor do module writers need to cater for a hypothetical LUA_OPTIMIZE_MEMORY < 2 scenario. --- app/Makefile | 4 +- app/lua/lbaselib.c | 1 + app/lua/ldblib.c | 1 + app/lua/liolib.c | 1 + app/lua/lmathlib.c | 1 + app/lua/loadlib.c | 1 + app/lua/lstrlib.c | 1 + app/lua/ltablib.c | 1 + app/lua/luac_cross/loslib.c | 1 + app/modules/adc.c | 16 +----- app/modules/auxmods.h | 98 ------------------------------------- app/modules/bit.c | 9 +--- app/modules/bmp085.c | 9 +--- app/modules/cjson.c | 18 +------ app/modules/coap.c | 55 +-------------------- app/modules/crypto.c | 18 +------ app/modules/dht.c | 15 +----- app/modules/enduser_setup.c | 9 +--- app/modules/file.c | 18 +------ app/modules/gpio.c | 28 +---------- app/modules/hx711.c | 9 +--- app/modules/i2c.c | 20 +------- app/modules/mqtt.c | 35 +------------ app/modules/net.c | 60 +---------------------- app/modules/node.c | 17 +------ app/modules/ow.c | 17 +------ app/modules/pwm.c | 14 +----- app/modules/rc.c | 10 ++-- app/modules/rtcfifo.c | 8 +-- app/modules/rtcmem.c | 8 +-- app/modules/rtctime.c | 8 +-- app/modules/sntp.c | 8 +-- app/modules/spi.c | 25 +--------- app/modules/tmr.c | 16 ------ app/modules/tsl2561.c | 9 +--- app/modules/u8g.c | 50 +------------------ app/modules/uart.c | 17 +------ app/modules/ucg.c | 50 +------------------ app/modules/wifi.c | 59 +--------------------- app/modules/ws2801.c | 9 +--- app/modules/ws2812.c | 9 +--- 41 files changed, 52 insertions(+), 711 deletions(-) diff --git a/app/Makefile b/app/Makefile index bc0bfa9e..62819e9d 100644 --- a/app/Makefile +++ b/app/Makefile @@ -134,9 +134,11 @@ DEPENDS_eagle.app.v6 = \ # -DWLAN_CONFIG_CCX CONFIGURATION_DEFINES = -D__ets__ \ -DICACHE_FLASH \ + -DLUA_OPTIMIZE_MEMORY=2 \ + -DMIN_OPT_LEVEL=2 \ -DLWIP_OPEN_SRC \ -DPBUF_RSV_FOR_WLAN \ - -DEBUF_LWIP + -DEBUF_LWIP \ DEFINES += \ $(UNIVERSAL_TARGET_DEFINES) \ diff --git a/app/lua/lbaselib.c b/app/lua/lbaselib.c index 7c05a6ea..02dad463 100644 --- a/app/lua/lbaselib.c +++ b/app/lua/lbaselib.c @@ -489,6 +489,7 @@ static int luaB_newproxy (lua_State *L) { {LSTRKEY("xpcall"), LFUNCVAL(luaB_xpcall)} #if LUA_OPTIMIZE_MEMORY == 2 +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 2 #include "lrodefs.h" const LUA_REG_TYPE base_funcs_list[] = { diff --git a/app/lua/ldblib.c b/app/lua/ldblib.c index c9eaa644..10978970 100644 --- a/app/lua/ldblib.c +++ b/app/lua/ldblib.c @@ -383,6 +383,7 @@ static int db_errorfb (lua_State *L) { return 1; } +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE dblib[] = { diff --git a/app/lua/liolib.c b/app/lua/liolib.c index b6e377ff..654dd6fa 100644 --- a/app/lua/liolib.c +++ b/app/lua/liolib.c @@ -538,6 +538,7 @@ static int f_flush (lua_State *L) { return pushresult(L, fs_flush(tofile(L)) == 0, NULL); } +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 2 #include "lrodefs.h" #if LUA_OPTIMIZE_MEMORY == 2 diff --git a/app/lua/lmathlib.c b/app/lua/lmathlib.c index 42ab2b8a..e3fda445 100644 --- a/app/lua/lmathlib.c +++ b/app/lua/lmathlib.c @@ -311,6 +311,7 @@ static int math_randomseed (lua_State *L) { +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE math_map[] = { diff --git a/app/lua/loadlib.c b/app/lua/loadlib.c index 00e8f8dd..6a0aaf17 100644 --- a/app/lua/loadlib.c +++ b/app/lua/loadlib.c @@ -646,6 +646,7 @@ static const lua_CFunction loaders[] = {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; #if LUA_OPTIMIZE_MEMORY > 0 +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE lmt[] = { diff --git a/app/lua/lstrlib.c b/app/lua/lstrlib.c index 58f702ac..4271c282 100644 --- a/app/lua/lstrlib.c +++ b/app/lua/lstrlib.c @@ -825,6 +825,7 @@ static int str_format (lua_State *L) { return 1; } +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE strlib[] = { diff --git a/app/lua/ltablib.c b/app/lua/ltablib.c index a7182b00..8b0a810b 100644 --- a/app/lua/ltablib.c +++ b/app/lua/ltablib.c @@ -266,6 +266,7 @@ static int sort (lua_State *L) { /* }====================================================== */ +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE tab_funcs[] = { diff --git a/app/lua/luac_cross/loslib.c b/app/lua/luac_cross/loslib.c index 5bff693b..a35d2941 100644 --- a/app/lua/luac_cross/loslib.c +++ b/app/lua/luac_cross/loslib.c @@ -221,6 +221,7 @@ static int os_exit (lua_State *L) { c_exit(luaL_optint(L, 1, EXIT_SUCCESS)); } +#undef MIN_OPT_LEVEL #define MIN_OPT_LEVEL 1 #include "lrodefs.h" const LUA_REG_TYPE syslib[] = { diff --git a/app/modules/adc.c b/app/modules/adc.c index 2c646870..a0d24870 100644 --- a/app/modules/adc.c +++ b/app/modules/adc.c @@ -1,11 +1,9 @@ // Module for interfacing with adc -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" #include "user_interface.h" @@ -28,26 +26,14 @@ static int adc_readvdd33( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE adc_map[] = { { LSTRKEY( "read" ), LFUNCVAL( adc_sample ) }, { LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_adc( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_ADC, adc_map ); - // Add constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/auxmods.h b/app/modules/auxmods.h index 602da99a..12a67f41 100644 --- a/app/modules/auxmods.h +++ b/app/modules/auxmods.h @@ -5,95 +5,6 @@ #ifndef __AUXMODS_H__ #define __AUXMODS_H__ -#include "lua.h" - -#define AUXLIB_GPIO "gpio" -LUALIB_API int ( luaopen_gpio )( lua_State *L ); - -#define AUXLIB_SPI "spi" -LUALIB_API int ( luaopen_spi )( lua_State *L ); - -#define AUXLIB_CAN "can" -LUALIB_API int ( luaopen_can )( lua_State *L ); - -#define AUXLIB_TMR "tmr" -LUALIB_API int ( luaopen_tmr )( lua_State *L ); - -#define AUXLIB_PD "pd" -LUALIB_API int ( luaopen_pd )( lua_State *L ); - -#define AUXLIB_UART "uart" -LUALIB_API int ( luaopen_uart )( lua_State *L ); - -#define AUXLIB_TERM "term" -LUALIB_API int ( luaopen_term )( lua_State *L ); - -#define AUXLIB_PWM "pwm" -LUALIB_API int ( luaopen_pwm )( lua_State *L ); - -#define AUXLIB_PACK "pack" -LUALIB_API int ( luaopen_pack )( lua_State *L ); - -#define AUXLIB_BIT "bit" -LUALIB_API int ( luaopen_bit )( lua_State *L ); - -#define AUXLIB_NET "net" -LUALIB_API int ( luaopen_net )( lua_State *L ); - -#define AUXLIB_CPU "cpu" -LUALIB_API int ( luaopen_cpu )( lua_State* L ); - -#define AUXLIB_ADC "adc" -LUALIB_API int ( luaopen_adc )( lua_State *L ); - -#define AUXLIB_RPC "rpc" -LUALIB_API int ( luaopen_rpc )( lua_State *L ); - -#define AUXLIB_BITARRAY "bitarray" -LUALIB_API int ( luaopen_bitarray )( lua_State *L ); - -#define AUXLIB_ELUA "elua" -LUALIB_API int ( luaopen_elua )( lua_State *L ); - -#define AUXLIB_I2C "i2c" -LUALIB_API int ( luaopen_i2c )( lua_State *L ); - -#define AUXLIB_WIFI "wifi" -LUALIB_API int ( luaopen_wifi )( lua_State *L ); - -#define AUXLIB_COAP "coap" -LUALIB_API int ( luaopen_coap )( lua_State *L ); - -#define AUXLIB_MQTT "mqtt" -LUALIB_API int ( luaopen_mqtt )( lua_State *L ); - -#define AUXLIB_U8G "u8g" -LUALIB_API int ( luaopen_u8g )( lua_State *L ); - -#define AUXLIB_UCG "ucg" -LUALIB_API int ( luaopen_ucg )( lua_State *L ); - -#define AUXLIB_NODE "node" -LUALIB_API int ( luaopen_node )( lua_State *L ); - -#define AUXLIB_FILE "file" -LUALIB_API int ( luaopen_file )( lua_State *L ); - -#define AUXLIB_OW "ow" -LUALIB_API int ( luaopen_ow )( lua_State *L ); - -#define AUXLIB_CJSON "cjson" -LUALIB_API int ( luaopen_cjson )( lua_State *L ); - -#define AUXLIB_CRYPTO "crypto" -LUALIB_API int ( luaopen_crypto )( lua_State *L ); - -#define AUXLIB_RC "rc" -LUALIB_API int ( luaopen_rc )( lua_State *L ); - -#define AUXLIB_DHT "dht" -LUALIB_API int ( luaopen_dht )( lua_State *L ); - // Helper macros #define MOD_CHECK_ID( mod, id )\ if( !platform_ ## mod ## _exists( id ) )\ @@ -109,13 +20,4 @@ LUALIB_API int ( luaopen_dht )( lua_State *L ); if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\ return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id ) -#define MOD_REG_NUMBER( L, name, val )\ - lua_pushnumber( L, val );\ - lua_setfield( L, -2, name ) - -#define MOD_REG_LUDATA( L, name, val )\ - lua_pushlightuserdata( L, val );\ - lua_setfield( L, -2, name ) - #endif - diff --git a/app/modules/bit.c b/app/modules/bit.c index ac2d19ba..31ae96d3 100644 --- a/app/modules/bit.c +++ b/app/modules/bit.c @@ -7,11 +7,8 @@ #include "c_limits.h" -//#include "lua.h" #include "lauxlib.h" -#include "auxmods.h" -// #include "type.h" -#include "lrotable.h" +#include "lrodefs.h" /* FIXME: Assume size_t is an unsigned lua_Integer */ typedef size_t lua_UInteger; @@ -122,8 +119,6 @@ static int bit_clear( lua_State* L ) return 1; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE bit_map[] = { { LSTRKEY( "bnot" ), LFUNCVAL( bit_bnot ) }, { LSTRKEY( "band" ), LFUNCVAL( bit_band ) }, @@ -141,5 +136,5 @@ const LUA_REG_TYPE bit_map[] = { }; LUALIB_API int luaopen_bit (lua_State *L) { - LREGISTER( L, "bit", bit_map ); + return 0; } diff --git a/app/modules/bmp085.c b/app/modules/bmp085.c index 31b7d6af..d75379e8 100644 --- a/app/modules/bmp085.c +++ b/app/modules/bmp085.c @@ -1,8 +1,6 @@ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" @@ -185,8 +183,6 @@ static int ICACHE_FLASH_ATTR bmp085_lua_pressure(lua_State* L) { return 1; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE bmp085_map[] = { { LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )}, @@ -197,7 +193,6 @@ const LUA_REG_TYPE bmp085_map[] = }; LUALIB_API int luaopen_bmp085(lua_State *L) { - LREGISTER(L, "bmp085", bmp085_map); - return 1; + return 0; } diff --git a/app/modules/cjson.c b/app/modules/cjson.c index 2fb10c6c..73a49dbf 100644 --- a/app/modules/cjson.c +++ b/app/modules/cjson.c @@ -40,8 +40,8 @@ #include "c_string.h" #include "c_math.h" #include "c_limits.h" -#include "lua.h" #include "lauxlib.h" +#include "lrodefs.h" #include "flash_api.h" #include "strbuf.h" @@ -1531,8 +1531,6 @@ static int json_protect_conversion(lua_State *l) #endif // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE cjson_map[] = { { LSTRKEY( "encode" ), LFUNCVAL( json_encode ) }, @@ -1545,9 +1543,6 @@ const LUA_REG_TYPE cjson_map[] = // { LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) }, // { LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) }, // { LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; @@ -1560,18 +1555,7 @@ LUALIB_API int luaopen_cjson( lua_State *L ) if(-1==cfg_init(&_cfg)){ return luaL_error(L, "BUG: Unable to init config for cjson");; } -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_CJSON, cjson_map ); - // Add constants - /* Set cjson.null */ - lua_pushlightuserdata(l, NULL); - lua_setfield(l, -2, "null"); - - /* Return cjson table */ - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } #if 0 diff --git a/app/modules/coap.c b/app/modules/coap.c index 72502ebc..6da5cb4f 100644 --- a/app/modules/coap.c +++ b/app/modules/coap.c @@ -1,11 +1,8 @@ // Module for coapwork -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -561,8 +558,6 @@ static int coap_client_delete( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" static const LUA_REG_TYPE coap_server_map[] = { { LSTRKEY( "listen" ), LFUNCVAL ( coap_server_listen ) }, @@ -570,9 +565,7 @@ static const LUA_REG_TYPE coap_server_map[] = { LSTRKEY( "var" ), LFUNCVAL ( coap_server_var ) }, { LSTRKEY( "func" ), LFUNCVAL ( coap_server_func ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( coap_server_delete ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( coap_server_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -583,9 +576,7 @@ static const LUA_REG_TYPE coap_client_map[] = { LSTRKEY( "put" ), LFUNCVAL ( coap_client_put ) }, { LSTRKEY( "delete" ), LFUNCVAL ( coap_client_delete ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( coap_client_gcdelete ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( coap_client_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -593,7 +584,6 @@ const LUA_REG_TYPE coap_map[] = { { LSTRKEY( "Server" ), LFUNCVAL ( coap_createServer ) }, { LSTRKEY( "Client" ), LFUNCVAL ( coap_createClient ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) }, { LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) }, { LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) }, @@ -604,56 +594,13 @@ const LUA_REG_TYPE coap_map[] = { LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) }, { LSTRKEY( "__metatable" ), LROVAL( coap_map ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_coap( lua_State *L ) { endpoint_setup(); -#if LUA_OPTIMIZE_MEMORY > 0 luaL_rometatable(L, "coap_server", (void *)coap_server_map); // create metatable for coap_server luaL_rometatable(L, "coap_client", (void *)coap_client_map); // create metatable for coap_client return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - int n; - luaL_register( L, AUXLIB_COAP, coap_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - MOD_REG_NUMBER( L, "CON", COAP_TYPE_CON ); - MOD_REG_NUMBER( L, "NON", COAP_TYPE_NONCON ); - MOD_REG_NUMBER( L, "TEXT_PLAIN", COAP_CONTENTTYPE_TEXT_PLAIN ); - MOD_REG_NUMBER( L, "LINKFORMAT", COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ); - MOD_REG_NUMBER( L, "XML", COAP_CONTENTTYPE_APPLICATION_XML); - MOD_REG_NUMBER( L, "OCTET_STREAM", COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM); - MOD_REG_NUMBER( L, "EXI", COAP_CONTENTTYPE_APPLICATION_EXI); - MOD_REG_NUMBER( L, "JSON", COAP_CONTENTTYPE_APPLICATION_JSON); - - n = lua_gettop(L); - - // create metatable - luaL_newmetatable(L, "coap_server"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, coap_server_map ); - - lua_settop(L, n); - // create metatable - luaL_newmetatable(L, "coap_client"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, coap_client_map ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/crypto.c b/app/modules/crypto.c index 3639e436..7557dd14 100644 --- a/app/modules/crypto.c +++ b/app/modules/crypto.c @@ -1,11 +1,8 @@ // Module for cryptography -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" #include "c_stdlib.h" #include "../crypto/digests.h" @@ -153,8 +150,6 @@ static int crypto_lhmac (lua_State *L) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE crypto_map[] = { { LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) }, @@ -163,21 +158,10 @@ const LUA_REG_TYPE crypto_map[] = { LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) }, { LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) }, { LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) }, - -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_crypto( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_CRYPTO, crypto_map ); - // Add constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/dht.c b/app/modules/dht.c index fcbf5fdb..3d7067ec 100644 --- a/app/modules/dht.c +++ b/app/modules/dht.c @@ -1,9 +1,8 @@ // Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44). -#include "lualib.h" #include "lauxlib.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "cpu_esp8266.h" #include "dht.h" @@ -100,30 +99,18 @@ static int dht_lapi_readxx( lua_State *L ) // } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE dht_map[] = { { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_dht( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_DHT, dht_map ); - - // Add the constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/enduser_setup.c b/app/modules/enduser_setup.c index 19825284..d1cb83b5 100644 --- a/app/modules/enduser_setup.c +++ b/app/modules/enduser_setup.c @@ -32,11 +32,9 @@ */ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -929,8 +927,6 @@ static int enduser_setup_stop(lua_State* L) } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE enduser_setup_map[] = { { LSTRKEY( "start" ), LFUNCVAL( enduser_setup_start )}, @@ -939,7 +935,6 @@ const LUA_REG_TYPE enduser_setup_map[] = }; LUALIB_API int luaopen_enduser_setup(lua_State *L) { - LREGISTER(L, "enduser_setup", enduser_setup_map); - return 1; + return 0; } diff --git a/app/modules/file.c b/app/modules/file.c index b4a9cfa0..e41e4841 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -1,11 +1,8 @@ // Module for interfacing with file system -#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" #include "flash_fs.h" @@ -299,8 +296,6 @@ static int file_writeline( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE file_map[] = { { LSTRKEY( "list" ), LFUNCVAL( file_list ) }, @@ -319,22 +314,11 @@ const LUA_REG_TYPE file_map[] = // { LSTRKEY( "check" ), LFUNCVAL( file_check ) }, { LSTRKEY( "rename" ), LFUNCVAL( file_rename ) }, { LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) }, -#endif - -#if LUA_OPTIMIZE_MEMORY > 0 - #endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_file( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_FILE, file_map ); - // Add constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/gpio.c b/app/modules/gpio.c index 69875050..49b1507c 100644 --- a/app/modules/gpio.c +++ b/app/modules/gpio.c @@ -1,11 +1,9 @@ // Module for interfacing with GPIO -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" -#include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "platform.h" +#include "lrodefs.h" #include "c_types.h" #include "c_string.h" @@ -223,8 +221,6 @@ static int lgpio_serout( lua_State* L ) #undef DELAY_TABLE_MAX_LEN // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE gpio_map[] = { { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) }, @@ -233,9 +229,6 @@ const LUA_REG_TYPE gpio_map[] = { LSTRKEY( "serout" ), LFUNCVAL( lgpio_serout ) }, #ifdef GPIO_INTERRUPT_ENABLE { LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) }, -#endif -#if LUA_OPTIMIZE_MEMORY > 0 -#ifdef GPIO_INTERRUPT_ENABLE { LSTRKEY( "INT" ), LNUMVAL( INTERRUPT ) }, #endif { LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) }, @@ -244,7 +237,6 @@ const LUA_REG_TYPE gpio_map[] = { LSTRKEY( "LOW" ), LNUMVAL( LOW ) }, { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) }, { LSTRKEY( "PULLUP" ), LNUMVAL( PULLUP ) }, -#endif { LNILKEY, LNILVAL } }; @@ -257,21 +249,5 @@ LUALIB_API int luaopen_gpio( lua_State *L ) } platform_gpio_init(gpio_intr_callback); #endif - -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_GPIO, gpio_map ); - // Add constants -#ifdef GPIO_INTERRUPT_ENABLE - MOD_REG_NUMBER( L, "INT", INTERRUPT ); -#endif - MOD_REG_NUMBER( L, "OUTPUT", OUTPUT ); - MOD_REG_NUMBER( L, "INPUT", INPUT ); - MOD_REG_NUMBER( L, "HIGH", HIGH ); - MOD_REG_NUMBER( L, "LOW", LOW ); - MOD_REG_NUMBER( L, "FLOAT", FLOAT ); - MOD_REG_NUMBER( L, "PULLUP", PULLUP ); - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/hx711.c b/app/modules/hx711.c index e172643b..37e59ad7 100644 --- a/app/modules/hx711.c +++ b/app/modules/hx711.c @@ -1,11 +1,10 @@ // Module for HX711 load cell amplifier // https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -67,8 +66,6 @@ static int ICACHE_FLASH_ATTR hx711_read(lua_State* L) { return 1; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE hx711_map[] = { { LSTRKEY( "init" ), LFUNCVAL( hx711_init )}, @@ -77,8 +74,6 @@ const LUA_REG_TYPE hx711_map[] = }; LUALIB_API int luaopen_hx711(lua_State *L) { - // TODO: the below todo was inherited from the ws2812 code but is still valid. // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "hx711", hx711_map); - return 1; + return 0; } diff --git a/app/modules/i2c.c b/app/modules/i2c.c index c231aeec..0b241d2d 100644 --- a/app/modules/i2c.c +++ b/app/modules/i2c.c @@ -1,11 +1,9 @@ // Module for interfacing with the I2C interface -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" // Lua: speed = i2c.setup( id, sda, scl, speed ) static int i2c_setup( lua_State *L ) @@ -143,8 +141,6 @@ static int i2c_read( lua_State *L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE i2c_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( i2c_setup ) }, @@ -153,29 +149,15 @@ const LUA_REG_TYPE i2c_map[] = { LSTRKEY( "address" ), LFUNCVAL( i2c_address ) }, { LSTRKEY( "write" ), LFUNCVAL( i2c_write ) }, { LSTRKEY( "read" ), LFUNCVAL( i2c_read ) }, -#if LUA_OPTIMIZE_MEMORY > 0 // { LSTRKEY( "FAST" ), LNUMVAL( PLATFORM_I2C_SPEED_FAST ) }, { LSTRKEY( "SLOW" ), LNUMVAL( PLATFORM_I2C_SPEED_SLOW ) }, { LSTRKEY( "TRANSMITTER" ), LNUMVAL( PLATFORM_I2C_DIRECTION_TRANSMITTER ) }, { LSTRKEY( "RECEIVER" ), LNUMVAL( PLATFORM_I2C_DIRECTION_RECEIVER ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_i2c( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_I2C, i2c_map ); - - // Add the stop bits and parity constants (for i2c.setup) - // MOD_REG_NUMBER( L, "FAST", PLATFORM_I2C_SPEED_FAST ); - MOD_REG_NUMBER( L, "SLOW", PLATFORM_I2C_SPEED_SLOW ); - MOD_REG_NUMBER( L, "TRANSMITTER", PLATFORM_I2C_DIRECTION_TRANSMITTER ); - MOD_REG_NUMBER( L, "RECEIVER", PLATFORM_I2C_DIRECTION_RECEIVER ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/mqtt.c b/app/modules/mqtt.c index 0741cb6d..1cf595a9 100644 --- a/app/modules/mqtt.c +++ b/app/modules/mqtt.c @@ -1,11 +1,9 @@ // Module for mqtt -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -1392,9 +1390,6 @@ static int mqtt_socket_lwt( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" - static const LUA_REG_TYPE mqtt_socket_map[] = { { LSTRKEY( "connect" ), LFUNCVAL ( mqtt_socket_connect ) }, @@ -1404,47 +1399,19 @@ static const LUA_REG_TYPE mqtt_socket_map[] = { LSTRKEY( "lwt" ), LFUNCVAL ( mqtt_socket_lwt ) }, { LSTRKEY( "on" ), LFUNCVAL ( mqtt_socket_on ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( mqtt_delete ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( mqtt_socket_map ) }, -#endif { LNILKEY, LNILVAL } }; const LUA_REG_TYPE mqtt_map[] = { { LSTRKEY( "Client" ), LFUNCVAL ( mqtt_socket_client ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - { LSTRKEY( "__metatable" ), LROVAL( mqtt_map ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_mqtt( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 luaL_rometatable(L, "mqtt.socket", (void *)mqtt_socket_map); // create metatable for mqtt.socket return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - int n; - luaL_register( L, AUXLIB_MQTT, mqtt_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - // MOD_REG_NUMBER( L, "TCP", TCP ); - - // create metatable - luaL_newmetatable(L, "mqtt.socket"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, mqtt_socket_map ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/net.c b/app/modules/net.c index 73afe2b3..6aeed291 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1,11 +1,9 @@ // Module for network -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -1508,8 +1506,6 @@ static int expose_array(lua_State* L, char *array, unsigned short len) { #endif // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" static const LUA_REG_TYPE net_server_map[] = { { LSTRKEY( "listen" ), LFUNCVAL ( net_server_listen ) }, @@ -1518,9 +1514,7 @@ static const LUA_REG_TYPE net_server_map[] = { LSTRKEY( "send" ), LFUNCVAL ( net_udpserver_send ) }, // { LSTRKEY( "delete" ), LFUNCVAL ( net_server_delete ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( net_server_delete ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( net_server_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -1536,9 +1530,7 @@ static const LUA_REG_TYPE net_socket_map[] = { LSTRKEY( "getpeer" ), LFUNCVAL ( net_socket_getpeer ) }, // { LSTRKEY( "delete" ), LFUNCVAL ( net_socket_delete ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( net_socket_delete ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( net_socket_map ) }, -#endif { LNILKEY, LNILVAL } }; #if 0 @@ -1564,13 +1556,11 @@ const LUA_REG_TYPE net_map[] = { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) }, { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "dns" ), LROVAL( net_dns_map ) }, { LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, { LSTRKEY( "UDP" ), LNUMVAL( UDP ) }, { LSTRKEY( "__metatable" ), LROVAL( net_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -1582,58 +1572,10 @@ LUALIB_API int luaopen_net( lua_State *L ) socket[i] = LUA_NOREF; } -#if LUA_OPTIMIZE_MEMORY > 0 luaL_rometatable(L, "net.server", (void *)net_server_map); // create metatable for net.server luaL_rometatable(L, "net.socket", (void *)net_socket_map); // create metatable for net.socket #if 0 luaL_rometatable(L, "net.array", (void *)net_array_map); // create metatable for net.array #endif return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - int n; - luaL_register( L, AUXLIB_NET, net_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - MOD_REG_NUMBER( L, "TCP", TCP ); - MOD_REG_NUMBER( L, "UDP", UDP ); - - n = lua_gettop(L); - - // create metatable - luaL_newmetatable(L, "net.server"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, net_server_map ); - - lua_settop(L, n); - // create metatable - luaL_newmetatable(L, "net.socket"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, net_socket_map ); -#if 0 - lua_settop(L, n); - // create metatable - luaL_newmetatable(L, "net.array"); - // Setup the methods inside metatable - luaL_register( L, NULL, net_array_map ); -#endif - - lua_settop(L, n); - lua_newtable( L ); - luaL_register( L, NULL, net_dns_map ); - lua_setfield( L, -2, "dns" ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/node.c b/app/modules/node.c index d3154746..2b4869c9 100644 --- a/app/modules/node.c +++ b/app/modules/node.c @@ -1,6 +1,5 @@ // Module for interfacing with system -#include "lua.h" #include "lauxlib.h" #include "ldebug.h" @@ -15,14 +14,12 @@ #include "lundump.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" #include "romfs.h" #include "c_string.h" #include "driver/uart.h" -//#include "spi_flash.h" #include "user_interface.h" #include "flash_api.h" #include "flash_fs.h" @@ -550,8 +547,6 @@ static int node_stripdebug (lua_State *L) { #endif // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE node_map[] = { { LSTRKEY( "restart" ), LFUNCVAL( node_restart ) }, @@ -581,20 +576,10 @@ const LUA_REG_TYPE node_map[] = // Combined to dsleep(us, option) // { LSTRKEY( "dsleepsetoption" ), LFUNCVAL( node_deepsleep_setoption) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_node( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_NODE, node_map ); - // Add constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/ow.c b/app/modules/ow.c index c9cbfa1a..be92bb6b 100644 --- a/app/modules/ow.c +++ b/app/modules/ow.c @@ -1,10 +1,8 @@ // Module for interfacing with the OneWire interface -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "driver/onewire.h" // Lua: ow.setup( id ) @@ -282,8 +280,6 @@ static int ow_crc16( lua_State *L ) #endif // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE ow_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( ow_setup ) }, @@ -306,22 +302,11 @@ const LUA_REG_TYPE ow_map[] = { LSTRKEY( "check_crc16" ), LFUNCVAL( ow_check_crc16 ) }, { LSTRKEY( "crc16" ), LFUNCVAL( ow_crc16 ) }, #endif -#endif -#if LUA_OPTIMIZE_MEMORY > 0 - #endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_ow( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_OW, ow_map ); - - // Add the constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/pwm.c b/app/modules/pwm.c index fbf29fe4..b88a2dd8 100644 --- a/app/modules/pwm.c +++ b/app/modules/pwm.c @@ -1,11 +1,9 @@ // Module for interfacing with PWM -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" @@ -125,8 +123,6 @@ static int lpwm_getduty( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE pwm_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( lpwm_setup ) }, @@ -137,18 +133,10 @@ const LUA_REG_TYPE pwm_map[] = { LSTRKEY( "getclock" ), LFUNCVAL( lpwm_getclock ) }, { LSTRKEY( "setduty" ), LFUNCVAL( lpwm_setduty ) }, { LSTRKEY( "getduty" ), LFUNCVAL( lpwm_getduty ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_pwm( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_PWM, pwm_map ); - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/rc.c b/app/modules/rc.c index 964896c4..24df4a10 100644 --- a/app/modules/rc.c +++ b/app/modules/rc.c @@ -1,8 +1,6 @@ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" //#include "driver/easygpio.h" //static Ping_Data pingA; #define defPulseLen 185 @@ -80,8 +78,7 @@ static int ICACHE_FLASH_ATTR rc_send(lua_State* L) { return 1; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" + const LUA_REG_TYPE rc_map[] = { { LSTRKEY( "send" ), LFUNCVAL( rc_send )}, @@ -91,6 +88,5 @@ const LUA_REG_TYPE rc_map[] = //LUALIB_API int luaopen_ultra(lua_State *L) { LUALIB_API int luaopen_rc(lua_State *L) { // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "rc", rc_map); - return 1; + return 0; } diff --git a/app/modules/rtcfifo.c b/app/modules/rtcfifo.c index 20492ffa..c347c635 100644 --- a/app/modules/rtcfifo.c +++ b/app/modules/rtcfifo.c @@ -1,6 +1,7 @@ // Module for RTC sample FIFO storage #include "lauxlib.h" +#include "lrodefs.h" #include "user_modules.h" #include "rtc/rtctime.h" #define RTCTIME_SLEEP_ALIGNED rtctime_deep_sleep_until_aligned_us @@ -164,8 +165,6 @@ static int rtcfifo_dsleep_until_sample (lua_State *L) #endif // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE rtcfifo_map[] = { { LSTRKEY("prepare"), LFUNCVAL(rtcfifo_prepare) }, @@ -183,10 +182,5 @@ const LUA_REG_TYPE rtcfifo_map[] = LUALIB_API int luaopen_rtcfifo (lua_State *L) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else - luaL_register (L, AUXLIB_RTCFIFO, rtcfifo_map); - return 1; -#endif } diff --git a/app/modules/rtcmem.c b/app/modules/rtcmem.c index 4baa9c68..db03e515 100644 --- a/app/modules/rtcmem.c +++ b/app/modules/rtcmem.c @@ -1,6 +1,7 @@ // Module for RTC user memory access #include "lauxlib.h" +#include "lrodefs.h" #include "rtc/rtcaccess.h" static int rtcmem_read32 (lua_State *L) @@ -40,8 +41,6 @@ static int rtcmem_write32 (lua_State *L) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE rtcmem_map[] = { { LSTRKEY("read32"), LFUNCVAL(rtcmem_read32) }, @@ -51,10 +50,5 @@ const LUA_REG_TYPE rtcmem_map[] = LUALIB_API int luaopen_rtcmem (lua_State *L) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else - luaL_register (L, AUXLIB_RTCMEM, rtcmem_map); - return 1; -#endif } diff --git a/app/modules/rtctime.c b/app/modules/rtctime.c index 2ee298dd..fdb223ac 100644 --- a/app/modules/rtctime.c +++ b/app/modules/rtctime.c @@ -1,6 +1,7 @@ // Module for RTC time keeping #include "lauxlib.h" +#include "lrodefs.h" #include "rtc/rtctime_internal.h" #include "rtc/rtctime.h" @@ -115,8 +116,6 @@ static int rtctime_dsleep_aligned (lua_State *L) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE rtctime_map[] = { { LSTRKEY("set"), LFUNCVAL(rtctime_set) }, @@ -128,10 +127,5 @@ const LUA_REG_TYPE rtctime_map[] = LUALIB_API int luaopen_rtctime (lua_State *L) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else - luaL_register (L, AUXLIB_RTCTIME, rtctime_map); - return 1; -#endif } diff --git a/app/modules/sntp.c b/app/modules/sntp.c index 0f08301c..3ae5e1a9 100644 --- a/app/modules/sntp.c +++ b/app/modules/sntp.c @@ -34,6 +34,7 @@ // Module for Simple Network Time Protocol (SNTP) #include "lauxlib.h" +#include "lrodefs.h" #include "os_type.h" #include "osapi.h" #include "lwip/udp.h" @@ -375,8 +376,6 @@ error: // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE sntp_map[] = { { LSTRKEY("sync"), LFUNCVAL(sntp_sync) }, @@ -385,10 +384,5 @@ const LUA_REG_TYPE sntp_map[] = LUALIB_API int luaopen_sntp (lua_State *L) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else - luaL_register (L, AUXLIB_SNTP, sntp_map); - return 1; -#endif } diff --git a/app/modules/spi.c b/app/modules/spi.c index 6074ec82..40e9ee53 100644 --- a/app/modules/spi.c +++ b/app/modules/spi.c @@ -1,11 +1,9 @@ // Module for interfacing with the SPI interface -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #define SPI_HALFDUPLEX 0 #define SPI_FULLDUPLEX 1 @@ -309,8 +307,6 @@ static int spi_transaction( lua_State *L ) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE spi_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( spi_setup ) }, @@ -319,7 +315,6 @@ const LUA_REG_TYPE spi_map[] = { LSTRKEY( "set_mosi" ), LFUNCVAL( spi_set_mosi ) }, { LSTRKEY( "get_miso" ), LFUNCVAL( spi_get_miso ) }, { LSTRKEY( "transaction" ), LFUNCVAL( spi_transaction ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "MASTER" ), LNUMVAL( PLATFORM_SPI_MASTER ) }, { LSTRKEY( "SLAVE" ), LNUMVAL( PLATFORM_SPI_SLAVE) }, { LSTRKEY( "CPHA_LOW" ), LNUMVAL( PLATFORM_SPI_CPHA_LOW) }, @@ -329,28 +324,10 @@ const LUA_REG_TYPE spi_map[] = { LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) }, { LSTRKEY( "HALFDUPLEX" ), LNUMVAL( SPI_HALFDUPLEX ) }, { LSTRKEY( "FULLDUPLEX" ), LNUMVAL( SPI_FULLDUPLEX ) }, -#endif // #if LUA_OPTIMIZE_MEMORY > 0 { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_spi( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_SPI, spi_map ); - - // Add constants - MOD_REG_NUMBER( L, "MASTER", PLATFORM_SPI_MASTER); - MOD_REG_NUMBER( L, "SLAVE", PLATFORM_SPI_SLAVE); - MOD_REG_NUMBER( L, "CPHA_LOW" , PLATFORM_SPI_CPHA_LOW); - MOD_REG_NUMBER( L, "CPHA_HIGH", PLATFORM_SPI_CPHA_HIGH); - MOD_REG_NUMBER( L, "CPOL_LOW" , PLATFORM_SPI_CPOL_LOW); - MOD_REG_NUMBER( L, "CPOL_HIGH", PLATFORM_SPI_CPOL_HIGH); - MOD_REG_NUMBER( L, "DATABITS_8", 8 ); - MOD_REG_NUMBER( L, "HALFDUPLEX", SPI_HALFDUPLEX ); - MOD_REG_NUMBER( L, "FULLDUPLEX", SPI_FULLDUPLEX ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/tmr.c b/app/modules/tmr.c index cad2e8dd..c49d5169 100755 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -48,13 +48,9 @@ tmr.softwd(int) the timer units are seconds */ -#define MIN_OPT_LEVEL 2 - -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" #include "lrodefs.h" #include "c_types.h" @@ -324,11 +320,9 @@ const LUA_REG_TYPE tmr_map[] = { { LSTRKEY( "unregister" ), LFUNCVAL ( tmr_unregister ) }, { LSTRKEY( "state" ), LFUNCVAL ( tmr_state ) }, { LSTRKEY( "interval" ), LFUNCVAL ( tmr_interval) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) }, { LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) }, { LSTRKEY( "ALARM_AUTO" ), LNUMVAL( TIMER_MODE_AUTO ) }, -#endif { LNILKEY, LNILVAL } }; @@ -344,16 +338,6 @@ LUALIB_API int luaopen_tmr( lua_State *L ){ ets_timer_setfn(&rtc_timer, rtc_callback, NULL); ets_timer_arm_new(&rtc_timer, 1000, 1, 1); -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else - luaL_register( L, AUXLIB_TMR, tmr_map ); - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - MOD_REG_NUMBER( L, "ALARM_SINGLE", TIMER_MODE_SINGLE ); - MOD_REG_NUMBER( L, "ALARM_SEMI", TIMER_MODE_SEMI ); - MOD_REG_NUMBER( L, "ALARM_AUTO", TIMER_MODE_AUTO ); - return 1; -#endif } diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index a0abbb96..6c8fd96c 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -4,11 +4,9 @@ * Created on: Aug 21, 2015 * Author: Michael Lucas (Aeprox @github) */ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "../tsl2561/tsl2561.h" static uint16_t ch0; @@ -102,8 +100,6 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { return 3; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE tsl2561_map[] = { { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, @@ -134,6 +130,5 @@ const LUA_REG_TYPE tsl2561_map[] = }; LUALIB_API int luaopen_tsl2561(lua_State *L) { - LREGISTER(L, "tsl2561", tsl2561_map); - return 1; + return 0; } diff --git a/app/modules/u8g.c b/app/modules/u8g.c index 0397e23a..32102f51 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -1,10 +1,8 @@ // Module for U8glib -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" @@ -1027,9 +1025,6 @@ U8G_DISPLAY_TABLE_SPI // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" - static const LUA_REG_TYPE lu8g_display_map[] = { { LSTRKEY( "begin" ), LFUNCVAL( lu8g_begin ) }, @@ -1083,9 +1078,7 @@ static const LUA_REG_TYPE lu8g_display_map[] = { LSTRKEY( "undoRotation" ), LFUNCVAL( lu8g_undoRotation ) }, { LSTRKEY( "undoScale" ), LFUNCVAL( lu8g_undoScale ) }, { LSTRKEY( "__gc" ), LFUNCVAL( lu8g_close_display ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( lu8g_display_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -1096,8 +1089,6 @@ const LUA_REG_TYPE lu8g_map[] = U8G_DISPLAY_TABLE_I2C U8G_DISPLAY_TABLE_SPI -#if LUA_OPTIMIZE_MEMORY > 0 - // Register fonts #undef U8G_FONT_TABLE_ENTRY #define U8G_FONT_TABLE_ENTRY(font) { LSTRKEY( #font ), LUDATA( (void *)(u8g_ ## font) ) }, @@ -1115,50 +1106,11 @@ const LUA_REG_TYPE lu8g_map[] = { LSTRKEY( "MODE_GRAY2BIT" ), LNUMVAL( U8G_MODE_GRAY2BIT ) }, { LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_u8g( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 luaL_rometatable(L, "u8g.display", (void *)lu8g_display_map); // create metatable return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - int n; - luaL_register( L, AUXLIB_U8G, lu8g_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - - // Register fonts -#undef U8G_FONT_TABLE_ENTRY -#define U8G_FONT_TABLE_ENTRY(font) MOD_REG_LUDATA( L, #font, (void *)(u8g_ ## font) ); - U8G_FONT_TABLE - - // Options for circle/ ellipse drawing - MOD_REG_NUMBER( L, "DRAW_UPPER_RIGHT", U8G_DRAW_UPPER_RIGHT ); - MOD_REG_NUMBER( L, "DRAW_UPPER_LEFT", U8G_DRAW_UPPER_LEFT ); - MOD_REG_NUMBER( L, "DRAW_LOWER_RIGHT", U8G_DRAW_LOWER_RIGHT ); - MOD_REG_NUMBER( L, "DRAW_LOWER_LEFT", U8G_DRAW_LOWER_LEFT ); - MOD_REG_NUMBER( L, "DRAW_ALL", U8G_DRAW_ALL ); - - // Display modes - MOD_REG_NUMBER( L, "MODE_BW", U8G_MODE_BW ); - MOD_REG_NUMBER( L, "MODE_GRAY2BIT", U8G_MODE_GRAY2BIT ); - - // create metatable - luaL_newmetatable(L, "u8g.display"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, u8g_display_map ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/uart.c b/app/modules/uart.c index c4fdcc6f..d5d81bd7 100755 --- a/app/modules/uart.c +++ b/app/modules/uart.c @@ -1,11 +1,9 @@ // Module for interfacing with serial -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" #include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_types.h" #include "c_string.h" @@ -158,29 +156,16 @@ static int uart_write( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE uart_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( uart_setup ) }, { LSTRKEY( "write" ), LFUNCVAL( uart_write ) }, { LSTRKEY( "on" ), LFUNCVAL( uart_on ) }, { LSTRKEY( "alt" ), LFUNCVAL( uart_alt ) }, - -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_uart( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_UART, uart_map ); - // Add constants - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/ucg.c b/app/modules/ucg.c index 3d51265f..82fc3067 100644 --- a/app/modules/ucg.c +++ b/app/modules/ucg.c @@ -1,10 +1,8 @@ // Module for Ucglib -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" @@ -876,9 +874,6 @@ UCG_DISPLAY_TABLE // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" - static const LUA_REG_TYPE lucg_display_map[] = { { LSTRKEY( "begin" ), LFUNCVAL( lucg_begin ) }, @@ -926,9 +921,7 @@ static const LUA_REG_TYPE lucg_display_map[] = { LSTRKEY( "undoScale" ), LFUNCVAL( lucg_undoScale ) }, { LSTRKEY( "__gc" ), LFUNCVAL( lucg_close_display ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( lucg_display_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -938,8 +931,6 @@ const LUA_REG_TYPE lucg_map[] = #define UCG_DISPLAY_TABLE_ENTRY(binding, device, extension) { LSTRKEY( #binding ), LFUNCVAL ( lucg_ ##binding ) }, UCG_DISPLAY_TABLE -#if LUA_OPTIMIZE_MEMORY > 0 - // Register fonts #undef UCG_FONT_TABLE_ENTRY #define UCG_FONT_TABLE_ENTRY(font) { LSTRKEY( #font ), LUDATA( (void *)(ucg_ ## font) ) }, @@ -957,50 +948,11 @@ const LUA_REG_TYPE lucg_map[] = { LSTRKEY( "DRAW_ALL" ), LNUMVAL( UCG_DRAW_ALL ) }, { LSTRKEY( "__metatable" ), LROVAL( lucg_map ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_ucg( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 luaL_rometatable(L, "ucg.display", (void *)lucg_display_map); // create metatable return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - int n; - luaL_register( L, AUXLIB_UCG, lucg_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - - // Register fonts -#undef UCG_FONT_TABLE_ENTRY -#define UCG_FONT_TABLE_ENTRY(font) MOD_REG_LUDATA( L, #font, (void *)(ucg_ ## font) ); - UCG_FONT_TABLE - - // Font modes - MOD_REG_NUMBER( L, "FONT_MODE_TRANSPARENT", UCG_FONT_MODE_TRANSPARENT ); - MOD_REG_NUMBER( L, "FONT_MODE_SOLID", UCG_FONT_MODE_SOLID ); - - // Options for circle/ disc drawing - MOD_REG_NUMBER( L, "DRAW_UPPER_RIGHT", UCG_DRAW_UPPER_RIGHT ); - MOD_REG_NUMBER( L, "DRAW_UPPER_LEFT", UCG_DRAW_UPPER_LEFT ); - MOD_REG_NUMBER( L, "DRAW_LOWER_RIGHT", UCG_DRAW_LOWER_RIGHT ); - MOD_REG_NUMBER( L, "DRAW_LOWER_LEFT", UCG_DRAW_LOWER_LEFT ); - MOD_REG_NUMBER( L, "DRAW_ALL", UCG_DRAW_ALL ); - - // create metatable - luaL_newmetatable(L, "ucg.display"); - // metatable.__index = metatable - lua_pushliteral(L, "__index"); - lua_pushvalue(L,-2); - lua_rawset(L,-3); - // Setup the methods inside metatable - luaL_register( L, NULL, lucg_display_map ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/wifi.c b/app/modules/wifi.c index 9d84ce50..2b076b8f 100644 --- a/app/modules/wifi.c +++ b/app/modules/wifi.c @@ -1,11 +1,8 @@ // Module for interfacing with WIFI -//#include "lua.h" -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -1340,8 +1337,6 @@ static int wifi_ap_dhcp_stop( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" static const LUA_REG_TYPE wifi_station_map[] = { { LSTRKEY( "getconfig" ), LFUNCVAL ( wifi_station_getconfig ) }, @@ -1380,11 +1375,9 @@ static const LUA_REG_TYPE wifi_ap_map[] = { LSTRKEY( "setmac" ), LFUNCVAL ( wifi_ap_setmac ) }, { LSTRKEY( "getclient" ), LFUNCVAL ( wifi_ap_listclient ) }, { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig ) }, -#if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) }, // { LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) }, -#endif { LNILKEY, LNILVAL } }; @@ -1399,7 +1392,7 @@ const LUA_REG_TYPE wifi_map[] = { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) }, { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) }, { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) }, -#if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) }, { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) }, @@ -1430,58 +1423,10 @@ const LUA_REG_TYPE wifi_map[] = { LSTRKEY( "STA_GOTIP" ), LNUMVAL( STATION_GOT_IP ) }, { LSTRKEY( "__metatable" ), LROVAL( wifi_map ) }, -#endif { LNILKEY, LNILVAL } }; LUALIB_API int luaopen_wifi( lua_State *L ) { -#if LUA_OPTIMIZE_MEMORY > 0 return 0; -#else // #if LUA_OPTIMIZE_MEMORY > 0 - luaL_register( L, AUXLIB_WIFI, wifi_map ); - - // Set it as its own metatable - lua_pushvalue( L, -1 ); - lua_setmetatable( L, -2 ); - - // Module constants - // MOD_REG_NUMBER( L, "NULLMODE", NULL_MODE ); - MOD_REG_NUMBER( L, "STATION", STATION_MODE ); - MOD_REG_NUMBER( L, "SOFTAP", SOFTAP_MODE ); - MOD_REG_NUMBER( L, "STATIONAP", STATIONAP_MODE ); - - MOD_REG_NUMBER( L, "NONE_SLEEP", NONE_SLEEP_T ); - MOD_REG_NUMBER( L, "LIGHT_SLEEP", LIGHT_SLEEP_T ); - MOD_REG_NUMBER( L, "MODEM_SLEEP", MODEM_SLEEP_T ); - - MOD_REG_NUMBER( L, "OPEN", AUTH_OPEN ); - // MOD_REG_NUMBER( L, "WEP", AUTH_WEP ); - MOD_REG_NUMBER( L, "WPA_PSK", AUTH_WPA_PSK ); - MOD_REG_NUMBER( L, "WPA2_PSK", AUTH_WPA2_PSK ); - MOD_REG_NUMBER( L, "WPA_WPA2_PSK", AUTH_WPA_WPA2_PSK ); - - // MOD_REG_NUMBER( L, "STA_IDLE", STATION_IDLE ); - // MOD_REG_NUMBER( L, "STA_CONNECTING", STATION_CONNECTING ); - // MOD_REG_NUMBER( L, "STA_WRONGPWD", STATION_WRONG_PASSWORD ); - // MOD_REG_NUMBER( L, "STA_APNOTFOUND", STATION_NO_AP_FOUND ); - // MOD_REG_NUMBER( L, "STA_FAIL", STATION_CONNECT_FAIL ); - // MOD_REG_NUMBER( L, "STA_GOTIP", STATION_GOT_IP ); - - // Setup the new tables (station and ap) inside wifi - lua_newtable( L ); - luaL_register( L, NULL, wifi_station_map ); - lua_setfield( L, -2, "sta" ); - - lua_newtable( L ); - luaL_register( L, NULL, wifi_ap_map ); - lua_setfield( L, -2, "ap" ); - - // Setup the new table (dhcp) inside ap - lua_newtable( L ); - luaL_register( L, NULL, wifi_ap_dhcp_map ); - lua_setfield( L, -1, "dhcp" ); - - return 1; -#endif // #if LUA_OPTIMIZE_MEMORY > 0 } diff --git a/app/modules/ws2801.c b/app/modules/ws2801.c index 6ba2199e..9fe04c86 100644 --- a/app/modules/ws2801.c +++ b/app/modules/ws2801.c @@ -1,8 +1,6 @@ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" @@ -124,8 +122,6 @@ static int ICACHE_FLASH_ATTR ws2801_writergb(lua_State* L) { return 0; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE ws2801_map[] = { { LSTRKEY( "write" ), LFUNCVAL( ws2801_writergb )}, @@ -134,7 +130,6 @@ const LUA_REG_TYPE ws2801_map[] = }; LUALIB_API int luaopen_ws2801(lua_State *L) { - LREGISTER(L, "ws2801", ws2801_map); - return 1; + return 0; } diff --git a/app/modules/ws2812.c b/app/modules/ws2812.c index 6875416d..e23a4b95 100644 --- a/app/modules/ws2812.c +++ b/app/modules/ws2812.c @@ -1,8 +1,6 @@ -#include "lualib.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" +#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -127,8 +125,6 @@ static int ICACHE_FLASH_ATTR ws2812_writegrb(lua_State* L) { return 0; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" const LUA_REG_TYPE ws2812_map[] = { { LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )}, @@ -138,6 +134,5 @@ const LUA_REG_TYPE ws2812_map[] = LUALIB_API int luaopen_ws2812(lua_State *L) { // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "ws2812", ws2812_map); - return 1; + return 0; } From b773290b8cae175e1d25e1de8319fa78e4a586de Mon Sep 17 00:00:00 2001 From: TerryE Date: Sun, 13 Dec 2015 02:29:37 +0000 Subject: [PATCH 2/5] Major cleanup of module registration pass 2. carrying on Johny's edits as per my comments on #810 --- app/modules/adc.c | 13 +-- app/modules/auxmods.h | 23 ----- app/modules/bit.c | 4 + app/modules/bmp085.c | 16 ++-- app/modules/cjson.c | 61 +++++++------- app/modules/coap.c | 53 ++++++------ app/modules/crypto.c | 20 +++-- app/modules/dht.c | 22 ++--- app/modules/enduser_setup.c | 7 +- app/modules/file.c | 39 ++++----- app/modules/gpio.c | 30 +++---- app/modules/hx711.c | 9 +- app/modules/i2c.c | 29 +++---- app/modules/mqtt.c | 29 +++---- app/modules/net.c | 85 +++++++++---------- app/modules/ow.c | 40 ++++----- app/modules/pwm.c | 23 ++--- app/modules/rc.c | 8 +- app/modules/rtcfifo.c | 7 +- app/modules/rtcmem.c | 10 ++- app/modules/rtctime.c | 16 ++-- app/modules/sntp.c | 10 ++- app/modules/spi.c | 29 +++---- app/modules/tmr.c | 36 ++++---- app/modules/tsl2561.c | 45 +++++----- app/modules/u8g.c | 162 ++++++++++++++++++------------------ app/modules/uart.c | 17 ++-- app/modules/wifi.c | 139 +++++++++++++++---------------- app/platform/platform.h | 16 ++++ 29 files changed, 515 insertions(+), 483 deletions(-) delete mode 100644 app/modules/auxmods.h diff --git a/app/modules/adc.c b/app/modules/adc.c index a0d24870..aedd16e1 100644 --- a/app/modules/adc.c +++ b/app/modules/adc.c @@ -2,7 +2,6 @@ #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" #include "lrodefs.h" #include "c_types.h" @@ -26,14 +25,16 @@ static int adc_readvdd33( lua_State* L ) } // Module function map -const LUA_REG_TYPE adc_map[] = -{ - { LSTRKEY( "read" ), LFUNCVAL( adc_sample ) }, +const LUA_REG_TYPE adc_map[] = { + { LSTRKEY( "read" ), LFUNCVAL( adc_sample ) }, { LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_adc( lua_State *L ) -{ +LUALIB_API int luaopen_adc( lua_State *L ) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/auxmods.h b/app/modules/auxmods.h deleted file mode 100644 index 12a67f41..00000000 --- a/app/modules/auxmods.h +++ /dev/null @@ -1,23 +0,0 @@ -// Auxiliary Lua modules. All of them are declared here, then each platform -// decides what module(s) to register in the src/platform/xxxxx/platform_conf.h file -// FIXME: no longer platform_conf.h - either CPU header file, or board file - -#ifndef __AUXMODS_H__ -#define __AUXMODS_H__ - -// Helper macros -#define MOD_CHECK_ID( mod, id )\ - if( !platform_ ## mod ## _exists( id ) )\ - return luaL_error( L, #mod" %d does not exist", ( unsigned )id ) - -#define MOD_CHECK_TIMER( id )\ - if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\ - return luaL_error( L, "the system timer is not available on this platform" );\ - if( !platform_timer_exists( id ) )\ - return luaL_error( L, "timer %d does not exist", ( unsigned )id )\ - -#define MOD_CHECK_RES_ID( mod, id, resmod, resid )\ - if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\ - return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id ) - -#endif diff --git a/app/modules/bit.c b/app/modules/bit.c index 31ae96d3..d6914457 100644 --- a/app/modules/bit.c +++ b/app/modules/bit.c @@ -136,5 +136,9 @@ const LUA_REG_TYPE bit_map[] = { }; LUALIB_API int luaopen_bit (lua_State *L) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/bmp085.c b/app/modules/bmp085.c index d75379e8..57a73bc1 100644 --- a/app/modules/bmp085.c +++ b/app/modules/bmp085.c @@ -183,16 +183,18 @@ static int ICACHE_FLASH_ATTR bmp085_lua_pressure(lua_State* L) { return 1; } -const LUA_REG_TYPE bmp085_map[] = -{ - { LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )}, - { LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )}, +const LUA_REG_TYPE bmp085_map[] = { + { LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )}, + { LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )}, { LSTRKEY( "pressure_raw" ), LFUNCVAL( bmp085_lua_pressure_raw )}, - { LSTRKEY( "init" ), LFUNCVAL( bmp085_init )}, + { LSTRKEY( "init" ), LFUNCVAL( bmp085_init )}, { LNILKEY, LNILVAL} }; LUALIB_API int luaopen_bmp085(lua_State *L) { - return 0; +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 + return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } - diff --git a/app/modules/cjson.c b/app/modules/cjson.c index 73a49dbf..23947a5d 100644 --- a/app/modules/cjson.c +++ b/app/modules/cjson.c @@ -1528,37 +1528,7 @@ static int json_protect_conversion(lua_State *l) * errors are memory related */ return luaL_error(l, "Memory allocation error in CJSON protected call"); } -#endif -// Module function map -const LUA_REG_TYPE cjson_map[] = -{ - { LSTRKEY( "encode" ), LFUNCVAL( json_encode ) }, - { LSTRKEY( "decode" ), LFUNCVAL( json_decode ) }, - // { LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) }, - // { LSTRKEY( "encode_max_depth" ), LFUNCVAL( json_cfg_encode_max_depth ) }, - // { LSTRKEY( "decode_max_depth" ), LFUNCVAL( json_cfg_decode_max_depth ) }, - // { LSTRKEY( "encode_number_precision" ), LFUNCVAL( json_cfg_encode_number_precision ) }, - // { LSTRKEY( "encode_keep_buffer" ), LFUNCVAL( json_cfg_encode_keep_buffer ) }, - // { LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) }, - // { LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) }, - // { LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) }, - { LNILKEY, LNILVAL } -}; - -LUALIB_API int luaopen_cjson( lua_State *L ) -{ - cjson_mem_setlua (L); - - /* Initialise number conversions */ - // fpconv_init(); // not needed for a specific cpu. - if(-1==cfg_init(&_cfg)){ - return luaL_error(L, "BUG: Unable to init config for cjson");; - } - return 0; -} - -#if 0 /* Return cjson module table */ static int lua_cjson_new(lua_State *l) { @@ -1628,5 +1598,36 @@ int luaopen_cjson_safe(lua_State *l) return 1; } #endif + +// Module function map +const LUA_REG_TYPE cjson_map[] = { + { LSTRKEY( "encode" ), LFUNCVAL( json_encode ) }, + { LSTRKEY( "decode" ), LFUNCVAL( json_decode ) }, +//{ LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) }, +//{ LSTRKEY( "encode_max_depth" ), LFUNCVAL( json_cfg_encode_max_depth ) }, +//{ LSTRKEY( "decode_max_depth" ), LFUNCVAL( json_cfg_decode_max_depth ) }, +//{ LSTRKEY( "encode_number_precision" ), LFUNCVAL( json_cfg_encode_number_precision ) }, +//{ LSTRKEY( "encode_keep_buffer" ), LFUNCVAL( json_cfg_encode_keep_buffer ) }, +//{ LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) }, +//{ LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) }, +//{ LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) }, + { LNILKEY, LNILVAL } +}; + +LUALIB_API int luaopen_cjson( lua_State *L ) +{ + cjson_mem_setlua (L); + + /* Initialise number conversions */ + // fpconv_init(); // not needed for a specific cpu. + if(-1==cfg_init(&_cfg)){ + return luaL_error(L, "BUG: Unable to init config for cjson");; + } +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 + return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif +} /* vi:ai et sw=4 ts=4: */ diff --git a/app/modules/coap.c b/app/modules/coap.c index 6da5cb4f..94d2b670 100644 --- a/app/modules/coap.c +++ b/app/modules/coap.c @@ -558,41 +558,38 @@ static int coap_client_delete( lua_State* L ) } // Module function map -static const LUA_REG_TYPE coap_server_map[] = -{ - { LSTRKEY( "listen" ), LFUNCVAL ( coap_server_listen ) }, - { LSTRKEY( "close" ), LFUNCVAL ( coap_server_close ) }, - { LSTRKEY( "var" ), LFUNCVAL ( coap_server_var ) }, - { LSTRKEY( "func" ), LFUNCVAL ( coap_server_func ) }, - { LSTRKEY( "__gc" ), LFUNCVAL ( coap_server_delete ) }, - { LSTRKEY( "__index" ), LROVAL ( coap_server_map ) }, +static const LUA_REG_TYPE coap_server_map[] = { + { LSTRKEY( "listen" ), LFUNCVAL( coap_server_listen ) }, + { LSTRKEY( "close" ), LFUNCVAL( coap_server_close ) }, + { LSTRKEY( "var" ), LFUNCVAL( coap_server_var ) }, + { LSTRKEY( "func" ), LFUNCVAL( coap_server_func ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( coap_server_delete ) }, + { LSTRKEY( "__index" ), LROVAL( coap_server_map ) }, { LNILKEY, LNILVAL } }; -static const LUA_REG_TYPE coap_client_map[] = -{ - { LSTRKEY( "get" ), LFUNCVAL ( coap_client_get ) }, - { LSTRKEY( "post" ), LFUNCVAL ( coap_client_post ) }, - { LSTRKEY( "put" ), LFUNCVAL ( coap_client_put ) }, - { LSTRKEY( "delete" ), LFUNCVAL ( coap_client_delete ) }, - { LSTRKEY( "__gc" ), LFUNCVAL ( coap_client_gcdelete ) }, - { LSTRKEY( "__index" ), LROVAL ( coap_client_map ) }, +static const LUA_REG_TYPE coap_client_map[] = { + { LSTRKEY( "get" ), LFUNCVAL( coap_client_get ) }, + { LSTRKEY( "post" ), LFUNCVAL( coap_client_post ) }, + { LSTRKEY( "put" ), LFUNCVAL( coap_client_put ) }, + { LSTRKEY( "delete" ), LFUNCVAL( coap_client_delete ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( coap_client_gcdelete ) }, + { LSTRKEY( "__index" ), LROVAL( coap_client_map ) }, { LNILKEY, LNILVAL } }; const LUA_REG_TYPE coap_map[] = { - { LSTRKEY( "Server" ), LFUNCVAL ( coap_createServer ) }, - { LSTRKEY( "Client" ), LFUNCVAL ( coap_createClient ) }, - { LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) }, - { LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) }, - { LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) }, - { LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) }, - { LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) }, + { LSTRKEY( "Server" ), LFUNCVAL( coap_createServer ) }, + { LSTRKEY( "Client" ), LFUNCVAL( coap_createClient ) }, + { LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) }, + { LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) }, + { LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) }, + { LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) }, + { LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) }, { LSTRKEY( "OCTET_STREAM"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM ) }, - { LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) }, - { LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) }, - + { LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) }, + { LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) }, { LSTRKEY( "__metatable" ), LROVAL( coap_map ) }, { LNILKEY, LNILVAL } }; @@ -602,5 +599,9 @@ LUALIB_API int luaopen_coap( lua_State *L ) endpoint_setup(); luaL_rometatable(L, "coap_server", (void *)coap_server_map); // create metatable for coap_server luaL_rometatable(L, "coap_client", (void *)coap_client_map); // create metatable for coap_client +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/crypto.c b/app/modules/crypto.c index 7557dd14..e9067495 100644 --- a/app/modules/crypto.c +++ b/app/modules/crypto.c @@ -150,18 +150,20 @@ static int crypto_lhmac (lua_State *L) // Module function map -const LUA_REG_TYPE crypto_map[] = -{ - { LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) }, +const LUA_REG_TYPE crypto_map[] = { + { LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) }, { LSTRKEY( "toBase64" ), LFUNCVAL( crypto_base64_encode ) }, - { LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) }, - { LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) }, - { LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) }, - { LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) }, + { LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) }, + { LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) }, + { LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) }, + { LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_crypto( lua_State *L ) -{ +LUALIB_API int luaopen_crypto( lua_State *L ) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/dht.c b/app/modules/dht.c index 3d7067ec..c951341f 100644 --- a/app/modules/dht.c +++ b/app/modules/dht.c @@ -1,8 +1,8 @@ // Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44). #include "lauxlib.h" -#include "auxmods.h" #include "lrodefs.h" +#include "platform.h" #include "cpu_esp8266.h" #include "dht.h" @@ -99,18 +99,20 @@ static int dht_lapi_readxx( lua_State *L ) // } // Module function map -const LUA_REG_TYPE dht_map[] = -{ - { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, - { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, - { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, - { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, +const LUA_REG_TYPE dht_map[] = { + { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, + { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, + { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, + { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, - { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, + { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_dht( lua_State *L ) -{ +LUALIB_API int luaopen_dht( lua_State *L ) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/enduser_setup.c b/app/modules/enduser_setup.c index d1cb83b5..a7b06a57 100644 --- a/app/modules/enduser_setup.c +++ b/app/modules/enduser_setup.c @@ -927,14 +927,17 @@ static int enduser_setup_stop(lua_State* L) } -const LUA_REG_TYPE enduser_setup_map[] = -{ +const LUA_REG_TYPE enduser_setup_map[] = { { LSTRKEY( "start" ), LFUNCVAL( enduser_setup_start )}, { LSTRKEY( "stop" ), LFUNCVAL( enduser_setup_stop )}, { LNILKEY, LNILVAL} }; LUALIB_API int luaopen_enduser_setup(lua_State *L) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/file.c b/app/modules/file.c index e41e4841..80f8ec54 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -296,29 +296,30 @@ static int file_writeline( lua_State* L ) } // Module function map -const LUA_REG_TYPE file_map[] = -{ - { LSTRKEY( "list" ), LFUNCVAL( file_list ) }, - { LSTRKEY( "open" ), LFUNCVAL( file_open ) }, - { LSTRKEY( "close" ), LFUNCVAL( file_close ) }, - { LSTRKEY( "write" ), LFUNCVAL( file_write ) }, +const LUA_REG_TYPE file_map[] = { + { LSTRKEY( "list" ), LFUNCVAL( file_list ) }, + { LSTRKEY( "open" ), LFUNCVAL( file_open ) }, + { LSTRKEY( "close" ), LFUNCVAL( file_close ) }, + { LSTRKEY( "write" ), LFUNCVAL( file_write ) }, { LSTRKEY( "writeline" ), LFUNCVAL( file_writeline ) }, - { LSTRKEY( "read" ), LFUNCVAL( file_read ) }, - { LSTRKEY( "readline" ), LFUNCVAL( file_readline ) }, - { LSTRKEY( "format" ), LFUNCVAL( file_format ) }, -#if defined(BUILD_WOFS) -#elif defined(BUILD_SPIFFS) - { LSTRKEY( "remove" ), LFUNCVAL( file_remove ) }, - { LSTRKEY( "seek" ), LFUNCVAL( file_seek ) }, - { LSTRKEY( "flush" ), LFUNCVAL( file_flush ) }, - // { LSTRKEY( "check" ), LFUNCVAL( file_check ) }, - { LSTRKEY( "rename" ), LFUNCVAL( file_rename ) }, - { LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) }, + { LSTRKEY( "read" ), LFUNCVAL( file_read ) }, + { LSTRKEY( "readline" ), LFUNCVAL( file_readline ) }, + { LSTRKEY( "format" ), LFUNCVAL( file_format ) }, +#if defined(BUILD_SPIFFS) && !defined(BUILD_WOFS) + { LSTRKEY( "remove" ), LFUNCVAL( file_remove ) }, + { LSTRKEY( "seek" ), LFUNCVAL( file_seek ) }, + { LSTRKEY( "flush" ), LFUNCVAL( file_flush ) }, +//{ LSTRKEY( "check" ), LFUNCVAL( file_check ) }, + { LSTRKEY( "rename" ), LFUNCVAL( file_rename ) }, + { LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) }, #endif { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_file( lua_State *L ) -{ +LUALIB_API int luaopen_file( lua_State *L ) { +#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; +#else +# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif } diff --git a/app/modules/gpio.c b/app/modules/gpio.c index 49b1507c..d19c0220 100644 --- a/app/modules/gpio.c +++ b/app/modules/gpio.c @@ -1,7 +1,6 @@ // Module for interfacing with GPIO #include "lauxlib.h" -#include "auxmods.h" #include "platform.h" #include "lrodefs.h" @@ -221,27 +220,25 @@ static int lgpio_serout( lua_State* L ) #undef DELAY_TABLE_MAX_LEN // Module function map -const LUA_REG_TYPE gpio_map[] = -{ - { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) }, - { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) }, - { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) }, +const LUA_REG_TYPE gpio_map[] = { + { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) }, + { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) }, + { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) }, { LSTRKEY( "serout" ), LFUNCVAL( lgpio_serout ) }, #ifdef GPIO_INTERRUPT_ENABLE - { LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) }, - { LSTRKEY( "INT" ), LNUMVAL( INTERRUPT ) }, + { LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) }, + { LSTRKEY( "INT" ), LNUMVAL( INTERRUPT ) }, #endif { LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) }, - { LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) }, - { LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) }, - { LSTRKEY( "LOW" ), LNUMVAL( LOW ) }, - { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) }, + { LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) }, + { LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) }, + { LSTRKEY( "LOW" ), LNUMVAL( LOW ) }, + { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) }, { LSTRKEY( "PULLUP" ), LNUMVAL( PULLUP ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_gpio( lua_State *L ) -{ +LUALIB_API int luaopen_gpio( lua_State *L ) { #ifdef GPIO_INTERRUPT_ENABLE int i; for(i=0;i Date: Wed, 16 Dec 2015 16:04:58 +1100 Subject: [PATCH 3/5] Reworked module registration, removed modules.h Module creation & registration now made a lot simpler. In essence, each module file is now self-contained and only needs a NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname); line to both be automatically recognised by the Lua initialization as well as honor the LUA_USE_MODULES_MYNAME #define. --- app/include/module.h | 74 +++++++++ app/include/user_modules.h | 5 +- app/lua/lrodefs.h | 2 +- app/modules/adc.c | 12 +- app/modules/bit.c | 12 +- app/modules/bmp085.c | 12 +- app/modules/cjson.c | 12 +- app/modules/coap.c | 12 +- app/modules/crypto.c | 12 +- app/modules/dht.c | 12 +- app/modules/enduser_setup.c | 14 +- app/modules/file.c | 12 +- app/modules/gpio.c | 13 +- app/modules/hx711.c | 12 +- app/modules/i2c.c | 13 +- app/modules/linit.c | 119 ++++----------- app/modules/modules.h | 289 ------------------------------------ app/modules/mqtt.c | 12 +- app/modules/net.c | 12 +- app/modules/node.c | 8 +- app/modules/ow.c | 12 +- app/modules/pwm.c | 13 +- app/modules/rc.c | 13 +- app/modules/rtcfifo.c | 13 +- app/modules/rtcmem.c | 12 +- app/modules/rtctime.c | 12 +- app/modules/sntp.c | 12 +- app/modules/spi.c | 12 +- app/modules/tmr.c | 13 +- app/modules/tsl2561.c | 12 +- app/modules/u8g.c | 12 +- app/modules/uart.c | 12 +- app/modules/ucg.c | 8 +- app/modules/wifi.c | 13 +- app/modules/ws2801.c | 9 +- app/modules/ws2812.c | 8 +- ld/nodemcu.ld | 10 ++ 37 files changed, 229 insertions(+), 636 deletions(-) create mode 100644 app/include/module.h delete mode 100644 app/modules/modules.h diff --git a/app/include/module.h b/app/include/module.h new file mode 100644 index 00000000..9f00a69a --- /dev/null +++ b/app/include/module.h @@ -0,0 +1,74 @@ +#ifndef __MODULE_H__ +#define __MODULE_H__ + +#include "user_modules.h" +#include "lrodefs.h" + +/* Registering a module within NodeMCU is really easy these days! + * + * Most of the work is done by a combination of pre-processor, compiler + * and linker "magic". Gone are the days of needing to update 4+ separate + * files just to register a module! + * + * You will need: + * - to include this header + * - a name for the module + * - a LUA_REG_TYPE module map + * - optionally, an init function + * + * Then simply put a line like this at the bottom of your module file: + * + * NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname); + * + * or perhaps + * + * NODEMCU_MODULE(MYNAME, "myname", myname_map, NULL); + * + * if you don't need an init function. + * + * When you've done this, the module can be enabled in user_modules.h with: + * + * #define LUA_USE_MODULES_MYNAME + * + * and within NodeMCU you access it with myname.foo(), assuming you have + * a foo function in your module. + */ + +#define MODULE_STRIFY__(x) #x +#define MODULE_STRIFY_(x) MODULE_STRIFY__(x) +#define MODULE_EXPAND_(x) x +#define MODULE_PASTE_(x,y) x##y + +/* Given an uppercase name XYZ, look at the corresponding LUA_USE_MODULES_XYZ + * and append that to the section name prefix (e.g. "lua_libs"). For an + * included module, this will yield either ".lua_libs" (for an empty #define) + * or ".lua_libs1" (if #defined to 1). The linker script will then + * pick up all items in those sections and construct an array for us. + * A non-included module ZZZ ends up in a section named + * ".lua_libsLUA_USE_MODULES_ZZZ" which gets discarded by the linker (together + * with the associated module code). + */ +#define PICK_SECTION_(pfx, name) \ + pfx MODULE_STRIFY_(MODULE_EXPAND_(LUA_USE_MODULES_ ## name)) + +#define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \ + static const __attribute__((used,unused,section(PICK_SECTION_(".lua_libs",cfgname)))) \ + luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \ + static const __attribute__((used,unused,section(PICK_SECTION_(".lua_rotable",cfgname)))) \ + luaR_table MODULE_PASTE_(lua_rotable_,cfgname) = { luaname, map } + + +/* System module registration support, not using LUA_USE_MODULES_XYZ. */ +#define BUILTIN_LIB_INIT(name, luaname, initfunc) \ + static const __attribute__((used,unused,section(".lua_libs"))) \ + luaL_Reg MODULE_PASTE_(lua_lib_,name) = { luaname, initfunc } + +#define BUILTIN_LIB(name, luaname, map) \ + static const __attribute__((used,unused,section(".lua_rotable"))) \ + luaR_table MODULE_PASTE_(lua_rotable_,name) = { luaname, map } + +#if !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2) +# error "NodeMCU modules must be built with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" +#endif + +#endif diff --git a/app/include/user_modules.h b/app/include/user_modules.h index 1a4646e2..f3bb0fee 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -12,9 +12,7 @@ #define LUA_USE_BUILTIN_DEBUG_MINIMAL // for debug.getregistry() and debug.traceback() #ifndef LUA_CROSS_COMPILER -#define LUA_USE_MODULES -#ifdef LUA_USE_MODULES #define LUA_USE_MODULES_ADC #define LUA_USE_MODULES_BIT //#define LUA_USE_MODULES_BMP085 @@ -47,7 +45,6 @@ //#define LUA_USE_MODULES_WS2801 #define LUA_USE_MODULES_WS2812 -#endif /* LUA_USE_MODULES */ -#endif +#endif /* LUA_CROSS_COMPILER */ #endif /* __USER_MODULES_H__ */ diff --git a/app/lua/lrodefs.h b/app/lua/lrodefs.h index 384be3ee..e9bbe7fe 100644 --- a/app/lua/lrodefs.h +++ b/app/lua/lrodefs.h @@ -16,7 +16,7 @@ #undef LREGISTER #if (MIN_OPT_LEVEL > 0) && (LUA_OPTIMIZE_MEMORY >= MIN_OPT_LEVEL) -#define LUA_REG_TYPE luaR_entry ICACHE_RODATA_ATTR +#define LUA_REG_TYPE luaR_entry #define LSTRKEY LRO_STRKEY #define LNUMKEY LRO_NUMKEY #define LNILKEY LRO_NILKEY diff --git a/app/modules/adc.c b/app/modules/adc.c index aedd16e1..15bf7de9 100644 --- a/app/modules/adc.c +++ b/app/modules/adc.c @@ -1,8 +1,8 @@ // Module for interfacing with adc +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_types.h" #include "user_interface.h" @@ -25,16 +25,10 @@ static int adc_readvdd33( lua_State* L ) } // Module function map -const LUA_REG_TYPE adc_map[] = { +static const LUA_REG_TYPE adc_map[] = { { LSTRKEY( "read" ), LFUNCVAL( adc_sample ) }, { LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_adc( lua_State *L ) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(ADC, "adc", adc_map, NULL); diff --git a/app/modules/bit.c b/app/modules/bit.c index d6914457..4d196465 100644 --- a/app/modules/bit.c +++ b/app/modules/bit.c @@ -5,10 +5,10 @@ // Modified by BogdanM for eLua +#include "module.h" #include "c_limits.h" #include "lauxlib.h" -#include "lrodefs.h" /* FIXME: Assume size_t is an unsigned lua_Integer */ typedef size_t lua_UInteger; @@ -119,7 +119,7 @@ static int bit_clear( lua_State* L ) return 1; } -const LUA_REG_TYPE bit_map[] = { +static const LUA_REG_TYPE bit_map[] = { { LSTRKEY( "bnot" ), LFUNCVAL( bit_bnot ) }, { LSTRKEY( "band" ), LFUNCVAL( bit_band ) }, { LSTRKEY( "bor" ), LFUNCVAL( bit_bor ) }, @@ -135,10 +135,4 @@ const LUA_REG_TYPE bit_map[] = { { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_bit (lua_State *L) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(BIT, "bit", bit_map, NULL); diff --git a/app/modules/bmp085.c b/app/modules/bmp085.c index 57a73bc1..17a7651f 100644 --- a/app/modules/bmp085.c +++ b/app/modules/bmp085.c @@ -1,6 +1,6 @@ +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" @@ -183,7 +183,7 @@ static int ICACHE_FLASH_ATTR bmp085_lua_pressure(lua_State* L) { return 1; } -const LUA_REG_TYPE bmp085_map[] = { +static const LUA_REG_TYPE bmp085_map[] = { { LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )}, { LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )}, { LSTRKEY( "pressure_raw" ), LFUNCVAL( bmp085_lua_pressure_raw )}, @@ -191,10 +191,4 @@ const LUA_REG_TYPE bmp085_map[] = { { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_bmp085(lua_State *L) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(BMP085, "bmp085", bmp085_map, NULL); diff --git a/app/modules/cjson.c b/app/modules/cjson.c index 23947a5d..b611c05f 100644 --- a/app/modules/cjson.c +++ b/app/modules/cjson.c @@ -37,11 +37,11 @@ */ // #include +#include "module.h" #include "c_string.h" #include "c_math.h" #include "c_limits.h" #include "lauxlib.h" -#include "lrodefs.h" #include "flash_api.h" #include "strbuf.h" @@ -1600,7 +1600,7 @@ int luaopen_cjson_safe(lua_State *l) #endif // Module function map -const LUA_REG_TYPE cjson_map[] = { +static const LUA_REG_TYPE cjson_map[] = { { LSTRKEY( "encode" ), LFUNCVAL( json_encode ) }, { LSTRKEY( "decode" ), LFUNCVAL( json_decode ) }, //{ LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) }, @@ -1614,7 +1614,7 @@ const LUA_REG_TYPE cjson_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_cjson( lua_State *L ) +int luaopen_cjson( lua_State *L ) { cjson_mem_setlua (L); @@ -1623,11 +1623,9 @@ LUALIB_API int luaopen_cjson( lua_State *L ) if(-1==cfg_init(&_cfg)){ return luaL_error(L, "BUG: Unable to init config for cjson");; } -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif } + +NODEMCU_MODULE(CJSON, "cjson", cjson_map, luaopen_cjson); /* vi:ai et sw=4 ts=4: */ diff --git a/app/modules/coap.c b/app/modules/coap.c index 94d2b670..2b6efb9e 100644 --- a/app/modules/coap.c +++ b/app/modules/coap.c @@ -1,8 +1,8 @@ // Module for coapwork +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -578,7 +578,7 @@ static const LUA_REG_TYPE coap_client_map[] = { { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE coap_map[] = +static const LUA_REG_TYPE coap_map[] = { { LSTRKEY( "Server" ), LFUNCVAL( coap_createServer ) }, { LSTRKEY( "Client" ), LFUNCVAL( coap_createClient ) }, @@ -594,14 +594,12 @@ const LUA_REG_TYPE coap_map[] = { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_coap( lua_State *L ) +int luaopen_coap( lua_State *L ) { endpoint_setup(); luaL_rometatable(L, "coap_server", (void *)coap_server_map); // create metatable for coap_server luaL_rometatable(L, "coap_client", (void *)coap_client_map); // create metatable for coap_client -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif } + +NODEMCU_MODULE(COAP, "coap", coap_map, luaopen_coap); diff --git a/app/modules/crypto.c b/app/modules/crypto.c index e9067495..4a151579 100644 --- a/app/modules/crypto.c +++ b/app/modules/crypto.c @@ -1,8 +1,8 @@ // Module for cryptography +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_types.h" #include "c_stdlib.h" #include "../crypto/digests.h" @@ -150,7 +150,7 @@ static int crypto_lhmac (lua_State *L) // Module function map -const LUA_REG_TYPE crypto_map[] = { +static const LUA_REG_TYPE crypto_map[] = { { LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) }, { LSTRKEY( "toBase64" ), LFUNCVAL( crypto_base64_encode ) }, { LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) }, @@ -160,10 +160,4 @@ const LUA_REG_TYPE crypto_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_crypto( lua_State *L ) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(CRYPTO, "crypto", crypto_map, NULL); diff --git a/app/modules/dht.c b/app/modules/dht.c index c951341f..673ed2f2 100644 --- a/app/modules/dht.c +++ b/app/modules/dht.c @@ -1,7 +1,7 @@ // Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44). +#include "module.h" #include "lauxlib.h" -#include "lrodefs.h" #include "platform.h" #include "cpu_esp8266.h" #include "dht.h" @@ -99,7 +99,7 @@ static int dht_lapi_readxx( lua_State *L ) // } // Module function map -const LUA_REG_TYPE dht_map[] = { +static const LUA_REG_TYPE dht_map[] = { { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, @@ -109,10 +109,4 @@ const LUA_REG_TYPE dht_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_dht( lua_State *L ) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(DHT, "dht", dht_map, NULL); diff --git a/app/modules/enduser_setup.c b/app/modules/enduser_setup.c index a7b06a57..1e02e0a3 100644 --- a/app/modules/enduser_setup.c +++ b/app/modules/enduser_setup.c @@ -31,10 +31,9 @@ * @author Robert Foss */ - +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -927,17 +926,10 @@ static int enduser_setup_stop(lua_State* L) } -const LUA_REG_TYPE enduser_setup_map[] = { +static const LUA_REG_TYPE enduser_setup_map[] = { { LSTRKEY( "start" ), LFUNCVAL( enduser_setup_start )}, { LSTRKEY( "stop" ), LFUNCVAL( enduser_setup_stop )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_enduser_setup(lua_State *L) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} - +NODEMCU_MODULE(ENDUSER_SETUP, "enduser_setup", enduser_setup_map, NULL); diff --git a/app/modules/file.c b/app/modules/file.c index 80f8ec54..d5e4fd26 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -1,8 +1,8 @@ // Module for interfacing with file system +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_types.h" #include "flash_fs.h" @@ -296,7 +296,7 @@ static int file_writeline( lua_State* L ) } // Module function map -const LUA_REG_TYPE file_map[] = { +static const LUA_REG_TYPE file_map[] = { { LSTRKEY( "list" ), LFUNCVAL( file_list ) }, { LSTRKEY( "open" ), LFUNCVAL( file_open ) }, { LSTRKEY( "close" ), LFUNCVAL( file_close ) }, @@ -316,10 +316,4 @@ const LUA_REG_TYPE file_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_file( lua_State *L ) { -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 - return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif -} +NODEMCU_MODULE(FILE, "file", file_map, NULL); diff --git a/app/modules/gpio.c b/app/modules/gpio.c index d19c0220..d779a903 100644 --- a/app/modules/gpio.c +++ b/app/modules/gpio.c @@ -1,8 +1,8 @@ // Module for interfacing with GPIO +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_types.h" #include "c_string.h" @@ -220,7 +220,7 @@ static int lgpio_serout( lua_State* L ) #undef DELAY_TABLE_MAX_LEN // Module function map -const LUA_REG_TYPE gpio_map[] = { +static const LUA_REG_TYPE gpio_map[] = { { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) }, { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) }, { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) }, @@ -238,7 +238,7 @@ const LUA_REG_TYPE gpio_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_gpio( lua_State *L ) { +int luaopen_gpio( lua_State *L ) { #ifdef GPIO_INTERRUPT_ENABLE int i; for(i=0;i 0 - #if defined(LUA_USE_BUILTIN_STRING) - {LUA_STRLIBNAME, strlib}, - #endif +extern const luaL_Reg lua_libs[]; - #if defined(LUA_USE_BUILTIN_TABLE) - {LUA_TABLIBNAME, tab_funcs}, - #endif - - #if defined(LUA_USE_BUILTIN_DEBUG) || defined(LUA_USE_BUILTIN_DEBUG_MINIMAL) - {LUA_DBLIBNAME, dblib}, - #endif - - #if defined(LUA_USE_BUILTIN_COROUTINE) - {LUA_COLIBNAME, co_funcs}, - #endif - - #if defined(LUA_USE_BUILTIN_MATH) - {LUA_MATHLIBNAME, math_map}, - #endif - - #if defined(LUA_USE_BUILTIN_OS) - {LUA_OSLIBNAME, syslib}, - #endif - -#if defined(LUA_MODULES_ROM) && LUA_OPTIMIZE_MEMORY == 2 -#undef _ROM -#define _ROM( name, openf, table ) { name, table }, - LUA_MODULES_ROM -#endif -#endif - {NULL, NULL} -}; - -LUALIB_API void luaL_openlibs (lua_State *L) { - const luaL_Reg *lib = lualibs; - for (; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); +void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib = lua_libs; + for (; lib->name; lib++) { + if (lib->func) + { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } } } diff --git a/app/modules/modules.h b/app/modules/modules.h deleted file mode 100644 index 7292b6e9..00000000 --- a/app/modules/modules.h +++ /dev/null @@ -1,289 +0,0 @@ -/** - * External modules library - */ - -#ifndef __MODULES_H__ -#define __MODULES_H__ - -#if defined(LUA_USE_MODULES_GPIO) -#define MODULES_GPIO "gpio" -#define ROM_MODULES_GPIO \ - _ROM(MODULES_GPIO, luaopen_gpio, gpio_map) -#else -#define ROM_MODULES_GPIO -#endif - -#if defined(LUA_USE_MODULES_PWM) -#define MODULES_PWM "pwm" -#define ROM_MODULES_PWM \ - _ROM(MODULES_PWM, luaopen_pwm, pwm_map) -#else -#define ROM_MODULES_PWM -#endif - -#if defined(LUA_USE_MODULES_WIFI) -#define MODULES_WIFI "wifi" -#define ROM_MODULES_WIFI \ - _ROM(MODULES_WIFI, luaopen_wifi, wifi_map) -#else -#define ROM_MODULES_WIFI -#endif - -#if defined(LUA_USE_MODULES_NET) -#define MODULES_NET "net" -#define ROM_MODULES_NET \ - _ROM(MODULES_NET, luaopen_net, net_map) -#else -#define ROM_MODULES_NET -#endif - -#if defined(LUA_USE_MODULES_COAP) -#define MODULES_COAP "coap" -#define ROM_MODULES_COAP \ - _ROM(MODULES_COAP, luaopen_coap, coap_map) -#else -#define ROM_MODULES_COAP -#endif - -#if defined(LUA_USE_MODULES_MQTT) -#define MODULES_MQTT "mqtt" -#define ROM_MODULES_MQTT \ - _ROM(MODULES_MQTT, luaopen_mqtt, mqtt_map) -#else -#define ROM_MODULES_MQTT -#endif - -#if defined(LUA_USE_MODULES_U8G) -#define MODULES_U8G "u8g" -#define ROM_MODULES_U8G \ - _ROM(MODULES_U8G, luaopen_u8g, lu8g_map) -#else -#define ROM_MODULES_U8G -#endif - -#if defined(LUA_USE_MODULES_UCG) -#define MODULES_UCG "ucg" -#define ROM_MODULES_UCG \ - _ROM(MODULES_UCG, luaopen_ucg, lucg_map) -#else -#define ROM_MODULES_UCG -#endif - -#if defined(LUA_USE_MODULES_I2C) -#define MODULES_I2C "i2c" -#define ROM_MODULES_I2C \ - _ROM(MODULES_I2C, luaopen_i2c, i2c_map) -#else -#define ROM_MODULES_I2C -#endif - -#if defined(LUA_USE_MODULES_SPI) -#define MODULES_SPI "spi" -#define ROM_MODULES_SPI \ - _ROM(MODULES_SPI, luaopen_spi, spi_map) -#else -#define ROM_MODULES_SPI -#endif - -#if defined(LUA_USE_MODULES_TMR) -#define MODULES_TMR "tmr" -#define ROM_MODULES_TMR \ - _ROM(MODULES_TMR, luaopen_tmr, tmr_map) -#else -#define ROM_MODULES_TMR -#endif - -#if defined(LUA_USE_MODULES_NODE) -#define MODULES_NODE "node" -#define ROM_MODULES_NODE \ - _ROM(MODULES_NODE, luaopen_node, node_map) -#else -#define ROM_MODULES_NODE -#endif - -#if defined(LUA_USE_MODULES_FILE) -#define MODULES_FILE "file" -#define ROM_MODULES_FILE \ - _ROM(MODULES_FILE, luaopen_file, file_map) -#else -#define ROM_MODULES_FILE -#endif - -#if defined(LUA_USE_MODULES_ADC) -#define MODULES_ADC "adc" -#define ROM_MODULES_ADC \ - _ROM(MODULES_ADC, luaopen_adc, adc_map) -#else -#define ROM_MODULES_ADC -#endif - -#if defined(LUA_USE_MODULES_UART) -#define MODULES_UART "uart" -#define ROM_MODULES_UART \ - _ROM(MODULES_UART, luaopen_uart, uart_map) -#else -#define ROM_MODULES_UART -#endif - -#if defined(LUA_USE_MODULES_OW) -#define MODULES_OW "ow" -#define ROM_MODULES_OW \ - _ROM(MODULES_OW, luaopen_ow, ow_map) -#else -#define ROM_MODULES_OW -#endif - -#if defined(LUA_USE_MODULES_BIT) -#define MODULES_BIT "bit" -#define ROM_MODULES_BIT \ - _ROM(MODULES_BIT, luaopen_bit, bit_map) -#else -#define ROM_MODULES_BIT -#endif - -#if defined(LUA_USE_MODULES_WS2801) -#define MODULES_WS2801 "ws2801" -#define ROM_MODULES_WS2801 \ - _ROM(MODULES_WS2801, luaopen_ws2801, ws2801_map) -#else -#define ROM_MODULES_WS2801 -#endif - -#if defined(LUA_USE_MODULES_WS2812) -#define MODULES_WS2812 "ws2812" -#define ROM_MODULES_WS2812 \ - _ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map) -#else -#define ROM_MODULES_WS2812 -#endif - -#if defined(LUA_USE_MODULES_ENDUSER_SETUP) -#define MODULES_ENDUSER_SETUP "enduser_setup" -#define ROM_MODULES_ENDUSER_SETUP \ - _ROM(MODULES_ENDUSER_SETUP, luaopen_enduser_setup, enduser_setup_map) -#else -#define ROM_MODULES_ENDUSER_SETUP -#endif - -#if defined(LUA_USE_MODULES_CJSON) -#define MODULES_CJSON "cjson" -#define ROM_MODULES_CJSON \ - _ROM(MODULES_CJSON, luaopen_cjson, cjson_map) -#else -#define ROM_MODULES_CJSON -#endif - -#if defined(LUA_USE_MODULES_CRYPTO) -#define MODULES_CRYPTO "crypto" -#define ROM_MODULES_CRYPTO \ - _ROM(MODULES_CRYPTO, luaopen_crypto, crypto_map) -#else -#define ROM_MODULES_CRYPTO -#endif - -#if defined(LUA_USE_MODULES_RC) -#define MODULES_RC "rc" -#define ROM_MODULES_RC \ - _ROM(MODULES_RC, luaopen_rc, rc_map) -#else -#define ROM_MODULES_RC -#endif - -#if defined(LUA_USE_MODULES_DHT) -#define MODULES_DHT "dht" -#define ROM_MODULES_DHT \ - _ROM(MODULES_DHT, luaopen_dht, dht_map) -#else -#define ROM_MODULES_DHT -#endif - -#if defined(LUA_USE_MODULES_RTCMEM) -#define MODULES_RTCMEM "rtcmem" -#define ROM_MODULES_RTCMEM \ - _ROM(MODULES_RTCMEM, luaopen_rtcmem, rtcmem_map) -#else -#define ROM_MODULES_RTCMEM -#endif - -#if defined(LUA_USE_MODULES_RTCTIME) -#define MODULES_RTCTIME "rtctime" -#define ROM_MODULES_RTCTIME \ - _ROM(MODULES_RTCTIME, luaopen_rtctime, rtctime_map) -#else -#define ROM_MODULES_RTCTIME -#endif - -#if defined(LUA_USE_MODULES_RTCFIFO) -#define MODULES_RTCFIFO "rtcfifo" -#define ROM_MODULES_RTCFIFO \ - _ROM(MODULES_RTCFIFO, luaopen_rtcfifo, rtcfifo_map) -#else -#define ROM_MODULES_RTCFIFO -#endif - -#if defined(LUA_USE_MODULES_SNTP) -#define MODULES_SNTP "sntp" -#define ROM_MODULES_SNTP \ - _ROM(MODULES_SNTP, luaopen_sntp, sntp_map) -#else -#define ROM_MODULES_SNTP -#endif - -#if defined(LUA_USE_MODULES_BMP085) -#define MODULES_BMP085 "bmp085" -#define ROM_MODULES_BMP085 \ - _ROM(MODULES_BMP085, luaopen_bmp085, bmp085_map) -#else -#define ROM_MODULES_BMP085 -#endif - -#if defined(LUA_USE_MODULES_TSL2561) -#define MODULES_TSL2561 "tsl2561" -#define ROM_MODULES_TSL2561 \ - _ROM(MODULES_TSL2561, luaopen_tsl2561, tsl2561_map) -#else -#define ROM_MODULES_TSL2561 -#endif - -#if defined(LUA_USE_MODULES_HX711) -#define MODULES_HX711 "hx711" -#define ROM_MODULES_HX711 \ - _ROM(MODULES_HX711, luaopen_hx711, hx711_map) -#else -#define ROM_MODULES_HX711 -#endif - -#define LUA_MODULES_ROM \ - ROM_MODULES_GPIO \ - ROM_MODULES_PWM \ - ROM_MODULES_WIFI \ - ROM_MODULES_COAP \ - ROM_MODULES_MQTT \ - ROM_MODULES_U8G \ - ROM_MODULES_UCG \ - ROM_MODULES_I2C \ - ROM_MODULES_SPI \ - ROM_MODULES_TMR \ - ROM_MODULES_NODE \ - ROM_MODULES_FILE \ - ROM_MODULES_NET \ - ROM_MODULES_ADC \ - ROM_MODULES_UART \ - ROM_MODULES_OW \ - ROM_MODULES_BIT \ - ROM_MODULES_ENDUSER_SETUP \ - ROM_MODULES_WS2801 \ - ROM_MODULES_WS2812 \ - ROM_MODULES_CJSON \ - ROM_MODULES_CRYPTO \ - ROM_MODULES_RC \ - ROM_MODULES_DHT \ - ROM_MODULES_RTCMEM \ - ROM_MODULES_RTCTIME \ - ROM_MODULES_RTCFIFO \ - ROM_MODULES_SNTP \ - ROM_MODULES_BMP085 \ - ROM_MODULES_TSL2561 \ - ROM_MODULES_HX711 - -#endif diff --git a/app/modules/mqtt.c b/app/modules/mqtt.c index 7d396133..2dde4de6 100644 --- a/app/modules/mqtt.c +++ b/app/modules/mqtt.c @@ -1,8 +1,8 @@ // Module for mqtt +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -1401,18 +1401,16 @@ static const LUA_REG_TYPE mqtt_socket_map[] = { { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE mqtt_map[] = { +static const LUA_REG_TYPE mqtt_map[] = { { LSTRKEY( "Client" ), LFUNCVAL( mqtt_socket_client ) }, { LSTRKEY( "__metatable" ), LROVAL( mqtt_map ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_mqtt( lua_State *L ) +int luaopen_mqtt( lua_State *L ) { luaL_rometatable(L, "mqtt.socket", (void *)mqtt_socket_map); // create metatable for mqtt.socket -#if MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2 return 0; -#else -# error "NodeMCU modules must be build with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)" -#endif } + +NODEMCU_MODULE(MQTT, "mqtt", mqtt_map, luaopen_mqtt); diff --git a/app/modules/net.c b/app/modules/net.c index 9555324e..f0eee3b6 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1,8 +1,8 @@ // Module for network +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "lrodefs.h" #include "c_string.h" #include "c_stdlib.h" @@ -1545,7 +1545,7 @@ static const LUA_REG_TYPE net_dns_map[] = { { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE net_map[] = { +static const LUA_REG_TYPE net_map[] = { { LSTRKEY( "createServer" ), LFUNCVAL( net_createServer ) }, { LSTRKEY( "createConnection" ), LFUNCVAL( net_createConnection ) }, { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, @@ -1557,7 +1557,7 @@ const LUA_REG_TYPE net_map[] = { { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_net( lua_State *L ) { +int luaopen_net( lua_State *L ) { int i; for(i=0;i Date: Thu, 17 Dec 2015 12:37:01 +1100 Subject: [PATCH 4/5] Deal with ld's overly eager discarding of globals. --- app/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Makefile b/app/Makefile index 62819e9d..83cb23cc 100644 --- a/app/Makefile +++ b/app/Makefile @@ -86,8 +86,10 @@ COMPONENTS_eagle.app.v6 = \ crypto/libcrypto.a \ dhtlib/libdhtlib.a \ tsl2561/tsl2561lib.a \ - modules/libmodules.a + modules/libmodules.a \ +# Special consideration for modules to support NODEMCU_MODULE handling +MODULES_LIB = $(filter %modules.a, $(DEP_LIBS_eagle.app.v6)) LINKFLAGS_eagle.app.v6 = \ -Wl,--gc-sections \ @@ -98,6 +100,9 @@ LINKFLAGS_eagle.app.v6 = \ -Wl,--no-check-sections \ -Wl,--wrap=_xtos_set_exception_handler \ -Wl,-static \ + -Wl,--whole-archive \ + $(MODULES_LIB) \ + -Wl,--no-whole-archive \ -Wl,--start-group \ -lc \ -lgcc \ From 9003d3e8fb283cc242e1d1962cdc17efe3749119 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Fri, 18 Dec 2015 13:10:48 +1100 Subject: [PATCH 5/5] Rework NODEMCU_MODULE() to not pull in extra gunk. Third time lucky. I hope. Hi Terry. --- app/Makefile | 11 ++++++----- app/include/module.h | 36 ++++++++++++++++++------------------ ld/nodemcu.ld | 4 ++-- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/app/Makefile b/app/Makefile index 83cb23cc..77e20ab1 100644 --- a/app/Makefile +++ b/app/Makefile @@ -88,8 +88,11 @@ COMPONENTS_eagle.app.v6 = \ tsl2561/tsl2561lib.a \ modules/libmodules.a \ -# Special consideration for modules to support NODEMCU_MODULE handling -MODULES_LIB = $(filter %modules.a, $(DEP_LIBS_eagle.app.v6)) +# Inspect the modules library and work out which modules need to be linked. +# For each enabled module, a symbol name of the form XYZ_module_selected is +# returned. At link time those names are declared undefined, so those (and +# only those) modules are pulled in. +SELECTED_MODULE_SYMS=$(filter %_module_selected %module_selected1,$(shell $(NM) modules/.output/$(TARGET)/$(FLAVOR)/lib/libmodules.a)) LINKFLAGS_eagle.app.v6 = \ -Wl,--gc-sections \ @@ -100,9 +103,7 @@ LINKFLAGS_eagle.app.v6 = \ -Wl,--no-check-sections \ -Wl,--wrap=_xtos_set_exception_handler \ -Wl,-static \ - -Wl,--whole-archive \ - $(MODULES_LIB) \ - -Wl,--no-whole-archive \ + $(addprefix -u , $(SELECTED_MODULE_SYMS)) \ -Wl,--start-group \ -lc \ -lgcc \ diff --git a/app/include/module.h b/app/include/module.h index 9f00a69a..919d5758 100644 --- a/app/include/module.h +++ b/app/include/module.h @@ -34,37 +34,37 @@ * a foo function in your module. */ -#define MODULE_STRIFY__(x) #x -#define MODULE_STRIFY_(x) MODULE_STRIFY__(x) #define MODULE_EXPAND_(x) x #define MODULE_PASTE_(x,y) x##y +#define MODULE_EXPAND_PASTE_(x,y) MODULE_PASTE_(x,y) -/* Given an uppercase name XYZ, look at the corresponding LUA_USE_MODULES_XYZ - * and append that to the section name prefix (e.g. "lua_libs"). For an - * included module, this will yield either ".lua_libs" (for an empty #define) - * or ".lua_libs1" (if #defined to 1). The linker script will then - * pick up all items in those sections and construct an array for us. - * A non-included module ZZZ ends up in a section named - * ".lua_libsLUA_USE_MODULES_ZZZ" which gets discarded by the linker (together - * with the associated module code). +#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(s))) + +/* For the ROM table, we name the variable according to ( | denotes concat): + * cfgname | _module_selected | LUA_USE_MODULES_##cfgname + * where the LUA_USE_MODULES_XYZ macro is first expanded to yield either + * an empty string (or 1) if the module has been enabled, or the literal + * LUA_USE_MOUDLE_XYZ in the case it hasn't. Thus, the name of the variable + * ends up looking either like XYZ_module_enabled, or if not enabled, + * XYZ_module_enabledLUA_USE_MODULES_XYZ. This forms the basis for + * letting the build system detect automatically (via nm) which modules need + * to be linked in. */ -#define PICK_SECTION_(pfx, name) \ - pfx MODULE_STRIFY_(MODULE_EXPAND_(LUA_USE_MODULES_ ## name)) - #define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \ - static const __attribute__((used,unused,section(PICK_SECTION_(".lua_libs",cfgname)))) \ + const LOCK_IN_SECTION(".lua_libs") \ luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \ - static const __attribute__((used,unused,section(PICK_SECTION_(".lua_rotable",cfgname)))) \ - luaR_table MODULE_PASTE_(lua_rotable_,cfgname) = { luaname, map } + const LOCK_IN_SECTION(".lua_rotable") \ + luaR_table MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(LUA_USE_MODULES_,cfgname))) \ + = { luaname, map } /* System module registration support, not using LUA_USE_MODULES_XYZ. */ #define BUILTIN_LIB_INIT(name, luaname, initfunc) \ - static const __attribute__((used,unused,section(".lua_libs"))) \ + const LOCK_IN_SECTION(".lua_libs") \ luaL_Reg MODULE_PASTE_(lua_lib_,name) = { luaname, initfunc } #define BUILTIN_LIB(name, luaname, map) \ - static const __attribute__((used,unused,section(".lua_rotable"))) \ + const LOCK_IN_SECTION(".lua_rotable") \ luaR_table MODULE_PASTE_(lua_rotable_,name) = { luaname, map } #if !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2) diff --git a/ld/nodemcu.ld b/ld/nodemcu.ld index e67e9af0..8c4ee61a 100644 --- a/ld/nodemcu.ld +++ b/ld/nodemcu.ld @@ -83,10 +83,10 @@ SECTIONS . = ALIGN(4); lua_libs = ABSOLUTE(.); /* Allow either empty define or defined-to-1 to include the module */ - KEEP(*(.lua_libs .lua_libs1)) + KEEP(*(.lua_libs)) LONG(0) LONG(0) /* Null-terminate the array */ lua_rotable = ABSOLUTE(.); - KEEP(*(.lua_rotable .lua_rotable1)) + KEEP(*(.lua_rotable)) LONG(0) LONG(0) /* Null-terminate the array */ /* These are *only* pulled in by Lua, and therefore safe to put in flash */