queue.ino
2.22 KB
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
#include "queue.h"
void insert(char *command, double start, double duration, int level) {
struct action *ptr = head;
// no dupes, only create new if it is scheduled
while(ptr != NULL) {
if ( command == T_PUMPLEVEL
|| command == T_PUMPDURATION
|| command == T_PUMPABORT
&&
ptr->command == T_PUMPLEVEL
|| ptr->command == T_PUMPDURATION
) {
break;
}
}
if ( ptr == NULL ) {
ptr = (struct action*) malloc(sizeof(struct action));
ptr->next = NULL;
ptr->command = command;
ptr->running = 0;
// in an empty list, this is head
if (head == NULL) {
head = ptr;
}
// previous tail is not last anymore
if (tail != NULL) {
tail->next = ptr;
}
// this is now tail
tail = ptr;
}
if ( command == T_PUMPABORT ) {
ptr->end = 1;
ptr->running = 1;
} else {
ptr->start = start;
ptr->duration = duration;
ptr->level = level;
}
}
void loop_queue() {
struct action *ptr = head;
struct action *next = NULL;
struct action *prev = NULL;
unsigned int ut = time(NULL);
while(ptr != NULL) {
next=ptr->next;
Serial.println("---------------");
Serial.println(ptr->command);
Serial.println(ptr->running);
Serial.println(ut);
Serial.println(ptr->start);
Serial.println(ptr->end);
Serial.println(ptr->duration);
Serial.println("---------------");
///////////////////////// start action
if ( ptr->running == 0
&& ut >= ptr->start
) {
if ( pump_start(ptr) ) { // pump is blocked ?
ptr->end = ptr->duration + ut;
ptr->running = 1;
} else {
ptr->start++;
}
}
///////////////////////// action
if ( ptr->running == 1 ) {
pump_running(ptr);
}
///////////////////////// end action
if ( ptr->running == 1
&& ut >= ptr->end
) {
pump_end(ptr);
/* action is due to removal */
if (prev != NULL) {
prev->next = ptr->next;
}
if (ptr == head) {
head = ptr->next;
}
if (ptr == tail) {
tail = prev;
}
free(ptr);
} else {
prev = ptr;
}
ptr = next;
}
}
void setup_queue() {
}