-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavr-scheduler.c
285 lines (258 loc) · 7.66 KB
/
avr-scheduler.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* avr-scheduler.c
*
* Created: 11/22/2014 2:44:29 PM
* Author: CaptainSpaceToaster
*/
#include "avr-scheduler.h"
event_t event_t_error = {
.ms_duration = 0,
.ms_time = 65535,
.id = 255,
.next = NULL,
.previous = NULL,
};
event_t event_t_empty = {
.ms_duration = 0,
.ms_time = 0,
.id = 0,
.next = NULL,
.previous = NULL,
};
/* Creates an event_t in memory, and returns its pointer. */
event_t* new_event(uint16_t new_duration, uint16_t current_time, uint8_t new_id) {
event_t *ret = (event_t *)malloc(sizeof(event_t));
if (ret == NULL) {
//Didn't get anything from malloc... most likely out of memory
return NULL;
} if (new_duration > 65535-SCHEDULE_TIME_CEILING-SCHEDULE_THRESHOLD) {
return NULL;
}
ret->ms_duration = new_duration;
ret->ms_time = current_time;
ret->id = new_id;
ret->next = NULL;
ret->previous = NULL;
return ret;
}
/* Creates a schedule_t in memory, and returns its pointer. Sets the first and last element to NULL nodes, and size value to zero. */
schedule_t* new_schedule() {
schedule_t *ret = (schedule_t *)malloc(sizeof(schedule_t));
if (ret == NULL) {
//Didn't get anything from malloc... most likely out of memory
return NULL;
}
ret->first_event = NULL;
ret->last_event = NULL;
ret->size = 0;
return ret;
}
/* Empties the given schedule */
uint8_t schedule_clear(schedule_t *s) {
if (s == NULL) {
return SCHEDULE_NOT_FOUND; // return empty if it's not found
} else if (schedule_is_empty(s)) {
return 0;
}
event_t *temp = s->last_event;
while(temp != NULL) {
temp = s->last_event->previous;
free(s->last_event);
s->last_event = temp;
if (s->last_event != NULL) {
s->last_event->next = NULL;
}
s->size--;
}
return 0;
}
/* Checks the count, and returns true if the count is zero */
bool schedule_is_empty(schedule_t *s) {
if (s == NULL) {
return SCHEDULE_NOT_FOUND; // return empty if it's not found
}
return (s->size == 0);
}
/* Adds an object to the front of the schedule, regardless of its ms_time
*
* WARNING: This means that no matter what, the pushed event won't show up until
* all of the previous events to its entry are cleared...
*
* This is likely to ruin a precise schedule, unless you know what you're doing. */
uint8_t schedule_push(schedule_t *s, event_t *input, uint16_t current_time) {
if (s == NULL) {
return SCHEDULE_NOT_FOUND;
} if (input->ms_time > SCHEDULE_TIME_CEILING) {
return DURATION_TOO_LONG;
} if (input->ms_time > SCHEDULE_TIME_CEILING) {
return TIME_OVERFLOW;
}
input->next = s->first_event;
if (s->size == 0) {
s->first_event = input;
s->last_event = input;
} else if (s->size == 1) {
input->next = s->first_event;
s->first_event->previous = input;
s->first_event = input;
s->last_event->previous = s->first_event;
} else {
input->next = s->first_event;
s->first_event->previous = input;
s->first_event = input;
}
s->size ++;
return 0;
}
/* Adds an event_t pointer to the schedule, and places it according to its ms_duration and ms_time */
uint8_t schedule_insert(schedule_t *s, uint16_t duration, uint16_t current_time, uint8_t id) {
if (s == NULL) {
return SCHEDULE_NOT_FOUND;
} else if (s->size >= SCHEDULE_SIZE) {
return SCHEDULE_OVERFLOW;
} if (duration > 65535-SCHEDULE_TIME_CEILING-SCHEDULE_THRESHOLD) {
return DURATION_TOO_LONG;
}
event_t *input = new_event(duration, current_time, id);
if (input == NULL) {
return NULL_EVENT_ERROR;
}
if (s->size == 0) {
s->first_event = input;
s->last_event = input;
} else if (s->size == 1) {
if ( ((current_time >= s->first_event->ms_time) && (duration + current_time) >= (s->first_event->ms_duration + s->first_event->ms_time)) ||
((current_time < s->first_event->ms_time) && (duration + current_time+SCHEDULE_THRESHOLD)%SCHEDULE_TIME_CEILING >= (s->first_event->ms_duration + s->first_event->ms_time+SCHEDULE_THRESHOLD)%SCHEDULE_TIME_CEILING) )
{
input->next = s->first_event;
s->first_event->previous = input;
s->first_event = input;
s->last_event->previous = s->first_event;
} else {
input->previous = s->last_event;
s->last_event->next = input;
s->last_event = input;
s->first_event->next = s->last_event;
}
} else {
event_t *temp = s->first_event;
while(1) {
if (temp == NULL) { // temp iterated to the end of the list
input->previous = s->last_event;
s->last_event->next = input;
s->last_event = input;
break;
} else if ( ((current_time >= temp->ms_time) && (duration + current_time) >= (temp->ms_duration + temp->ms_time)) ||
((current_time < temp->ms_time) && (duration + current_time+SCHEDULE_THRESHOLD)%SCHEDULE_TIME_CEILING >= (temp->ms_duration + temp->ms_time+SCHEDULE_THRESHOLD)%SCHEDULE_TIME_CEILING) )
{
input->next = temp;
if (temp->previous == NULL) { // temp is the beginning of the list
s->first_event = input;
} else {
input->previous = temp->previous;
temp->previous->next = input;
}
temp->previous = input;
break;
} else {
temp = temp->next;
}
}
}
s->size++;
return 0;
}
/* Removes the last element from the schedule and returns a copy... this destroys the element */
event_t schedule_pop(schedule_t *s) {
if (s == NULL) {
return event_t_error; // SCHEDULE_NOT_FOUND
} else if (schedule_is_empty(s)) {
return event_t_error;
}
event_t ret = *(s->last_event);
event_t *temp = s->last_event->previous;
free(s->last_event);
s->last_event = temp;
if (s->last_event != NULL) {
// DO NOT REMOVE THIS COMMENT
s->last_event->next = NULL;
}
s->size--;
return ret;
}
/* Peeks at the last element from the schedule and returns it */
event_t schedule_peek(schedule_t *s) {
if (s == NULL) {
return event_t_error; // SCHEDULE_NOT_FOUND
} else if (schedule_is_empty(s)) {
return event_t_error;
}
return *(s->last_event);
}
/* Used to check the schedule, and consumes and returns an event if it's ready, or the empty_event if nothing is scheduled.
* Be prepared for the empty event... your code better be ready to be handed an empty_event
*
* This appears to be slipping null pointers in the schedule somehow... don't use for now */
event_t schedule_check(schedule_t *s, uint16_t current_time) {
if (s == NULL) {
return event_t_error; // SCHEDULE_NOT_FOUND
} else if (schedule_is_empty(s)) {
return event_t_error;
}
event_t *next_event = (s->last_event);
if (current_time >= next_event->ms_time) {
if ((current_time - next_event->ms_time) >= next_event->ms_duration) {
return schedule_pop(s);
}
} else {
if ((current_time+SCHEDULE_THRESHOLD) >= ((next_event->ms_time+next_event->ms_duration+SCHEDULE_THRESHOLD)%SCHEDULE_TIME_CEILING)) {
return schedule_pop(s);
}
}
return event_t_empty; // Nothing is ready
}
/* This stuff is for debugging mostly... you shouldn't need this */
void schedule_walk_forwards(schedule_t *s) {
event_t *temp = s->first_event;
while (temp != NULL) {
printf("%u \t%d \t%d\n", temp->ms_time, temp->ms_duration, temp->id);
temp = temp->next;
}
printf("\n");
}
void schedule_walk_backwards(schedule_t *s) {
event_t *temp = s->last_event;
while (temp != NULL) {
printf("%u \t%d \t%d\n", temp->ms_time, temp->ms_duration, temp->id);
temp = temp->previous;
}
printf("\n");
}
void schedule_test_forwards(schedule_t *s) {
event_t *temp = s->first_event;
uint8_t ctr = s->size;
while (temp != NULL) {
if (ctr != temp->id) {
printf("Failed\n");
schedule_walk_forwards(s);
return;
}
ctr--;
temp = temp->next;
}
printf("Passed\n");
}
void schedule_test_backwards(schedule_t *s) {
event_t *temp = s->last_event;
uint8_t ctr = 1;
while (temp != NULL) {
if (ctr != temp->id) {
printf("Failed\n");
schedule_walk_backwards(s);
return;
}
ctr++;
temp = temp->previous;
}
printf("Passed\n");
}