lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I would personally set up a Lua event queue where all non Lua tasks post to the queue and a single Lua task pulls from the queue and then fires Lua handlers.  That is how I would do it..

Mike

On Mon, Mar 28, 2011 at 5:07 PM, Jeff Smith <spammealot1@live.co.uk> wrote:
Hi
 
Still a novice and learning the ropes here, I could do with some advice please.
 
I am adding Lua to an embedded system with a small embedded OS.  I have got Lua running OK on the OS, but I am needing a simple and clean way to respond in Lua to asynchronous events, for example a front panel key press, timer expiry, proximity sensor toggle etc etc.
 
I am running an OS task (analogous to a Windows thread ) for the Lua script, (the OS tasks use co-operative multi-tasking) so a simple main loop might look like
 
while true do
   tempString = string.format("-%07X",counter)
   acme.display.writeLine(tempString)
   
   acme.os.sleep(1000)  -- 1 sec sleep, other OS tasks will run
 
   counter = counter + 1
end
 
Then I have a keyboard OS task that is scanning a keypad, this will use the Lua C API to call a function in the same Lua script  when a front panel key is pressed, for example the above script will have a function in it like
 
function KEY_NUMERIC_DOWN(param)
   print(string.format("Lua: Numeric Key %d Pressed", param))
end
 
I have tested this briefly and it does seem to work provided the main Lua task and the keyboard handler task are at the same OS task priority.
If one task is a higher priority, I think it is pre-empting the Lua execution and causing corruption of the Lua state.
 
Making them of equal priority, means that for example the main loop will run to the sleep call then shedule the keyhandler task to run which then calls the key down Lua function, this seems to run without a problem, but I would like a second opinion on this technique please ?
 
Thanks for any help
 
Regards Geoff