#include <TaskManagerIO.h>
Public Member Functions | |
| TaskManager () | |
| taskid_t | execute (TimerFn workToDo) |
| taskid_t | execute (Executable *execToDo, bool deleteWhenDone=false) |
| taskid_t | schedule (const TimePeriod &when, TimerFn timerFunction) |
| taskid_t | schedule (const TimePeriod &when, Executable *execRef, bool deleteWhenDone=false) |
| taskid_t | scheduleOnce (uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS) |
| taskid_t | scheduleOnce (uint32_t when, Executable *execRef, TimerUnit timeUnit=TIME_MILLIS, bool deleteWhenDone=false) |
| taskid_t | scheduleFixedRate (uint32_t when, TimerFn timerFunction, TimerUnit timeUnit=TIME_MILLIS) |
| taskid_t | scheduleFixedRate (uint32_t when, Executable *execRef, TimerUnit timeUnit=TIME_MILLIS, bool deleteWhenDone=false) |
| taskid_t | registerEvent (BaseEvent *eventToAdd, bool deleteWhenDone=false) |
| ISR_ATTR void | triggerEvents () |
| void | addInterrupt (InterruptAbstraction *interruptAbstraction, pintype_t pin, uint8_t mode) |
| void | setInterruptCallback (InterruptFn handler) |
| void | cancelTask (taskid_t task) |
| void | setTaskEnabled (taskid_t task, bool ena) |
| virtual void | yieldForMicros (uint32_t micros) |
| void | runLoop () |
| void | reset () |
| char * | checkAvailableSlots (char *slotData, size_t slotDataSize) const |
| TimerTask * | getFirstTask () |
| TimerTask * | getTask (taskid_t task) |
| uint32_t | microsToNextTask () |
| TimerTask * | getRunningTask () |
Static Public Member Functions | |
| static void | markInterrupted (pintype_t interruptNo) |
Protected Attributes | |
| TaskBlock *volatile | taskBlocks [DEFAULT_TASK_BLOCKS] |
| volatile taskid_t | numberOfBlocks |
| tm_internal::TimerTaskAtomicPtr | first |
| volatile pintype_t | lastInterruptTrigger |
| volatile bool | interrupted |
| volatile InterruptFn | interruptCallback |
| tm_internal::TmAtomicBool | memLockerFlag |
| tm_internal::TimerTaskAtomicPtr | runningTask |
Friends | |
| class | TaskExecutionRecorder |
TaskManager is a lightweight cooperative co-routine implementation for Arduino, it works by scheduling tasks to be done either immediately, or at a future point in time. It is quite efficient at scheduling tasks as internally tasks are arranged in time order in a linked list. Tasks can be provided as either a function to be called, or a class that implements the Executable interface. Tasks can be scheduled to run either ASAP, once, or repeated.
Events can be added based on extensions of the BaseEvent class, these can be triggered outside of task manager and they can then "wake up" task manager, events can also be polled. In addition interrupts can be marshalled through task manager, any interrupt managed by task manager will be marshalled into a task. IE outside of an ISR.
There is a globally defined variable called taskManager and you should attach this to your main loop. You can create other task managers on different threads if required. For most use cases, the class is thread safe.
| TaskManager::TaskManager | ( | ) |
On all platforms there is a default instance of TaskManager called taskManager. You can create other instances that can run on other threads, for example to process long running tasks that should be processed separately.
Executes a task manager task as soon as possible. Useful to add work into task manager from another thread of execution. Shorthand for scheduleOnce(2, task);
Why not scheduleOnce with 0 you may ask, taskManager is cooperative which also means it is an unfair scheduler. Given this, it would always add at the front of the queue if the time was 0, but by making the time 2, we ensure it is queued behind other things needing immediate execution.
| workToDo | the work to be done |
|
inline |
Executes a task manager task as soon as possible. Useful to add work into task manager from another thread of execution. Shorthand for scheduleOnce(2, task);
Why not scheduleOnce with 0 you may ask, taskManager is cooperative which also means it is an unfair scheduler. Given this, it would always add at the front of the queue if the time was 0, but by making the time 2, we ensure it is queued behind other things needing immediate execution.
| execToDo | the work to be done |
| deleteWhenDone | default to false, task manager will reclaim the memory when done with this executable. |
| taskid_t TaskManager::schedule | ( | const TimePeriod & | when, |
| TimerFn | timerFunction ) |
Schedule using a time period, normally using the helper functions to quickly create the period, underneath this calls one of the existing schedule... methods. This schedules a no parameter function to be called. On larger boards with lambda support enabled, it actually schedules a std::function.
| when | the time period providing the schedule details |
| timerFunction | the function to call |
| taskid_t TaskManager::schedule | ( | const TimePeriod & | when, |
| Executable * | execRef, | ||
| bool | deleteWhenDone = false ) |
Schedule using a time period, normally using the helper functions to quickly create the period, underneath this calls one of the existing schedule... methods. This schedules an executable to be called back.
| when | the time period providing the schedule details |
| execRef | the function to call |
| deleteWhenDone | if true, once the task ends (cancelled or finished in once mode) it is deleted. |
| taskid_t TaskManager::scheduleOnce | ( | uint32_t | when, |
| TimerFn | timerFunction, | ||
| TimerUnit | timeUnit = TIME_MILLIS ) |
Schedules a task for one shot execution in the timeframe provided.
| millis | the time frame in which to schedule the task |
| timerFunction | the function to run at that time |
| timeUnit | defaults to TIME_MILLIS but can be any of the possible values. |
| taskid_t TaskManager::scheduleOnce | ( | uint32_t | when, |
| Executable * | execRef, | ||
| TimerUnit | timeUnit = TIME_MILLIS, | ||
| bool | deleteWhenDone = false ) |
Schedules a task for one shot execution in the timeframe provided calling back the exec function on the provided class extending Executable.
| millis | the time frame in which to schedule the task |
| execRef | a reference to a class extending Executable |
| timeUnit | defaults to TIME_MILLIS but can be any of the possible values. |
| deleteWhenDone | default to false, task manager will reclaim the memory when done with this executable. |
| taskid_t TaskManager::scheduleFixedRate | ( | uint32_t | when, |
| TimerFn | timerFunction, | ||
| TimerUnit | timeUnit = TIME_MILLIS ) |
Schedules a task for repeated execution at the frequency provided.
| millis | the frequency at which to execute |
| timerFunction | the function to run at that time |
| timeUnit | defaults to TIME_MILLIS but can be any of the possible values. |
| taskid_t TaskManager::scheduleFixedRate | ( | uint32_t | when, |
| Executable * | execRef, | ||
| TimerUnit | timeUnit = TIME_MILLIS, | ||
| bool | deleteWhenDone = false ) |
Schedules a task for repeated execution at the frequency provided calling back the exec method on the provided class extending Executable.
| millis | the frequency at which to execute |
| execRef | a reference to a class extending Executable |
| timeUnit | defaults to TIME_MILLIS but can be any of the possible values. |
| deleteWhenDone | true if taskManager should call delete on the object when done, otherwise false. |
Adds an event to task manager that can be triggered either once or can be repeated. See the BaseEvent interface for more details of the interaction with taskManager.
| eventToAdd | the event that is added to task manager |
| deleteWhenDone | true if taskManager should call delete on the object when done, otherwise false. |
|
inline |
Generally used by the BaseEvent class in the markTriggeredAndNotify to indicate that at least one event has now triggered and needs to be evaluated.
| void TaskManager::addInterrupt | ( | InterruptAbstraction * | interruptAbstraction, |
| pintype_t | pin, | ||
| uint8_t | mode ) |
Adds an interrupt that will be handled by task manager, such that it's marshalled into a task. This registers an interrupt with any IoAbstractionRef.
| ref | the Interrupt abstraction (or IoAbstractionRef) that we want to register the interrupt for |
| pin | the pin upon which to register (on the IoDevice above) |
| mode | the mode in which to register, eg. CHANGE, RISING, FALLING |
| void TaskManager::setInterruptCallback | ( | InterruptFn | handler | ) |
Sets the interrupt callback to be used when an interrupt is signalled. Note that you will be called back by task manager, and you are safe to use any variables as normal. Task manager marshalls the interrupt for you.
| handler | the interrupt handler |
| void TaskManager::cancelTask | ( | taskid_t | task | ) |
Stop a task from executing or cancel it from executing again if it is a repeating task
| task | the task ID returned from the schedule call |
| void TaskManager::setTaskEnabled | ( | taskid_t | task, |
| bool | ena ) |
Sets a tasks enable status. An enabled task is scheduled whereas a disabled task is not scheduled. Note that after disabling a task it may run one more time before switching state. Upon re-enablement then task manager will put the item back into the run queue if needed.
| task | the task to change status |
| ena | the enablement status |
|
virtual |
Use instead of delays or wait loops inside code that needs to perform timing functions. It will not call back until at least micros time has passed.
| micros | the number of microseconds to wait. |
Reimplemented in SimulatedTaskManager.
| void TaskManager::runLoop | ( | ) |
This should be called in the loop() method of your sketch, ensure that your loop method does not do anything that will unduly delay calling this method.
|
static |
Used internally by the interrupt handlers to tell task manager an interrupt is waiting. Not for external use.
|
inline |
Reset the task manager such that all current tasks are cleared, back to power on state.
| char * TaskManager::checkAvailableSlots | ( | char * | slotData, |
| size_t | slotDataSize ) const |
This method fills slotData with the current running conditions of each available task slot. Useful for debugging purposes, where each task the . Key:
| slotData | the char array to fill in with task information. Must be as long as number of tasks + 1. |
| slotDataSize | the size of the array passed in (normally using sizeof). |
|
inline |
Gets the first task in the run queue. Not often useful outside of testing.
Gets the underlying TimerTask variable associated with this task ID.
| task | the task's ID |
|
inline |
Gets the number of microseconds as an unsigned long to the next task execution. To convert to milliseconds: divide by 1000, to seconds divide by 1,000,000.
|
inline |
Gets the currently running task, this is only useful for places where re-entrant checking is needed to ensure the same task is taking the lock again for example. Never change the task state in this call, and also never store this pointer.