-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.h
197 lines (180 loc) · 4.26 KB
/
schedule.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
#ifndef __SCHEDULE_H_
#define __SCHEDULE_H_
#include <avr/sleep.h>
static constexpr uint16_t TickPrecision = 15;
class Task
{
protected:
enum class State {
Waiting,
Running
};
State current_state;
public:
virtual unsigned long step() {
return 0;
}
void play() {
current_state = State::Running;
}
unsigned long pause() {
current_state = State::Waiting;
return pause_wait;
}
Task(State start_state, unsigned long sleep_delay = 1000)
: current_state(start_state),
pause_wait(sleep_delay)
{
}
virtual unsigned long run() {
if(current_state == State::Running) {
return step();
}
return pause_wait;
}
virtual void setup() {}
virtual void wakeup() {}
virtual void sleep() {}
virtual void stop() {
current_state = State::Waiting;
}
bool is_running() {
return current_state == State::Running;
}
virtual bool can_sleep() const {
return true;
}
private:
unsigned long pause_wait;
};
class Scheduler
{
private:
struct TaskNode
{
Task* task;
unsigned long delay;
TaskNode* next;
TaskNode(Task* t, unsigned long dl, TaskNode* n)
: task(t), delay(dl), next(n){}
};
void update_delays() {
auto current_millis = millis();
if(current_millis == prev_millis) {
return;
}
auto diff = current_millis - prev_millis;
prev_millis = current_millis;
TaskNode* n = tasks;
while(n) {
if(n->delay > diff) {
n->delay -= diff;
} else {
n->delay = 0;
}
n = n->next;
}
}
TaskNode* get_next_task() {
update_delays();
if(tasks && tasks->delay == 0) {
auto n = tasks;
tasks = tasks->next;
n->next = nullptr;
return n;
}
return nullptr;
}
void add_task_sorted(TaskNode* tsk) {
if(!tasks || tasks->delay > tsk->delay) {
tsk->next = tasks;
tasks = tsk;
} else {
auto p = tasks;
auto n = tasks->next;
while(n && n->delay <= tsk->delay) {
p = n;
n = n->next;
}
tsk->next = n;
p->next = tsk;
}
}
void sleep_tasks() {
TaskNode *p = tasks;
while(p) {
p->task->sleep();
p = p->next;
}
}
void wakeup_tasks() {
TaskNode *p = tasks;
while(p) {
p->task->wakeup();
p = p->next;
}
}
void sleep_now() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
}
void sleep() {
sleep_tasks();
delay(100);
sleep_now();
enter_sleep = false;
wakeup_tasks();
}
TaskNode* tasks;
TaskNode* unscheduled;
unsigned long prev_millis;
bool enter_sleep;
public:
Scheduler() : tasks(nullptr), unscheduled(nullptr), prev_millis(millis()), enter_sleep(false) {
}
void add_task(Task& task) {
tasks = new TaskNode { &task, 0, tasks};
}
void setup_tasks() {
TaskNode *p = tasks;
while(p) {
p->task->setup();
p = p->next;
}
}
void run_next() {
if(enter_sleep) {
sleep();
}
TaskNode* tsk = get_next_task();
while(tsk)
{
auto next_delay = tsk->task->run();
if(next_delay == static_cast<unsigned long>(-1)) {
tsk->next = unscheduled;
unscheduled = tsk;
}
else {
tsk->delay = next_delay;
add_task_sorted(tsk);
}
if(enter_sleep) {
sleep();
}
tsk = get_next_task();
}
if(tasks->delay > 0) {
delay(tasks->delay);
}
}
void start_sleep() {
enter_sleep = true;
}
};
#endif