如何在pyRTOS中构建一个非周期性任务?

huangapple go评论55阅读模式
英文:

How to structure a non-periodic task in pyRTOS?

问题

这段代码是用于非周期性任务吗?由于函数内部有一个 while 循环,我想象这个函数会无限运行,并且偶尔被 yield 以允许其他任务也运行。这并不是我们想要的 Task 2。我们希望它只在特定时刻运行 - 在按下按钮后。应该如何实现这一点?我应该删除循环还是只需在函数内插入一个条件?

英文:

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?

答案1

得分: 0

你可以为非周期性任务使用通知。非周期性任务仍然会有一个while循环。但在while循环内,它将等待通知而不是延迟。请参阅pyRTOS通知示例。根据您的要求,当按钮被按下时,代码应设置通知,这将导致任务2运行。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月12日 04:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定