Dies ist eine alte Version des Dokuments!


Usage of Lua in a Lab Device

The following function is called when the user presses the „init“ button on the GUI. It calls the Lua VM with the init command string (“cube.init()“)

void MainWindow::pushButtonInitPressed(void)
{
  LuaVM->run("ok, result = cube.init()");
}

The LuaVM receives a string to execute. The string must be a valid lua chunk. The function creates and initializes the Lua VM and executes the function dostring() to process the lua chunk. At the end with lua_close() the allocated memory is freed.

bool LuaVM::run(QString luaCommand)
{
    L = lua_open();  /* create state */
    lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
    luaL_openlibs(L);  /* open libraries */
    luaopen_cube(L);    
    lua_gc(L, LUA_GCRESTART, 0);
    dostring(L, qPrintable(luaCommand));
    ....
    lua_close(L);
}

C++ implementation of the Lua function cube.init()

static int cube_init(lua_State *L)
{
  e_ERROR err = ERROR_OK;
  err = cube->init();
  lua_pushinteger(L, err);
  return 1;
}

Table with all Lua functions and the corresponding implementations in C++.

static const struct luaL_reg cube_functions[] = {
  { "init", cube_init },
  { NULL, NULL }
};

This function connects the Lua function cube.init() with its implementation in C++.

int luaopen_cube (lua_State *L)
{
  luaL_openlib(L,"cube",   cube_functions,0);
  return 1;
}
 
lua_lab_device.1422953759.txt.gz · Zuletzt geändert: 2015/02/03 09:55 von Claus Kühnel
 
Falls nicht anders bezeichnet, ist der Inhalt dieses Wikis unter der folgenden Lizenz veröffentlicht:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki