lua-users home
lua-l archive

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

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.os.sleep(1000)  -- 1 sec sleep, other OS tasks will run
   counter = counter + 1
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))
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