2016-09-16 10:10:18 +02:00
|
|
|
#ifndef __MODULE_H__
|
|
|
|
#define __MODULE_H__
|
|
|
|
|
2016-09-20 05:35:56 +02:00
|
|
|
#include "sdkconfig.h"
|
2019-07-22 11:13:43 +02:00
|
|
|
#include "lrotable.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*
|
2016-09-20 05:35:56 +02:00
|
|
|
* When you've done this, you can simply add a Kconfig entry for the module
|
|
|
|
* to control whether it gets linked in or not. With it linked in, you can
|
|
|
|
* then within NodeMCU access it with myname.foo(), assuming you have
|
2016-09-16 10:10:18 +02:00
|
|
|
* 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)
|
|
|
|
|
2019-07-22 11:13:43 +02:00
|
|
|
#ifdef LUA_CROSS_COMPILER
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
//on msvc it is necessary to go through more pre-processor hoops to get the
|
|
|
|
//section name built; string merging does not happen in the _declspecs.
|
|
|
|
//NOTE: linker magic is invoked via the magical '$' character. Caveat editor.
|
|
|
|
#define __TOKIFY(s) .rodata1$##s
|
|
|
|
#define __TOTOK(s) __TOKIFY(s)
|
|
|
|
#define __STRINGIFY(x) #x
|
|
|
|
#define __TOSTRING(x) __STRINGIFY(x)
|
|
|
|
#define __ROSECNAME(s) __TOSTRING(__TOTOK(s))
|
|
|
|
#define LOCK_IN_SECTION(s) __declspec ( allocate( __ROSECNAME(s) ) )
|
|
|
|
#else
|
|
|
|
#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(".rodata1." #s)))
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(".lua_" #s)))
|
|
|
|
#endif
|
2016-09-16 10:10:18 +02:00
|
|
|
/* For the ROM table, we name the variable according to ( | denotes concat):
|
2020-08-23 22:49:18 +02:00
|
|
|
* cfgname | _module_selected | CONFIG_NODEMCU_CMODULE_##cfgname
|
|
|
|
* where the CONFIG_NODEMCU_CMODULE_XYZ macro is first expanded to yield either
|
2016-09-16 10:10:18 +02:00
|
|
|
* an empty string (or 1) if the module has been enabled, or the literal
|
2020-08-23 22:49:18 +02:00
|
|
|
* CONFIG_NODEMCU_CMODULE_XYZ in the case it hasn't. Thus, the name of the variable
|
2016-09-16 10:10:18 +02:00
|
|
|
* ends up looking either like XYZ_module_enabled, or if not enabled,
|
2020-08-23 22:49:18 +02:00
|
|
|
* XYZ_module_enabledCONFIG_NODEMCU_CMODULE_XYZ. This forms the basis for
|
2016-09-16 10:10:18 +02:00
|
|
|
* letting the build system detect automatically (via nm) which modules need
|
|
|
|
* to be linked in.
|
|
|
|
*/
|
|
|
|
#define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \
|
2019-07-22 11:13:43 +02:00
|
|
|
const LOCK_IN_SECTION(libs) \
|
|
|
|
luaR_entry MODULE_PASTE_(lua_lib_,cfgname) = { luaname, LRO_FUNCVAL(initfunc) }; \
|
|
|
|
const LOCK_IN_SECTION(rotable) \
|
2020-08-23 22:49:18 +02:00
|
|
|
luaR_entry MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(CONFIG_NODEMCU_CMODULE_,cfgname))) \
|
2019-07-22 11:13:43 +02:00
|
|
|
= {luaname, LRO_ROVAL(map ## _map)}
|
2016-09-16 10:10:18 +02:00
|
|
|
#endif
|
2020-07-27 19:08:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
// helper stringing macros
|
|
|
|
#define xstr(s) str(s)
|
|
|
|
#define str(s) #s
|
|
|
|
|
|
|
|
// EXTMODNAME is injected by the generated component.mk
|
|
|
|
#ifdef EXTMODNAME
|
|
|
|
#define MODNAME xstr(EXTMODNAME)
|
|
|
|
#else
|
|
|
|
#define MODNAME "module"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// use NODEMCU_MODULE_METATABLE() to generate a unique metatable name for your objects:
|
|
|
|
#define NODEMCU_MODULE_METATABLE() MODULE_EXPAND_(MODNAME xstr(__COUNTER__))
|
|
|
|
|
|
|
|
// NODEMCU_MODULE_STD() defines the entry points for an external module:
|
|
|
|
#define NODEMCU_MODULE_STD() \
|
|
|
|
static const LOCK_IN_SECTION(libs) \
|
|
|
|
luaR_entry lua_lib_module = {MODNAME, LRO_FUNCVAL(module_init)}; \
|
|
|
|
const LOCK_IN_SECTION(rotable) \
|
|
|
|
luaR_entry MODULE_EXPAND_PASTE_(EXTMODNAME, _entry) = {MODNAME, LRO_ROVAL(module_map)};
|