-
Notifications
You must be signed in to change notification settings - Fork 5
/
Cron.h
233 lines (205 loc) · 5.13 KB
/
Cron.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef CRON_H
#define CRON_H
#include <WProgram.h>
#include "Core.h"
#include "TokenParser.h"
#define cronSlots 32
#define invalidSlot 0xff
class Cron {
public:
typedef enum {
Ok = 0,
Error,
SlotsFull,
NotFound
} CronErrorCode;
typedef enum {
SingleThread = 0,
MultiThread
} CronOptions;
typedef struct {
fptr function;
unsigned long yield;
us16 temp;
us16 temp2;
us16 temp3;
us16 counter;
} CronDetail;
typedef us8 CronId;
typedef unsigned long (*ulong_fptr)();
Cron(ulong_fptr timer = millis)
{
systemTimer = timer;
currentSlot = 0;
for(us8 i = 0; i < cronSlots; i++) {
resetDetails(i);
}
}
void scheduler(void)
{
if(systemTimer() >= slot[currentSlot].yield && slot[currentSlot].function != 0) {
cronStatus |= 0x01;
slot[currentSlot].yield = 0;
((fptr)(slot[currentSlot].function))();
cronStatus &= ~0x01;
if(slot[currentSlot].yield == 0) {
resetDetails(currentSlot);
}
}
currentSlot++;
// filter_max_value(currentSlots, CronSlots);
if(currentSlot >= cronSlots) {
currentSlot = 0;
}
}
CronId add(fptr fpointer, CronOptions options = SingleThread, us32 yield = 0)
{
CronId id = invalidSlot;
for(us8 i = 0; i < cronSlots; i++) {
if(options == MultiThread) {
if(slot[i].function == 0 && slot[i].yield == 0) {
id = i;
goto end;
}
}
else {
if(slot[i].function == fpointer) {
id = i;
goto end;
}
if(id == invalidSlot) {
if(slot[i].function == 0 && slot[i].yield == 0) {
id = i;
}
}
}
}
if(options == SingleThread && id != invalidSlot) {
goto end;
}
return invalidSlot;
end:
resetDetails(id);
slot[id].function = fpointer;
slot[id].yield = yield;
return id;
}
CronDetail* self()
{
if(cronStatus & 0x01) {
return &(slot[currentSlot]);
}
return 0;
}
CronDetail* at(us8 id)
{
if(id < cronSlots) {
return &(slot[id]);
}
return 0;
}
CronErrorCode searchByFunction(fptr fpointer, CronId *id)
{
us8 x = 0;
us8 i;
for(i = 0; i < cronSlots; i++) {
if(slot[i].function == fpointer) {
if(id != 0) {
*id = i;
}
x++;
}
}
if(x == 0) {
return NotFound;
}
return Ok;
}
void stopAll()
{
us8 i;
for(i = 0; i < cronSlots; i++) {
resetDetails(i);
}
currentSlot = 0;
}
CronErrorCode stopById(CronId id)
{
if(id >= cronSlots) {
return NotFound;
}
resetDetails(id);
return Ok;
}
CronErrorCode stopByFunction(fptr fpointer)
{
us8 x = 0;
us8 i;
for(i = 0; i < cronSlots; i++) {
if(slot[i].function == fpointer) {
resetDetails(i);
x++;
}
}
if(x == 0) {
return NotFound;
}
return Ok;
}
bool toggleFunction(fptr fpointer)
{
CronId id;
if(searchByFunction(fpointer, &id) == Cron::Ok) {
stopById(id);
}
else {
add(fpointer, Cron::SingleThread, 0);
return true;
}
return false;
}
void resetDetails(us8 i)
{
if(i < cronSlots) {
slot[i].function = 0;
slot[i].yield = 0;
slot[i].temp = 0;
slot[i].temp2 = 0;
slot[i].temp3 = 0;
slot[i].counter = 0;
}
}
us8 usedSlots()
{
us8 x = 0;
us8 i;
for(i = 0; i < cronSlots; i++) {
if(slot[i].yield != 0) {
x++;
}
}
return x;
}
void command(TokenParser *parser)
{
if(parser->compare("cron")) {
parser->nextToken();
if(parser->compare("status")) {
String s((us16)usedSlots());
s += "/";
s += cronSlots;
Serial.println(s);
}
if(parser->compare("stop")) {
stopAll();
Serial.println("OK");
}
}
}
private:
ulong_fptr systemTimer;
us8 currentSlot;
us8 cronStatus;
CronDetail slot[cronSlots];
};
#endif // CRON_H