我想为类的每个实例创建一个具有延迟的函数。如果发生某种情况,将创建一个任务,并且延迟只会发生在这个实例上。
/* Task to be created. */
void vTaskCode( void * pvParameters ){
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
//Do some logic...
vTaskDelay(xDelay);
//indicate the task has finished
configASSERT(1);
}
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
class foo {
void afunc(){
//Create a task of vTaskCode if it doesnt exist
if (xHandle == NULL){
xReturned = xTaskCreate(
vTaskCode, /* Function that implements the task. */
"Auxiliary", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle );
}}
//Should be called after the task is complete.
if( xReturned == pdPASS )
{
vTaskDelete( xHandle );
}
}
//main loop
void MainLOOP( void * pvParameters ){
for (;;){
const TickType_t xDelay = 5 / portTICK_PERIOD_MS;
for (std::list<foo>::iterator itr = foolist.begin(); itr != foolist.end(); ++itr)
{
itr->afunc();
}
}}
void setup(){
std::list<foo> foolist;
///...SOME CODE TO POPULATE FOOLIST
xTaskCreate(
MainLOOP, /* Function that implements the task. */
"MAIN", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
NULL);
}
每次延迟都会起作用吗?或者在每次迭代中都会运行一个新任务?for 循环会被阻塞,所以每次迭代都会停止还是异步工作?
整个想法是避免使用 millis() 函数并且不阻塞列表的每个迭代。另一个限制是速度,因为这个循环以 5ms 工作,我想使用核心 0。
编辑:@HS2 提出更好的方法应该是使用 FreeRTOS 计时器