diff --git a/app/Makefile b/app/Makefile index bc0bfa9e..77e20ab1 100644 --- a/app/Makefile +++ b/app/Makefile @@ -86,8 +86,13 @@ COMPONENTS_eagle.app.v6 = \ crypto/libcrypto.a \ dhtlib/libdhtlib.a \ tsl2561/tsl2561lib.a \ - modules/libmodules.a + modules/libmodules.a \ +# 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 \ @@ -98,6 +103,7 @@ LINKFLAGS_eagle.app.v6 = \ -Wl,--no-check-sections \ -Wl,--wrap=_xtos_set_exception_handler \ -Wl,-static \ + $(addprefix -u , $(SELECTED_MODULE_SYMS)) \ -Wl,--start-group \ -lc \ -lgcc \ @@ -134,9 +140,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/include/module.h b/app/include/module.h new file mode 100644 index 00000000..919d5758 --- /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_EXPAND_(x) x +#define MODULE_PASTE_(x,y) x##y +#define MODULE_EXPAND_PASTE_(x,y) MODULE_PASTE_(x,y) + +#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 NODEMCU_MODULE(cfgname, luaname, map, initfunc) \ + const LOCK_IN_SECTION(".lua_libs") \ + luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \ + 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) \ + const LOCK_IN_SECTION(".lua_libs") \ + luaL_Reg MODULE_PASTE_(lua_lib_,name) = { luaname, initfunc } + +#define BUILTIN_LIB(name, luaname, map) \ + const LOCK_IN_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/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/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/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..15bf7de9 100644 --- a/app/modules/adc.c +++ b/app/modules/adc.c @@ -1,11 +1,8 @@ // Module for interfacing with adc -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_types.h" #include "user_interface.h" @@ -28,26 +25,10 @@ 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 ) }, +static 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 -} +NODEMCU_MODULE(ADC, "adc", adc_map, NULL); diff --git a/app/modules/auxmods.h b/app/modules/auxmods.h deleted file mode 100644 index 602da99a..00000000 --- a/app/modules/auxmods.h +++ /dev/null @@ -1,121 +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__ - -#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 ) )\ - 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 ) - -#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..4d196465 100644 --- a/app/modules/bit.c +++ b/app/modules/bit.c @@ -5,13 +5,10 @@ // Modified by BogdanM for eLua +#include "module.h" #include "c_limits.h" -//#include "lua.h" #include "lauxlib.h" -#include "auxmods.h" -// #include "type.h" -#include "lrotable.h" /* FIXME: Assume size_t is an unsigned lua_Integer */ typedef size_t lua_UInteger; @@ -122,9 +119,7 @@ static int bit_clear( lua_State* L ) return 1; } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -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 ) }, @@ -140,6 +135,4 @@ const LUA_REG_TYPE bit_map[] = { { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_bit (lua_State *L) { - LREGISTER( L, "bit", bit_map ); -} +NODEMCU_MODULE(BIT, "bit", bit_map, NULL); diff --git a/app/modules/bmp085.c b/app/modules/bmp085.c index 31b7d6af..17a7651f 100644 --- a/app/modules/bmp085.c +++ b/app/modules/bmp085.c @@ -1,8 +1,6 @@ -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" #include "c_string.h" @@ -185,19 +183,12 @@ 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 )}, - { LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )}, +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 )}, - { LSTRKEY( "init" ), LFUNCVAL( bmp085_init )}, + { LSTRKEY( "init" ), LFUNCVAL( bmp085_init )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_bmp085(lua_State *L) { - LREGISTER(L, "bmp085", bmp085_map); - return 1; -} - +NODEMCU_MODULE(BMP085, "bmp085", bmp085_map, NULL); diff --git a/app/modules/cjson.c b/app/modules/cjson.c index 2fb10c6c..b611c05f 100644 --- a/app/modules/cjson.c +++ b/app/modules/cjson.c @@ -37,10 +37,10 @@ */ // #include +#include "module.h" #include "c_string.h" #include "c_math.h" #include "c_limits.h" -#include "lua.h" #include "lauxlib.h" #include "flash_api.h" @@ -1528,53 +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 -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -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 ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif - { 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 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 /* Return cjson module table */ static int lua_cjson_new(lua_State *l) { @@ -1644,5 +1598,34 @@ int luaopen_cjson_safe(lua_State *l) return 1; } #endif + +// Module function 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 ) }, +//{ 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 } +}; + +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; +} + +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 72502ebc..2b6efb9e 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 "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_string.h" #include "c_stdlib.h" @@ -561,99 +558,48 @@ 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 ) }, - { LSTRKEY( "close" ), LFUNCVAL ( coap_server_close ) }, - { 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 +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 ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - { LSTRKEY( "__index" ), LROVAL ( coap_client_map ) }, -#endif +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[] = +static 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 ) }, - { 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 ) }, -#endif { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_coap( lua_State *L ) +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 } + +NODEMCU_MODULE(COAP, "coap", coap_map, luaopen_coap); diff --git a/app/modules/crypto.c b/app/modules/crypto.c index 3639e436..4a151579 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 "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_types.h" #include "c_stdlib.h" #include "../crypto/digests.h" @@ -153,31 +150,14 @@ 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 ) }, +static 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 ) }, - -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif + { 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 ) -{ -#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 -} +NODEMCU_MODULE(CRYPTO, "crypto", crypto_map, NULL); diff --git a/app/modules/dht.c b/app/modules/dht.c index fcbf5fdb..673ed2f2 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 "module.h" #include "lauxlib.h" -#include "auxmods.h" -#include "lrotable.h" +#include "platform.h" #include "cpu_esp8266.h" #include "dht.h" @@ -100,30 +99,14 @@ 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 ) }, +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 ) }, + { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, - { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, -#endif + { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, { 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 -} +NODEMCU_MODULE(DHT, "dht", dht_map, NULL); diff --git a/app/modules/enduser_setup.c b/app/modules/enduser_setup.c index 19825284..1e02e0a3 100644 --- a/app/modules/enduser_setup.c +++ b/app/modules/enduser_setup.c @@ -31,12 +31,9 @@ * @author Robert Foss */ - -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -929,17 +926,10 @@ static int enduser_setup_stop(lua_State* L) } -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -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) { - LREGISTER(L, "enduser_setup", enduser_setup_map); - return 1; -} - +NODEMCU_MODULE(ENDUSER_SETUP, "enduser_setup", enduser_setup_map, NULL); diff --git a/app/modules/file.c b/app/modules/file.c index b4a9cfa0..d5e4fd26 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 "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_types.h" #include "flash_fs.h" @@ -299,42 +296,24 @@ 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 ) }, - { LSTRKEY( "open" ), LFUNCVAL( file_open ) }, - { LSTRKEY( "close" ), LFUNCVAL( file_close ) }, - { LSTRKEY( "write" ), LFUNCVAL( file_write ) }, +static 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 ) }, -#endif - -#if LUA_OPTIMIZE_MEMORY > 0 - + { 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 ) -{ -#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 -} +NODEMCU_MODULE(FILE, "file", file_map, NULL); diff --git a/app/modules/gpio.c b/app/modules/gpio.c index 69875050..d779a903 100644 --- a/app/modules/gpio.c +++ b/app/modules/gpio.c @@ -1,11 +1,8 @@ // Module for interfacing with GPIO -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_types.h" #include "c_string.h" @@ -223,33 +220,25 @@ 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 ) }, - { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) }, - { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) }, +static 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 ) }, -#endif -#if LUA_OPTIMIZE_MEMORY > 0 -#ifdef GPIO_INTERRUPT_ENABLE - { 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 ) }, -#endif { 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 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 } + +NODEMCU_MODULE(GPIO, "gpio", gpio_map, luaopen_gpio); diff --git a/app/modules/hx711.c b/app/modules/hx711.c index e172643b..c5bee07e 100644 --- a/app/modules/hx711.c +++ b/app/modules/hx711.c @@ -1,11 +1,9 @@ // Module for HX711 load cell amplifier // https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -67,18 +65,16 @@ 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[] = -{ +// Module function map +static const LUA_REG_TYPE hx711_map[] = { { LSTRKEY( "init" ), LFUNCVAL( hx711_init )}, { LSTRKEY( "read" ), LFUNCVAL( hx711_read )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_hx711(lua_State *L) { - // TODO: the below todo was inherited from the ws2812 code but is still valid. +int luaopen_hx711(lua_State *L) { // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "hx711", hx711_map); - return 1; + return 0; } + +NODEMCU_MODULE(HX711, "hx711", hx711_map, luaopen_hx711); diff --git a/app/modules/i2c.c b/app/modules/i2c.c index c231aeec..0ed64920 100644 --- a/app/modules/i2c.c +++ b/app/modules/i2c.c @@ -1,11 +1,8 @@ // Module for interfacing with the I2C interface -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" // Lua: speed = i2c.setup( id, sda, scl, speed ) static int i2c_setup( lua_State *L ) @@ -143,39 +140,18 @@ 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 ) }, - { LSTRKEY( "start" ), LFUNCVAL( i2c_start ) }, - { LSTRKEY( "stop" ), LFUNCVAL( i2c_stop ) }, - { 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 ) }, +static const LUA_REG_TYPE i2c_map[] = { + { LSTRKEY( "setup" ), LFUNCVAL( i2c_setup ) }, + { LSTRKEY( "start" ), LFUNCVAL( i2c_start ) }, + { LSTRKEY( "stop" ), LFUNCVAL( i2c_stop ) }, + { LSTRKEY( "address" ), LFUNCVAL( i2c_address ) }, + { LSTRKEY( "write" ), LFUNCVAL( i2c_write ) }, + { LSTRKEY( "read" ), LFUNCVAL( i2c_read ) }, + //{ 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 + { LSTRKEY( "RECEIVER" ), LNUMVAL( PLATFORM_I2C_DIRECTION_RECEIVER ) }, { 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 -} - +NODEMCU_MODULE(I2C, "i2c", i2c_map, NULL); diff --git a/app/modules/linit.c b/app/modules/linit.c index a0fbf96f..3eba138d 100644 --- a/app/modules/linit.c +++ b/app/modules/linit.c @@ -13,124 +13,61 @@ #include "lualib.h" #include "lauxlib.h" -#include "lrotable.h" #include "luaconf.h" +#include "module.h" -#include "user_modules.h" -#if defined(LUA_USE_MODULES) -#include "modules.h" -#endif +BUILTIN_LIB_INIT( BASE, "", luaopen_base); +BUILTIN_LIB_INIT( LOADLIB, LUA_LOADLIBNAME, luaopen_package); -#if defined(LUA_MODULES_ROM) -#undef _ROM -#define _ROM( name, openf, table ) extern int openf(lua_State *); -LUA_MODULES_ROM -#endif - -static const luaL_Reg lualibs[] = { - {"", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, #if defined(LUA_USE_BUILTIN_IO) - {LUA_IOLIBNAME, luaopen_io}, +BUILTIN_LIB_INIT( IO, LUA_IOLIBNAME, luaopen_io); #endif -#if defined(LUA_USE_BUILTIN_STRING) - {LUA_STRLIBNAME, luaopen_string}, -#endif - -#if LUA_OPTIMIZE_MEMORY == 0 - #if defined(LUA_USE_BUILTIN_MATH) - {LUA_MATHLIBNAME, luaopen_math}, - #endif - - #if defined(LUA_USE_BUILTIN_TABLE) - {LUA_TABLIBNAME, luaopen_table}, - #endif - - #if defined(LUA_USE_BUILTIN_DEBUG) || defined(LUA_USE_BUILTIN_DEBUG_MINIMAL) - {LUA_DBLIBNAME, luaopen_debug}, - #endif -#endif -#if defined(LUA_MODULES_ROM) -#undef _ROM -#define _ROM( name, openf, table ) { name, openf }, - LUA_MODULES_ROM -#endif - {NULL, NULL} -}; - -#if defined(LUA_USE_BUILTIN_STRING) +#if defined (LUA_USE_BUILTIN_STRING) extern const luaR_entry strlib[]; -#endif - -#if defined(LUA_USE_BUILTIN_OS) -extern const luaR_entry syslib[]; +BUILTIN_LIB_INIT( STRING, LUA_STRLIBNAME, luaopen_string); +BUILTIN_LIB( STRING, LUA_STRLIBNAME, strlib); #endif #if defined(LUA_USE_BUILTIN_TABLE) extern const luaR_entry tab_funcs[]; +BUILTIN_LIB_INIT( TABLE, LUA_TABLIBNAME, luaopen_table); +BUILTIN_LIB( TABLE, LUA_TABLIBNAME, tab_funcs); #endif #if defined(LUA_USE_BUILTIN_DEBUG) || defined(LUA_USE_BUILTIN_DEBUG_MINIMAL) extern const luaR_entry dblib[]; +BUILTIN_LIB_INIT( DBG, LUA_DBLIBNAME, luaopen_debug); +BUILTIN_LIB( DBG, LUA_DBLIBNAME, dblib); +#endif + +#if defined(LUA_USE_BUILTIN_OS) +extern const luaR_entry syslib[]; +BUILTIN_LIB( OS, LUA_OSLIBNAME, syslib); #endif #if defined(LUA_USE_BUILTIN_COROUTINE) extern const luaR_entry co_funcs[]; +BUILTIN_LIB( CO, LUA_COLIBNAME, co_funcs); #endif #if defined(LUA_USE_BUILTIN_MATH) extern const luaR_entry math_map[]; +BUILTIN_LIB( MATH, LUA_MATHLIBNAME, math_map); #endif -#if defined(LUA_MODULES_ROM) && LUA_OPTIMIZE_MEMORY == 2 -#undef _ROM -#define _ROM( name, openf, table ) extern const luaR_entry table[]; -LUA_MODULES_ROM -#endif -const luaR_table lua_rotable[] = -{ -#if LUA_OPTIMIZE_MEMORY > 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 0741cb6d..2dde4de6 100644 --- a/app/modules/mqtt.c +++ b/app/modules/mqtt.c @@ -1,11 +1,8 @@ // Module for mqtt -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_string.h" #include "c_stdlib.h" @@ -1392,59 +1389,28 @@ 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 ) }, - { LSTRKEY( "close" ), LFUNCVAL ( mqtt_socket_close ) }, - { LSTRKEY( "publish" ), LFUNCVAL ( mqtt_socket_publish ) }, - { LSTRKEY( "subscribe" ), LFUNCVAL ( mqtt_socket_subscribe ) }, - { 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 +static const LUA_REG_TYPE mqtt_socket_map[] = { + { LSTRKEY( "connect" ), LFUNCVAL( mqtt_socket_connect ) }, + { LSTRKEY( "close" ), LFUNCVAL( mqtt_socket_close ) }, + { LSTRKEY( "publish" ), LFUNCVAL( mqtt_socket_publish ) }, + { LSTRKEY( "subscribe" ), LFUNCVAL( mqtt_socket_subscribe ) }, + { LSTRKEY( "lwt" ), LFUNCVAL( mqtt_socket_lwt ) }, + { LSTRKEY( "on" ), LFUNCVAL( mqtt_socket_on ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( mqtt_delete ) }, + { LSTRKEY( "__index" ), LROVAL( mqtt_socket_map ) }, { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE mqtt_map[] = -{ - { LSTRKEY( "Client" ), LFUNCVAL ( mqtt_socket_client ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - +static const LUA_REG_TYPE mqtt_map[] = { + { LSTRKEY( "Client" ), LFUNCVAL( mqtt_socket_client ) }, { LSTRKEY( "__metatable" ), LROVAL( mqtt_map ) }, -#endif { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_mqtt( lua_State *L ) +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 } + +NODEMCU_MODULE(MQTT, "mqtt", mqtt_map, luaopen_mqtt); diff --git a/app/modules/net.c b/app/modules/net.c index 73afe2b3..f0eee3b6 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1,11 +1,8 @@ // Module for network -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_string.h" #include "c_stdlib.h" @@ -1508,132 +1505,72 @@ 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 ) }, - { LSTRKEY( "close" ), LFUNCVAL ( net_server_close ) }, - { LSTRKEY( "on" ), LFUNCVAL ( net_udpserver_on ) }, - { 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 +static const LUA_REG_TYPE net_server_map[] = { + { LSTRKEY( "listen" ), LFUNCVAL( net_server_listen ) }, + { LSTRKEY( "close" ), LFUNCVAL( net_server_close ) }, + { LSTRKEY( "on" ), LFUNCVAL( net_udpserver_on ) }, + { LSTRKEY( "send" ), LFUNCVAL( net_udpserver_send ) }, +//{ LSTRKEY( "delete" ), LFUNCVAL( net_server_delete ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( net_server_delete ) }, + { LSTRKEY( "__index" ), LROVAL( net_server_map ) }, { LNILKEY, LNILVAL } }; -static const LUA_REG_TYPE net_socket_map[] = -{ +static const LUA_REG_TYPE net_socket_map[] = { { LSTRKEY( "connect" ), LFUNCVAL( net_socket_connect ) }, - { LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) }, - { LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) }, - { LSTRKEY( "send" ), LFUNCVAL ( net_socket_send ) }, - { LSTRKEY( "hold" ), LFUNCVAL ( net_socket_hold ) }, - { LSTRKEY( "unhold" ), LFUNCVAL ( net_socket_unhold ) }, - { LSTRKEY( "dns" ), LFUNCVAL ( net_socket_dns ) }, - { 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 + { LSTRKEY( "close" ), LFUNCVAL( net_socket_close ) }, + { LSTRKEY( "on" ), LFUNCVAL( net_socket_on ) }, + { LSTRKEY( "send" ), LFUNCVAL( net_socket_send ) }, + { LSTRKEY( "hold" ), LFUNCVAL( net_socket_hold ) }, + { LSTRKEY( "unhold" ), LFUNCVAL( net_socket_unhold ) }, + { LSTRKEY( "dns" ), LFUNCVAL( net_socket_dns ) }, + { LSTRKEY( "getpeer" ), LFUNCVAL( net_socket_getpeer ) }, +//{ LSTRKEY( "delete" ), LFUNCVAL( net_socket_delete ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( net_socket_delete ) }, + { LSTRKEY( "__index" ), LROVAL( net_socket_map ) }, { LNILKEY, LNILVAL } }; #if 0 -static const LUA_REG_TYPE net_array_map[] = -{ - { LSTRKEY( "__index" ), LFUNCVAL( net_array_index ) }, +static const LUA_REG_TYPE net_array_map[] = { + { LSTRKEY( "__index" ), LFUNCVAL( net_array_index ) }, { LSTRKEY( "__newindex" ), LFUNCVAL( net_array_newindex ) }, { LNILKEY, LNILVAL } }; #endif -static const LUA_REG_TYPE net_dns_map[] = -{ - { LSTRKEY( "setdnsserver" ), LFUNCVAL ( net_setdnsserver ) }, - { LSTRKEY( "getdnsserver" ), LFUNCVAL ( net_getdnsserver ) }, - { LSTRKEY( "resolve" ), LFUNCVAL ( net_dns_static ) }, +static const LUA_REG_TYPE net_dns_map[] = { + { LSTRKEY( "setdnsserver" ), LFUNCVAL( net_setdnsserver ) }, + { LSTRKEY( "getdnsserver" ), LFUNCVAL( net_getdnsserver ) }, + { LSTRKEY( "resolve" ), LFUNCVAL( net_dns_static ) }, { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE net_map[] = -{ - { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) }, - { 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 +static const LUA_REG_TYPE net_map[] = { + { LSTRKEY( "createServer" ), LFUNCVAL( net_createServer ) }, + { LSTRKEY( "createConnection" ), LFUNCVAL( net_createConnection ) }, + { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, + { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) }, + { LSTRKEY( "dns" ), LROVAL( net_dns_map ) }, + { LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, + { LSTRKEY( "UDP" ), LNUMVAL( UDP ) }, + { LSTRKEY( "__metatable" ), LROVAL( net_map ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_net( lua_State *L ) -{ +int luaopen_net( lua_State *L ) { int i; for(i=0;i 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 + 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 } + +NODEMCU_MODULE(NET, "net", net_map, luaopen_net); diff --git a/app/modules/node.c b/app/modules/node.c index d3154746..4ae045e8 100644 --- a/app/modules/node.c +++ b/app/modules/node.c @@ -1,6 +1,6 @@ // Module for interfacing with system -#include "lua.h" +#include "module.h" #include "lauxlib.h" #include "ldebug.h" @@ -15,14 +15,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,9 +548,7 @@ 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[] = +static const LUA_REG_TYPE node_map[] = { { LSTRKEY( "restart" ), LFUNCVAL( node_restart ) }, { LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) }, @@ -581,20 +577,7 @@ 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 -} +NODEMCU_MODULE(NODE, "node", node_map, NULL); diff --git a/app/modules/ow.c b/app/modules/ow.c index c9cbfa1a..6ce773bf 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 "module.h" #include "lauxlib.h" -#include "auxmods.h" -#include "lrotable.h" +#include "platform.h" #include "driver/onewire.h" // Lua: ow.setup( id ) @@ -282,46 +280,29 @@ 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 ) }, - { LSTRKEY( "reset" ), LFUNCVAL( ow_reset ) }, - { LSTRKEY( "skip" ), LFUNCVAL( ow_skip ) }, - { LSTRKEY( "select" ), LFUNCVAL( ow_select ) }, - { LSTRKEY( "write" ), LFUNCVAL( ow_write ) }, - { LSTRKEY( "write_bytes" ), LFUNCVAL( ow_write_bytes ) }, - { LSTRKEY( "read" ), LFUNCVAL( ow_read ) }, - { LSTRKEY( "read_bytes" ), LFUNCVAL( ow_read_bytes ) }, - { LSTRKEY( "depower" ), LFUNCVAL( ow_depower ) }, +static const LUA_REG_TYPE ow_map[] = { + { LSTRKEY( "setup" ), LFUNCVAL( ow_setup ) }, + { LSTRKEY( "reset" ), LFUNCVAL( ow_reset ) }, + { LSTRKEY( "skip" ), LFUNCVAL( ow_skip ) }, + { LSTRKEY( "select" ), LFUNCVAL( ow_select ) }, + { LSTRKEY( "write" ), LFUNCVAL( ow_write ) }, + { LSTRKEY( "write_bytes" ), LFUNCVAL( ow_write_bytes ) }, + { LSTRKEY( "read" ), LFUNCVAL( ow_read ) }, + { LSTRKEY( "read_bytes" ), LFUNCVAL( ow_read_bytes ) }, + { LSTRKEY( "depower" ), LFUNCVAL( ow_depower ) }, #if ONEWIRE_SEARCH - { LSTRKEY( "reset_search" ), LFUNCVAL( ow_reset_search ) }, + { LSTRKEY( "reset_search" ), LFUNCVAL( ow_reset_search ) }, { LSTRKEY( "target_search" ), LFUNCVAL( ow_target_search ) }, - { LSTRKEY( "search" ), LFUNCVAL( ow_search ) }, + { LSTRKEY( "search" ), LFUNCVAL( ow_search ) }, #endif #if ONEWIRE_CRC - { LSTRKEY( "crc8" ), LFUNCVAL( ow_crc8 ) }, + { LSTRKEY( "crc8" ), LFUNCVAL( ow_crc8 ) }, #if ONEWIRE_CRC16 - { LSTRKEY( "check_crc16" ), LFUNCVAL( ow_check_crc16 ) }, - { LSTRKEY( "crc16" ), LFUNCVAL( ow_crc16 ) }, + { 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 -} +NODEMCU_MODULE(OW, "ow", ow_map, NULL); diff --git a/app/modules/pwm.c b/app/modules/pwm.c index fbf29fe4..162ca856 100644 --- a/app/modules/pwm.c +++ b/app/modules/pwm.c @@ -1,12 +1,8 @@ // Module for interfacing with PWM -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" - #include "c_types.h" // Lua: realfrequency = setup( id, frequency, duty ) @@ -125,30 +121,16 @@ 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 ) }, - { LSTRKEY( "close" ), LFUNCVAL( lpwm_close ) }, - { LSTRKEY( "start" ), LFUNCVAL( lpwm_start ) }, - { LSTRKEY( "stop" ), LFUNCVAL( lpwm_stop ) }, +static const LUA_REG_TYPE pwm_map[] = { + { LSTRKEY( "setup" ), LFUNCVAL( lpwm_setup ) }, + { LSTRKEY( "close" ), LFUNCVAL( lpwm_close ) }, + { LSTRKEY( "start" ), LFUNCVAL( lpwm_start ) }, + { LSTRKEY( "stop" ), LFUNCVAL( lpwm_stop ) }, { LSTRKEY( "setclock" ), LFUNCVAL( lpwm_setclock ) }, { LSTRKEY( "getclock" ), LFUNCVAL( lpwm_getclock ) }, - { LSTRKEY( "setduty" ), LFUNCVAL( lpwm_setduty ) }, - { LSTRKEY( "getduty" ), LFUNCVAL( lpwm_getduty ) }, -#if LUA_OPTIMIZE_MEMORY > 0 - -#endif + { LSTRKEY( "setduty" ), LFUNCVAL( lpwm_setduty ) }, + { LSTRKEY( "getduty" ), LFUNCVAL( lpwm_getduty ) }, { 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 -} +NODEMCU_MODULE(PWM, "pwm", pwm_map, NULL); diff --git a/app/modules/rc.c b/app/modules/rc.c index 964896c4..d8301743 100644 --- a/app/modules/rc.c +++ b/app/modules/rc.c @@ -1,8 +1,6 @@ -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" //#include "driver/easygpio.h" //static Ping_Data pingA; #define defPulseLen 185 @@ -80,17 +78,16 @@ 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[] = -{ + +// Module function map +static const LUA_REG_TYPE rc_map[] = { { LSTRKEY( "send" ), LFUNCVAL( rc_send )}, { LNILKEY, LNILVAL} }; -//LUALIB_API int luaopen_ultra(lua_State *L) { -LUALIB_API int luaopen_rc(lua_State *L) { +int luaopen_rc(lua_State *L) { // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "rc", rc_map); - return 1; + return 0; } + +NODEMCU_MODULE(RC, "rc", rc_map, luaopen_rc); diff --git a/app/modules/rtcfifo.c b/app/modules/rtcfifo.c index 20492ffa..4e0e54ab 100644 --- a/app/modules/rtcfifo.c +++ b/app/modules/rtcfifo.c @@ -1,5 +1,6 @@ // Module for RTC sample FIFO storage +#include "module.h" #include "lauxlib.h" #include "user_modules.h" #include "rtc/rtctime.h" @@ -164,10 +165,7 @@ 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[] = -{ +static const LUA_REG_TYPE rtcfifo_map[] = { { LSTRKEY("prepare"), LFUNCVAL(rtcfifo_prepare) }, { LSTRKEY("ready"), LFUNCVAL(rtcfifo_ready) }, { LSTRKEY("put"), LFUNCVAL(rtcfifo_put) }, @@ -181,12 +179,4 @@ const LUA_REG_TYPE rtcfifo_map[] = { LNILKEY, LNILVAL } }; -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 -} +NODEMCU_MODULE(RTCFIFO, "rtcfifo", rtcfifo_map, NULL); diff --git a/app/modules/rtcmem.c b/app/modules/rtcmem.c index 4baa9c68..1aea2acb 100644 --- a/app/modules/rtcmem.c +++ b/app/modules/rtcmem.c @@ -1,5 +1,6 @@ // Module for RTC user memory access +#include "module.h" #include "lauxlib.h" #include "rtc/rtcaccess.h" @@ -40,21 +41,10 @@ static int rtcmem_write32 (lua_State *L) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -const LUA_REG_TYPE rtcmem_map[] = -{ +static const LUA_REG_TYPE rtcmem_map[] = { { LSTRKEY("read32"), LFUNCVAL(rtcmem_read32) }, { LSTRKEY("write32"), LFUNCVAL(rtcmem_write32) }, { LNILKEY, LNILVAL } }; -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 -} +NODEMCU_MODULE(RTCMEM, "rtcmem", rtcmem_map, NULL); diff --git a/app/modules/rtctime.c b/app/modules/rtctime.c index 2ee298dd..76ca4392 100644 --- a/app/modules/rtctime.c +++ b/app/modules/rtctime.c @@ -1,5 +1,6 @@ // Module for RTC time keeping +#include "module.h" #include "lauxlib.h" #include "rtc/rtctime_internal.h" @@ -115,23 +116,12 @@ 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) }, - { LSTRKEY("get"), LFUNCVAL(rtctime_get) }, - { LSTRKEY("dsleep"), LFUNCVAL(rtctime_dsleep) }, +static const LUA_REG_TYPE rtctime_map[] = { + { LSTRKEY("set"), LFUNCVAL(rtctime_set) }, + { LSTRKEY("get"), LFUNCVAL(rtctime_get) }, + { LSTRKEY("dsleep"), LFUNCVAL(rtctime_dsleep) }, { LSTRKEY("dsleep_aligned"), LFUNCVAL(rtctime_dsleep_aligned) }, { LNILKEY, LNILVAL } }; -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 -} +NODEMCU_MODULE(RTCTIME, "rtctime", rtctime_map, NULL); diff --git a/app/modules/sntp.c b/app/modules/sntp.c index 0f08301c..afffe4c9 100644 --- a/app/modules/sntp.c +++ b/app/modules/sntp.c @@ -33,6 +33,7 @@ // Module for Simple Network Time Protocol (SNTP) +#include "module.h" #include "lauxlib.h" #include "os_type.h" #include "osapi.h" @@ -375,20 +376,9 @@ error: // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -const LUA_REG_TYPE sntp_map[] = -{ +static const LUA_REG_TYPE sntp_map[] = { { LSTRKEY("sync"), LFUNCVAL(sntp_sync) }, { LNILKEY, LNILVAL } }; -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 -} +NODEMCU_MODULE(SNTP, "sntp", sntp_map, NULL); diff --git a/app/modules/spi.c b/app/modules/spi.c index c7453e7d..f7e8a853 100644 --- a/app/modules/spi.c +++ b/app/modules/spi.c @@ -1,11 +1,8 @@ // Module for interfacing with the SPI interface -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #define SPI_HALFDUPLEX 0 #define SPI_FULLDUPLEX 1 @@ -313,48 +310,23 @@ static int spi_transaction( lua_State *L ) // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -const LUA_REG_TYPE spi_map[] = -{ +static const LUA_REG_TYPE spi_map[] = { { LSTRKEY( "setup" ), LFUNCVAL( spi_setup ) }, { LSTRKEY( "send" ), LFUNCVAL( spi_send_recv ) }, { LSTRKEY( "recv" ), LFUNCVAL( spi_recv ) }, { 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) }, - { LSTRKEY( "CPHA_HIGH" ), LNUMVAL( PLATFORM_SPI_CPHA_HIGH) }, - { LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) }, - { LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) }, - { LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) }, - { LSTRKEY( "HALFDUPLEX" ), LNUMVAL( SPI_HALFDUPLEX ) }, - { LSTRKEY( "FULLDUPLEX" ), LNUMVAL( SPI_FULLDUPLEX ) }, -#endif // #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) }, + { LSTRKEY( "CPHA_HIGH" ), LNUMVAL( PLATFORM_SPI_CPHA_HIGH) }, + { LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) }, + { LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) }, + { LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) }, + { LSTRKEY( "HALFDUPLEX" ), LNUMVAL( SPI_HALFDUPLEX ) }, + { LSTRKEY( "FULLDUPLEX" ), LNUMVAL( SPI_FULLDUPLEX ) }, { 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 -} +NODEMCU_MODULE(SPI, "spi", spi_map, NULL); diff --git a/app/modules/tmr.c b/app/modules/tmr.c index cad2e8dd..cb13d45e 100755 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -48,14 +48,9 @@ tmr.softwd(int) the timer units are seconds */ -#define MIN_OPT_LEVEL 2 - -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" -#include "lrodefs.h" #include "c_types.h" #define TIMER_MODE_OFF 3 @@ -311,28 +306,26 @@ static int tmr_softwd( lua_State* L ){ // Module function map -const LUA_REG_TYPE tmr_map[] = { - { LSTRKEY( "delay" ), LFUNCVAL( tmr_delay ) }, - { LSTRKEY( "now" ), LFUNCVAL( tmr_now ) }, - { LSTRKEY( "wdclr" ), LFUNCVAL( tmr_wdclr ) }, - { LSTRKEY( "softwd" ), LFUNCVAL( tmr_softwd ) }, - { LSTRKEY( "time" ), LFUNCVAL( tmr_time ) }, - { LSTRKEY( "register" ), LFUNCVAL ( tmr_register ) }, - { LSTRKEY( "alarm" ), LFUNCVAL( tmr_alarm ) }, - { LSTRKEY( "start" ), LFUNCVAL ( tmr_start ) }, - { LSTRKEY( "stop" ), LFUNCVAL ( tmr_stop ) }, - { LSTRKEY( "unregister" ), LFUNCVAL ( tmr_unregister ) }, - { LSTRKEY( "state" ), LFUNCVAL ( tmr_state ) }, - { LSTRKEY( "interval" ), LFUNCVAL ( tmr_interval) }, -#if LUA_OPTIMIZE_MEMORY > 0 +static const LUA_REG_TYPE tmr_map[] = { + { LSTRKEY( "delay" ), LFUNCVAL( tmr_delay ) }, + { LSTRKEY( "now" ), LFUNCVAL( tmr_now ) }, + { LSTRKEY( "wdclr" ), LFUNCVAL( tmr_wdclr ) }, + { LSTRKEY( "softwd" ), LFUNCVAL( tmr_softwd ) }, + { LSTRKEY( "time" ), LFUNCVAL( tmr_time ) }, + { LSTRKEY( "register" ), LFUNCVAL( tmr_register ) }, + { LSTRKEY( "alarm" ), LFUNCVAL( tmr_alarm ) }, + { LSTRKEY( "start" ), LFUNCVAL( tmr_start ) }, + { LSTRKEY( "stop" ), LFUNCVAL( tmr_stop ) }, + { LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) }, + { LSTRKEY( "state" ), LFUNCVAL( tmr_state ) }, + { LSTRKEY( "interval" ), LFUNCVAL( tmr_interval) }, { LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) }, - { LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) }, - { LSTRKEY( "ALARM_AUTO" ), LNUMVAL( TIMER_MODE_AUTO ) }, -#endif + { LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) }, + { LSTRKEY( "ALARM_AUTO" ), LNUMVAL( TIMER_MODE_AUTO ) }, { LNILKEY, LNILVAL } }; -LUALIB_API int luaopen_tmr( lua_State *L ){ +int luaopen_tmr( lua_State *L ){ int i; for(i=0; i 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 + return 0; } +NODEMCU_MODULE(TMR, "tmr", tmr_map, luaopen_tmr); diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index a0abbb96..77a72163 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 "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "../tsl2561/tsl2561.h" static uint16_t ch0; @@ -102,38 +100,28 @@ 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)}, - { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, - { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, - { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, - - { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK )}, +// Module function map +static const LUA_REG_TYPE tsl2561_map[] = { + { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, + { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, + { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, + { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, + { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK )}, { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT )}, { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY )}, - { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT )}, - { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST )}, - - { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS )}, + { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT )}, + { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST )}, + { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS )}, { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS )}, { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS )}, - { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X )}, - { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X )}, - - { LSTRKEY( "PACKAGE_CS" ), LNUMVAL( TSL2561_PACKAGE_CS )}, - { LSTRKEY( "PACKAGE_T_FN_CL" ), LNUMVAL( TSL2561_PACKAGE_T_FN_CL )}, - - { LSTRKEY( "ADDRESS_GND" ), LNUMVAL( TSL2561_ADDRESS_GND )}, - { LSTRKEY( "ADDRESS_FLOAT" ), LNUMVAL( TSL2561_ADDRESS_FLOAT )}, - { LSTRKEY( "ADDRESS_VDD" ), LNUMVAL( TSL2561_ADDRESS_VDD )}, - + { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X )}, + { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X )}, + { LSTRKEY( "PACKAGE_CS" ), LNUMVAL( TSL2561_PACKAGE_CS )}, + { LSTRKEY( "PACKAGE_T_FN_CL" ), LNUMVAL( TSL2561_PACKAGE_T_FN_CL )}, + { LSTRKEY( "ADDRESS_GND" ), LNUMVAL( TSL2561_ADDRESS_GND )}, + { LSTRKEY( "ADDRESS_FLOAT" ), LNUMVAL( TSL2561_ADDRESS_FLOAT )}, + { LSTRKEY( "ADDRESS_VDD" ), LNUMVAL( TSL2561_ADDRESS_VDD )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_tsl2561(lua_State *L) { - LREGISTER(L, "tsl2561", tsl2561_map); - return 1; -} +NODEMCU_MODULE(TSL2561, "tsl2561", tsl2561_map, NULL); diff --git a/app/modules/u8g.c b/app/modules/u8g.c index 0397e23a..1600dc10 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -1,10 +1,8 @@ // Module for U8glib -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" @@ -1027,138 +1025,90 @@ 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 ) }, - { LSTRKEY( "drawBitmap" ), LFUNCVAL( lu8g_drawBitmap ) }, - { LSTRKEY( "drawBox" ), LFUNCVAL( lu8g_drawBox ) }, - { LSTRKEY( "drawCircle" ), LFUNCVAL( lu8g_drawCircle ) }, - { LSTRKEY( "drawDisc" ), LFUNCVAL( lu8g_drawDisc ) }, - { LSTRKEY( "drawEllipse" ), LFUNCVAL( lu8g_drawEllipse ) }, - { LSTRKEY( "drawFilledEllipse" ), LFUNCVAL( lu8g_drawFilledEllipse ) }, - { LSTRKEY( "drawFrame" ), LFUNCVAL( lu8g_drawFrame ) }, - { LSTRKEY( "drawHLine" ), LFUNCVAL( lu8g_drawHLine ) }, - { LSTRKEY( "drawLine" ), LFUNCVAL( lu8g_drawLine ) }, - { LSTRKEY( "drawPixel" ), LFUNCVAL( lu8g_drawPixel ) }, - { LSTRKEY( "drawRBox" ), LFUNCVAL( lu8g_drawRBox ) }, - { LSTRKEY( "drawRFrame" ), LFUNCVAL( lu8g_drawRFrame ) }, - { LSTRKEY( "drawStr" ), LFUNCVAL( lu8g_drawStr ) }, - { LSTRKEY( "drawStr90" ), LFUNCVAL( lu8g_drawStr90 ) }, - { LSTRKEY( "drawStr180" ), LFUNCVAL( lu8g_drawStr180 ) }, - { LSTRKEY( "drawStr270" ), LFUNCVAL( lu8g_drawStr270 ) }, - { LSTRKEY( "drawTriangle" ), LFUNCVAL( lu8g_drawTriangle ) }, - { LSTRKEY( "drawVLine" ), LFUNCVAL( lu8g_drawVLine ) }, - { LSTRKEY( "drawXBM" ), LFUNCVAL( lu8g_drawXBM ) }, - { LSTRKEY( "firstPage" ), LFUNCVAL( lu8g_firstPage ) }, - { LSTRKEY( "getColorIndex" ), LFUNCVAL( lu8g_getColorIndex ) }, - { LSTRKEY( "getFontAscent" ), LFUNCVAL( lu8g_getFontAscent ) }, - { LSTRKEY( "getFontDescent" ), LFUNCVAL( lu8g_getFontDescent ) }, - { LSTRKEY( "getFontLineSpacing" ), LFUNCVAL( lu8g_getFontLineSpacing ) }, - { LSTRKEY( "getHeight" ), LFUNCVAL( lu8g_getHeight ) }, - { LSTRKEY( "getMode" ), LFUNCVAL( lu8g_getMode ) }, - { LSTRKEY( "getStrWidth" ), LFUNCVAL( lu8g_getStrWidth ) }, - { LSTRKEY( "getWidth" ), LFUNCVAL( lu8g_getWidth ) }, - { LSTRKEY( "nextPage" ), LFUNCVAL( lu8g_nextPage ) }, - { LSTRKEY( "setColorIndex" ), LFUNCVAL( lu8g_setColorIndex ) }, - { LSTRKEY( "setDefaultBackgroundColor" ), LFUNCVAL( lu8g_setDefaultBackgroundColor ) }, - { LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) }, - { LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) }, - { LSTRKEY( "setFontLineSpacingFactor" ), LFUNCVAL( lu8g_setFontLineSpacingFactor ) }, - { LSTRKEY( "setFontPosBaseline" ), LFUNCVAL( lu8g_setFontPosBaseline ) }, - { LSTRKEY( "setFontPosBottom" ), LFUNCVAL( lu8g_setFontPosBottom ) }, - { LSTRKEY( "setFontPosCenter" ), LFUNCVAL( lu8g_setFontPosCenter ) }, - { LSTRKEY( "setFontPosTop" ), LFUNCVAL( lu8g_setFontPosTop ) }, - { LSTRKEY( "setFontRefHeightAll" ), LFUNCVAL( lu8g_setFontRefHeightAll ) }, - { LSTRKEY( "setFontRefHeightExtendedText" ), LFUNCVAL( lu8g_setFontRefHeightExtendedText ) }, - { LSTRKEY( "setFontRefHeightText" ), LFUNCVAL( lu8g_setFontRefHeightText ) }, - { LSTRKEY( "setRot90" ), LFUNCVAL( lu8g_setRot90 ) }, - { LSTRKEY( "setRot180" ), LFUNCVAL( lu8g_setRot180 ) }, - { LSTRKEY( "setRot270" ), LFUNCVAL( lu8g_setRot270 ) }, - { LSTRKEY( "setScale2x2" ), LFUNCVAL( lu8g_setScale2x2 ) }, - { LSTRKEY( "sleepOff" ), LFUNCVAL( lu8g_sleepOff ) }, - { LSTRKEY( "sleepOn" ), LFUNCVAL( lu8g_sleepOn ) }, - { 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 } +static const LUA_REG_TYPE lu8g_display_map[] = { + { LSTRKEY( "begin" ), LFUNCVAL( lu8g_begin ) }, + { LSTRKEY( "drawBitmap" ), LFUNCVAL( lu8g_drawBitmap ) }, + { LSTRKEY( "drawBox" ), LFUNCVAL( lu8g_drawBox ) }, + { LSTRKEY( "drawCircle" ), LFUNCVAL( lu8g_drawCircle ) }, + { LSTRKEY( "drawDisc" ), LFUNCVAL( lu8g_drawDisc ) }, + { LSTRKEY( "drawEllipse" ), LFUNCVAL( lu8g_drawEllipse ) }, + { LSTRKEY( "drawFilledEllipse" ), LFUNCVAL( lu8g_drawFilledEllipse ) }, + { LSTRKEY( "drawFrame" ), LFUNCVAL( lu8g_drawFrame ) }, + { LSTRKEY( "drawHLine" ), LFUNCVAL( lu8g_drawHLine ) }, + { LSTRKEY( "drawLine" ), LFUNCVAL( lu8g_drawLine ) }, + { LSTRKEY( "drawPixel" ), LFUNCVAL( lu8g_drawPixel ) }, + { LSTRKEY( "drawRBox" ), LFUNCVAL( lu8g_drawRBox ) }, + { LSTRKEY( "drawRFrame" ), LFUNCVAL( lu8g_drawRFrame ) }, + { LSTRKEY( "drawStr" ), LFUNCVAL( lu8g_drawStr ) }, + { LSTRKEY( "drawStr90" ), LFUNCVAL( lu8g_drawStr90 ) }, + { LSTRKEY( "drawStr180" ), LFUNCVAL( lu8g_drawStr180 ) }, + { LSTRKEY( "drawStr270" ), LFUNCVAL( lu8g_drawStr270 ) }, + { LSTRKEY( "drawTriangle" ), LFUNCVAL( lu8g_drawTriangle ) }, + { LSTRKEY( "drawVLine" ), LFUNCVAL( lu8g_drawVLine ) }, + { LSTRKEY( "drawXBM" ), LFUNCVAL( lu8g_drawXBM ) }, + { LSTRKEY( "firstPage" ), LFUNCVAL( lu8g_firstPage ) }, + { LSTRKEY( "getColorIndex" ), LFUNCVAL( lu8g_getColorIndex ) }, + { LSTRKEY( "getFontAscent" ), LFUNCVAL( lu8g_getFontAscent ) }, + { LSTRKEY( "getFontDescent" ), LFUNCVAL( lu8g_getFontDescent ) }, + { LSTRKEY( "getFontLineSpacing" ), LFUNCVAL( lu8g_getFontLineSpacing ) }, + { LSTRKEY( "getHeight" ), LFUNCVAL( lu8g_getHeight ) }, + { LSTRKEY( "getMode" ), LFUNCVAL( lu8g_getMode ) }, + { LSTRKEY( "getStrWidth" ), LFUNCVAL( lu8g_getStrWidth ) }, + { LSTRKEY( "getWidth" ), LFUNCVAL( lu8g_getWidth ) }, + { LSTRKEY( "nextPage" ), LFUNCVAL( lu8g_nextPage ) }, + { LSTRKEY( "setColorIndex" ), LFUNCVAL( lu8g_setColorIndex ) }, + { LSTRKEY( "setDefaultBackgroundColor" ), LFUNCVAL( lu8g_setDefaultBackgroundColor ) }, + { LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) }, + { LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) }, + { LSTRKEY( "setFontLineSpacingFactor" ), LFUNCVAL( lu8g_setFontLineSpacingFactor ) }, + { LSTRKEY( "setFontPosBaseline" ), LFUNCVAL( lu8g_setFontPosBaseline ) }, + { LSTRKEY( "setFontPosBottom" ), LFUNCVAL( lu8g_setFontPosBottom ) }, + { LSTRKEY( "setFontPosCenter" ), LFUNCVAL( lu8g_setFontPosCenter ) }, + { LSTRKEY( "setFontPosTop" ), LFUNCVAL( lu8g_setFontPosTop ) }, + { LSTRKEY( "setFontRefHeightAll" ), LFUNCVAL( lu8g_setFontRefHeightAll ) }, + { LSTRKEY( "setFontRefHeightExtendedText" ), LFUNCVAL( lu8g_setFontRefHeightExtendedText ) }, + { LSTRKEY( "setFontRefHeightText" ), LFUNCVAL( lu8g_setFontRefHeightText ) }, + { LSTRKEY( "setRot90" ), LFUNCVAL( lu8g_setRot90 ) }, + { LSTRKEY( "setRot180" ), LFUNCVAL( lu8g_setRot180 ) }, + { LSTRKEY( "setRot270" ), LFUNCVAL( lu8g_setRot270 ) }, + { LSTRKEY( "setScale2x2" ), LFUNCVAL( lu8g_setScale2x2 ) }, + { LSTRKEY( "sleepOff" ), LFUNCVAL( lu8g_sleepOff ) }, + { LSTRKEY( "sleepOn" ), LFUNCVAL( lu8g_sleepOn ) }, + { LSTRKEY( "undoRotation" ), LFUNCVAL( lu8g_undoRotation ) }, + { LSTRKEY( "undoScale" ), LFUNCVAL( lu8g_undoScale ) }, + { LSTRKEY( "__gc" ), LFUNCVAL( lu8g_close_display ) }, + { LSTRKEY( "__index" ), LROVAL( lu8g_display_map ) }, + { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE lu8g_map[] = -{ #undef U8G_DISPLAY_TABLE_ENTRY -#define U8G_DISPLAY_TABLE_ENTRY(device) { LSTRKEY( #device ), LFUNCVAL ( lu8g_ ##device ) }, - 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) ) }, - U8G_FONT_TABLE - // Options for circle/ ellipse drawing - { LSTRKEY( "DRAW_UPPER_RIGHT" ), LNUMVAL( U8G_DRAW_UPPER_RIGHT ) }, - { LSTRKEY( "DRAW_UPPER_LEFT" ), LNUMVAL( U8G_DRAW_UPPER_LEFT ) }, - { LSTRKEY( "DRAW_LOWER_RIGHT" ), LNUMVAL( U8G_DRAW_LOWER_RIGHT ) }, - { LSTRKEY( "DRAW_LOWER_LEFT" ), LNUMVAL( U8G_DRAW_LOWER_LEFT ) }, - { LSTRKEY( "DRAW_ALL" ), LNUMVAL( U8G_DRAW_ALL ) }, - - // Display modes - { LSTRKEY( "MODE_BW" ), LNUMVAL( U8G_MODE_BW ) }, - { LSTRKEY( "MODE_GRAY2BIT" ), LNUMVAL( U8G_MODE_GRAY2BIT ) }, - - { LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) }, -#endif - { LNILKEY, LNILVAL } +static const LUA_REG_TYPE lu8g_map[] = { +#define U8G_DISPLAY_TABLE_ENTRY(device) \ + { LSTRKEY( #device ), LFUNCVAL ( lu8g_ ##device ) }, + U8G_DISPLAY_TABLE_I2C + U8G_DISPLAY_TABLE_SPI +// Register fonts +#define U8G_FONT_TABLE_ENTRY(font) \ + { LSTRKEY( #font ), LUDATA( (void *)(u8g_ ## font) ) }, + U8G_FONT_TABLE + // Options for circle/ ellipse drawing + { LSTRKEY( "DRAW_UPPER_RIGHT" ), LNUMVAL( U8G_DRAW_UPPER_RIGHT ) }, + { LSTRKEY( "DRAW_UPPER_LEFT" ), LNUMVAL( U8G_DRAW_UPPER_LEFT ) }, + { LSTRKEY( "DRAW_LOWER_RIGHT" ), LNUMVAL( U8G_DRAW_LOWER_RIGHT ) }, + { LSTRKEY( "DRAW_LOWER_LEFT" ), LNUMVAL( U8G_DRAW_LOWER_LEFT ) }, + { LSTRKEY( "DRAW_ALL" ), LNUMVAL( U8G_DRAW_ALL ) }, + // Display modes + { LSTRKEY( "MODE_BW" ), LNUMVAL( U8G_MODE_BW ) }, + { LSTRKEY( "MODE_GRAY2BIT" ), LNUMVAL( U8G_MODE_GRAY2BIT ) }, + { LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) }, + { 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 +int luaopen_u8g( lua_State *L ) { + luaL_rometatable(L, "u8g.display", (void *)lu8g_display_map); // create metatable + return 0; } + +NODEMCU_MODULE(U8G, "u8g", lu8g_map, luaopen_u8g); diff --git a/app/modules/uart.c b/app/modules/uart.c index c4fdcc6f..150c4051 100755 --- a/app/modules/uart.c +++ b/app/modules/uart.c @@ -1,11 +1,8 @@ // Module for interfacing with serial -//#include "lua.h" -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_types.h" #include "c_string.h" @@ -158,29 +155,12 @@ static int uart_write( lua_State* L ) } // Module function map -#define MIN_OPT_LEVEL 2 -#include "lrodefs.h" -const LUA_REG_TYPE uart_map[] = -{ +static 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 + { LSTRKEY( "write" ), LFUNCVAL( uart_write ) }, + { LSTRKEY( "on" ), LFUNCVAL( uart_on ) }, + { LSTRKEY( "alt" ), LFUNCVAL( uart_alt ) }, { 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 -} +NODEMCU_MODULE(UART, "uart", uart_map, NULL); diff --git a/app/modules/ucg.c b/app/modules/ucg.c index 3d51265f..9c0506d2 100644 --- a/app/modules/ucg.c +++ b/app/modules/ucg.c @@ -1,10 +1,8 @@ // Module for Ucglib -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.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,20 +921,16 @@ 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 } }; -const LUA_REG_TYPE lucg_map[] = +static const LUA_REG_TYPE lucg_map[] = { #undef UCG_DISPLAY_TABLE_ENTRY #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,13 @@ 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 ) +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 } + +NODEMCU_MODULE(UCG, "ucg", lucg_map, luaopen_ucg); diff --git a/app/modules/wifi.c b/app/modules/wifi.c index 9d84ce50..79af7c0b 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 "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_string.h" #include "c_stdlib.h" @@ -1340,148 +1337,88 @@ 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 ) }, - { LSTRKEY( "config" ), LFUNCVAL ( wifi_station_config ) }, - { LSTRKEY( "connect" ), LFUNCVAL ( wifi_station_connect4lua ) }, - { LSTRKEY( "disconnect" ), LFUNCVAL ( wifi_station_disconnect4lua ) }, - { LSTRKEY( "autoconnect" ), LFUNCVAL ( wifi_station_setauto ) }, - { LSTRKEY( "getip" ), LFUNCVAL ( wifi_station_getip ) }, - { LSTRKEY( "setip" ), LFUNCVAL ( wifi_station_setip ) }, - { LSTRKEY( "getbroadcast" ), LFUNCVAL ( wifi_station_getbroadcast) }, - { LSTRKEY( "getmac" ), LFUNCVAL ( wifi_station_getmac ) }, - { LSTRKEY( "setmac" ), LFUNCVAL ( wifi_station_setmac ) }, - { LSTRKEY( "getap" ), LFUNCVAL ( wifi_station_listap ) }, - { LSTRKEY( "status" ), LFUNCVAL ( wifi_station_status ) }, - { LSTRKEY( "eventMonReg" ), LFUNCVAL ( wifi_station_event_mon_reg ) }, - { LSTRKEY( "eventMonStart" ), LFUNCVAL ( wifi_station_event_mon_start ) }, - { LSTRKEY( "eventMonStop" ), LFUNCVAL ( wifi_station_event_mon_stop ) }, +static const LUA_REG_TYPE wifi_station_map[] = { + { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_station_getconfig ) }, + { LSTRKEY( "config" ), LFUNCVAL( wifi_station_config ) }, + { LSTRKEY( "connect" ), LFUNCVAL( wifi_station_connect4lua ) }, + { LSTRKEY( "disconnect" ), LFUNCVAL( wifi_station_disconnect4lua ) }, + { LSTRKEY( "autoconnect" ), LFUNCVAL( wifi_station_setauto ) }, + { LSTRKEY( "getip" ), LFUNCVAL( wifi_station_getip ) }, + { LSTRKEY( "setip" ), LFUNCVAL( wifi_station_setip ) }, + { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_station_getbroadcast) }, + { LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) }, + { LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) }, + { LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) }, + { LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) }, + { LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) }, + { LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) }, + { LSTRKEY( "eventMonStop" ), LFUNCVAL( wifi_station_event_mon_stop ) }, { LNILKEY, LNILVAL } }; -static const LUA_REG_TYPE wifi_ap_dhcp_map[] = -{ - { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_dhcp_config ) }, - { LSTRKEY( "start" ), LFUNCVAL( wifi_ap_dhcp_start ) }, - { LSTRKEY( "stop" ), LFUNCVAL( wifi_ap_dhcp_stop ) }, +static const LUA_REG_TYPE wifi_ap_dhcp_map[] = { + { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_dhcp_config ) }, + { LSTRKEY( "start" ), LFUNCVAL( wifi_ap_dhcp_start ) }, + { LSTRKEY( "stop" ), LFUNCVAL( wifi_ap_dhcp_stop ) }, { LNILKEY, LNILVAL } }; -static const LUA_REG_TYPE wifi_ap_map[] = -{ - { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) }, - { LSTRKEY( "getip" ), LFUNCVAL ( wifi_ap_getip ) }, - { LSTRKEY( "setip" ), LFUNCVAL ( wifi_ap_setip ) }, - { LSTRKEY( "getbroadcast" ), LFUNCVAL ( wifi_ap_getbroadcast) }, - { LSTRKEY( "getmac" ), LFUNCVAL ( wifi_ap_getmac ) }, - { 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 +static const LUA_REG_TYPE wifi_ap_map[] = { + { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) }, + { LSTRKEY( "getip" ), LFUNCVAL( wifi_ap_getip ) }, + { LSTRKEY( "setip" ), LFUNCVAL( wifi_ap_setip ) }, + { LSTRKEY( "getbroadcast" ), LFUNCVAL( wifi_ap_getbroadcast) }, + { LSTRKEY( "getmac" ), LFUNCVAL( wifi_ap_getmac ) }, + { LSTRKEY( "setmac" ), LFUNCVAL( wifi_ap_setmac ) }, + { LSTRKEY( "getclient" ), LFUNCVAL( wifi_ap_listclient ) }, + { LSTRKEY( "getconfig" ), LFUNCVAL( wifi_ap_getconfig ) }, + { LSTRKEY( "dhcp" ), LROVAL( wifi_ap_dhcp_map ) }, +//{ LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) }, { LNILKEY, LNILVAL } }; -const LUA_REG_TYPE wifi_map[] = -{ - { LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) }, - { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) }, - { LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) }, - { LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) }, - { LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) }, - { LSTRKEY( "sleep" ), LFUNCVAL( wifi_sleep ) }, - { 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 ) }, +static const LUA_REG_TYPE wifi_map[] = { + { LSTRKEY( "setmode" ), LFUNCVAL( wifi_setmode ) }, + { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) }, + { LSTRKEY( "getchannel" ), LFUNCVAL( wifi_getchannel ) }, + { LSTRKEY( "setphymode" ), LFUNCVAL( wifi_setphymode ) }, + { LSTRKEY( "getphymode" ), LFUNCVAL( wifi_getphymode ) }, + { LSTRKEY( "sleep" ), LFUNCVAL( wifi_sleep ) }, + { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) }, + { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) }, + { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) }, - { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) }, - { LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) }, - { LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) }, - { LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) }, + { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) }, + { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) }, - { LSTRKEY( "PHYMODE_B" ), LNUMVAL( PHY_MODE_11B ) }, - { LSTRKEY( "PHYMODE_G" ), LNUMVAL( PHY_MODE_11G ) }, - { LSTRKEY( "PHYMODE_N" ), LNUMVAL( PHY_MODE_11N ) }, + { LSTRKEY( "NULLMODE" ), LNUMVAL( NULL_MODE ) }, + { LSTRKEY( "STATION" ), LNUMVAL( STATION_MODE ) }, + { LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) }, + { LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) }, - { LSTRKEY( "NONE_SLEEP" ), LNUMVAL( NONE_SLEEP_T ) }, - { LSTRKEY( "LIGHT_SLEEP" ), LNUMVAL( LIGHT_SLEEP_T ) }, - { LSTRKEY( "MODEM_SLEEP" ), LNUMVAL( MODEM_SLEEP_T ) }, + { LSTRKEY( "PHYMODE_B" ), LNUMVAL( PHY_MODE_11B ) }, + { LSTRKEY( "PHYMODE_G" ), LNUMVAL( PHY_MODE_11G ) }, + { LSTRKEY( "PHYMODE_N" ), LNUMVAL( PHY_MODE_11N ) }, - { LSTRKEY( "OPEN" ), LNUMVAL( AUTH_OPEN ) }, - // { LSTRKEY( "WEP" ), LNUMVAL( AUTH_WEP ) }, - { LSTRKEY( "WPA_PSK" ), LNUMVAL( AUTH_WPA_PSK ) }, - { LSTRKEY( "WPA2_PSK" ), LNUMVAL( AUTH_WPA2_PSK ) }, - { LSTRKEY( "WPA_WPA2_PSK" ), LNUMVAL( AUTH_WPA_WPA2_PSK ) }, + { LSTRKEY( "NONE_SLEEP" ), LNUMVAL( NONE_SLEEP_T ) }, + { LSTRKEY( "LIGHT_SLEEP" ), LNUMVAL( LIGHT_SLEEP_T ) }, + { LSTRKEY( "MODEM_SLEEP" ), LNUMVAL( MODEM_SLEEP_T ) }, - { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) }, - { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) }, - { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) }, - { LSTRKEY( "STA_APNOTFOUND" ), LNUMVAL( STATION_NO_AP_FOUND ) }, - { LSTRKEY( "STA_FAIL" ), LNUMVAL( STATION_CONNECT_FAIL ) }, - { LSTRKEY( "STA_GOTIP" ), LNUMVAL( STATION_GOT_IP ) }, + { LSTRKEY( "OPEN" ), LNUMVAL( AUTH_OPEN ) }, +//{ LSTRKEY( "WEP" ), LNUMVAL( AUTH_WEP ) }, + { LSTRKEY( "WPA_PSK" ), LNUMVAL( AUTH_WPA_PSK ) }, + { LSTRKEY( "WPA2_PSK" ), LNUMVAL( AUTH_WPA2_PSK ) }, + { LSTRKEY( "WPA_WPA2_PSK" ), LNUMVAL( AUTH_WPA_WPA2_PSK ) }, - { LSTRKEY( "__metatable" ), LROVAL( wifi_map ) }, -#endif + { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) }, + { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) }, + { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) }, + { LSTRKEY( "STA_APNOTFOUND" ), LNUMVAL( STATION_NO_AP_FOUND ) }, + { LSTRKEY( "STA_FAIL" ), LNUMVAL( STATION_CONNECT_FAIL ) }, + { LSTRKEY( "STA_GOTIP" ), LNUMVAL( STATION_GOT_IP ) }, + + { LSTRKEY( "__metatable" ), LROVAL( wifi_map ) }, { 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 -} +NODEMCU_MODULE(WIFI, "wifi", wifi_map, NULL); diff --git a/app/modules/ws2801.c b/app/modules/ws2801.c index 6ba2199e..26b56211 100644 --- a/app/modules/ws2801.c +++ b/app/modules/ws2801.c @@ -1,8 +1,6 @@ -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" #include "c_string.h" @@ -124,17 +122,11 @@ 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[] = +static const LUA_REG_TYPE ws2801_map[] = { { LSTRKEY( "write" ), LFUNCVAL( ws2801_writergb )}, { LSTRKEY( "init" ), LFUNCVAL( ws2801_init_lua )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_ws2801(lua_State *L) { - LREGISTER(L, "ws2801", ws2801_map); - return 1; -} - +NODEMCU_MODULE(WS2801, "ws2801", ws2801_map, NULL); diff --git a/app/modules/ws2812.c b/app/modules/ws2812.c index 6875416d..98cd23f1 100644 --- a/app/modules/ws2812.c +++ b/app/modules/ws2812.c @@ -1,8 +1,6 @@ -#include "lualib.h" +#include "module.h" #include "lauxlib.h" #include "platform.h" -#include "auxmods.h" -#include "lrotable.h" #include "c_stdlib.h" #include "c_string.h" #include "user_interface.h" @@ -127,17 +125,16 @@ 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[] = +static const LUA_REG_TYPE ws2812_map[] = { { LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )}, { LSTRKEY( "write" ), LFUNCVAL( ws2812_writegrb )}, { LNILKEY, LNILVAL} }; -LUALIB_API int luaopen_ws2812(lua_State *L) { +int luaopen_ws2812(lua_State *L) { // TODO: Make sure that the GPIO system is initialized - LREGISTER(L, "ws2812", ws2812_map); - return 1; + return 0; } + +NODEMCU_MODULE(WS2812, "ws2812", ws2812_map, luaopen_ws2812); diff --git a/app/platform/platform.h b/app/platform/platform.h index 47f0b5a7..58bdbe05 100644 --- a/app/platform/platform.h +++ b/app/platform/platform.h @@ -253,4 +253,20 @@ uint32_t platform_flash_mapped2phys (uint32_t mapped_addr); void* platform_get_first_free_ram( unsigned id ); void* platform_get_last_free_ram( unsigned id ); +// ***************************************************************************** +// 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/ld/nodemcu.ld b/ld/nodemcu.ld index 2588e72c..8c4ee61a 100644 --- a/ld/nodemcu.ld +++ b/ld/nodemcu.ld @@ -79,6 +79,16 @@ SECTIONS *(.rodata*) *(.sdk.version) + /* Link-time arrays containing the defs for the included modules */ + . = ALIGN(4); + lua_libs = ABSOLUTE(.); + /* Allow either empty define or defined-to-1 to include the module */ + KEEP(*(.lua_libs)) + LONG(0) LONG(0) /* Null-terminate the array */ + lua_rotable = ABSOLUTE(.); + 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 */ */libc.a:lib_a-isalnum.o(.text* .literal*) */libc.a:lib_a-isalpha.o(.text* .literal*)