-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialManager.cpp
411 lines (388 loc) · 12.3 KB
/
SerialManager.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "SerialManager.h"
#include <DS3232RTC.h> // http://github.com/JChristensen/DS3232RTC
#include <EEPROMWearLevel.h> // https://github.com/PRosenb/EEPROMWearLevel
SerialManager::SerialManager(byte bluetoothEnablePin) : bluetoothEnablePin(bluetoothEnablePin) {
if (bluetoothEnablePin != UNDEFINED) {
pinMode(bluetoothEnablePin, OUTPUT);
}
int freeRamValue = freeRam();
Serial.begin(9600);
// wait for serial port to connect
while (!Serial);
// print startup message
Serial.println();
Serial.print(F("------------------- startup, free RAM: "));
Serial.println(freeRamValue);
setSyncProvider(RTC.get);
startupTime = now();
if (timeStatus() != timeSet) {
Serial.println(F("RTC: Unable to sync"));
} else {
Serial.println(F("RTC: Set the system time"));
}
printTime(startupTime);
}
void SerialManager::setWaterManager(WaterManager *waterManager) {
SerialManager::waterManager = waterManager;
}
void SerialManager::startSerial() {
if (bluetoothEnablePin != UNDEFINED) {
digitalWrite(bluetoothEnablePin, HIGH);
}
serialLastActiveMillis = millis();
if (!aquiredWakeLock) {
aquiredWakeLock = true;
scheduler.acquireNoSleepLock();
}
if (!scheduler.isScheduled(this) ) {
scheduler.schedule(this);
}
}
void SerialManager::run() {
if (Serial.available() > 0) {
serialLastActiveMillis = millis();
handleSerialInput();
}
unsigned long serialSleepTimeoutMs = SERIAL_SLEEP_TIMEOUT_MS_DEFAULT;
EEPROMwl.get(EEPROM_INDEX_SERIAL_SLEEP_TIMEOUT_MS, serialSleepTimeoutMs);
if (millis() - serialLastActiveMillis > serialSleepTimeoutMs) {
if (aquiredWakeLock) {
aquiredWakeLock = false;
scheduler.releaseNoSleepLock();
Serial.println(F("stop serial"));
delay(150);
if (bluetoothEnablePin != UNDEFINED) {
digitalWrite(bluetoothEnablePin, LOW);
}
}
} else {
scheduler.scheduleDelayed(this, 1000);
}
}
void SerialManager::printTime(time_t t) {
Serial.print(year(t));
Serial.print(F("-"));
printTwoDigits(month(t));
Serial.print(F("-"));
printTwoDigits(day(t));
Serial.print(F(" "));
printTwoDigits(hour(t));
Serial.print(F(":"));
printTwoDigits(minute(t));
Serial.print(F(":"));
printTwoDigits(second(t));
Serial.print(F(", free RAM: "));
Serial.println(freeRam());
}
// sets the system and RTC time.
// used format: 2016-01-03T16:43
void SerialManager::handleSetDateTime() {
int yearValue = serialReadInt(4);
char dash1 = Serial.available() ? Serial.read() : 0;
int monthValue = serialReadInt(2);
char dash2 = Serial.available() ? Serial.read() : 0;
int dayValue = serialReadInt(2);
char tsign = Serial.available() ? Serial.read() : 0;
int hours = serialReadInt(2);
char colon = Serial.available() ? Serial.read() : 0;
int minutes = serialReadInt(2);
if (colon == ':' && dash1 == '-' && dash2 == '-' && (tsign == 'T' || tsign == ' ')
&& monthValue >= 1 && monthValue <= 12
&& dayValue >= 1 && dayValue <= 31
&& hours >= 0 && hours <= 24
&& minutes >= 0 && minutes <= 60) {
//set the system time (Time lib)
// setTime(hr,min,sec,day,month,yr)
setTime(hours, minutes, 0, dayValue, monthValue, yearValue);
//set the RTC from the system time
RTC.set(now());
} else {
Serial.println(F("Wrong format, e.g. use d2016-01-03T16:43"));
}
tmElements_t tm;
RTC.read(tm);
Serial.print(F("RTC values: "));
Serial.print(tm.Year + 1970, DEC);
Serial.print(F("-"));
printTwoDigits(tm.Month);
Serial.print(F("-"));
printTwoDigits(tm.Day);
Serial.print(F(" "));
printTwoDigits(tm.Hour);
Serial.print(F(":"));
printTwoDigits(tm.Minute);
Serial.print(F(":"));
printTwoDigits(tm.Second);
Serial.println();
}
void SerialManager::handleSetAlarmTime() {
int alarmNr = serialReadInt(1);
Serial.read(); // the :
// + 1 to allow one space for the Null termination
char inData[3 + 1];
readSerial(inData, 3 + 1);
if (strcmp(inData, "off") == 0) {
switch (alarmNr) {
case 1: {
RTC.alarmInterrupt(ALARM_1, false);
Serial.println(F("deactivate alarm 1"));
break;
}
case 2: {
RTC.alarmInterrupt(ALARM_2, false);
Serial.println(F("deactivate alarm 2"));
break;
}
default: {
Serial.println(F("wrong alarm number"));
break;
}
}
} else {
inData[2] = '\0'; // Null terminate the string
int hours = atoi(inData);
int minutes = serialReadInt(2);
if (hours >= 0 && hours <= 24 && minutes >= 0 && minutes <= 60 && (alarmNr == 1 || alarmNr == 2)) {
//setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate);
switch (alarmNr) {
case 1: {
RTC.setAlarm(ALM1_MATCH_HOURS, 0, minutes, hours, 0);
RTC.alarmInterrupt(ALARM_1, true);
break;
}
case 2: {
RTC.setAlarm(ALM2_MATCH_HOURS, 0, minutes, hours, 0);
RTC.alarmInterrupt(ALARM_2, true);
break;
}
}
Serial.print(F("set start time "));
Serial.print(alarmNr);
Serial.print(F(" to "));
Serial.print(hours);
Serial.print(F(":"));
Serial.println(minutes);
} else {
Serial.println(F("set start time failed, wrong format, expect s<hh>:<mm>, e.g. a14:45"));
}
}
}
#ifdef RTC_SUPPORTS_READ_ALARM
void SerialManager::handleGetAlarmTime(byte alarmNumber) {
if (alarmNumber == 0) {
alarmNumber = 1;
}
Serial.print(F("Alarm"));
Serial.print(alarmNumber);
Serial.print(F(": "));
if (RTC.isAlarmInterrupt(alarmNumber)) {
tmElements_t tm;
ALARM_TYPES_t alarmType = RTC.readAlarm(alarmNumber, tm);
printTwoDigits(tm.Hour);
Serial.print(F(":"));
printTwoDigits(tm.Minute);
Serial.print(F(":"));
printTwoDigits(tm.Second);
Serial.print(F(", day: "));
Serial.print(tm.Day);
Serial.print(F(", wday: "));
Serial.print(tm.Wday);
Serial.print(F(", alarmType: "));
switch (alarmType) {
case ALM1_EVERY_SECOND:
Serial.print(F("ALM1_EVERY_SECOND"));
break;
case ALM1_MATCH_SECONDS:
Serial.print(F("ALM1_MATCH_SECONDS"));
break;
case ALM1_MATCH_MINUTES:
Serial.print(F("ALM1_MATCH_MINUTES"));
break;
case ALM1_MATCH_HOURS:
Serial.print(F("ALM1_MATCH_HOURS"));
break;
case ALM1_MATCH_DATE:
Serial.print(F("ALM1_MATCH_DATE"));
break;
case ALM1_MATCH_DAY:
Serial.print(F("ALM1_MATCH_DAY"));
break;
case ALM2_EVERY_MINUTE:
Serial.print(F("ALM2_EVERY_MINUTE"));
break;
case ALM2_MATCH_MINUTES:
Serial.print(F("ALM2_MATCH_MINUTES"));
break;
case ALM2_MATCH_HOURS:
Serial.print(F("ALM2_MATCH_HOURS"));
break;
case ALM2_MATCH_DATE:
Serial.print(F("ALM2_MATCH_DATE"));
break;
case ALM2_MATCH_DAY:
Serial.print(F("ALM2_MATCH_DAY"));
break;
}
Serial.print(F(", cmd: a"));
Serial.print(alarmNumber);
Serial.print(F(":"));
printTwoDigits(tm.Hour);
Serial.print(F(":"));
printTwoDigits(tm.Minute);
Serial.println();
} else {
Serial.println(F("off"));
}
}
#endif // RTC_SUPPORTS_READ_ALARM
void SerialManager::handleStatus() {
const byte subCommand = Serial.available() ? Serial.read() : 0;
if (subCommand == 'e') {
Serial.available() ? Serial.read() : 0; // char colon
int startAddress = serialReadInt(3);
Serial.available() ? Serial.read() : 0; // char comma
int endAddress = serialReadInt(3);
EEPROMwl.printStatus(Serial);
if (endAddress > 0) {
EEPROMwl.printBinary(Serial, startAddress, endAddress);
Serial.println();
}
} else {
Serial.print(F("Startup time: "));
printTime(startupTime);
Serial.print(F("Current time: "));
setSyncProvider(RTC.get);
printTime(now());
int resetCount = 0;
EEPROMwl.get(EEPROM_INDEX_WATCHDOG_RESET_COUNT, resetCount);
Serial.print(F("WD reset count: "));
Serial.println(resetCount);
unsigned long serialSleepTimeoutMs = SERIAL_SLEEP_TIMEOUT_MS_DEFAULT;
EEPROMwl.get(EEPROM_INDEX_SERIAL_SLEEP_TIMEOUT_MS, serialSleepTimeoutMs);
Serial.print(F("Serial sleep timeout: "));
Serial.print(serialSleepTimeoutMs / 1000 / 60);
Serial.println(F(" min"));
waterManager->printStatus();
}
}
void SerialManager::handleSerialInput() {
char command = Serial.read();
switch (command) {
case 'd':
handleSetDateTime();
break;
case 'a':
handleSetAlarmTime();
break;
case 't':
RTC.setAlarm(ALM1_MATCH_SECONDS, 0, 0, 0, 0);
Serial.println(F("alarm every minute"));
break;
#ifdef RTC_SUPPORTS_READ_ALARM
case 'g':
handleGetAlarmTime(1);
handleGetAlarmTime(2);
break;
#endif // RTC_SUPPORTS_READ_ALARM
case 'm':
waterManager->modeClicked();
break;
case 'i':
waterManager->startAutomatic();
break;
case 'j':
waterManager->startAutomaticRtc();
break;
case 's':
handleStatus();
break;
case 'w':
handleWrite();
break;
default:
Serial.print(F("Unknown command: "));
Serial.println(command);
Serial.println(F("Supported commands:"));
Serial.println(F("a1:<hh>:<mm>: set alarm 1 time"));
Serial.println(F("a1:off: deactivate alarm 1"));
Serial.println(F("a2:<hh>:<mm>: set alarm 2 time"));
Serial.println(F("a2:off: deactivate alarm2"));
Serial.println(F("t set alarm every minute (for testing)"));
#ifdef RTC_SUPPORTS_READ_ALARM
Serial.println(F("g: get alarm times"));
#endif // RTC_SUPPORTS_READ_ALARM
Serial.println(F("d<YYYY>-<MM>-<DD>T<hh>:<mm>: set date/time"));
Serial.println(F("m: change mode"));
Serial.println(F("i: start automatic"));
Serial.println(F("j: start automatic RTC"));
Serial.println(F("wz<zone>:<value 3 digits> write zone duration in minutes"));
Serial.println(F("wm:<value 3 digits> write water meter stop threshold"));
Serial.println(F("ws:<value 3 digits> write serial sleep timeout in minutes"));
Serial.println(F("s print status"));
Serial.println(F("se print status of EEPROM"));
Serial.println(F("se:<from 3 digits>,<to 3 digits> print status of EEPROM"));
}
}
void SerialManager::handleWrite() {
char writeType = Serial.read();
switch (writeType) {
case 'z': {
int zoneNr = serialReadInt(1);
Serial.read(); // the :
int durationMin = serialReadInt(3);
Serial.print(F("handleWrite: "));
Serial.print(writeType);
Serial.print(F(" "));
Serial.print(zoneNr);
Serial.print(F(" "));
Serial.println(durationMin);
unsigned int durationSec = durationMin * 60UL;
waterManager->setZoneDuration(zoneNr, durationSec);
break;
}
case 's': {
Serial.read(); // the :
int serialSleepTimeoutMin = serialReadInt(3);
Serial.print(F("serialSleepTimeoutMin: "));
Serial.println(serialSleepTimeoutMin);
unsigned long serialSleepTimeoutMs = serialSleepTimeoutMin * 60 * 1000L;
EEPROMwl.put(EEPROM_INDEX_SERIAL_SLEEP_TIMEOUT_MS, serialSleepTimeoutMs);
break;
}
case 'm': {
Serial.read(); // the :
int waterMeterStopThreshold = serialReadInt(3);
Serial.print(F("waterMeterStopThreshold: "));
Serial.println(waterMeterStopThreshold);
waterManager->setWaterMeterStopThreshold(waterMeterStopThreshold);
break;
}
}
}
// ----------------------------------------------------------------------------------
// helper
// ----------------------------------------------------------------------------------
void SerialManager::readSerial(char inData[], int inDataLength) {
byte index = 0;
// index + 1 to allow one space for the Null termination
for (; Serial.available() > 0 && index + 1 < inDataLength; index++) {
inData[index] = Serial.read();
}
inData[index] = '\0'; // Null terminate the string
}
int SerialManager::serialReadInt(int length) {
char inData[length + 1];
readSerial(inData, length + 1);
return atoi(inData);
}
void SerialManager::printTwoDigits(unsigned int value) {
if (value < 10) {
Serial.print(F("0"));
}
Serial.print(value);
}
int SerialManager::freeRam() {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}