-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatering_program.cpp
176 lines (135 loc) · 4.7 KB
/
watering_program.cpp
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
#include "watering_program.h"
#include "time_helper.h"
#include "pump_helper.h"
#include "convertors.h"
WateringProgram wateringScripts[SCRIPTS_COUNT];
void readWateringScripts(WateringProgram buf[]) {
int startAddress = START_SCRIPTS_ADDRESS;
int structureSize = sizeof(WateringProgram);
for (int i = 0; i < SCRIPTS_COUNT; i++) {
EEPROM.get(startAddress, buf[i]);
startAddress += structureSize;
}
}
void initializeWateringScripts() {
if (!checkIsInitialized()) {
initializeEEPROM();
}
readWateringScripts(wateringScripts);
}
void saveScript(int pos, WateringProgram script) {
int address = START_SCRIPTS_ADDRESS + pos * sizeof(WateringProgram);
EEPROM.put(address, script);
}
byte editWateringProgram(WateringProgram *p, unsigned long intervalSeconds, unsigned long nextWateringTime,
unsigned short durationSeconds, byte purposeId, bool enabled) {
if (intervalSeconds < MIN_INTERVAL_SECONDS or intervalSeconds < durationSeconds) {
return 2; // invalid interval
}
if (durationSeconds > MAX_WATERING_DURATION) {
return 3; // invalid duration
}
if (purposeId > SCRIPTS_COUNT - 1) {
return 4; // invalid pump id
}
if (nextWateringTime < currentTimeSeconds()) {
return 5; // next watering time can't be in the past
}
p->intervalSeconds = intervalSeconds;
p->durationSeconds = durationSeconds;
p->purposeId = purposeId;
p->enabled = enabled;
p->lastWateringTime = 0;
p->nextWateringTime = nextWateringTime;
return 1;
}
bool needToExecute(WateringProgram * script) {
if (!script->enabled || script->durationSeconds < 1) {
return false;
}
return currentTimeSeconds() > script->nextWateringTime;
}
byte tryToExecuteProgram(WateringProgram * script) {
if (!needToExecute(script)) {
return -2; // Этот скрипт выключен либо его время еще не пришло
}
if (!isWaterTankNotEmpty()) {
return -1; // Не может полить, так как нет воды в баке
}
turnOnOffThePump(true);
bool runOutOfWater = false;
int delayMs = 500;
int start = currentTimeSeconds();
int t = start;
while (t - start < script -> durationSeconds) {
delay(delayMs);
runOutOfWater = !isWaterTankNotEmpty();
if (runOutOfWater) {
break;
}
t = currentTimeSeconds();
}
turnOnOffThePump(false);
script->lastWateringTime = start;
script->nextWateringTime = script->nextWateringTime + script->intervalSeconds;
if (runOutOfWater) {
return 0; // Полив начался, но был прерван так как закончилась вода в емкости
}
return 1; // Полив успешно выполнен
}
void printScript(WateringProgram script) {
char buf[50];
snprintf(buf, sizeof(buf), "%lu %hu %d %lu %lu %d",
script.intervalSeconds, script.durationSeconds, script.purposeId, script.lastWateringTime, script.nextWateringTime, script.enabled);
Serial.print("Script str: ");
Serial.println(buf);
}
bool checkIsInitialized() {
bool isInitialized = true;
for (int i = 0; i < 3; i++) {
byte header = EEPROM.read(i);
isInitialized = (header == 0xFF);
if (!isInitialized) break;
}
return isInitialized;
}
void initializeEEPROM() {
// Write 0xFFFFFF header to memory (determines that memory is initialized)
for (int i = 0; i < START_SCRIPTS_ADDRESS; i++) {
EEPROM.write(i, 0xFF);
}
int startAddress = START_SCRIPTS_ADDRESS;
WateringProgram empty = {
0, 0, 0, 0, 0, false
};
int sizeOfStructure = sizeof(WateringProgram);
// Write empty scripts to memory
for (int i = 0; i < SCRIPTS_COUNT; i++) {
EEPROM.put(startAddress, empty);
startAddress += sizeOfStructure;
}
}
void cpyWateringProgramToBuf(WateringProgram p, byte buf[]) {
writeULongToBuf(buf, p.intervalSeconds);
writeUShortToBuf(buf + 4, p.durationSeconds);
writeULongToBuf(buf + 6, p.lastWateringTime);
writeULongToBuf(buf + 10, p.nextWateringTime);
buf[14] = p.purposeId;
buf[15] = (byte) p.enabled;
}
WateringProgram convertBufToWateringProgram(byte buf[], WateringProgram p) {
p.intervalSeconds = uLongFromBuf(buf);
p.durationSeconds = uShortFromBuf(buf+4);
p.lastWateringTime = uLongFromBuf(buf+6);
p.nextWateringTime = uLongFromBuf(buf+10);
p.purposeId = buf[14];
p.enabled = (bool) buf[15];
return p;
}
void cpyAllScriptsToBuf(byte buf[]) {
byte offset = 0;
for (int i = 0; i < SCRIPTS_COUNT; i++) {
cpyWateringProgramToBuf(wateringScripts[i], buf + offset);
offset += SCRIPT_BYTES_LEN;
}
}