User Tools

Site Tools


doc:lua:start

This is an old revision of the document!


Make your own Lua Scripts

FIXME under construction

What is Lua?

Simply said, Lua is “something” (a so called script interpreter) which is made to allow a program to run another program internally. The trick in that is, that the internal program does not need to be there when the “outer” program is made, it can be loaded later, just when you need it.

This allows to make a program now but change its behavior later as needed. This is widely used in the OOBD script engine: The script engine is there from the beginning, but what it does is defined by the script which is loaded at runtime. Another script, another functionality..

Luckely Lua became quite popular in the last years, so it's available for all major platforms, like Desktop PCs, Smartphone and Mobile Phones.

There are tons of documentation about Lua available in the internet, so we'll not explain the wheel here once more, we'll just focus on how Lua works inside OOBD.

The Lua - OOBD Interface

If Lua runs inside another program, without further assistance it does it like in a black box: No connection from the inside to the outside, no inputs, no feedbacks. This is obviously not very senseful, so when Lua is implemented into another program, there are normally some interfaces made up to let Lua and its host communicate to each other.

From the Lua perspective, these interfaces act as normal Lua function calls, but when such a function is called, the execution branches into the outer program, does something there and finally returns from that function back to Lua.

In the moment the OOBD interface supports two kinds of interfaces for Lua: Some for creating the menus, and some to talk to the serial line 1)

But before we look in detail in the program flow itself, we need to understand how the lua compiler (luac) works and what that means for the program initialization:

Compiling and Startup code

To save memory and reduce the startup time, the Lua interpreter inside OOBD is not feed with the Lua programm source code text directly as this would need to be compiled first to be executed, which is time & memory consuming. Instead the compiling is done beforehand and just the compiled Lua program code is than used to be loaded into OOBD.

The compile process works like that, that the Lua compiler (a command line program called luac) translates the source code(s) (with the extension .lua) into a single file (with the extension .lbc) which contains the compiled program.

The command would be like this:

luac -o outputfile.lbc inputsource1.lua inputsource2.lua ...

In the OOBD source code repository there are some samples about how to do this.

If you want to set up your own compile process, there are two things you need to know, as luac works partly different as normal compilers

  1. luac does not evaluate any require() or doFile() statements, so these files would be missed in the output file. All needed files must be listed instead as input files to be contained in the output
  2. also the sequence of the input files is important: Luac glues the files together in the same sequence as their are given; and in the same sequence the file is executed later.
    Here you have to take care that variables and functions are been declared before they are used first time, otherways you'll get a “variable/function is null” error

Because of item 2, the lua function, which sets up the whole lua structures, should be the last and only command in the lasr file given to luac.

The Start("","") Command

To give the OOBD application the possibility to reinitialize the system (like after an communication loss), the name of such a initialization function is defined: Start(“”,“”). Each OOBD program has to contain this function; please make sure that also your program contain this and that your Start() function contains all necessary initialization code.

The Menu commands

OOBD uses just tree functions to set up the menu structures. Let's have a look into some sample code first:

function Start(oldvalue,id)
	identifyOOBDInterface()
	setSendID("$7E8") -- set not UDS compatible sender (=answer) address for OOBD firmware
	openPage("OOBD-ME Main")
	addElement("Sensor Data >", "createCMD01Menu",">>>",0x1, "")
        addElement("Snapshot Data >", "createCMD02Menu",">>>",0x1, "")
        addElement("Dynamic Menu3 >", "createCMD03Menu",">>>",0x1, "")
	addElement("Trouble Codes", "showdtcs","-",0x1, "")
	addElement("VIN Number", "vin","-",0x2, "")
	addElement("Clear Trouble Codes", "clearDTC","-",0x0, "")
	addElement("System Info >>>", "SysInfo_Menu",">>>",0x1, "")
	addElement("Greetings", "greet","",0x1, "")
	pageDone()
	return oldvalue
end
 
 
----------------- Do the initial settings --------------
 
Start("","")
return

Compile your Scripts

1)
this is certainly subject to change in the future, when OOBD becomes more generic and the data links become more abstract
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
doc/lua/start.1307258500.txt.gz · Last modified: 2011/06/05 09:21 by admin