Architecture for Qt Application Using Lua Scripting - Suspending Execution

My embedded project contains a Qt PC application which is basically a simulator for debugging and testing. In the application, I can create multiple widgets that either represent my firmware, or simulate application-driven hardware, or can generate external inputs for testing.

I am planning to improve the application by adding Lua scripts so that widgets can be created or manipulated by a script. I need an elegant way for single step scripts. I am planning scripts like:

createThermometerWidget(10,20,30)
while time < maxTime do
  setTemperature(20+time/1000)
  pauseSimulation()
  time = time + 1
end

      

The custom function pauseSimulation

should stop the Lua script, enable the Qt event loop so that it can interact with the software (like setting other inputs), and once the button is pressed, the script will continue.

My first idea was to create a separate thread for Lua execution that would be stopped at pauseSimulation

and released by a button. But Qt widgets cannot be created from a non-main thread, so I will have to create all widgets in the main thread and pass all constructor parameters from Lua functions to the main thread.

Is there a smoother way?

+2


source to share


1 answer


Coroutines is one way to do this. Yours pauseSimulation()

can be invoked internally coroutine.yield()

and restarted later by calling coroutine.resume()

from a button action. The problem is that your UI is at the mercy of your script fragments, as the only way to stop a running coroutine is to make a call yield()

.



Alternatively, you can use the Lanes module to put a portion of your Lua application on a separate thread. You would use Linda to pass messages from the main Qt widget thread to the simulator worker thread. This has the advantage that the UI thread is not blocked by the simulation running on its own thread.

+3


source







All Articles