-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAS.cpp
219 lines (193 loc) · 7.3 KB
/
LAS.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "LAS.h"
using namespace LASUtils;
bool LAS::schedulerRunning = false;
void CallableVoidFunction::run() {
this->func();
}
CallableVoidFunction::CallableVoidFunction(void (*funcIn)()) {
this->func = funcIn;
}
int LAS::getActiveTaskIndex() {
return activeTaskIndex;
}
Task LAS::getActiveTask() {
return getTask(activeTaskIndex);
}
int LAS::determineFirstInactiveIndex(Task array[], int length) {
for (int index = 0; index < length; index++) {
if (!array[index].isActive) {
return index;
}
}
return length + 1;
}
void LAS::scheduleCallable(Callable *callable, long triggerTime, bool deleteAfter, bool repeat, int repeatInterval, int remainingRepeats) {
if (!LAS::schedulerInitialized) {
Serial.println("SCHEDULER/LOGGER NOT INITIALIZED. RUN initScheduler() FIRST!");
return;
}
Task newTask = Task{
true,
callable,
deleteAfter,
max(ASAP, triggerTime),
repeat,
max(ASAP, repeatInterval),
max(-1, remainingRepeats)
};
int freeIndex = LAS::determineFirstInactiveIndex(schedule, config.scheduleSize);
if (freeIndex > config.scheduleSize) {
logger.printline("SCHEDULE IS FULL! ABORTING TO AVOID UNDEFINED BEHAVIOUR.", "severe");
//abort
while (true)
;
}
schedule[freeIndex] = newTask;
schedule[freeIndex].callable->taskPtr = &schedule[freeIndex];
char buffer[config.strSize / 2] = "";
snprintf(buffer, sizeof(char) * config.strSize, "scheduled Task at %p", &schedule[freeIndex]);
logger.printline(buffer, logger.LogLevel::Debug);
}
void LAS::scheduleFunction(void (*func)(), long triggerTime, bool deleteAfter, bool repeat, int repeatInterval, int remainingRepeats) {
scheduleCallable(new CallableVoidFunction(func), triggerTime, deleteAfter, repeat, repeatInterval, remainingRepeats);
}
void LAS::scheduleIn(void (*func)(), long triggerDelay, bool deleteAfter) {
scheduleFunction(func, millis() + triggerDelay, deleteAfter);
}
void LAS::scheduleIn(Callable *callable, long triggerDelay, bool deleteAfter) {
scheduleCallable(callable, millis() + triggerDelay, deleteAfter);
}
void LAS::scheduleRepeated(void (*func)(), int repeatInterval, int repeats, bool deleteAfter) {
scheduleFunction(func, millis() + repeatInterval, deleteAfter, true, repeatInterval, repeats);
}
void LAS::scheduleRepeated(Callable *callable, int repeatInterval, int repeats, bool deleteAfter) {
scheduleCallable(callable, millis() + repeatInterval, deleteAfter, true, repeatInterval, repeats);
}
void LAS::printWelcome() {
Serial.println("This Unit is running");
Serial.println(" __ ______ ____ ");
Serial.println("/\\ \\ /\\ _ \\/\\ _`\\ ");
Serial.println("\\ \\ \\ \\ \\ \\L\\ \\ \\,\\L\\_\\ ");
Serial.println(" \\ \\ \\ __\\ \\ __ \\/_\\__ \\ ");
Serial.println(" \\ \\ \\L\\ \\\\ \\ \\/\\ \\/\\ \\L\\ \\ ");
Serial.println(" \\ \\____/ \\ \\_\\ \\_\\ `\\____\\\\");
Serial.println(" \\/___/ \\/_/\\/_/\\/_____/");
Serial.println("");
Serial.print("Lukacho's Amazing Scheduler - alpha ");
Serial.print(LAS_VERSION);
Serial.println(" - it\'s a class now!");
Serial.println();
}
void LAS::finishTask(Task *task) {
task->isActive = false;
task->callable->onFinish();
if (task->deleteAfter) {
delete task->callable;
task->callable = nullptr;
}
}
void LAS::startScheduler() {
logger.printline("starting scheduler...");
if (!LAS::schedulerInitialized) {
Serial.println("SCHEDULER/LOGGER NOT INITIALIZED. RUN initScheduler() FIRST!");
return;
}
printWelcome();
if (schedule[0].isActive && schedule[0].triggerTime != ASAP) {
logger.printline("Please consider adding initial Tasks through an ASAP Task for high precision apps", logger.LogLevel::Warning);
}
if (LAS::schedulerRunning) {
logger.printline("THERE MIGHT BE ANOTHER SCHEDULER ALREADY RUNNING! ABORTING...", logger.LogLevel::Severe);
return;
} else {
LAS::schedulerRunning = true;
}
while (true) {
for (int index = 0; index < config.scheduleSize; index++) {
activeTaskIndex = index;
Task currentTask = schedule[index];
if ((currentTask.isActive) && (currentTask.callable != nullptr) && (currentTask.triggerTime <= millis())) {
if (config.lagWarning && millis() - currentTask.triggerTime >= config.maxLagMs && currentTask.triggerTime != 0) {
logger.printline("SCHEDULER IS FALLING BEHIND CRITICALLY!", logger.LogLevel::Warning);
}
currentTask.callable->run();
if (currentTask.repeat) {
schedule[index].triggerTime = millis() + currentTask.repeatInterval;
if (schedule[index].remainingRepeats == 0) {
finishTask(&schedule[index]);
char buffer[config.strSize / 2] = "";
snprintf(buffer, sizeof(char) * config.strSize, "finished repeat Task at %p", &schedule[index]);
logger.printline(buffer, logger.LogLevel::Debug);
} else if (currentTask.remainingRepeats != ENDLESS_LOOP) {
schedule[index].remainingRepeats--;
}
} else {
finishTask(&schedule[index]);
char buffer[config.strSize / 2] = "";
snprintf(buffer, sizeof(char) * config.strSize, "finished Task at %p", &schedule[index]);
logger.printline(buffer, logger.LogLevel::Debug);
}
} else if (currentTask.callable != nullptr && !currentTask.isActive && currentTask.deleteAfter) {
delete currentTask.callable;
currentTask.callable = nullptr;
}
}
}
}
void LAS::initScheduler(LASConfig config, Logger logger) {
interrupts();
logger = logger;
config = config;
schedule = new Task[config.scheduleSize]{ Task{
false,
nullptr,
true,
0,
false,
0 } };
schedulerInitialized = true;
logger.printline("scheduler initialized");
logger.printline("logger test", logger.LogLevel::Debug);
}
LAS::~LAS() {
LAS::schedulerRunning = false;
clearSchedule();
delete [] schedule;
}
void LAS::clearSchedule() {
logger.printline("Clearing schedule as demanded programatically.", "warning");
for (int i = 0; i < config.scheduleSize; i++) {
finishTask(&schedule[i]);
}
}
char *LAS::taskToCharStr(Task *task) {
static char* buffer = new char[config.strSize];
char buffer_2[config.strSize];
snprintf(buffer, sizeof(char) * config.strSize, "Task %p:\n isActive: %i\n deleteAfter: %i\n callable: %p\n triggerTime: %d\n",
task, task->isActive, task->deleteAfter, task->callable, task->triggerTime);
snprintf(buffer_2, sizeof(char) * config.strSize, "\n repeat: %i\n repeatInterval: %i\n remainingRepeats: %i",
task->repeat, task->repeatInterval, task->remainingRepeats);
strcat(buffer, buffer_2);
return buffer;
}
char *LAS::scheduleToCharStr() {
static char* buffer = new char[config.strSize];
for (int index = 0; index < config.scheduleSize; index++) {
static char indexBuffer[3];
snprintf(indexBuffer, sizeof(char) * config.strSize, "\n%d:\n", index);
strcat(buffer, indexBuffer);
strcat(buffer, taskToCharStr(&schedule[index]));
}
return buffer;
}
Task LAS::getTask(int index) {
return schedule[index];
}
void LAS::printSchedule() {
for (int index = 0; index < config.scheduleSize; index++) {
char buffer[config.strSize];
snprintf(buffer, sizeof(char) * config.strSize, "%d:", index);
logger.printline(buffer);
logger.printline(taskToCharStr(&schedule[index]));
}
}