-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.cpp
More file actions
107 lines (95 loc) · 2.67 KB
/
Scheduler.cpp
File metadata and controls
107 lines (95 loc) · 2.67 KB
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
/** A Scheduler schedules iterative commands to be run, and then executes them
* in order. Commands can be added, and removed, can be scheduled to run in parallel
* with other commands, and can be scheduled to run persistently. Credits to PeterMitrano
* for some of the initial code
* @author Jordan Burklund
* @version 1.0
* @date Sept. 2015
**/
#include "Scheduler.h"
Scheduler* Scheduler::instance = NULL;
/** Constructor **/
Scheduler::Scheduler() {
}
/** Get the singleton instance of the scheduler
* @return The instance of the scheduler
**/
Scheduler* Scheduler::getInstance() {
if(instance == NULL) {
instance = new Scheduler();
}
return instance;
}
/** Adds a command to the end of the scheduled
* list of commands, and does change the parallel flag
* @param command Pointer to the command to add
**/
void Scheduler::addCommand(Command* command) {
/* #TODO Should we prevent scheduling the same command?
if(!commands.contains(command)) {
commands.add(command);
} */
commands.add(command);
}
/** Adds a command to the end of the scheduled list, and sets
* the flag to run in parallel
* @param command Pointer to the command to add
* @note this may change soon with command groups
**/
void Scheduler::addParallelCommand(Command* command) {
command->setParallel(true);
commands.add(command);
}
/** Adds a command to the end of the scheduled list, and sets
* the flag to not run in parallel
* @param command Pointer to the command to add
* @note this may change soon with command groups
**/
void Scheduler::addSequentialCommand(Command* command) {
command->setParallel(false);
commands.add(command);
}
/** Execute an iteration of the scheduled commands
**/
void Scheduler::run() {
//Execute commands until there is a command that is not parallel
int size = commands.size();
Command* command;
bool isFinished;
int i = 0;
while(i<size) {
//Get the command
command = commands.get(i);
isFinished = command->cycle();
//Remove the command if it is finished
if(isFinished) {
commands.remove(i);
size = commands.size();
if(command->isParallel()) {
//don't increment, since just removed
} else {
//command was sequential, done with this loop
break;
}
} else {
if(command->isParallel()) {
//Command is parallel, increment the pointer
i++;
} else {
//Command is sequential, done with this loop
break;
}
}
}
}
/** Print out the current list of scheduled commands
**/
void Scheduler::printCommands() {
if(commands.size() > 0) {
for(int i=0, n=commands.size(); i<n; i++) {
Serial.println(commands.get(i)->name);
}
} else {
Serial.println("Scheduler: No Commands Scheduled");
}
}