-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinytasker.h
179 lines (168 loc) · 4.23 KB
/
tinytasker.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
#pragma once
/*
* Copyright 2019, Edward L. Taychert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MAXTASKS
#define MAXTASKS 10
#endif
// just for documentation purposes ...
#ifndef USETINYSEMAPHORES
#undef USETINYSEMAPHORES
#endif
#ifndef BLINK13TASK
#undef BLINK13TASK
#endif
class Runable {
protected:
byte state ; // run-state not implented yet
byte taskID ;
unsigned long interval;
unsigned long wait ;
public :
enum { WAITING = 0, RUNNING};
virtual int setup (long) = 0 ;
virtual int loop (int) = 0 ;
void setTaskID(int n){taskID = n;};
int getTaskID() { return taskID;};
int getState () { return state; };
int setState (int s) { return (state=s);};
int ms() {
unsigned long now ;
now = millis() ;
if ( wait <= 0 ) { // setup a new delay
wait = now + interval ;
return 0 ;
}
if ( wait < now ) { // delay has expired
wait = now + interval ; // set up for next use
return 1 ;
}
return 0 ; // keep waiting ...
};
} ;
class Periodic : protected Runable {
public:
int setup(long interval) {
this->interval = interval;
state = RUNNING;
wait = 0 ;
return 0 ;
};
int loop(int arg = -1) {
if (state == RUNNING )
return ms();
else return WAITING ;
};
Periodic() {
wait = 0L;
};
};
// TBD: Derived classes are expensive, about 22 bytes each at runtime.
class Taskable : protected Runable {
protected:
friend class TinyTasker ;
public:
Taskable(long ms=4) {
setup(ms);
}
int setup(long ms) {
interval = ms;
state = RUNNING;
wait = 0 ;
return 0 ;
}
virtual int loop(int arg ) = 0 ;
};
class TinyTasker : protected Taskable {
Taskable* tasks[MAXTASKS];
int numRunable ;
public:
TinyTasker(long ms =4) {
setup(ms);
numRunable = 0 ;
}
int add(Taskable *t) {
t->setTaskID(-1);
for (int i=0 ; i<MAXTASKS; i++ ) {
if (tasks[i] == 0 ) {
tasks[i] = t;
t->setTaskID(i);
return i;
}
}
return 0;
}
int remove (int ti ) {
if ( ti < MAXTASKS )
if ( tasks[ti] != 0 ) {
tasks[ti] = 0 ;
return 1 ;
}
return 0;
}
int remove (Taskable *t) {
for (int i = 0 ; i < MAXTASKS ; i++) {
if ( tasks[i] == t) {
tasks[i] = 0;
return 1;
}
}
return 0;
}
int loop(int arg=0 ) {
if ( ms() ) {
long now = millis();
for (int i = 0 ; i < MAXTASKS ; i ++) {
if (tasks[i] != 0 ) {
if (tasks[i]->wait > now )
continue;
if ( tasks[i]->wait <= now )
tasks[i]->loop(0);
// rescedule next (including starting first)
tasks[i]->wait = now + tasks[i]->interval ;
}
}
// Future: Compare loop execution time and adduust as necessary
// TBD compare "now" to currect millis() to get loop time.
return 1;
}
}
};
#ifdef BLINK13TASK
class Blink13Task : public Taskable {
int pin;
public:
Blink13Task() {
pinMode(13, OUTPUT);
pin = 13;
Taskable:: setup(200);
}
int loop (int arg=0) {
if (interval < 500)
digitalWrite(pin, 0);
else
digitalWrite(pin, 1);
setup(1000 - interval);
return 0;
}
} blink13Task ;
#endif
#ifdef USETINYSEMAPHORES
#ifndef MAXSEMAS
#define MAXSEMAS 10
#endif
volatile int Semaphore[MAXSEMIS];
#define semaInc(a) (a)++
#define semaDec(a) (a)--
#define semaDrain(a) (a) = 0
#endif