module.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef __MODULE_H__
  2. #define __MODULE_H__
  3. #include "user_modules.h"
  4. #include "lrodefs.h"
  5. /* Registering a module within NodeMCU is really easy these days!
  6. *
  7. * Most of the work is done by a combination of pre-processor, compiler
  8. * and linker "magic". Gone are the days of needing to update 4+ separate
  9. * files just to register a module!
  10. *
  11. * You will need:
  12. * - to include this header
  13. * - a name for the module
  14. * - a LUA_REG_TYPE module map
  15. * - optionally, an init function
  16. *
  17. * Then simply put a line like this at the bottom of your module file:
  18. *
  19. * NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname);
  20. *
  21. * or perhaps
  22. *
  23. * NODEMCU_MODULE(MYNAME, "myname", myname_map, NULL);
  24. *
  25. * if you don't need an init function.
  26. *
  27. * When you've done this, the module can be enabled in user_modules.h with:
  28. *
  29. * #define LUA_USE_MODULES_MYNAME
  30. *
  31. * and within NodeMCU you access it with myname.foo(), assuming you have
  32. * a foo function in your module.
  33. */
  34. #define MODULE_EXPAND_(x) x
  35. #define MODULE_PASTE_(x,y) x##y
  36. #define MODULE_EXPAND_PASTE_(x,y) MODULE_PASTE_(x,y)
  37. #define LOCK_IN_SECTION(s) __attribute__((used,unused,section(s)))
  38. /* For the ROM table, we name the variable according to ( | denotes concat):
  39. * cfgname | _module_selected | LUA_USE_MODULES_##cfgname
  40. * where the LUA_USE_MODULES_XYZ macro is first expanded to yield either
  41. * an empty string (or 1) if the module has been enabled, or the literal
  42. * LUA_USE_MOUDLE_XYZ in the case it hasn't. Thus, the name of the variable
  43. * ends up looking either like XYZ_module_enabled, or if not enabled,
  44. * XYZ_module_enabledLUA_USE_MODULES_XYZ. This forms the basis for
  45. * letting the build system detect automatically (via nm) which modules need
  46. * to be linked in.
  47. */
  48. #define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \
  49. const LOCK_IN_SECTION(".lua_libs") \
  50. luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \
  51. const LOCK_IN_SECTION(".lua_rotable") \
  52. luaR_table MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(LUA_USE_MODULES_,cfgname))) \
  53. = { luaname, map }
  54. /* System module registration support, not using LUA_USE_MODULES_XYZ. */
  55. #define BUILTIN_LIB_INIT(name, luaname, initfunc) \
  56. const LOCK_IN_SECTION(".lua_libs") \
  57. luaL_Reg MODULE_PASTE_(lua_lib_,name) = { luaname, initfunc }
  58. #define BUILTIN_LIB(name, luaname, map) \
  59. const LOCK_IN_SECTION(".lua_rotable") \
  60. luaR_table MODULE_PASTE_(lua_rotable_,name) = { luaname, map }
  61. #if !defined(LUA_CROSS_COMPILER) && !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2)
  62. # error "NodeMCU modules must be built with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)"
  63. #endif
  64. #endif