I want to create a function with a delay for each instance of a class. If some condition occur a task will be created and a delay will happen only to this instance.
/* 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);
}
Will each delay work? Or at each iteration a new task will run? Will the for loop be blocked so each iteration stops or it will work async?
The whole idea is to avoid using millis() function and not blocking each iteraction of the list. Another restrain is speed as this loop work at 5ms and i would like to use the core 0.
EDIT: @HS2 raised that a better approach should be using a FreeRTOS timer