-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskScheduler.h
172 lines (140 loc) · 5.91 KB
/
TaskScheduler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************************//**
MIT License
Copyright (c) 2017 Jon Knoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file task_scheduler.h
@license MIT
@version 3.0.0
@description Simple Task Scheduler
This is basically a set of timers and a very simple run-to-completion
scheduler. The scheduler runs off the millis() counter so all delays are in
milliseconds.
Currently supported:
- 256 tasks
- 65535 ms max delay
Each task has 4 states:
- TASK_OFF - completely disabled and will not run, regardless of its timer
- TASK_STOP - task has run to competion and has not been put to sleep
- TASK_PEND - pending on its timer (sleeping, waiting for timeout)
- TASK_RUN - task is running (or ready to run)
In your program you need to set up a task block array, where each entry is
the starting parameters for a task. Here is an example of what should be at
the top of your sketch. The enumeration and NUM_TASKS are optional, if you
just want to use the actual numbers.
// Task list where each line matches a line in the task block
enum
{
LED_BLINK_TASK = 0,
HELLO_WORLD_TASK
};
// The task block with starting values
TaskBlock taskBlock[] =
{
// Entry Point State Starting Count
{&ledBlinkTask, TASK_RUN, 0}, // LED_BLINK_TASK
{&helloWorldTask, TASK_RUN, 0} // HELLO_WORLD_TASK
};
// The number of tasks (automatically calculated
#define NUM_TASKS (sizeof(taskBlock)/sizeof(TaskBlock))
Then in your setup():
setup()
{
// put other setup stuff here
void TS.initialize(taskBlock, NUM_TASKS);
}
loop()
{
TS.run();
}
*******************************************************************************/
#ifndef TASK_SCHEDULER_H
#define TASK_SCHEDULER_H
#include "Arduino.h"
typedef enum
{
TASK_OFF, // disabled (will not run)
TASK_STOP, // active but not running
TASK_PEND, // active but pending (sleeping, waiting for timeout)
TASK_RUN, // running (or ready to run)
} TaskState;
typedef void (*TaskFuncPtr)(void);
typedef struct
{
TaskFuncPtr funcPtr; // function pointer -- the task entry point
TaskState state; // the current task state
uint16_t timer; // count down timer to the next task run
} TaskBlock;
class TaskScheduler
{
public:
TaskScheduler(void);
/**
* @description Initialize the scheduler with the task block. Place this in
* the setup() function.
* @param taskBlockPtr - pointer to the start of your task block
* @param taskBlockSize - the number of tasks in the task block
*/
void initialize(TaskBlock *taskBlockPtr, uint8_t numTasks);
/**
* @description Run the scheduler. Place this in the loop() function (it
* should be the only thing in the loop() function).
*/
void run(void);
/**
* @description Add set a sleep time for the task.
* This sets the task's timer to the number of milliseconds the scheduler
* should wait before running again.
* NOTE: Depending on the run time of the tasks, the delay cannot be
* guaranteed. It might be longer than specified.
* @param taskId - the task ID (array entry in the task block)
* @param ms - number of milliseconds to sleep
*/
void sleep(uint8_t taskId, uint16_t ms);
/**
* @description Set the entry point for the task.
* This is a more advanced function. It is for special cases where you
* want to change where the task starts from, the next time it runs.
* Normally you would build your task as a state machine (all in one
* function) but if you wanted to separate the states into different
* functions, you can modify the task's entry point on the fly.
* @param taskId - the task ID (array entry in the task block)
* @param newFuncPtr - new function pointer for the task
*/
void setEntryPoint(uint8_t taskId, TaskFuncPtr newFuncPtr);
/**
* @description Get the task's state.
* Get the current state of a task.
* @param taskId - the task ID (array entry in the task block)
* @returns TS_taskState - the task's current state
*/
TaskState getState(uint8_t taskId);
/***************************************************************************//**
* @description Set the task's state.
* Normally the scheduler automatically switches the state between
* TASK_PEND and TASK_RUN. But you can manually override what the
* scheduler is doing or you can disable the task altogether with
* TASK_OFF.
* @param taskId - the task ID (array entry in the task block)
* @param newState - new state for the task
*/
void setState(uint8_t taskId, TaskState newState);
private:
TaskBlock *ts_taskBlockPtr;
uint8_t ts_taskBlockSize;
uint32_t ts_prevMs;
};
#endif