-
Notifications
You must be signed in to change notification settings - Fork 0
/
HourInputManager.h
147 lines (123 loc) · 2.78 KB
/
HourInputManager.h
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
#include "LcdManager.h"
#ifndef HourInputManager_h
#define HourInputManager_h
class CallbackHourInput{
public:
virtual void callback( uint8_t hours, uint8_t minutes ){
}
};
class HourInputManager: public GlobalHandler, public Looper {
private:
enum HourInputState {
HOURS,
MINUTES
};
ButtonsManager *buttonsManager;
LcdManager *lcdManager;
Button *okButton;
Button *upButton;
Button *downButton;
bool showing = false;
uint8_t inputHours;
uint8_t inputMinutes;
HourInputState hourInputState;
CallbackHourInput *callbackHourInput;
String number2digit(uint8_t nb) {
String val = "";
if (nb < 10)
val = "0" + String(nb);
else
val = String(nb);
return val;
}
void displayInputTime() {
// Make it blink !
String displayedHours;
String displayedMinutes;
if( (((millis()/1000) % 2) == 0) ){
switch( hourInputState ){
case HOURS:
displayedHours = " ";
displayedMinutes = number2digit(inputMinutes);
break;
case MINUTES:
displayedHours = number2digit(inputHours);
displayedMinutes = " ";
break;
}
}
else{
displayedHours = number2digit(inputHours);
displayedMinutes = number2digit(inputMinutes);
}
String hourStr = displayedHours + ":" + displayedMinutes;
lcdManager->displayLcd(hourStr, "");
}
void doLoop() {
if( showing ){
displayInputTime();
}
}
public:
HourInputManager(
ButtonsManager *buttonsManager,
LcdManager *lcdManager,
Button *okButton,
Button *upButton,
Button *downButton) {
this->buttonsManager = buttonsManager;
this->lcdManager = lcdManager;
this->okButton = okButton;
this->upButton = upButton;
this->downButton = downButton;
this->showing = false;
buttonsManager->addGlobalHandler(this);
}
bool mustCheck() {
return showing;
}
void handleButtonPressed(Button *button) {
if( button == this->okButton ){
switch( hourInputState ){
case HOURS:
hourInputState = MINUTES;
break;
case MINUTES:
// Serial.println("A1");
showing = false;
// Serial.println("A2");
this->callbackHourInput->callback(inputHours, inputMinutes);
// Serial.println("A3");
break;
}
}
else if( button == this->upButton ){
switch( hourInputState ){
case HOURS:
inputHours = (inputHours+25)%24;
break;
case MINUTES:
inputMinutes = (inputMinutes+61)%60;
break;
}
}
else if( button == this->downButton ){
switch( hourInputState ){
case HOURS:
inputHours = (inputHours+23)%24;
break;
case MINUTES:
inputMinutes = (inputMinutes+59)%60;
break;
}
}
}
void show( uint8_t hours, uint8_t minutes, CallbackHourInput *callbackHourInput ){
hourInputState = HOURS;
this->inputHours = hours;
this->inputMinutes = minutes;
this->callbackHourInput = callbackHourInput;
showing = true;
}
};
#endif