forked from arkhipenko/apis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.ino
executable file
·1495 lines (1238 loc) · 36.3 KB
/
apis.ino
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------
Automatic Plant Irrigation System - APIS
Code Version 1.8.1
Parameters Version 13
Change Log:
2015-02-27:
v0.9.0 - added support for RTC for no irrigation at night & watering log & brightness control
v0.9.0 - fixed run log issue with index overwrite
2015-03-10:
v1.0.0 - support for parameterized weekend adjustment time
2015-03-24:
v1.1.0 - support for adjusting settings and time with 3 buttons: Select, + and -
v1.1.0 - added suturation in minutes to the list of configurable parameters
v1.1.0 - RTC_Millis is used for testing
2015-04-27:
v1.2.0 - delay after exiting the log (bug fix) - v11 triggered interrupt too quickly
v1.2.0 - force pumping as one of the options via buttons. Only works if hum < upper threshold.
v1.2.0 - different handling of error situation (Err for 24h and then restart to try again)
v1.2.0 - compiled with PROGMEM support in TM1650 library
2015-05-16:
v1.4.0 - hardware change: power soil probe from the digital pin to limit the time probe is under voltage
v1.4.0 - change measure routine to enable power digital pin first/wait for 1 minute to settle and then measure
v1.4.0 - move to measuring once per 1 hour with display off most of the time
v1.4.0 - display humidity only once per hour -OR- on a button press on demand - no need to show previous run since a log option is available
2015-05-18:
v1.5.0 - use of PinChange library to enable interrupts on all button pins
2015-08-25:
v1.6.0 - fix bug: saturation animation not turned off for the night
v1.6.0 - enable probe during water runs, and disable during saturation
v1.6.0 - switched to EnableInterrupt library
v1.6.0 - switched to TaskScheduler 1.8.0
v1.6.0 - implement automatic DST switchover via use of Timezone library. Device's RTC will run on UTC time.
2015-10-21:
v1.7.0 - switch to EEPROM.update - a write function only writes value if it is different from the value already stored in the cell
2015-11-16:
v1.7.0 - switch to DirectIO library
v1.7.0 - implement button repeat with varying (increasing) rate
2015-11-17:
v1.7.1 - debounce the buttons with 10 ms delay
v1.7.1 - complie against the fork of Time to exclude time provider sync at every call to now()
2015-11-19
v1.8.0 - more extensive logging, including start/stop time of the watering run, number of runs and start/stop humidity
2015-11-20
v1.8.1 - bug fix: Settings timeout should not kick in while displying running text
v1.8.1 - bug fix: Cancel settings leads to 30 seconds delay instead of 5 seconds (TaskScheduler bug discovered)
2015-12-23
v1.8.2 - complied with latest TM1650 ro enable gradual brightness 7 seg panel On and Off
v1.8.2 - compiled with the latest TaskScheduler v2.0.0
----------------------------------------*/
#include <TM1650.h>
#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
#include <EEPROM.h>
#include <Wire.h>
#include <RTClib.h>
#include <AvgFilter.h>
//#define EI_ARDUINO_INTERRUPTED_PIN
#include <EnableInterrupt.h>
//#include <Time.h>
#include <Timezone.h>
#include <DirectIO.h>
//#define _DEBUG_
//#define _TEST_
//#define _USERTCMILLIS_
#define RETRIES 3
#define RETRIES_MIN 1
#define RETRIES_MAX 10
// Current pump is rated at 100 ml/min
// split BTW 2 plants: 50 ml per run
// max watering is 200 ml with 4 default runs
// Time to run pump within one water run
#define WATERTIME 60 //Seconds
#define WATERTIME_MIN 5 //Seconds
#define WATERTIME_MAX 120 //Seconds
// Time to saturate
#define SATURATE 5 // 5 minutes
#define SATURATE_MIN 1 // 1 minute
#define SATURATE_MAX 90 // 10 minutes
// % soil humidity to start pumping (low threshold)
#define NEEDWATER 50 // % to start pumping
#define NEEDWATER_MIN 20 // % to start pumping
#define NEEDWATER_MAX 75 // % to start pumping
// % soil humidity to stop pumping (high threshold)
#define STOPWATER 60 // % to stop pumping
#define STOPWATER_MIN 25 // % to stop pumping
#define STOPWATER_MAX 90 // % to stop pumping
// Hour of the day to "go to sleep" (i.e., do not operate after this hour)
#define GOTOSLEEP 22 // hour to go to sleep
#define GOTOSLEEP_MIN 0 // hour to go to sleep
#define GOTOSLEEP_MAX 24 // hour to go to sleep
// Hour of the day to "wake up" (i.e., operate after this hour)
#define WAKEUP 7 // hour to wake up
#define WAKEUP_MIN 0 // hour to wake up
#define WAKEUP_MAX 24 // hour to wake up
// Number of hours to add to wake up time on a weekend
#define WEEKENDADJ 2 // number of hours to add for the wakeup on a weekend
#define WEEKENDADJ_MIN 0 // number of hours to add for the wakeup on a weekend
#define WEEKENDADJ_MAX 12 // number of hours to add for the wakeup on a weekend
#define PARAMADDR 128
#define LOGIDXADDR (PARAMADDR+sizeof(parameters))
#define LOGADDR (LOGIDXADDR+2)
#define MAXLOGS 20
#define RUNNING_DISPLAY_START_DELAY 1000
#define RUNNING_DISPLAY_DELAY 250
#define SENSOR_OUT_VALUE 1000
#define MOISTUREPIN A1
#define POWERUP_PIN 10
#define BTN_SEL_PIN 2
#define BTN_PLUS_PIN 8
#define BTN_MINUS_PIN 9
#define BTN_INIT 2000
#define BTN_REPEAT 1000
#define BNT_RPT_CNT 5
#define BTN_RAPID 250
#define BNT_DEBOUNCE 20
AnalogInput<MOISTUREPIN> pMoisture;
Output<POWERUP_PIN> pPowerup;
Input<BTN_SEL_PIN> pSelect;
Input<BTN_PLUS_PIN> pPlus;
Input<BTN_MINUS_PIN> pMinus;
Output<13> pLED;
// L293D pinout
#define M1P1 6
#define M1P2 7
#define M1E1 5
Output<M1P1> pM1P1;
Output<M1P2> pM1P2;
Output<M1E1> pM1E1;
const char CToken[] = "APIS13\0"; // Eeprom token: Automatic Plant Irrigation System
int state;
boolean error, night_time;
#ifndef _TEST_
int currentHumidity = 0;
#else
int currentHumidity = 60;
#endif;
enum Display_Options : byte {
DHUMIDITY,
DHIGH_MARK,
DLOW_MARK,
DWATER_TIME,
DWATER_RETRIES,
DERROR,
DGOODNIGHT,
DLOG,
DLOG_V,
DLOG_P,
DSTORED,
DCANCELLED,
DCLK_SET,
DSET,
DSET_UL,
DSET_LL,
DSET_RT,
DSET_PT,
DSET_ST,
DSET_SL,
DSET_UP,
DSET_AD,
DCLK,
DCLK_CN, DCLK_YR, DCLK_MT, DCLK_DY, DCLK_HR, DCLK_MN,
DFRN,
DTIME,
DRUN,
DEMPTY,
};
Display_Options displayNow = DHUMIDITY;
TM1650 panel;
bool panel_status;
byte panel_brightness = TM1650_MAX_BRIGHT;
// volatile int buttonPressed;
#ifndef _TEST_
#define TMEASURE_INTERVAL 3570 // 59 minutes
#define TMEASURE_PRIME 30 // then 1 minute to power up the probe = 1 hour sensor interval
#else
#define TMEASURE_INTERVAL 45
#define TMEASURE_PRIME 5
#endif
#define TMEASURE_WATERTIME 5 // measure every 5 seconds while watering
#define TDISPLAY_INTERVAL 3600
#define TDISPLAY_TIMEOUT 30
#define TDISPLAY_SHORT_DELAY 5
#define TGOODNIGHT_INTERVAL 60
#define TSETTOUT_INTERVAL 15
#define ANIMATION_MILLIS 250
#define TI_INTERVAL 500 //button press delay
Scheduler ts;
// Forward definition of all callback methods is now required by v1.6.6
void buttonISR();
void goodnightCallbackInit();
void measureCallback();
void settingsToutCallback();
void displayCallback();
void interruptCallback();
void waterCallback();
bool waterOnEnable();
void waterOnDisable();
void animationWaterCallback();
void errorCallback();
void timeSyncCallback();
void displayRunningCallback();
void displayTimeout();
void waterOffCallback();
Task tIsr (BTN_REPEAT, TASK_FOREVER, &buttonISR, &ts);
Task tGoodnight (TGOODNIGHT_INTERVAL * TASK_SECOND, TASK_FOREVER, &goodnightCallbackInit, &ts, true);
Task tMeasure (TMEASURE_PRIME * TASK_SECOND, TASK_FOREVER, &measureCallback, &ts, true);
Task tSettingsTimeout (TSETTOUT_INTERVAL * TASK_SECOND, TASK_ONCE, &settingsToutCallback, &ts);
Task tDisplay (TDISPLAY_INTERVAL * TASK_SECOND, TASK_FOREVER, &displayCallback, &ts, true);
Task tDisplayRunning (RUNNING_DISPLAY_START_DELAY, TASK_FOREVER, &displayRunningCallback, &ts);
Task tDisplayTimeout (TDISPLAY_TIMEOUT * TASK_SECOND, TASK_FOREVER, &displayTimeout, &ts, true);
Task tInterrupt (TI_INTERVAL, TASK_ONCE, &interruptCallback, &ts);
Task tWater (SATURATE * TASK_MINUTE, RETRIES, &waterCallback, &ts, false, &waterOnEnable, &waterOnDisable);
Task tWaterOff (WATERTIME * TASK_SECOND, TASK_ONCE, &waterOffCallback, &ts);
Task tWaterAnimation (ANIMATION_MILLIS, TASK_FOREVER, &animationWaterCallback, &ts);
Task tError (TASK_IMMEDIATE, TASK_FOREVER, &errorCallback, &ts);
#ifdef _TEST_
void testHumidityCallback();
Task tTestHumidity(20000, TASK_FOREVER, &testHumidityCallback, &ts); // temporarly call now() every minute to advance system time in Time library
#endif
struct {
char token[7]; // +0: 6 digit token = APISxx, where xx is a version + '\0'
byte high; // +7: high humidity mark - stop watering
byte low; // +8: low humidity mark - start watering
byte retries; // +9: number of watering runs before give up (if high not reached)
byte watertime; // +10: pumping duration
byte saturate; // +11: saturation duration
byte gotosleep; // +12: hour to go goodnight (e.g., 22)
byte wakeup; // +13: hour to wake up (e.g., 08)
byte wkendadj; // +14: weekend wake up adjustment time
} parameters;
struct {
time_t water_start; // time watering run started
byte hum_start; // humidity at the start
byte num_runs; // number of runs it took to water
byte run_duration; // run durations
time_t water_end; // time watering stopped
byte hum_end; // humidity at the end of run
} water_log;
struct {
byte cn;
byte yr;
byte mt;
byte dy;
byte hr;
byte mn;
} time;
byte *adjParam;
int paramIndex;
#define PARAMIDXSTART 0
#define CLOCKIDXSTART 8
#define DAYSIDX 11
byte paramMin[] = {STOPWATER_MIN, NEEDWATER_MIN, RETRIES_MIN, WATERTIME_MIN, SATURATE_MIN, GOTOSLEEP_MIN, WAKEUP_MIN, WEEKENDADJ_MIN, 19, 00, 01, 01, 00, 00 };
byte paramMax[] = {STOPWATER_MAX, NEEDWATER_MAX, RETRIES_MAX, WATERTIME_MAX, SATURATE_MAX, GOTOSLEEP_MAX, WAKEUP_MAX, WEEKENDADJ_MAX, 29, 99, 12, 31, 23, 59 };
char paramChar[] = "ULrPSbuacytdhn";
#ifndef _USERTCMILLIS_
RTC_DS1307 rtc;
#else
RTC_Millis rtc;
#endif
// Support for timezones:
//US Eastern Time Zone (New York)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
byte logIndex;
byte logCount;
//
// CODE
//
void motorOff()
{
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": motorOff"));
#endif
// digitalWrite(M1E1, LOW);
// digitalWrite(M1P1, LOW);
// digitalWrite(M1P2, LOW);
pM1E1 = LOW;
pM1P1 = LOW;
pM1P2 = LOW;
}
void motorOn()
{
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": motorOn"));
#endif
// digitalWrite(M1P1, HIGH);
// digitalWrite(M1P2, LOW);
// digitalWrite(M1E1, HIGH);
pM1P1 = HIGH;
pM1P2 = LOW;
pM1E1 = HIGH;
}
void panelOn() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": panelOn"));
#endif
panel_status = true;
panel.displayOn();
panel.setBrightnessGradually(panel_brightness);
}
void panelOff() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": panelOff"));
#endif
panel_status = false;
panel.setBrightnessGradually(TM1650_MIN_BRIGHT);
panel.displayOff();
}
void measurePower(bool aStatus) {
#ifdef _TEST_
// digitalWrite(13, aStatus ? HIGH : LOW);
pLED = aStatus ? HIGH : LOW;
#else
// digitalWrite(POWERUP_PIN, aStatus ? HIGH : LOW);
pPowerup = aStatus ? HIGH : LOW;
#endif
}
long humData[5];
avgFilter hum(5, humData);
long measureHumidity()
{
long r, l;
// l = analogRead( MOISTUREPIN );
l = pMoisture;
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": measureHumidity. l = "));
Serial.println(l);
#endif
#ifdef _TEST_
return currentHumidity;
#endif
if (l > SENSOR_OUT_VALUE) return 0;
// if (l > SENSOR_OUT_VALUE) return hum.value(currentHumidity);
r = (1023 - l) * 100L / 1023L;
return hum.value(r);
}
#ifdef _TEST_
void testHumidityCallback() {
if (currentHumidity == 0) currentHumidity = 65;
if (tWater.isEnabled()) {
currentHumidity++;
if (tWaterOff.isEnabled())
tTestHumidity.setInterval(2000);
else
tTestHumidity.setInterval(10000);
}
else {
currentHumidity--;
tTestHumidity.setInterval(10000);
}
if (currentHumidity > 95) currentHumidity = 95;
if (currentHumidity < 20) currentHumidity = 20;
}
#endif
void loadParameters() {
int paramLen = sizeof (parameters);
byte *p;
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": loadParameters."));
#endif
// Let's see if we have the defaults stored already
// First lets read the token.
p = (byte *) ¶meters;
for (int i = 0; i < paramLen; i++, p++) {
*p = EEPROM.read(PARAMADDR + i);
}
logIndex = EEPROM.read(LOGIDXADDR);
logCount = EEPROM.read(LOGIDXADDR + 1);
if (strcmp(CToken, parameters.token) != 0) {
// Write down token and defaults
strncpy(parameters.token, CToken, 7);
parameters.high = (byte) STOPWATER;
parameters.low = (byte) NEEDWATER;
parameters.retries = (byte) RETRIES;
parameters.watertime = (byte) WATERTIME;
parameters.saturate = (byte) SATURATE;
parameters.gotosleep = (byte) GOTOSLEEP;
parameters.wakeup = (byte) WAKEUP;
parameters.wkendadj = (byte) WEEKENDADJ;
saveParameters();
logIndex = 0;
logCount = 0;
EEPROM.update(LOGIDXADDR, logIndex);
EEPROM.update(LOGIDXADDR + 1, logCount);
}
}
void saveParameters() {
int paramLen = sizeof (parameters);
byte *p;
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": saveParameters."));
#endif
p = (byte *) ¶meters;
for (int i = 0; i < paramLen; i++, p++) {
EEPROM.update (PARAMADDR + i, *p);
}
}
void writeLogEntry() {
byte *p = (byte *) &water_log;
int len = sizeof(water_log);
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": writeLogEntry."));
#endif
for (int i = 0; i < len; i++, p++) {
EEPROM.update (LOGADDR + logIndex * len + i, *p);
}
if (++logIndex >= MAXLOGS) logIndex = 0;
if (++logCount >= MAXLOGS) logCount = MAXLOGS;
EEPROM.update(LOGIDXADDR, logIndex);
EEPROM.update(LOGIDXADDR + 1, logCount);
}
bool lastLogEntry() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": lastLogEntryDate."));
#endif
return readLogEntry(0);
}
bool readLogEntry(int aIndex) {
byte *p = (byte *) &water_log;
int len = sizeof(water_log);
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": readLogEntryDate."));
#endif
if (logCount == 0) return false;
if (aIndex < 0 || aIndex > logCount) return false;
int index = logIndex - aIndex - 1;
if (index < 0) index += MAXLOGS;
for (int j = 0; j < len; j++, p++) {
*p = EEPROM.read (LOGADDR + index * len + j);
}
return true;
}
void measurePowerupCallback() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": measurePowerupCallback."));
#endif
measurePower(true);
tMeasure.set(TMEASURE_PRIME * TASK_SECOND, TASK_FOREVER, &measureCallback);
tMeasure.enableDelayed();
}
void measureCallback() {
#ifdef _TEST_
tTestHumidity.enableIfNot();
#endif
for (int i=0; i<5; i++) {
currentHumidity = measureHumidity();
delay(10);
}
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": measureCallback. Humidity ="));
Serial.print(currentHumidity);
Serial.println("%");
#endif
if (currentHumidity > 0 && currentHumidity < parameters.low) {
if (!tWater.isEnabled()) {
showHumidity(0);
// tWater.set(parameters.saturate * TASK_MINUTE, parameters.retries + 1, &waterCallback, &waterOnEnable, &waterOnDisable);
tWater.setInterval( parameters.saturate * TASK_MINUTE );
tWater.setIterations( parameters.retries + 1 );
tWater.restart();
tMeasure.set(TMEASURE_WATERTIME * TASK_SECOND, TASK_FOREVER, &measureCallback);
tMeasure.enableDelayed();
error = false;
}
return;
}
showHumidity(1);
if (tWater.isEnabled() && currentHumidity >= parameters.high) {
tWater.disable();
}
if (!tWater.isEnabled() ) {
measurePower(false);
tMeasure.set(TMEASURE_INTERVAL * TASK_SECOND, TASK_FOREVER, &measurePowerupCallback);
tMeasure.enableDelayed();
}
}
bool waterOnEnable() {
if (night_time) {
motorOff();
return false;
}
water_log.water_start = now();
water_log.hum_start = currentHumidity;
water_log.num_runs = 0;
water_log.run_duration = parameters.watertime;
return true;
}
void waterOnDisable() {
tWaterOff.disable();
tWaterAnimation.disable();
motorOff();
water_log.water_end = now();
water_log.hum_end = currentHumidity;
writeLogEntry();
}
void waterCallback() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": waterCallback. Iteration: "));
Serial.println(tWater.getIterations());
#endif
if (!tWater.isLastIteration()) {
motorOn();
water_log.num_runs++;
tWaterOff.set(parameters.watertime * TASK_SECOND, 1, &waterOffCallback);
tWaterOff.restartDelayed();
tWaterAnimation.setCallback(&animationWaterCallback);
tWaterAnimation.enable();
showHumidity(0);
return;
}
currentHumidity = measureHumidity();
if ( currentHumidity >= parameters.low ) {
tWater.disable();
showHumidity(1);
return;
}
motorOff;
ts.disableAll();
tError.enable();
}
void waterOffCallback() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": waterOffCallback."));
#endif
motorOff();
tWater.delay();
tWaterAnimation.setCallback(&animationSaturateCallback);
tWaterAnimation.enable();
}
int animCounter ;
void animationWaterCallback() {
// if (tWaterAnimation.isFirstIteration()) animCounter = 3;
panel.setDot(animCounter++, false);
animCounter &= 3;
panel.setDot(animCounter, true);
}
void animationSaturateCallback() {
// if (tWaterAnimation.isFirstIteration()) animCounter = 0;
switch (animCounter) {
case 0:
panel.setDot(0, false);
panel.setDot(1, true);
panel.setDot(2, true);
panel.setDot(3, false);
break;
case 1:
panel.setDot(0, true);
panel.setDot(1, false);
panel.setDot(2, false);
panel.setDot(3, true);
break;
case 2:
case 3:
panel.setDot(0, false);
panel.setDot(1, false);
panel.setDot(2, false);
panel.setDot(3, false);
break;
}
animCounter++;
animCounter &= 3;
}
void displayTimeout() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": displayTimeout."));
#endif
if (!tWater.isEnabled()) panelOff();
}
#define LINELEN (100)
void displayCallback() {
char line[LINELEN+1];
int h = currentHumidity;
time_t tnow;
tDisplayTimeout.delay();
if (tDisplayRunning.isEnabled()) return;
// panelOn();
switch (displayNow) {
case DHUMIDITY:
snprintf(line, 5, "h%3d", h);
break;
case DGOODNIGHT:
snprintf(line, LINELEN, "good night ");
line[strlen(line) - 1] |= 0x80; // put a dot so we see it's on
panel.displayRunning(line);
tDisplayRunning.setInterval(RUNNING_DISPLAY_START_DELAY);
tDisplayRunning.enableDelayed();
switchDisplayNow(DEMPTY, 1);
break;
case DEMPTY:
sprintf(line, " ");
line[3] |= 0x80;
break;
case DSTORED:
sprintf(line, "Strd");
showHumidity(TDISPLAY_SHORT_DELAY * TASK_SECOND);
break;
case DCANCELLED:
sprintf(line, "Canc");
showHumidity(TDISPLAY_SHORT_DELAY * TASK_SECOND);
break;
case DCLK_SET:
sprintf(line, "Set ");
showHumidity(TDISPLAY_SHORT_DELAY * TASK_SECOND);
break;
case DSET:
sprintf(line, "SETT");
break;
case DCLK:
sprintf(line, "CLOC");
break;
case DFRN:
sprintf(line, "Frun");
break;
case DLOG:
sprintf(line, "LOG ");
break;
case DSET_UL:
case DSET_LL:
case DSET_PT:
case DSET_RT:
case DSET_ST:
case DSET_SL:
case DSET_UP:
case DSET_AD:
case DCLK_CN:
case DCLK_YR:
case DCLK_MT:
case DCLK_DY:
case DCLK_HR:
case DCLK_MN:
snprintf(line, 5, "%4d", *adjParam);
line[0] = paramChar[paramIndex] | 0x80;
break;
case DLOG_V:
snprintf(line, LINELEN, "%02d start on %02d-%02d at %02d%02d %02d h %02d runs end on %02d-%02d at %02d%02d %02d h", paramIndex+1,\
month(myTZ.toLocal(water_log.water_start)), day(myTZ.toLocal(water_log.water_start)), hour(myTZ.toLocal(water_log.water_start)),\
minute(myTZ.toLocal(water_log.water_start)), water_log.hum_start, water_log.num_runs,\
month(myTZ.toLocal(water_log.water_end)), day(myTZ.toLocal(water_log.water_end)), hour(myTZ.toLocal(water_log.water_end)),\
minute(myTZ.toLocal(water_log.water_end)), water_log.hum_end );
line[1] |= 0x80;
line[22] |= 0x80; //enable dot
line[56] |= 0x80; //enable dot
panel.displayRunning(line);
tDisplayRunning.setInterval(RUNNING_DISPLAY_START_DELAY);
tDisplayRunning.enableDelayed();
switchDisplayNow(DLOG_P, 1);
return;
case DTIME:
tnow = myTZ.toLocal( now() );
snprintf(line, LINELEN, "date %04d-%02d-%02d %02d%02d", year(tnow), month(tnow), day(tnow), hour(tnow), minute(tnow));
line[17] |= 0b10000000; //dot
panel.displayRunning(line);
tDisplayRunning.setInterval(RUNNING_DISPLAY_START_DELAY);
tDisplayRunning.enableDelayed();
showHumidity(TDISPLAY_SHORT_DELAY * TASK_SECOND);
return;
case DRUN:
if (lastLogEntry()) {
snprintf(line, LINELEN, "last run start on %02d-%02d at %02d%02d %02d h %02d runs end on %02d-%02d at %02d%02d %02d h",\
month(myTZ.toLocal(water_log.water_start)), day(myTZ.toLocal(water_log.water_start)), hour(myTZ.toLocal(water_log.water_start)),\
minute(myTZ.toLocal(water_log.water_start)), water_log.hum_start, water_log.num_runs,\
month(myTZ.toLocal(water_log.water_end)), day(myTZ.toLocal(water_log.water_end)), hour(myTZ.toLocal(water_log.water_end)),\
minute(myTZ.toLocal(water_log.water_end)), water_log.hum_end );
line[28] |= 0x80; //enable dot
line[62] |= 0x80; //enable dot
}
else {
snprintf(line, LINELEN, "no last run info");
}
panel.displayRunning(line);
tDisplayRunning.setInterval(RUNNING_DISPLAY_START_DELAY);
tDisplayRunning.enableDelayed();
showHumidity(TDISPLAY_SHORT_DELAY * TASK_SECOND);
return;
case DLOG_P:
return;
case DERROR:
break;
}
if (!tDisplayRunning.isEnabled()) {
panel.displayString(line);
panelOn();
}
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": displayCallback. line="));
Serial.println(line);
#endif
}
void showHumidity(int aDelay) {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": showHumidity"));
#endif
switchDisplayNow(DHUMIDITY, aDelay);
}
void switchDisplayNow(int aStatus, int aDelay) {
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": switchDisplayNow. delay="));
Serial.println(aDelay);
#endif
displayNow = (Display_Options) aStatus;
if (aDelay == 0) {
tDisplayRunning.disable();
tDisplay.enable();
}
else {
tDisplay.delay(aDelay);
}
}
void displayRunningCallback() {
tDisplayTimeout.delay();
tSettingsTimeout.delay();
tDisplayRunning.setInterval(RUNNING_DISPLAY_DELAY);
if (!panel.displayRunningShift()) {
tDisplay.delay(TDISPLAY_SHORT_DELAY * TASK_SECOND);
tDisplayRunning.disable();
}
panelOn();
}
bool isNight() {
time_t tnow = myTZ.toLocal( now() );
int hr = hour(tnow);
int wkp = parameters.wakeup;
// Add adjusting hours to the wakeup time for Saturday and Sunday
if (weekday(tnow) == dowSunday || weekday(tnow) == dowSaturday) wkp += parameters.wkendadj;
#ifdef _DEBUG_
Serial.print(millis());
Serial.print(F(": isNight. hr="));
Serial.println(hr);
#endif
return (hr >= parameters.gotosleep || hr < wkp);
}
void goodnightCallbackInit() {
night_time = !isNight();
tGoodnight.setCallback(&goodnightCallback);
tGoodnight.delay();
goodnightCallback();
}
void goodnightCallback() {
checkUpdateTime();
time_t tnow = myTZ.toLocal(now());
int hr = hour(tnow);
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": goodnightCallback."));
#endif
switch (hr) {
case 7:
panel_brightness = 3;
break;
case 8:
panel_brightness = 5;
break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
panel_brightness = 7;
break;
case 18:
panel_brightness = 6;
break;
case 19:
case 20:
panel_brightness = 3;
break;
default:
panel_brightness = 1;
}
panel.setBrightness(panel_brightness);
if (!night_time && isNight() ) {
night_time = true;
motorOff();
switchDisplayNow(DGOODNIGHT, 0);
tMeasure.disable();
measurePower(false);
tWater.disable();
}
if (night_time) {
if (!isNight() ) {
night_time = false;
measurePowerupCallback();
tDisplay.enable();
showHumidity(0);
}
else {
if (displayNow != DEMPTY) switchDisplayNow(DGOODNIGHT, 0); //switchDisplayNow(DEMPTY, 0);
}
}
}
int pressCnt = 0;
void interruptCallback() {
#ifdef _DEBUG_
Serial.print(millis());
Serial.println(F(": interruptCallback."));
#endif
enableInterrupt(BTN_SEL_PIN, &initButtons, RISING);
enableInterrupt(BTN_PLUS_PIN, &initButtons, RISING);
enableInterrupt(BTN_MINUS_PIN, &initButtons, RISING);
pressCnt = 0;
}
void initButtons() {
disableInterrupt(BTN_SEL_PIN);
disableInterrupt(BTN_PLUS_PIN);
disableInterrupt(BTN_MINUS_PIN);