-
Notifications
You must be signed in to change notification settings - Fork 2
/
coos.h
264 lines (248 loc) · 9.43 KB
/
coos.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Library COOS - COoperative Operating System
* Author A.Kouznetsov
* Rev 1.6 dated 13/10/2019
* Target Arduino
Redistribution and use in source and binary forms, with or without modification,
are permitted.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __COOS_H
#define __COOS_H
//##############################################################################
// Inc
//##############################################################################
#include <Arduino.h>
#include <setjmp.h>
//##############################################################################
// Typedef
//##############################################################################
#ifndef __UCHAR_DEFINED__
#define __UCHAR_DEFINED__
typedef unsigned char uchar;
typedef signed char schar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef signed long slong;
#endif
//##############################################################################
// Def
//##############################################################################
#define COOS_STOP -2 // set this value to stop task
#define COOS_DELAY(__delay) if (!setjmp(coos.task_context[coos.task_no])) {longjmp(coos.main_context, __delay+1);} else{}
#define COOS_REV_MAJ 1
#define COOS_REV_MIN 6
//##############################################################################
// Template class
//##############################################################################
template <unsigned char COOS_MAX_TASKS, char TIMING> class Coos{
public:
Coos(void); // constructor
void register_task(void (*tsk)(void)); // user's tasks must be registered first
void register_clock(void (*clk)(void)); // users's clock function will be invoked every minute
void start(void); // init scheduler once
void run(void); // COOS task switcher
uchar hour(void); // current hour since midnight
uchar minute(void); // current minute
jmp_buf main_context; // context of scheduler
jmp_buf task_context[COOS_MAX_TASKS]; // list of task contexts
uchar task_no; // current task No
int task_delay[COOS_MAX_TASKS]; // task delay in msec, task stopped if value is negative
unsigned int msec; // ms of current sec
unsigned long daysec; // seconds since midnight
unsigned long uptime; // seconds since start
private:
void (*tsk_p[COOS_MAX_TASKS])(void); // list of registered tasks
unsigned int ms;
unsigned char task_cnt; // counts registered coos tasks
void (*clkfunc)(void); // user's clock function
void update_time(void);
unsigned char stored_min;
};
//##############################################################################
// Implementation
//##############################################################################
// =================================
// Constructor
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> Coos<COOS_MAX_TASKS, TIMING>::Coos(void)
{
uptime = 0;
daysec = 0;
msec = 0;
ms = 0;
task_cnt = 0;
for (int i=0; i<COOS_MAX_TASKS; i++)
{
tsk_p[i] = NULL; // task is not registered
task_delay[i] = COOS_STOP; // all unregistered tasks stopped
}
clkfunc = NULL;
}
// =================================
// Register a task
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> void Coos<COOS_MAX_TASKS, TIMING>::register_task(void (*tsk)(void))
{
if (task_cnt < COOS_MAX_TASKS)
{
tsk_p[task_cnt++] = tsk;
}
}
// =================================
// Register user's clock function
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> void Coos<COOS_MAX_TASKS, TIMING>::register_clock(void (*clk)(void))
{
clkfunc = clk;
}
// =================================
// Update time
// =================================
// supposed to happen more often than every millisecond,
// task should not keep control for more than about 900 us
template <unsigned char COOS_MAX_TASKS, char TIMING> void Coos<COOS_MAX_TASKS, TIMING>::update_time(void)
{
unsigned int millisec = (unsigned int)millis();
if (TIMING) // ticks 1.024 ms
{
if (ms != millisec) // 1024 us passed, decrement task delays once
{
for (int i=0; i<task_cnt; i++)
{
if (task_delay[i] > 0) // decrement positive delays
{
task_delay[i]--;
}
}
}
while (ms != millisec) // about every 42 ms millis() is corrected, eg changed by 2
{
ms++;
if (++msec >= 1000) // if 1 sec passed
{
uptime++; // count seconds since start
if (++daysec >= 86400) // count seconds since midnight
{
daysec = 0;
}
msec = 0;
if (clkfunc) // if user's clock function registered
{
unsigned char min = (unsigned char)((daysec % 3600) / 60);
if ( min != stored_min)
{
stored_min = min;
clkfunc(); // invoke user's clock function
}
}
}
}
}
else // ticks 1 ms in average
{
while (ms != millisec) // catch up time
{
ms++;
for (int i=0; i<task_cnt; i++)
{
if (task_delay[i] > 0) // decrement positive delays
{
task_delay[i]--;
}
}
if (++msec >= 1000) // if 1 sec passed
{
uptime++; // count seconds since start
if (++daysec >= 86400) // count seconds since midnight
{
daysec = 0;
}
msec = 0;
if (clkfunc) // if user's clock function registered
{
unsigned char min = (unsigned char)((daysec % 3600) / 60);
if ( min != stored_min)
{
stored_min = min;
clkfunc(); // invoke user's clock function
}
}
}
}
}
}
// =================================
// Get current time of the day based on daysec
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> unsigned char Coos<COOS_MAX_TASKS, TIMING>::hour(void)
{
return (unsigned char)(daysec / 3600);
}
template <unsigned char COOS_MAX_TASKS, char TIMING> unsigned char Coos<COOS_MAX_TASKS, TIMING>::minute(void)
{
return (unsigned char)((daysec % 3600) / 60);
}
// =================================
// Start scheduler - init registered tasks
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> void Coos<COOS_MAX_TASKS, TIMING>::start(void)
{
int res;
void (*tsk)(void);
update_time();
for (task_no=0; task_no<task_cnt; task_no++)
{
if (tsk_p[task_no] != NULL) // if task was registered
{
res = setjmp(main_context);
if (res == 0)
{
tsk = tsk_p[task_no];
tsk(); // invoke task
}
else
{
task_delay[task_no] = --res; // task returns the required delay
}
}
update_time();
}
task_no = 0;
}
// =================================
// Run scheduler on regular basis
// =================================
template <unsigned char COOS_MAX_TASKS, char TIMING> void Coos<COOS_MAX_TASKS, TIMING>::run(void)
{
int res;
int tmp;
if (task_delay[task_no] == 0)
{
res = setjmp(main_context); // set return point and get delay value from the task
if (res == 0) // after setting return point
{
longjmp(task_context[task_no], 1); // invoke task
}
else // after returning from task
{
tmp = --res;
task_delay[task_no] = tmp; // set task delay (negative delay - task stopped)
}
}
if (++task_no >= task_cnt) // next task
{
task_no = 0; // round-robin
}
update_time();
}
#endif /* __COOS_H */