-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDoser.ino
1739 lines (1569 loc) · 44.2 KB
/
Doser.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
/*
Doser.ino - DIY 5 channel doser
Copyright (C) 2001-2012 Georgi Todorov
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
https://github.com/TeraHz/Doser
*/
// Debugging flags. Be careful when you enable debugging as
// memory is tight and the controller might crash if all
// info is enabled.
//#define DEBUG // enables serial and prints out memory
//#define DEBUG_DOSE // dosing related
//#define DEBUG_PUMP // pump related
//#define DEBUG_MENU // menu related
//#define DEBUG_IR // IR related
#include <Flash.h> //from http://arduiniana.org/libraries/flash/
#include <Wire.h>
#include <EEPROM.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <DS1302.h> //from http://www.henningkarlsen.com/electronics/library.php?id=5
#include "LCDi2c4bit.h"
#include "mcp23xx.h"
#include "EEPROMAnything.h"
#include "IRremote.h"
#include "MenuBackend.h"
#include "Pump.h"
#define PWM_BACKLIGHT_PIN 9 // pwm-controlled LED backlight
#define CONFIG_PIN 8 // pwm-controlled LED backlight
#define IR_PIN 12 // Sensor data-out pin, wired direct
#define ATO_FS1 15 // Analog 1 - float switch 1
#define ATO_FS2 16 // Analog 2 - float switch 2
#define ATO_FS3 17 // Analog 3 - float switch 3
#define ATO_RELAY 4 // Digital 4 - 5V relay for pump
// has to be the last entry in this list
#define K_KEY_SENTINEL 7 // this must always be the last in the list
#define MAX_FUNCTS K_KEY_SENTINEL
#define EEPROM_SAVED 500 // EEPROM location to save
#define EEPROM_SAVED_VALUE 2 // EEPROM value to check; change this to reinitialize the pumps
/*
* IFC = internal function codes
*
* these are logical mappings of physical IR keypad keys to internal callable functions.
* its the way we soft-map keys on a remote to things that happen when you press those keys.
*/
#define K_MENU 0 // enter menu mode
#define K_UP 1 // up-arrow
#define K_DOWN 2 // down-arrow
#define K_LEFT 3 // left-arrow
#define K_RIGHT 4 // right-arrow
#define K_OK 5 // Select/OK/Confirm btn
#define K_CANCEL 6 // Cancel/back/exit
uint8_t backlight_min = 255; // color-independant 'intensity'
uint8_t backlight_max = 255; // color-independant 'intensity'
uint16_t minCounter = 0;
uint16_t tempMinHolder = 0; // this is used for holding the temp value in menu setting
char strTime[20]; // temporary array for time output
char tmp[20]; // temporary array for random stuff
uint8_t psecond = 0; // Previous second
uint16_t decond = 0; // decond 0 - 14400 - 1/10th of a minute
uint8_t backupTimer = 0; // timer that will count seconds since the ATO started.
uint8_t backupMax = 10; // max seconds for timer to run. If this is reached, kill the ATO. (adjust timer based on your pump output)
uint8_t sPos = 1; // position for setting
uint8_t lcd_in_use_flag = 0; // is the LCD in use. Used for IR reading
uint8_t ATO_FS1_STATE = 0; // State holder for flat switch 1
uint8_t ATO_FS2_STATE = 0; // State holder for flat switch 2
uint8_t ATO_FS3_STATE = 0; // State holder for flat switch 3
Time currentTime; // hold current time
uint8_t i, global_mode,ts, tmi, th, tdw, tdm, tmo, ty;//variables for time
uint16_t key; // Store current hex of the IR key
uint8_t calPage = 0; // Which page on the calibration instruction are we on
uint32_t calTime = 0; // Store time it took to move X amount of water
float rate = 0.0; // rate for current pump
boolean calibrated = false; // flag that we've gone through calibration
FLASH_STRING (calibration_instructions,
"Prepare 250ml water.""Prime line. "
"Submerge line input.""Press OK to start. "
"Press OK when all ""water is gone ");
boolean first = true;
// this is a temporary holding area that we write to, key by key; and then dump all at once when the user finishes the last one
uint16_t ir_key_bank1[MAX_FUNCTS+1];
decode_results results;
// this is used in learn-mode, to prompt the user and assign enums to internal functions
struct _ir_keys {
uint16_t hex;
uint8_t internal_funct_code;
char name[7];
}
ir_keys[MAX_FUNCTS+1] = {
{ 0x00, K_MENU, "Menu" }
,{ 0x00, K_UP, "Up" }
,{ 0x00, K_DOWN, "Down" }
,{ 0x00, K_LEFT, "Left" }
,{ 0x00, K_RIGHT, "Right" }
,{ 0x00, K_OK, "OK" }
,{ 0x00, K_CANCEL, "Cancel" }
,{ 0x00, K_KEY_SENTINEL, "NULL" }
};
//Init the Real Time Clock
DS1302 rtc(13, 7, 2);
//Init the MCP port expander for LCD
MCP23XX lcd_mcp = MCP23XX(LCD_MCP_DEV_ADDR);
//Init the LCD
LCDI2C4Bit lcd = LCDI2C4Bit(LCD_MCP_DEV_ADDR, LCD_PHYS_LINES, LCD_PHYS_ROWS, PWM_BACKLIGHT_PIN);
//define menu strings in progmem
char mi_pump1_string[] = "Pump 1";
char mi_pump2_string[] = "Pump 2";
char mi_pump3_string[] = "Pump 3";
char mi_pump4_string[] = "Pump 4";
char mi_pump5_string[] = "Pump 5";
const char mi_set_string[] = "Set";
const char mi_review_string[] = "Review";
const char mi_calibrate_string[] = "Calibrate";
const char mi_dc_string[] = "Duty Cycle";
const char mi_settings_string[] = "Settings";
const char mi_clock_string[] = "Clock";
const char mi_display_sleep_string[] = "Display Sleep";
const char mi_ato_string[] = "ATO";
const char mi_ato_set_string[] = "Set Timeout";
//this controls the menu backend and the event generation
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
//beneath is list of menu items needed to build the menu
MenuItem settings = MenuItem(mi_settings_string);
MenuItem mi_clock = MenuItem(mi_clock_string);
MenuItem mi_sleep = MenuItem(mi_display_sleep_string);
MenuItem mi_pump1 = MenuItem(mi_pump1_string);
MenuItem mi_pump1_review = MenuItem(mi_review_string);
MenuItem mi_pump1_set = MenuItem(mi_set_string);
MenuItem mi_pump1_calibrate = MenuItem(mi_calibrate_string);
MenuItem mi_pump1_dc = MenuItem(mi_dc_string);
MenuItem mi_pump2 = MenuItem(mi_pump2_string);
MenuItem mi_pump2_review = MenuItem(mi_review_string);
MenuItem mi_pump2_set = MenuItem(mi_set_string);
MenuItem mi_pump2_calibrate = MenuItem(mi_calibrate_string);
MenuItem mi_pump2_dc = MenuItem(mi_dc_string);
MenuItem mi_pump3 = MenuItem(mi_pump3_string);
MenuItem mi_pump3_review = MenuItem(mi_review_string);
MenuItem mi_pump3_set = MenuItem(mi_set_string);
MenuItem mi_pump3_calibrate = MenuItem(mi_calibrate_string);
MenuItem mi_pump3_dc = MenuItem(mi_dc_string);
MenuItem mi_pump4 = MenuItem(mi_pump4_string);
MenuItem mi_pump4_review = MenuItem(mi_review_string);
MenuItem mi_pump4_set = MenuItem(mi_set_string);
MenuItem mi_pump4_calibrate = MenuItem(mi_calibrate_string);
MenuItem mi_pump4_dc = MenuItem(mi_dc_string);
MenuItem mi_pump5 = MenuItem(mi_pump5_string);
MenuItem mi_pump5_review = MenuItem(mi_review_string);
MenuItem mi_pump5_set = MenuItem(mi_set_string);
MenuItem mi_pump5_calibrate = MenuItem(mi_calibrate_string);
MenuItem mi_pump5_dc = MenuItem(mi_dc_string);
MenuItem mi_ATO = MenuItem(mi_ato_string);
MenuItem mi_ATO_set = MenuItem(mi_ato_set_string);
//Init the Pumps
//Pump(pin number, ml/s, daily dose, dsecription);
Pump * p1 = new Pump(3,31.12,0,100,"Pump 1");
Pump * p2 = new Pump(5,30.99,0,100,"Pump 2");
Pump * p3 = new Pump(6,33.78,0,100,"Pump 3");
Pump * p4 = new Pump(11,38.86,0,100,"Pump 4");
Pump * p5 = new Pump(10,34.64,0,100,"Pump 5");
Pump * currentPump;
IRrecv irrecv(IR_PIN);
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
// Serial.println("Hello");
Wire.begin();
// Enable config pin's pullup
pinMode(CONFIG_PIN,INPUT);
digitalWrite(CONFIG_PIN,HIGH);
pinMode(ATO_FS1, INPUT);
// Enable Float switch 1 pin's pullup
digitalWrite(ATO_FS1,HIGH);
pinMode(ATO_FS2, INPUT);
// Enable Float switch 2 pin's pullup
digitalWrite(ATO_FS2,HIGH);
pinMode(ATO_FS3, INPUT);
// Enable Float switch 3 pin's pullup
digitalWrite(ATO_FS3,HIGH);
pinMode(ATO_RELAY, OUTPUT);
p1->setEE(100);
p2->setEE(150);
p3->setEE(200);
p4->setEE(250);
p5->setEE(300);
if (EEPROM.read(EEPROM_SAVED) != EEPROM_SAVED_VALUE) {
p1->save();
p2->save();
p3->save();
p4->save();
p5->save();
EEPROM.write(EEPROM_SAVED,EEPROM_SAVED_VALUE);
}else{
p1->load();
p2->load();
p3->load();
p4->load();
p5->load();
}
//start IR sensor
irrecv.enableIRIn();
#ifdef DEBUG_PUMP
Serial.print(F("Pump 1 - pin:"));
Serial.print(p1->getPin());
Serial.print(F("desc:"));
Serial.println(p1->getDescription());
Serial.print(F("Pump 2 - pin:"));
Serial.print(p2->getPin());
Serial.print(F("desc:"));
Serial.println(p2->getDescription());
Serial.print(F("Pump 3 - pin:"));
Serial.print(p3->getPin());
Serial.print(F("desc:"));
Serial.println(p3->getDescription());
Serial.print(F("Pump 4 - pin:"));
Serial.print(p4->getPin());
Serial.print(F("desc:"));
Serial.println(p4->getDescription());
Serial.print(F("Pump 5 - pin:"));
Serial.print(p5->getPin());
Serial.print(F("desc:"));
Serial.println(p5->getDescription());
#endif
// Setup Serial connection
//start LCD
lcd.init();
lcd.SetInputKeysMask(LCD_MCP_INPUT_PINS_MASK);
lcd.backLight(255);
// // Set the clock to run-mode, and disable the write protection
// rtc.halt(false);
// rtc.writeProtect(false);
// // The following lines can be commented out to use the values already stored in the DS1302
// rtc.setDOW(WEDNESDAY); // Set Day-of-Week to FRIDAY
// rtc.setTime(22, 58, 00); // Set the time to 12:00:00 (24hr format)
// rtc.setDate(11, 01, 2012); // Set the date to August 6th, 2010
rtc.setTCR(TCR_D1R2K);
if (digitalRead(CONFIG_PIN) == LOW){
enter_setup_mode();
}
menuSetup();
//read remote keys from EEPROM
for (i=0; i<=MAX_FUNCTS; i++) {
EEPROM_readAnything(40 + i*sizeof(key), key);
ir_keys[i].hex = key;
}
}
void loop()
{
// Get data from the DS1302
currentTime = rtc.getTime();
uint8_t second = currentTime.sec;
if (global_mode == 0) { // home screen
onKeyPress();
}//global_mode == 0
else if (global_mode == 1){ //we're in menu
if (first){
while (menu.getCurrent().getLeft() != 0){
menu.moveLeft();
}
lcd.clear_L1();
lcd.clear_L2();
lcd.clear_L3();
lcd.cursorTo(0,0);
lcd.print((char*)menu.getCurrent().getName());
if (menu.getCurrent().getRight() != 0){
lcd.cursorTo(1,3);
lcd.print(F(">"));
}else{
lcd.cursorTo(1,3);
lcd.print(F(" "));
}
first=false;
}
show_menu();
} //global_mode == 1
else if (global_mode == 2){
cal_pump();
}//global_mode == 2
else if (global_mode == 3){
set_pump();
}//global_mode == 3
else if (global_mode == 4){
set_time();
}//global_mode == 4
else if (global_mode == 5){
handle_review_pump();
}//global_mode == 5
else if (global_mode == 6){
handle_pump_dc();
}
else{//we're somewhere else?
}
if (psecond != second){
psecond = second;
decond = (uint16_t)((currentTime.hour*60*60+currentTime.min*60+currentTime.sec)/6); // should give us 10 values per minute
run_sec();
}
delay(50);
}
/*********************************/
/****** RUN ONCE PER SECOND ******/
/*********************************/
void run_sec( void ){
// // It is midnight. Might be useful for something in the future :)
// if (currentTime.hour == 0 && currentTime.min == 0 && currentTime.sec == 0){
// }
do_ATO();
if (global_mode != 2){ // we don't want dosing to occur while we use the pumps for calibration
do_DOSING(p1);
do_DOSING(p2);
do_DOSING(p3);
do_DOSING(p4);
do_DOSING(p5);
}
update_clock(3,0);
if (!first && !calibrated){
calTime++;
}
#ifdef DEBUG_MODE
switch (global_mode){
case 0:
Serial.println(F("Global mode"));
break;
case 1:
Serial.println(F("Menu"));
break;
case 2:
Serial.println(F("Calibration"));
break;
case 3:
Serial.println(F("Set Dose"));
break;
case 4:
Serial.println(F("Set Time"));
break;
case 5:
Serial.println(F("Review"));
break;
}
#endif
#ifdef DEBUG
Serial.print("Mem: "); Serial.println(availableMemory());
#endif
}
/****************************/
/****** PERFORM DOSING ******/
/****************************/
void do_DOSING(Pump *pump){
#ifdef DEBUG_PUMP
Serial.print(F("Working on pump "));
Serial.println(pump->getDescription());
#endif
#ifdef DEBUG_DOSE
Serial.print(pump->getDescription());
Serial.print(" ");
#endif
uint16_t repeat = 14400/pump->getDose(); // how many times to dose daily 6sec at a time
#ifdef DEBUG_DOSE
Serial.print(repeat);
Serial.print(" ");
Serial.print(decond);
Serial.print(" ");
#endif
if (decond%repeat == 0){
#ifdef DEBUG_DOSE
Serial.print("start ");
#endif
pump->startDosing();
}else{
#ifdef DEBUG_DOSE
Serial.println(" stop");
#endif
pump->stopDosing();
}
}
/*******************************/
/****** PRINT TIME AT X,Y ******/
/*******************************/
void update_clock(uint8_t x, uint8_t y){
if (global_mode == 0 || global_mode == 1 || global_mode == 5){
//
// lcd.cursorTo(2,15);
// lcd.print(availableMemory());
lcd.cursorTo(x,y);
lcd.print(rtc.getTimeStr());
lcd.print(F(" "));
lcd.print(rtc.getDateStr());
}
}
/*******************************/
/****** PRINT TEMP AT X,Y ******/
/*******************************/
void update_temp(uint8_t x, uint8_t y){
lcd.cursorTo(x,y);
lcd.print(strTime);
}
/********************************/
/****** INITIAL SETUP MODE ******/
/********************************/
void enter_setup_mode( void ) {
uint8_t setup_finished = 0;
uint8_t idx = 0, i = 0;
uint8_t blink_toggle = 0;
uint8_t blink_count = 0;
float ratio;
uint8_t eeprom_index = 0;
uint8_t key_pressed = 0;
lcd.clear();
lcd.cursorTo(0,0);
lcd.print(F("Remote Learning"));
idx = 0;
while (!setup_finished) {
if (!strcmp(ir_keys[idx].name, "NULL")) {
setup_finished = 1; // signal we're done with the whole list
goto done_learn_mode;
}
// we embed the index inside our array of structs, so that even if the user comments-out blocks
// of it, we still have the same index # for the same row of content
eeprom_index = ir_keys[idx].internal_funct_code;
// prompt the user for which key to press
lcd.send_string(ir_keys[idx].name, LCD_CURS_POS_L2_HOME+1);
delay(300);
blink_toggle = 1;
blink_count = 0;
/*
* non-blocking poll for a keypress
*/
while ( (key = get_input_key()) == 0 ) {
if (blink_toggle == 1) {
blink_toggle = 0;
lcd.clear_L2(); // clear the string
delay(300); // debounce
}
else {
blink_toggle = 1;
++blink_count;
lcd.send_string(ir_keys[idx].name, LCD_CURS_POS_L2_HOME+1); // redraw the string
delay(600); // debounce
}
// check if we should exit (user got into this mode but had 2nd thoughts ;)
if ( blink_count >= 30 ) { // change the value of '30' if you need more time to find your keys ;)
setup_finished = 1;
global_mode = 0; // back to main 'everyday use' mode
lcd.clear();
lcd.cursorTo(0,0);
lcd.print(F("Abandon SETUP"));
/*
* read LAST GOOD soft-set IR-key mappings from EEPROM
*/
for (i=0; i<=MAX_FUNCTS; i++) {
EEPROM_readAnything(40 + i*sizeof(key), key);
ir_keys[i].hex = key;
}
delay(1000);
lcd.clear();
return;
} // if blink count was over the limit (ie, a user timeout)
} // while
// if we got here, a non-blank IR keypress was detected!
lcd.send_string("*", LCD_CURS_POS_L2_HOME);
lcd.send_string(ir_keys[idx].name, LCD_CURS_POS_L2_HOME+1); // redraw the string
delay(1000); // debounce a little more
// search the list of known keys to make sure this isn't a dupe or mistake
// [tbd]
// accept this keypress and save it in the array entry that matches this internal function call
ir_key_bank1[eeprom_index] = key;
idx++; // point to the next one
irrecv.resume(); // we just consumed one key; 'start' to receive the next value
delay(300);
} // while
done_learn_mode:
global_mode = 0; // back to main 'everyday use' mode
lcd.clear();
// lcd.send_string("Learning Done", LCD_CURS_POS_L1_HOME);
// delay(500);
// lcd.send_string("Saving Key Codes", LCD_CURS_POS_L2_HOME);
// copy (submit) all keys to the REAL working slots
for (i=0; i<=MAX_FUNCTS; i++) {
ir_keys[i].hex = ir_key_bank1[i];
EEPROM_writeAnything(40 + i*sizeof(ir_key_bank1[i]), ir_key_bank1[i]); // blocks of 4 bytes each (first 40 are reserved, though)
ratio = (float)i / (float)idx;
delay(50);
}
first = true;
delay(1000);
lcd.clear();
}
/*******************************/
/****** GET INFRARED KEY ******/
/******************************/
long get_input_key( void ) {
long my_result;
long last_value = results.value; // save the last one in case the new one is a 'repeat code'
if (irrecv.decode(&results)) {
// fix repeat codes (make them look like truly repeated keys)
if (results.value == 0xffffffff) {
if (last_value != 0xffffffff) {
results.value = last_value;
}
else {
results.value = 0;
}
}
if (results.value != 0xffffffff) {
my_result = results.value;
}
else {
my_result = last_value; // 0;
}
irrecv.resume(); // we just consumed one key; 'start' to receive the next value
return results.value; //my_result;
}
else {
return 0; // no key pressed
}
}
/********************************************/
/****** SETUP LCD FOR pump CALIBRATION ******/
/********************************************/
void pump_menu_cal(Pump *pump){
#ifdef DEBUG_PUMP
Serial.print(F("Working on pump "));
Serial.println(pump->getDescription());
#endif
currentPump = pump;
#ifdef DEBUG_MENU
Serial.print(F("Setting curentTemp to "));
Serial.print(pump->getDescription());
Serial.print(F("->"));
Serial.println(currentPump->getDescription());
#endif
lcd.clear();
lcd.cursorTo(0,0);
lcd.printL(pump->getDescription(), 8);
lcd.print(F("Calibration"));
global_mode = 2;
first = true;
calPage = 0;
update_cal_screen();
calTime = 0;
cal_pump();
}
/********************************************/
/****** PRINT CURRENT PAGE OF CAL INST ******/
/********************************************/
void update_cal_screen(){
lcd.clear_L4();
if (calPage !=3){
lcd.cursorTo(1,0);
for (uint8_t i = calPage*2*20; i < calPage*2*20+20; i++){
lcd.write(calibration_instructions[i]);
}
lcd.cursorTo(2,0);
for (uint8_t i = (calPage*2+1)*20; i < (calPage*2+1)*20+20; i++){
lcd.write(calibration_instructions[i]);
}
if (calPage != 0 && first){
lcd.cursorTo(3,0);
lcd.print(F("<Previous"));
}
if (calPage != 2){
lcd.cursorTo(3,15);
lcd.print(F("Next>"));
}
}else{
lcd.cursorTo(1,0);
lcd.print(F("Stats: "));
lcd.cursorTo(2,0);
rate = 250.0*60.0/calTime;
uint8_t frate = (uint8_t)rate; // compute the whole part of the float
uint16_t prate = (rate - frate)*100; // compute up to 2 decimal digits accuracy
sprintf(tmp,"t:%04ds ",calTime);
lcd.print(tmp);
sprintf(tmp,"r:%03u.",frate);
lcd.print(tmp);
sprintf(tmp,"%02uml/m", prate);
lcd.print(tmp);
lcd.cursorTo(3,0);
lcd.print(F("Press OK to store "));
}
}
/**************************************/
/****** SETUP LCD FOR pump SETUP ******/
/**************************************/
void pump_menu_set(Pump *pump){
#ifdef DEBUG_PUMP
Serial.print(F("Working on pump "));
Serial.println(pump->getDescription());
#endif
global_mode = 3;
tempMinHolder = pump->getDose();
float mlm = pump->getMlm();
float factor = (float)pump->getDC()/100.0;
mlm = mlm*factor; // apply duty cycle
mlm = mlm/10; // dose for 6 seconds (1/10th of a minute)
currentPump = pump;
#ifdef DEBUG_MENU
Serial.print(F("Setting curentTemp to "));
Serial.print(pump->getDescription());
Serial.print("->");
Serial.println(currentPump->getDescription());
#endif
lcd.clear();
lcd.cursorTo(0,0);
lcd.printL(pump->getDescription(), 8);
lcd.print(F("Setup"));
lcd.cursorTo(2,0);
lcd.print(F("Enter daily dose:"));
lcd.cursorTo(3,0);
sprintf(tmp," %05lu ml",(unsigned long)(tempMinHolder*mlm));
lcd.print(tmp);
set_pump();
}
/**********************/
/****** SET PUMP ******/
/**********************/
void set_pump(){
float mlm = currentPump->getMlm();
float factor = (float)currentPump->getDC()/100.0;
mlm = mlm*factor; // apply duty cycle
mlm = mlm/10; // dose for 6 seconds (1/10th of a minute)
uint16_t repeat = 14400/tempMinHolder; // how many times to dose daily 6sec at a time
key = get_input_key();
if (key == 0) {
return;
}
delay(50);
#ifdef DEBUG_DOSE
Serial.print(factor);
Serial.print(" ");
Serial.println(mlm);
#endif
// key = OK
if (key == ir_keys[K_OK].hex ) {
currentPump->setDose(tempMinHolder);
currentPump->save();
global_mode = 0;
lcd.clear();
first=true;
}
// key = Up
else if (key == ir_keys[K_UP].hex){
if (tempMinHolder < 14400){
while (repeat <= (uint16_t)(14400/tempMinHolder)){
tempMinHolder++;
if (tempMinHolder == 14400){
break;
}
}
lcd.clear_L2();
}
else{
tempMinHolder = 14400;
lcd.cursorTo(1,0);
sprintf(tmp,"Pump max: %luml",(unsigned long)(mlm*14400));
lcd.print(tmp);
delay(700);
lcd.clear_L2();
}
lcd.cursorTo(3,0);
sprintf(tmp," %05lu ml",(unsigned long)(tempMinHolder*mlm));
lcd.print(tmp);
}
// key = Down
else if (key == ir_keys[K_DOWN].hex){
if (tempMinHolder > 0){
while (repeat >= (uint16_t)(14400/tempMinHolder)){
tempMinHolder--;
if (tempMinHolder == 0){
break;
}
}
}
else{
tempMinHolder = 0;
lcd.cursorTo(1,0);
lcd.print(F("Turning pump OFF "));
}
lcd.cursorTo(3,0);
sprintf(tmp," %05lu ml",(unsigned long)(tempMinHolder*mlm));
lcd.print(tmp);
}
// key = Left
else if (key == ir_keys[K_LEFT].hex){
if (tempMinHolder > 0){
tempMinHolder=0;
}
else{
tempMinHolder = 0;
lcd.cursorTo(1,0);
lcd.print(F("Turning pump OFF "));
}
lcd.cursorTo(3,0);
sprintf(tmp," %05lu ml",(unsigned long)(tempMinHolder*mlm));
lcd.print(tmp);
}
// key = Right
else if (key == ir_keys[K_RIGHT].hex){
if (tempMinHolder <= 14399){
tempMinHolder=14400;
}
else{
tempMinHolder = 14400;
lcd.cursorTo(1,0);
sprintf(tmp,"Pump max: %luml",(unsigned long)(mlm*14400));
lcd.print(tmp);
delay(700);
lcd.clear_L2();
}
lcd.cursorTo(3,0);
sprintf(tmp," %05lu ml",(unsigned long)(tempMinHolder*mlm));
lcd.print(tmp);
}
// key = Cancel
else if (key == ir_keys[K_CANCEL].hex){
lcd.clear();
global_mode = 0;
delay (100);
first = true;
}else{
#ifdef DEBUG_IR
Serial << F("unknown key") << "\n\r";
#endif
}
delay(100);
}
/****************************/
/****** CALIBRATE PUMP ******/
/****************************/
void cal_pump(){
key = get_input_key();
if (key == 0) {
return;
}
delay(50);
// key = OK
if (key == ir_keys[K_OK].hex ) {
if (first){
if (calibrated){
currentPump->setMlm(rate);
currentPump->save();
calibrated=false;
calPage = 0;
global_mode = 0;
lcd.clear();
}else{
first = false;
calibrated = false;
currentPump->startDosing(255);
calPage = 2;
update_cal_screen();
}
}else{
first = true;
calibrated = true;
currentPump->stopDosing();
calPage = 3;
update_cal_screen();
}
}
// key = Left
else if (key == ir_keys[K_LEFT].hex){
if (calPage >0 && first){
calPage--;
update_cal_screen();
}
}
// key = Right
else if (key == ir_keys[K_RIGHT].hex){
if (calPage < 2){
calPage++;
update_cal_screen();
}
}
// key = Cancel
else if (key == ir_keys[K_CANCEL].hex){
currentPump->stopDosing();
lcd.clear();
global_mode = 0;
first = true;
calibrated = false;
}else{
#ifdef DEBUG_IR
Serial << F("unknown key") << "\n\r";
#endif
}
delay(100);
}
/********************************/
/****** PREPARE DUTY CYCLE ******/
/********************************/
void prep_pump_dc(Pump *pump){
#ifdef DEBUG_PUMP
Serial.print(F("Working on pump "));
Serial.println(pump->getDescription());
#endif
currentPump = pump;
global_mode = 6;
lcd.clear_L2();
lcd.clear_L4();
tempMinHolder = pump->getDC();
lcd.cursorTo(0,8);
lcd.print(F("Duty Cycle"));
update_effective_rate(tempMinHolder);
lcd.cursorTo(2,0);
handle_pump_dc();
}
/*****************************/
/****** HELPER TO PRINT ******/
/*****************************/
void update_effective_rate(uint8_t percent){
lcd.cursorTo(2,0);
lcd.print(F("Eff rate:"));
lcd.print((float)((currentPump->getMlm())*(float)tempMinHolder/100.0));
lcd.print(F("ml/m "));
lcd.cursorTo(3,0);
lcd.print(F(" "));
lcd.print((int)tempMinHolder);
lcd.print(F("% ("));
lcd.print((uint8_t)(tempMinHolder*2.55));
lcd.print(F(") "));
}
/*******************************/
/****** HANDLE DUTY CYCLE ******/
/*******************************/
void handle_pump_dc( void ){
key = get_input_key();
if (key == 0) {
return;
}
delay(50);
// key = OK
if (key == ir_keys[K_OK].hex ) {
currentPump->setDC(tempMinHolder);
currentPump->save();
global_mode = 0;
lcd.clear();
first=true;
}
// key = Up
else if (key == ir_keys[K_UP].hex){
if (tempMinHolder < 99){
lcd.clear_L2();
tempMinHolder++;
}
else{
tempMinHolder = 100;
lcd.cursorTo(1,0);
lcd.print(F("100% - the max value"));
delay(700);
lcd.clear_L2();
}
update_effective_rate(tempMinHolder);
}
// key = Down
else if (key == ir_keys[K_DOWN].hex){
if (tempMinHolder > 1){
tempMinHolder--;
}
else{
tempMinHolder = 0;
lcd.cursorTo(1,0);
lcd.print(F("Turning pump OFF "));
}