doc:lua:start
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:lua:start [2011/06/05 09:21] – admin | doc:lua:start [2011/06/13 19:29] – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Make your own Lua Scripts ====== | + | ====== Make your own OOBD Scripts ====== |
- | FIXME under construction | + | |
+ | |||
+ | The " | ||
Line 25: | Line 27: | ||
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 (( this is certainly subject to change in the future, when OOBD becomes more generic and the data links become more abstract)) | 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 (( this is certainly subject to change in the future, when OOBD becomes more generic and the data links become more abstract)) | ||
+ | |||
+ | |||
+ | **Important** To allow it during debugging to replace the hardcoded extended Lua functions against some own debugging code, normally the hardcoded functions are assigned to some Lua variables first, which then are used in the later script: | ||
+ | |||
+ | local serFlush =serFlushCall | ||
+ | serflush() | ||
+ | | ||
+ | | ||
+ | So by naming convention the hardcoded extended functions ends with xxx" | ||
+ | |||
+ | Please keep this in mind while reading this document or writing own code. | ||
Line 82: | Line 95: | ||
</ | </ | ||
- | [[make|Compile | + | |
+ | (BTW: Here you also see the start() function in action) | ||
+ | |||
+ | So what do we found here? First we have the | ||
+ | === openPage(" | ||
+ | |||
+ | This function tells OOBD that a new menu should be made. The //title// is used, what a surprise, as title for that new menu. | ||
+ | |||
+ | Then the menu is filled with | ||
+ | |||
+ | === addElement(Description, | ||
+ | |||
+ | This call adds a single element to the list. For now these function has the following parameters | ||
+ | |||
+ | * Description(String): | ||
+ | * function: Each menu is assigned to a Lua function, where one function can be shared between several menu items. The name of this function is given here (please take care about correct upper/lower characters) | ||
+ | * initialValue(String): | ||
+ | * Flagset(Integer): | ||
+ | * id(String): The id is used to add an individual marker to a menu item. This is widely used when one single Lua function should support many menu items. This is descripted in detail in the [[# | ||
+ | |||
+ | === pageDone() === | ||
+ | |||
+ | The pageDone() function is easy, it just tells OOBD that the menu is defined now and ready to be shown. | ||
+ | |||
+ | The final result looks than similar like this (you' | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | ==== The Menu Function Call ==== | ||
+ | |||
+ | As shown above, we've now set up the menu items - but now we want something happen when the user selects a menu item, won't we? | ||
+ | |||
+ | |||
+ | So let's assume the user selects an item. What happens now? | ||
+ | - OOBD looks, which Lua function is assigned to that menu item | ||
+ | - than that Lua function is called, with two parameters: | ||
+ | * the actual displayed value of that item | ||
+ | * and the id with we gave to that menu item during initialization | ||
+ | - than the called Lua function does something, e.g. a calculation or it starts to generate another submenu | ||
+ | - when the function is finished, it returns a string value | ||
+ | - that string value is then displayed as new value of the menu item | ||
+ | |||
+ | That's already all, that does the job :-) | ||
+ | |||
+ | |||
+ | ==== The Communication | ||
+ | |||
+ | FIXME under construction :-) | ||
+ | |||
+ | === serFlush() === | ||
+ | |||
+ | Simple, it just clears the the | ||
+ | |||
+ | |||
+ | === serWrite(String) === | ||
+ | === serSleep(milliseconds) === | ||
+ | === serReadLn(msTimeout) === | ||
+ | === serWait(OptionString, | ||
+ | |||
+ | |||
+ | |||
+ | ==== Miscellaneous | ||
+ | |||
+ | === serDisplayWrite(String) === | ||
+ | |||
+ | ===== Some Programming Tricks ===== | ||
+ | |||
+ | Coming from old fashioned programming languages, you might be used to simple and static variable types like numbers and Strings. But Lua offers much more comfort here, especially with the support of nested (associative) Arrays. With that some things can be realized quite easy. | ||
+ | |||
+ | Let's assume a common scenario: You want to use a number of menu items, which basically do all the same (getting a value from a vehice) just with different parameters. | ||
+ | |||
+ | In former days you had to write one function for each single value and a long list of menu items, but in Lua, you can store all parameters, their meanings and even the function reference to get them in a single array like | ||
+ | |||
+ | < | ||
+ | local Menu2Data = { | ||
+ | id0x0815 = { byte = 1 , size = 1 , mult = 0.392156862745 , offset = 0, unit = " | ||
+ | id0x4711 = { byte = 1 , size = 1 , mult = 0.392156862745 , offset = 0, unit = " | ||
+ | -- ... here are all the other parameter settings | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | then, during | ||
+ | |||
+ | <code lua> | ||
+ | openPage(" | ||
+ | for key,value in pairs(Menu2Data) do | ||
+ | res=_G[value.call](" | ||
+ | addElement(value.title, | ||
+ | end | ||
+ | pageDone() | ||
+ | |||
+ | </ | ||
+ | |||
+ | And again: That's it | ||
+ | |||
+ | When than later on a function is called by its menu item, it gets the hash key of the Menu2Data array as its " | ||
+ | |||
+ | Please also note the use of the _G array in the sample above: Lua stores also all functions into its global _G array, where they can be refenced by their name and used as a normal function. So the '' | ||
+ | - it finds a function by its name | ||
+ | - then it calls these function as if the user would do it | ||
+ | - it saves the actual return value (which represents e.g. the actual measuring data) in res | ||
+ | - res is then be used as initial displayed value when setting up the menu | ||
+ | |||
+ | By that it's possible without big efforts to fill a menu with real data already during initialization, | ||
+ |