-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottler.ino
1027 lines (906 loc) · 22.4 KB
/
bottler.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
// Copyright (C) 2020 Philipp Müller
// bottler is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// bottler 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
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with bottler. If not, see <https://www.gnu.org/licenses/>.
#include <RTClib.h>
#include <LiquidCrystal.h>
//////////////////////////////////////////////////////////////////////
// Board-specific constants
// Specifies whether the custom shield or - if not defined - the DF
// Robot LCD Keypad Shield was used.
#define CUSTOM_SHIELD
// Pin the positive potential of the background light is connected
// to. Only used if CUSTOM_SHIELD is defined.
#define LCD_POWER 3
// Whether or not a DS1307 chip is present on the custom shield
// #define USE_RTC
//////////////////////////////////////////////////////////////////////
// General constants
// Amount of microseconds the program will pause after detecting a
// button event. This is necessary since several button events per
// second would be detected otherwise.
#define DELAY_TIME 90
// Last X hours to take into account when calculating the total
// consumption.
#define CONSUMPTION_TIME_SPAN 24
// Default values to display in the newBottleView().
#define DEFAULT_VOLUME 180
#define DEFAULT_REMAINING 40
#define DEFAULT_SPOONS 6
// The (MAX_BOTTLES + 1)-th bottle will overwrite the first one.
#define MAX_BOTTLES 50
// Amount of seconds the display stays on until it powers off
// automatically. Between 1 and 60.
#define DISPLAY_ACTIVE_DURATION 60
//////////////////////////////////////////////////////////////////////
// Global variables, functions, and classes.
RTC_DS1307 rtc;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#ifndef USE_RTC
// Intermediate variable for adjusting the time if no RTC chip is
// present.
DateTime custom_time = DateTime(2020, 9, 9, 0, 0, 0);
#endif
// In case the DF Robot Shield (or just a LCD display with buttons but
// no RTC is used, we can not rely on it's now() function and have to
// use our own.
DateTime nowCustom() {
#ifdef USE_RTC
return rtc.now();
#else
return custom_time + TimeSpan(int32_t(millis()/1000));
#endif
}
enum Button { buttonRight, buttonUp, buttonDown, buttonLeft,
buttonSelect, buttonNone };
enum Views { viewHistory, viewMenu, viewNewBottle, viewConsumption, viewTime, viewNone };
enum BottleItem { bottleSpoons, bottleVolume, bottleRemaining, bottleTime, bottleNone };
struct Bottle {
float spoons;
int volume;
int remaining;
DateTime time;
void print() const;
// Since the spoons of milk powder themselves contribute to the
// total volume, they will be taken into account using this
// function.
//
// According to the instruction on the Aptamil package three
// spoons amount to 10ml.
float totalVolume() const;
float consumedVolume() const;
// Consumed volume time `spoons` to only account the milk which
// actually got drunken.
//
// According to the instruction on the Aptamil package a spoons
// corresponds to 4.6g.
float consumedPowder() const;
// Helper function determining whether the time stamp of a bottle
// does lie within the last `hours` regardless of the shield used.
bool consumedDuringLastXHours(byte hours) const;
};
void Bottle::print() const {
Serial.println("bottle:");
Serial.print("\tspoons: ");
Serial.println(spoons);
Serial.print("\tvolume: ");
Serial.println(volume);
Serial.print("\tremaining: ");
Serial.println(remaining);
Serial.print("\ttime: ");
Serial.println(time.timestamp());
}
float Bottle::totalVolume() const {
return static_cast<float>(volume) + spoons * 10 / 3;
}
float Bottle::consumedVolume() const {
return totalVolume() - remaining;
}
float Bottle::consumedPowder() const {
return consumedVolume() / totalVolume() * spoons * 4.6;
}
bool Bottle::consumedDuringLastXHours(byte hours) const {
DateTime now = nowCustom();
return time >= now - TimeSpan(0, hours, 0, 0);
}
class Crate {
public:
Crate();
~Crate();
// Calculates the amount of consumed volume and milk powder -
// based and the spoons used on percentage of the consumed volume
// - within the past `since` hours. The results are provided using
// the pointers `volume` and `powder`.
void totalConsumption(byte since, int *volume, float *powder) const;
int getCurrentBottleIdx() const;
Bottle getBottle(int idx) const;
void setBottle(int idx, Bottle b);
bool isBottleUndefined(int idx) const;
// Adds a new bottle to the global bottles array and increments
// current_bottle. This array will be filled in a cyclic fashion
// while overwriting the oldest entry.
//
// Upon adding a bottle is considered full.
void addBottle(float spoons, int volume, DateTime time);
void printContent() const;
private:
Bottle m_bottles[MAX_BOTTLES];
byte m_current_bottle_idx;
};
Crate::Crate() : m_current_bottle_idx( 0 )
, m_bottles {} {
}
Crate::~Crate(){
}
int Crate::getCurrentBottleIdx() const {
return m_current_bottle_idx;
}
Bottle Crate::getBottle(int idx) const {
return m_bottles[idx];
}
void Crate::setBottle(int idx, Bottle b) {
m_bottles[idx] = b;
}
bool Crate::isBottleUndefined(int idx) const {
if ( m_bottles[idx].spoons == 0 &&
m_bottles[idx].volume == 0 &&
m_bottles[idx].remaining == 0 ) {
return true;
}
return false;
}
void Crate::printContent() const {
Serial.println("\nBottles stored in crate:");
for ( int ii = 0; ii < MAX_BOTTLES; ii++ ) {
if ( !isBottleUndefined( ii ) ) {
Serial.print(ii);
Serial.print(":\t");
Serial.print(m_bottles[ii].spoons);
Serial.print("\t");
Serial.print(m_bottles[ii].volume);
Serial.print(" ml\t");
Serial.print(m_bottles[ii].remaining);
Serial.print(" ml\t");
Serial.println(m_bottles[ii].time.timestamp());
}
}
}
void Crate::addBottle(float spoons, int volume, DateTime time)
{
Bottle new_bottle = {spoons, volume, volume, time};
m_current_bottle_idx++;
if ( m_current_bottle_idx >= MAX_BOTTLES ) {
m_current_bottle_idx = 0;
}
m_bottles[m_current_bottle_idx] = new_bottle;
}
void Crate::totalConsumption(byte since, int *volume, float *powder) const {
float consumed_volume = 0;
float consumed_powder = 0;
for ( int ii = 0; ii < MAX_BOTTLES; ii++ ) {
if ( m_bottles[ii].consumedDuringLastXHours(since) ) {
consumed_volume += m_bottles[ii].consumedVolume();
consumed_powder += m_bottles[ii].consumedPowder();
}
}
*volume = consumed_volume;
*powder = consumed_powder;
}
class Interface {
public:
Interface();
~Interface();
void view();
private:
void bottleView(int index);
void historyView();
void menuView();
void newBottleView();
void consumptionView();
#ifndef USE_RTC
void setTimeView();
#endif
void toggleDisplay( bool on );
int readButtons() const;
// Resets the global variables to their initial state (done before
// entering the view).
void restoreDefaults();
Crate m_crate;
// Specifies whether to turn on the LCD display or not. This is
// used to reduce power consumption and the display will be set to
// off after `DISPLAY_ACTIVE_DURATION` without no user input
// automatically.
bool m_display_active;
DateTime m_last_user_interaction;
int m_current_view;
// Specifies which bottle in m_bottles is the most recent one.
int m_displayed_bottle;
bool m_update_display;
int m_menu_item;
int m_bottle_option;
float m_bottle_spoons;
int m_bottle_volume;
int m_bottle_remaining;
DateTime m_bottle_time;
// 0 - setting the hours and 1 - setting the minutes for both the
// custom time and a new bottle.
byte m_set_time_state;
};
Interface::Interface() : m_current_view( viewHistory )
, m_menu_item( viewNewBottle )
, m_bottle_option( bottleSpoons )
, m_bottle_spoons( DEFAULT_SPOONS )
, m_bottle_volume( DEFAULT_VOLUME )
, m_bottle_time( nowCustom() )
, m_displayed_bottle( 0 )
, m_display_active( true )
, m_update_display( true )
, m_last_user_interaction( nowCustom() )
, m_set_time_state( 0 )
, m_crate{}{
}
Interface::~Interface(){
}
int Interface::readButtons() const {
int adc_key_in = analogRead(0);
#ifndef CUSTOM_SHIELD
if ( adc_key_in > 790 ) {
return buttonNone;
}
#endif
delay(DELAY_TIME);
#ifdef CUSTOM_SHIELD
if ( adc_key_in < 500 ) {
return buttonNone;
}
m_last_user_interaction = nowCustom();
if ( adc_key_in < 720 ) {
return buttonSelect;
} else if ( adc_key_in < 900 ) {
return buttonLeft;
} else if ( adc_key_in < 950 ) {
return buttonDown;
} else if ( adc_key_in < 1005 ) {
return buttonUp;
} else {
return buttonRight;
}
#else
m_last_user_interaction = nowCustom();
if ( adc_key_in < 50 ) {
return buttonRight;
} else if ( adc_key_in < 195 ) {
return buttonUp;
} else if ( adc_key_in < 380 ) {
return buttonDown;
} else if ( adc_key_in < 555 ) {
return buttonLeft;
} else if ( adc_key_in < 790 ) {
return buttonSelect;
}
#endif
return buttonNone;
}
void Interface::toggleDisplay( bool on ) {
if ( !on && m_display_active ) {
if ( nowCustom() - TimeSpan(0, 0, 0, DISPLAY_ACTIVE_DURATION) >
m_last_user_interaction ) {
lcd.noDisplay();
#ifdef CUSTOM_SHIELD
digitalWrite(LCD_POWER, LOW);
#endif
m_display_active = false;
}
} else if ( on ) {
lcd.display();
#ifdef CUSTOM_SHIELD
digitalWrite(LCD_POWER, HIGH);
#endif
m_display_active = true;
m_update_display = true;
}
}
void Interface::restoreDefaults()
{
m_bottle_option = bottleSpoons;
m_bottle_spoons = DEFAULT_SPOONS;
m_bottle_volume = DEFAULT_VOLUME;
m_bottle_remaining = DEFAULT_REMAINING;
m_bottle_time = nowCustom();
m_displayed_bottle = m_crate.getCurrentBottleIdx();
m_set_time_state = 0;
}
void Interface::bottleView(int index)
{
Bottle current_bottle = m_crate.getBottle( index );
if ( m_update_display ) {
m_update_display = false;
lcd.clear();
lcd.setCursor(0,0);
// In case of an empty bottle, make 0 the default instead.
if ( current_bottle.volume == 0 ) {
m_bottle_remaining = 0;
}
switch ( m_bottle_option ) {
case bottleRemaining: {
lcd.print("Remaining:");
if ( m_bottle_remaining >= 100 ) {
lcd.setCursor(0,1);
}
else if ( m_bottle_remaining >= 10 ) {
lcd.setCursor(1,1);
} else {
lcd.setCursor(2,1);
}
lcd.print(m_bottle_remaining);
lcd.setCursor(3,1);
lcd.print("ml");
break;
}
default: {
lcd.setCursor( 0, 0 );
lcd.print( current_bottle.spoons );
if ( current_bottle.remaining >= 100 ) {
lcd.setCursor( 7, 0 );
} else if ( current_bottle.remaining >= 10 ) {
lcd.setCursor( 8, 0 );
} else {
lcd.setCursor(9,0);
}
lcd.print(current_bottle.remaining);
lcd.setCursor(10, 0);
lcd.print("/");
if (current_bottle.volume < 100) {
lcd.setCursor(12, 0);
} else {
lcd.setCursor(11, 0);
}
lcd.print(current_bottle.volume);
lcd.setCursor(14, 0);
lcd.print("ml");
if ( current_bottle.time.hour() < 10 ) {
lcd.setCursor(3,1);
lcd.print("0");
lcd.setCursor(4,1);
} else {
lcd.setCursor(3,1);
}
lcd.print(current_bottle.time.hour());
lcd.setCursor(5,1);
lcd.print(":");
if ( current_bottle.time.minute() < 10 ) {
lcd.setCursor(6,1);
lcd.print("0");
lcd.setCursor(7,1);
} else {
lcd.setCursor(6,1);
}
lcd.print(current_bottle.time.minute());
break;
}
}
}
int lcd_key = readButtons();
switch ( lcd_key ) {
case buttonRight: {
if ( m_bottle_option != bottleRemaining ) {
m_bottle_option = bottleRemaining;
} else {
current_bottle.remaining = m_bottle_remaining;
m_crate.setBottle( index, current_bottle );
m_bottle_option = bottleSpoons;
}
m_update_display = true;
break;
}
case buttonLeft: {
if ( m_bottle_option != bottleRemaining ) {
// Abort and return to current bottle view.
m_update_display = true;
view();
break;
} else {
m_bottle_option = bottleSpoons;
m_update_display = true;
break;
}
}
case buttonUp: {
if ( m_bottle_option != bottleRemaining ) {
if ( m_displayed_bottle != MAX_BOTTLES - 1 ){
m_displayed_bottle++;
} else {
m_displayed_bottle = 0;
}
} else {
if ( m_bottle_remaining < current_bottle.volume ) {
m_bottle_remaining += 10;
}
}
m_update_display = true;
break;
}
case buttonDown: {
if ( m_bottle_option != bottleRemaining ) {
if ( m_displayed_bottle != 0 ){
m_displayed_bottle--;
} else {
m_displayed_bottle = MAX_BOTTLES - 1;
}
} else {
if ( m_bottle_remaining > 0 ) {
m_bottle_remaining -= 10;
}
}
m_update_display = true;
break;
}
}
}
void Interface::menuView()
{
if ( m_update_display ){
m_update_display = false;
lcd.clear();
lcd.setCursor(0,0);
switch ( m_menu_item ) {
#ifndef USE_RTC
case viewNewBottle: {
lcd.print("> New bottle");
lcd.setCursor(2,1);
lcd.print("Total consumption");
break;
}
case viewConsumption: {
lcd.print("> Total consumption");
lcd.setCursor(2,1);
lcd.print("Set time");
break;
}
case viewTime: {
lcd.print("> Set time");
lcd.setCursor(2,1);
lcd.print("New bottle");
break;
}
default: {
Serial.println("Unknown m_menu_item in menuView");
}
}
#else
case viewNewBottle: {
lcd.print("> New bottle");
lcd.setCursor(2,1);
lcd.print("Total consumption");
break;
}
case viewConsumption: {
lcd.setCursor(2,0);
lcd.print("New bottle");
lcd.setCursor(0,1);
lcd.print("> Total consumption");
break;
}
default: {
Serial.println("Unknown m_menu_item in menuView");
}
}
#endif
}
int lcd_key = readButtons();
switch ( lcd_key ) {
case buttonRight: {
restoreDefaults();
// Enter the selected view.
m_current_view = m_menu_item;
m_update_display = true;
break;
}
case buttonLeft: {
restoreDefaults();
// Abort and return to current bottle view.
m_current_view = viewHistory;
m_displayed_bottle = m_crate.getCurrentBottleIdx();
m_update_display = true;
break;
}
case buttonUp: {
#ifndef USE_RTC
if ( m_menu_item == viewNewBottle ) {
m_menu_item = viewTime;
} else if ( m_menu_item == viewConsumption ) {
m_menu_item = viewNewBottle;
} else if ( m_menu_item == viewTime ) {
m_menu_item = viewConsumption;
} else {
Serial.println("Unsupported menu_item in menuView");
}
#else
if ( m_menu_item == viewNewBottle ) {
m_menu_item = viewConsumption;
} else if ( m_menu_item == viewConsumption ) {
m_menu_item = viewNewBottle;
} else {
Serial.println("Unsupported menu_item in menuView");
}
#endif
m_update_display = true;
break;
}
case buttonDown: {
#ifndef USE_RTC
if ( m_menu_item == viewNewBottle ) {
m_menu_item = viewConsumption;
} else if ( m_menu_item == viewConsumption ) {
m_menu_item = viewTime;
} else if ( m_menu_item == viewTime ) {
m_menu_item = viewNewBottle;
} else {
Serial.println("Unsupported menu_item in menuView");
}
#else
if ( m_menu_item == viewNewBottle ) {
m_menu_item = viewConsumption;
} else if ( m_menu_item == viewConsumption ) {
m_menu_item = viewNewBottle;
} else {
Serial.println("Unsupported menu_item in menuView");
}
#endif
m_update_display = true;
break;
}
}
}
void Interface::newBottleView()
{
if ( m_update_display ) {
m_update_display = false;
lcd.clear();
lcd.setCursor(0,0);
switch ( m_bottle_option ) {
case bottleSpoons: {
lcd.print("Spoons:");
lcd.setCursor(8,0);
lcd.print( m_bottle_spoons );
break;
}
case bottleVolume: {
lcd.print("Volume:");
if ( m_bottle_volume >= 100 ) {
lcd.setCursor(8,0);
} else {
lcd.setCursor(9,0);
}
lcd.print( m_bottle_volume );
lcd.setCursor(11,0);
lcd.print("ml");
break;
}
case bottleTime: {
lcd.print("Time:");
if ( m_bottle_time.hour() < 10 ) {
lcd.setCursor(6,0);
lcd.print("0");
lcd.setCursor(7,0);
} else {
lcd.setCursor(6,0);
}
lcd.print(m_bottle_time.hour());
lcd.setCursor(8,0);
lcd.print(":");
if ( m_bottle_time.minute() < 10 ) {
lcd.setCursor(9,0);
lcd.print("0");
lcd.setCursor(10,0);
} else {
lcd.setCursor(9,0);
}
lcd.print(m_bottle_time.minute());
if ( m_set_time_state == 0 ) {
lcd.setCursor(6,1);
lcd.print("--");
} else {
lcd.setCursor(9,1);
lcd.print("--");
} break;
}
default: {
Serial.println("Unsupported bottle_option in switch in newBottleView");
}
}
}
int lcd_key = readButtons();
switch ( lcd_key ) {
case buttonRight: {
if ( m_bottle_option == bottleSpoons ) {
m_bottle_option = bottleVolume;
} else if ( m_bottle_option == bottleVolume ) {
m_bottle_option = bottleTime;
} else if ( m_bottle_option == bottleTime &&
m_set_time_state == 0 ) {
m_set_time_state = 1;
} else {
m_crate.addBottle( m_bottle_spoons,
m_bottle_volume,
m_bottle_time );
m_displayed_bottle = m_crate.getCurrentBottleIdx();
restoreDefaults();
m_current_view = viewHistory;
}
m_update_display = true;
break;
}
case buttonLeft: {
if ( m_bottle_option == 0 ) {
restoreDefaults();
m_current_view = viewHistory;
} else if ( m_bottle_option == bottleVolume ) {
m_bottle_option = bottleSpoons;
} else {
if ( m_set_time_state == 0 ) {
m_bottle_option = bottleVolume;
} else {
m_set_time_state = 0;
}
}
m_update_display = true;
break;
}
case buttonUp: {
switch ( m_bottle_option ) {
case bottleSpoons: {
m_bottle_spoons = m_bottle_spoons + 0.25;
break;
}
case bottleVolume: {
// There is a maximum value of a bottle.
if ( m_bottle_volume < 250 ) {
m_bottle_volume = m_bottle_volume + 10;
}
break;
}
case bottleTime: {
if ( m_set_time_state == 0 ) {
m_bottle_time = m_bottle_time + TimeSpan(0,1,0,0);
} else {
m_bottle_time = m_bottle_time + TimeSpan(0,0,1,0);
}
}
default: {
Serial.println("Unsupported BottleItem for up in newBottleView");
break;
}
}
m_update_display = true;
break;
}
case buttonDown: {
switch ( m_bottle_option ) {
case bottleSpoons: {
if ( m_bottle_spoons > 0 ) {
m_bottle_spoons = m_bottle_spoons - 0.25;
}
break;
}
case bottleVolume: {
if ( m_bottle_volume > 0 ) {
m_bottle_volume = m_bottle_volume - 10;
}
break;
}
case bottleTime: {
if ( m_set_time_state == 0 ) {
m_bottle_time = m_bottle_time - TimeSpan(0,1,0,0);
} else {
m_bottle_time = m_bottle_time - TimeSpan(0,0,1,0);
}
break;
}
default: {
Serial.println("Unsupported BottleItem for up in newBottleView");
break;
}
}
m_update_display = true;
break;
}
}
}
// Displays the total consumption in volume and number of spoons
// within the last 24 hours.
void Interface::consumptionView()
{
if ( m_update_display ){
m_update_display = false;
int consumed_volume;
float consumed_powder;
m_crate.totalConsumption(CONSUMPTION_TIME_SPAN, &consumed_volume, &consumed_powder);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Volume:");
if ( consumed_volume > 1000 ) {
lcd.setCursor(9,0);
} else if ( consumed_volume > 100 ) {
lcd.setCursor(10,0);
} else {
lcd.setCursor(11,0);
}
lcd.print(consumed_volume);
lcd.setCursor(13,0);
lcd.print("ml");
lcd.setCursor(0,1);
lcd.print("Powder:");
lcd.setCursor(8,1);
lcd.print(consumed_powder);
lcd.setCursor(13,1);
lcd.print("g");
}
int lcd_key = readButtons();
if ( lcd_key == buttonLeft ) {
restoreDefaults();
m_current_view = viewHistory;
m_displayed_bottle = m_crate.getCurrentBottleIdx();
m_update_display = true;
}
}
// Displays all bottles stored in memory with latest first. Uses the
// currentBottleView() for a particular pair of bottles.
void Interface::historyView()
{
bottleView(m_displayed_bottle);
}
#ifndef USE_RTC
void Interface::setTimeView()
{
if ( m_update_display ) {
m_update_display = false;
lcd.clear();
lcd.print("Time:");
if ( nowCustom().hour() < 10 ) {
lcd.setCursor(6,0);
lcd.print("0");
lcd.setCursor(7,0);
} else {
lcd.setCursor(6,0);
}
lcd.print(nowCustom().hour());
lcd.setCursor(8,0);
lcd.print(":");
if ( nowCustom().minute() < 10 ) {
lcd.setCursor(9,0);
lcd.print("0");
lcd.setCursor(10,0);
} else {
lcd.setCursor(9,0);
}
lcd.print(nowCustom().minute());
if ( m_set_time_state == 0 ) {
lcd.setCursor(6,1);
lcd.print("--");
} else {
lcd.setCursor(9,1);
lcd.print("--");
}
}
int lcd_key = readButtons();
switch ( lcd_key ) {
case buttonRight: {
if ( m_set_time_state == 0 ) {
m_set_time_state = 1;
} else {
restoreDefaults();
m_current_view = viewHistory;
}
m_update_display = true;
break;
}
case buttonLeft: {
if ( m_set_time_state == 0 ) {
restoreDefaults();
m_current_view = viewHistory;
} else {
m_set_time_state = 0;
}
m_update_display = true;
break;
}
case buttonUp: {
if ( m_set_time_state == 0 ) {
custom_time = custom_time + TimeSpan(0,1,0,0);
} else {
custom_time = custom_time + TimeSpan(0,0,1,0);
}
m_update_display = true;
break;
}
case buttonDown: {
if ( m_set_time_state == 0 ) {
custom_time = custom_time - TimeSpan(0,1,0,0);
} else {
custom_time = custom_time - TimeSpan(0,0,1,0);
}
m_update_display = true;
break;
}
}
}
#endif
void Interface::view()
{
if ( m_display_active ) {
switch ( m_current_view ) {
case viewHistory: {
historyView();
break;
}
case viewMenu: {
menuView();
break;
}
case viewNewBottle: {
newBottleView();
break;
}
case viewConsumption: {
consumptionView();
break;
}
#ifndef USE_RTC
case viewTime: {
setTimeView();
break;
}
#endif
default: {
historyView();
break;
}
}
}
int lcd_key = readButtons();
if ( lcd_key != buttonNone ) {
if ( m_display_active ) {
if ( lcd_key == buttonSelect ) {
m_current_view = viewMenu;
m_menu_item = viewNewBottle;
m_update_display = true;
}
} else {
toggleDisplay( true );
}
} else {
// If no button was hit we will check if it is already time to
// automatically turn off the display.
toggleDisplay( false );
}
}
Interface app;
void setup() {