How to add your own module

yichone edited this page Nov 30, 2016 · 6 revisions

To add your own module is easy. Just follow the steps below:

  • Add the module source code to the path: LuaNode_Esp32/components/modules
  • If you add a module, say my_module, and your code named my_module.c, then you should register your module. Use the macro named LUANODE_MODULE to register your module.
// The registration macro     
// The first parameter MY_MODULE is a string to be used for registration. Use your module name. 
// The second parameter is the module name to be required in lua, you invoke the methods in your module by using this name. 
// The third parameter is a key-value map for your module methods and their names. 
// The last parameter is a init function, which will be called when module initialize. Usually, you can set it for       
NULL. LUANODE_MODULE(MY_MODULE, "my_module", my_module_map, NULL);
  • How to add methods to your module? Add your methods as well as their name to the key-value map mentioned above.
// The module function map
// In this example, we have two custom methods for my_module. The two methods are my_module_start and my_module_stop,
// and their corresponding name is start and stop respectively.
static const LUA_REG_TYPE my_module_map[] = {
    { LSTRKEY( "start" ), LFUNCVAL( my_module_start ) },
    { LSTRKEY( "stop" ), LFUNCVAL( my_module_stop ) },
    { LNILKEY, LNILVAL }
};

The implementation of your method:

void my_module_start(void) {
    printf("My module start\n");
}

void my_module_stop(void) {
    printf("My module stop\n");
}
The methods only output test message.

After adding your module, you can invoke them in Lua like this:

my_module.start();
my_module.stop();
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.