I am working on a Realtime Computing project using pyRTOS to create the tasks and applying the RTOS concepts. My project is a version of the game 2048 to run on a physical touchboard.
As part of the project requirements, we need to create tasks to run simultaneously on the system. So, we defined three tasks:
- Touch arrow buttons (natively implemented on the physical board)
- Move the blocks and use merge logic
- Update the tile numbers and colors
Task 2. needs to be non-periodic and 3. periodic.
This is a sample code of how to create a task:
import pyRTOS
# self is the thread object this runs in
def sample_task(self):
### Setup code here
### End Setup code
# Pass control back to RTOS
yield
# Thread loop
while True:
### Work code here
### End Work code
yield [pyRTOS.timeout(0.5)]
pyRTOS.add_task(pyRTOS.Task(sample_task))
pyRTOS.start()
My question is: Is this code used just for non-periodic tasks? Since there is a while
inside the function, I imagine that the function runs indefinitely, being yielded sometimes to allow other tasks to run too.
This is not what we want for task 2. We want it to run just in specific moments - after the touch of a button. How this should be implemented? Should I remove the loop or just insert a conditional inside the function?