Task scheduler library  1.3
Arduino library for simply executing tasks in parallel
Tasks.h File Reference

Library providing a simple task scheduler for multitasking. More...

#include <Arduino.h>

Go to the source code of this file.

Macros

#define MAX_TASK_CNT   8
 Maximum number of parallel tasks.
 

Typedefs

typedef void(* Task) (void)
 Example prototype for a function than can be executed as a task.
 

Functions

void Tasks_Init (void)
 Initialize timer and reset the tasks scheduler at first call. More...
 
void Tasks_Clear (void)
 Reset the tasks schedulder. More...
 
bool Tasks_Add (Task func, int16_t period, int16_t delay=0)
 Add a task to the task scheduler. More...
 
bool Tasks_Remove (Task func)
 Remove a task from the task scheduler. More...
 
bool Tasks_Delay (Task func, int16_t delay)
 Delay execution of a task. More...
 
bool Tasks_SetState (Task func, bool state)
 Enable or disable the execution of a task. More...
 
bool Tasks_Start_Task (Task func)
 Activate a task in the scheduler. More...
 
bool Tasks_Pause_Task (Task func)
 Deactivate a task in the scheduler. More...
 
void Tasks_Start (void)
 Start the task scheduler. More...
 
void Tasks_Pause (void)
 Pause the task scheduler. More...
 

Detailed Description

Library providing a simple task scheduler for multitasking.

This library implements a very basic scheduler that is executed in parallel to the 1ms timer interrupt used for the millis() function. It enables users to define cyclic tasks or tasks that should be executed in the future in parallel to the normal program execution inside the main loop.
The task scheduler is executed every 1ms.
The currently running task is always interrupted by this and only continued to be executed after all succeeding tasks have finished. This means that always the task started last has the highest priority. This effect needs to be kept in mind when programming a software using this library.
Deadlocks can appear when one task waits for another taks which was started before. Additionally it is likely that timing critical tasks will not execute properly when they are interrupted for too long by other tasks. Thus it is recommended to keep the tasks as small and fast as possible.
The Arduino ATMega (8-bit AVR) leaves the interrupts state shortly after starting the task scheduler which makes the scheduler reentrant and allows any other interrupt (timer, UART, etc.) to be triggered. The Arduino SAM (32-bit ARM) enables other interrupts by using the lowest possible priority (15) for the task scheduler interrupt.
Warning The Arduino SAM does not support reeantrant interrupts due to HW limitations. This means that unlike the Arduino ATMega the DUE is not able to execute fast 1ms tasks several times before finishing a slower tasks with for example a 10ms timebase. This problem will be especially visible if the sum of the execution time of all tasks is greater than the execution period of the fastest task.

Author
Kai Clemens Liebich, Georg Icking-Konert
Date
2019-11-20
Version
1.3

Definition in file Tasks.h.

Function Documentation

bool Tasks_Add ( Task  func,
int16_t  period,
int16_t  delay = 0 
)

Add a task to the task scheduler.

A new task is added to the scheduler with a given execution period and delay until first execution.
If no delay is given the task is executed at once or after starting the task scheduler (see Tasks_Start())
If a period of 0ms is given, the task is executed only once and then removed automatically.
To avoid ambiguities, a function can only be added once to the scheduler. Trying to add it a second time will reset and overwrite the settings of the existing task.
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction to be executed.
The function prototype should be similar to this: "void userFunction(void)"
[in]periodExecution period of the task in ms (0 to 32767; 0 = task only executes once)
[in]delayDelay until first execution of task in ms (0 to 32767)
Returns
true in case of success, false in case of failure (max. number of tasks reached, or duplicate function)
Note
The maximum number of tasks is defined as MAX_TASK_CNT in file Tasks.h

Definition at line 173 of file Tasks.cpp.

void Tasks_Clear ( void  )

Reset the tasks schedulder.

This function clears the task scheduler. Use with caution!

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Definition at line 144 of file Tasks.cpp.

bool Tasks_Delay ( Task  func,
int16_t  delay 
)

Delay execution of a task.

The task is delayed starting from the last 1ms timer tick which means the delay time is accurate to -1ms to 0ms.
This overwrites any previously set delay setting for this task and thus even allows earlier execution of a task. Delaying the task by <2ms forces it to be executed during the next 1ms timer tick. This means that the task might be called at any time anyway in case it was added multiple times to the task scheduler.
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction that should be delayed
[in]delayDelay in ms (0 to 32767)
Returns
true in case of success, false in case of failure (e.g. function not in not in scheduler table)

Definition at line 314 of file Tasks.cpp.

void Tasks_Init ( void  )

Initialize timer and reset the tasks scheduler at first call.

This function initializes the related timer and clears the task scheduler at first call.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Definition at line 130 of file Tasks.cpp.

void Tasks_Pause ( void  )

Pause the task scheduler.

Pause execution of the scheduler. All tasks are paused.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Definition at line 430 of file Tasks.cpp.

bool Tasks_Pause_Task ( Task  func)
inline

Deactivate a task in the scheduler.

Pause execution of the specified task. Possible parallel tasks are not affected. This is a simple inlined function setting the 'state' argument for Tasks_SetState().
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction to be paused
Returns
true in case of success, false in case of failure (e.g. function not in not in scheduler table)

Definition at line 199 of file Tasks.h.

bool Tasks_Remove ( Task  func)

Remove a task from the task scheduler.

Remove the specified task from the scheduler and free the slot again.
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction name that should be removed.
Returns
true in case of success, false in case of failure (e.g. function not in not in scheduler table)

Definition at line 258 of file Tasks.cpp.

bool Tasks_SetState ( Task  func,
bool  state 
)

Enable or disable the execution of a task.

Temporary pause or resume function for execution of single tasks by scheduler. This will not stop the task in case it is currently being executed but just prevents the task from being executed again in case its state is set to 'false' (inactive).
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction to be paused/resumed.
The function prototype should be similar to this: "void userFunction(void)"
[in]stateNew function state (false=pause, true=resume)
Returns
'true' in case of success, else 'false' (e.g. function not in not in scheduler table)

Definition at line 365 of file Tasks.cpp.

void Tasks_Start ( void  )

Start the task scheduler.

Resume execution of the scheduler. All active tasks are resumed.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Definition at line 404 of file Tasks.cpp.

bool Tasks_Start_Task ( Task  func)
inline

Activate a task in the scheduler.

Resume execution of the specified task. Possible parallel tasks are not affected. This is a simple inlined function setting the 'state' argument for Tasks_SetState().
For non-static member function use address from PTR_NON_STATIC_METHOD() macro.

Used HW blocks:
- Arduino ATMega: TIMER0_COMPA_vect
- Arduino SAM: TC3

Parameters
[in]funcFunction to be activated
Returns
true in case of success, false in case of failure (e.g. function not in not in scheduler table)

Definition at line 180 of file Tasks.h.