Using LUA in Final Product


#1

I wrote some functions in LUA inside ‘LUA interactive shell’ and some functions in C.
Inside the Shell all functions is working,now I would like to move to production - so all functions and variables will be at the flash and the software will start automatic
What is the best way to do this?
Where should I save all LUA functions in the modem or inside my open-AT shell file(dwl).
Please Advice of the right way to use LUA functions I wrote in the shell and to finish the development.

Thanks


#2

The simplest way is to compile them into a C char[], then link it to a minimalist C application. This will give you a standard, IDS-ready, Open AT application. There is a lua2c script in the Open AT Lua distribution which will:

  • compile your sources as bytecode
  • transform this bytecode into a .c file containing a const char [] value

Then you just have to run it with luaW_lrun(char*, size_t) in your main application. The tools and the latest version of Lua will be available in the next SDK release, as a regular plug-in, with slightly improved compilation tools and adequate documentation. Meanwhile, send me a private message if you have trouble using lua2c.

For instance, if your program is in file myappli.lua, you can run “lua2c myappli.lua”. You’ll get a myappli.c file, containing constant declarations “const char *myappli;” and “const int myappli_len;” (unless you use options to customize those names). Now you can run this program with a main function looking like:

#include "wiplua.h"
#include "mempool.h" 

/* Constants created by lua2c: */
extern char *myappli;
extern int myappli_len;

/* Just copy this from the standard Lua sample: */
static const luaL_Reg BOOT_TIME_MODULES [] = {
  /* Lua standard libs */
  { "base",        luaopen_base        },
  ... /* etc. */
  { NULL, NULL } };

void adl_main ( ) {
  MEMPOOL_INIT();
  wip_init();
  luaW_start( BOOT_TIME_MODULES); 
  luaW_lrun( myappli, myappli_len);
}

Don’t forget to include mempool.h, from the sample, in your application.


#3

Thanks for your reply fft, I will test it …


#4

An example of a Lua program packaged in a final C application has been added in the latest version, which you can download from here: Open AT Lua 1.00.2010. Look at the web_server sample, which is generated from script files web_server.lua and webapp_phone.lua.

To turn .lua files into .c files, which can be integrated into a regular Open AT project, you need lua2c, which is provided here:

Open AT Lua host-side tools

  • add the tools path into your %PATH% environment variable.
  • run lua2c. To figure out the arguments, look at the generated files web_server.c and webapp_phone.c in the sample: it contains a comment "This file has been generated with: ". You can also use option -h which displays a short usage note.
../../../OpenATLua/tools/lua51int ../../../OpenATLua/tools/lua2c.lua -C ../../../OpenATLua/tools/luac51int -v -o webapp_phone.c -s src_lua/webapp_phone.lua

PS: of course, these tools will eventually be included in the default IDE as well. However it will most likely happen after IDE v2.00 ships, as everything has been rearchitectured from scratch in it.