在lua里调用CSDK里的函数步骤

  1. 首先需要在\app\elua\modules\include\ auxmods.h中声明模块名字和模块初始化函数

    1
    2
    
    #define AUXLIB_XUEHU     "xuehu"
    LUALIB_API int ( luaopen_xuehu )( lua_State *L );
    
  2. 在app\elua\platform\openat\include\ platform_conf.h文件中完成将模块名和初始化函数注册到lua内核中

    1
    
    _ROM( AUXLIB_XUEHU, luaopen_xuehu, xuehu_map ) \
    
  3. 注册有哪些函数

    app\elua\modules\src新建xuehu.c

    cmakelist添加src/xuehu.c

    xuehu.c

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    // Module for interfacing with Xuehu
    
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    #include "lplatform.h"
    #include "auxmods.h"
    #include "lrotable.h"
    #include "platform_conf.h"
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    // Lua: num = xuehu.getnum( num )
    static int xuehu_getnum( lua_State* L )
    {
      int pin;
      pin = luaL_checkinteger( L, 1 );
      lua_pushinteger( L, pin+30 );
      return 1;  
    }
    
    #define MIN_OPT_LEVEL 2
    #include "lrodefs.h"
    static const LUA_REG_TYPE xuehu_map[] =
    {
        /*lua中函数名                      c语言函数名*/
      { LSTRKEY( "getnum" ), LFUNCVAL ( xuehu_getnum ) },
    
      { LNILKEY, LNILVAL }
    };
    
    
    LUALIB_API int luaopen_xuehu( lua_State *L )
    {
    #if LUA_OPTIMIZE_MEMORY > 0
      return 0;
    #else // #if LUA_OPTIMIZE_MEMORY > 0
    
      luaL_register( L, AUXLIB_XUEHU, xuehu_map );
    
      return 1;
    #endif // #if LUA_OPTIMIZE_MEMORY > 0
    }