Search code examples
real-timertos2048

How to structure a non-periodic task in pyRTOS?


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:

  1. Touch arrow buttons (natively implemented on the physical board)
  2. Move the blocks and use merge logic
  3. 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?


Solution

  • You can use a notification for an non-periodic task. The non-periodic task will still have a while loop. But within the while loop it will wait for a notification instead of delaying. See the pyRTOS Notification Examples. For your requirements, when the button is pushed then the code should set the notification that will cause task 2 to run.