pump.ino
2.32 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
#include "pump.h"
void insertSequence(int pump, int src_valve, int dst_valve, double duration, struct action *action) {
unsigned int ut = time(NULL);
struct sequence *ptr = (struct sequence*) malloc(sizeof(struct sequence));
sequence_tail->next = ptr;
if ( sequence_head == NULL ) {
sequence_head = ptr;
ptr->end = ut + duration;
ptr->start = ut;
}
ptr->pump = pump;
ptr->src_valve = src_valve;
ptr->dst_valve = dst_valve;
ptr->start = sequence_tail->start;
ptr->end = sequence_tail->end + duration;
ptr->action = action;
ptr->next = NULL;
sequence_tail=ptr;
}
void next_sequence_step() {
if ( sequence_head != NULL ) { // there's something to do
unsigned int ut = time(NULL);
if ( ut >= sequence_head->end ) { // do next step
struct sequence *tmp = (struct sequence*) malloc(sizeof(struct sequence));
tmp = sequence_head;
sequence_head = tmp->next;
if ( sequence_head != NULL ) {
set_relay(sequence_head);
}
free(tmp);
}
}
}
int pump_start(struct action *ptr) {
if ( sequence_head == NULL ) {
String remainingStr(ptr->duration);
mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr);
insertSequence( dive, src_valve_pump, dst_valve_rotate, ptr->duration, ptr );
insertSequence( off , off , off , 0 , ptr );
set_relay(sequence_head); // kickoff sequence
return 1;
} else {
return 0;
}
}
int pump_start(struct button *ptr){
return 1;
}
void pump_running(struct action *ptr) {
unsigned int ut = time(NULL);
String remainingStr(ptr->end - ut);
mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}
void pump_end(struct action *ptr) {
// abort
/* pcf8574.digitalWrite(leeren11, 1); */
/* pcf8574.digitalWrite(leeren1, 1); */
/* pcf8574.digitalWrite(leeren2, 1); */
/* pcf8574.digitalWrite(fuellen2, 1); */
String remainingStr(0);
mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}
void pump_end(struct button *ptr) {
}
void pump_end(int source) {
}
void set_relay(struct sequence *step) {
int pin = 1;
while ( pin < 7 ) {
if ( step->pump == pin
|| step->src_valve == pin
|| step->dst_valve == pin
) {
/* pcf8574.digitalWrite(pin,0); */
} else {
/* pcf8574.digitalWrite(pin,1); */
}
pin++;
}
}