This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Main.ino
312 lines (230 loc) · 7.75 KB
/
Main.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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/////Rotary Encoder Pin Assignment/////
#define rotaryPin_A 12
#define rotaryPin_B 13
#define rotaryButtonPin 8
#define inputTimeOut 10000 //time out for human input session, milliseconds
unsigned int maxMenuItems; //number of menu items
unsigned char encoder_A = 0;
unsigned char encoder_B = 0;
unsigned char encoder_A_prev = 0;
unsigned char encoder_C = 1; //encoder button
unsigned char encoder_C_prev = 1;
unsigned long currentTime;
unsigned long loopTime;
/////End of Rotary Encoder Declarations/////
/////Global time variable, Hour : Minute Settings
unsigned int setHour = 0;
unsigned int setMinute = 0;
////////
//////Menu declarations///////
unsigned int menuPos = 0; //current menu position
unsigned int lastMenuPos = 0; //previous menu position
unsigned int parentMenuPos = 0; //parent menu position
bool humanInputActive = false; //flag to indicate if input session is active
unsigned subMenuActive = 0; //flag to indicate a sub selection menu session is active: 0 - main menu; 1 - number selection 1- 255; 2 - binary selection yes/no; 3 - time setting;
unsigned int subMenuPos = 0; //sub menu rotary position
unsigned int subMenuClick = 0; //sub menu click counter
//unsigned long inputTimeOut = 10000; //time out for human input session, milliseconds
unsigned long lastInputTime = millis(); //keep track of time of last human input
typedef struct menu_type
{
menu_type()
: code(0), text("")
{
//do nothing
}
unsigned int code; //code that indicate address (position) in the menu tree
String text; //text to be displayed for the menu selection
} menu_type;
menu_type menu[100] = {}; //initilizing menu array, use a number >= than the number of menu options
/////End of Menu declarations////
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
Serial.begin(9600);
rotaryEncoderInit();
menuInit();
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
rotaryEncoderUpdate();
lcdBackLight();
}
void rotaryEncoderInit() {
//Rotary encoder initialization
pinMode(rotaryPin_A, INPUT_PULLUP);
pinMode(rotaryPin_B, INPUT_PULLUP);
pinMode(rotaryButtonPin, INPUT_PULLUP);
currentTime = millis();
loopTime = currentTime;
}
void rotaryEncoderUpdate() {
//rotary encoder update, to be called from main loop()
currentTime = millis();
if (currentTime >= (loopTime + 1) ) {
encoder_A = digitalRead(rotaryPin_A);
encoder_B = digitalRead(rotaryPin_B);
encoder_C = digitalRead(rotaryButtonPin);
//handling knob rotation
if ( (encoder_A == 0) && (encoder_A_prev == 1) ) { //encoder changed position
lastInputTime = millis();
if (encoder_B == 1) { //B is high, so encoder moved clockwise
//Serial.println("encoder rotated CW");
switch (subMenuActive) { //send encoder action to appropriate menu handler
case 0: //main menu
menuUpdate(2);
break;
case 1: //subMenu1
subMenu1Update(2);
break;
case 2: //subMenu2
subMenu2Update(2);
break;
case 3:
subMenu3Update(2);
break;
default:
menuUpdate(2);
break;
}
} else { //else, encoder moved counter-clockwise
//Serial.println("encoder rotated CCW");
switch (subMenuActive) { //call the appropriate menuupdater depending on which sub menu is active
case 0:
menuUpdate(3);
break;
case 1:
subMenu1Update(3);
break;
case 2:
subMenu2Update(3);
break;
case 3:
subMenu3Update(3);
break;
default:
menuUpdate(3);
break;
}
}
}
//handling push button
if ( (encoder_C == 0) && (encoder_C_prev == 1) ) { //button pushed
//Serial.println("encoder button closed.");
lastInputTime = millis();
switch (subMenuActive) { //call the appropriate menuupdater depending on which sub menu is active
case 0:
menuUpdate(1);
break;
case 1:
subMenu1Update(1);
break;
case 2:
subMenu2Update(1);
break;
case 3:
subMenu3Update(1);
break;
default:
menuUpdate(1);
break;
}
} else if ( (encoder_C == 1) && (encoder_C_prev == 0) ) { //button
//Serial.println("encoder button opened.");
}
encoder_A_prev = encoder_A;
encoder_C_prev = encoder_C;
loopTime = currentTime;
//input time out
if ( (millis() - lastInputTime) > inputTimeOut ) {
humanInputActive = false;
menuPos = 0;
lastMenuPos = 0;
menuUpdate(0);
subMenuActive = 0;
} else {
humanInputActive = true;
}
}
}
unsigned int menuVerifyPos(unsigned int menuCode) {
//accepts a code that represents position in the menu
//checks against the menu, verify it exist, and returns it
//if the menu code given does not exist,
//returns the closest code smaller than the one given
bool confirmCode = false; //flag to keep track of whether code has been confirmed in menu tree
for (unsigned int k = 0; k <= (maxMenuItems - 1); k++) {
if (menuCode == menu[k].code) { //found exact code, returns it
menuPos = menu[k].code;
confirmCode = true;
lastMenuPos = menuCode;
return menuPos;
}
}
if (confirmCode == false) {
menuPos = lastMenuPos;
return menuPos; //cannot find menu option, go back to previous menu option
}
}
void updateMenuDisplay(unsigned int menuCode) {
//prints menu selection to the LCD display
//in order to have a scrolling menu effect, this code looks at item before and after current menu item and display them in a row
String curMenu;
String curMenu2;
String prevMenu;
String nextMenu;
curMenu = findMenuTextFromCode(menuCode);
prevMenu = findMenuTextFromCode(menuCode - 1);
nextMenu = findMenuTextFromCode((menuCode + 1));
//if string is 20 or more characters long (for 20 char wide LCD display)
if( prevMenu.length() >= 20) {
prevMenu.remove(19); //remove anything after the 19th character from string
}
if(nextMenu.length() >= 20) {
nextMenu.remove(19); //remove anything after the 19th character from string
}
//starts printing to LCD
lcd.clear();
lcd.setCursor(1,0); //char index, row index, on LCD
lcd.print(prevMenu);
lcd.setCursor(0,1);
lcd.print(">");
//current menu option text is longer than 20 characters (on a 20 char wide LCD)
if(curMenu.length() >= 20) {
curMenu2 = curMenu;
curMenu.remove(19); //remove char from index 19 (20th char)
curMenu2.remove(0,19); //remove the first 19 char starting from index 0
lcd.setCursor(1,1); //print first 19 char of current menu text
lcd.print(curMenu);
lcd.setCursor(0,2);
lcd.print("-");
lcd.setCursor(1,2);
lcd.print(curMenu2); //print remainig char of current menu text
lcd.setCursor(1,3); //print the next menu text in the 4th row
lcd.print(nextMenu);
} else {
lcd.print(curMenu); //print the next menu text in the 3rd row
lcd.setCursor(1,2);
lcd.print(nextMenu);
}
}
String findMenuTextFromCode(unsigned int menuCode) {
//accepts a code representing the code in menu, and returns the corresponding text
for (unsigned int j = 0; j <= (maxMenuItems - 1); j++ ) {
if (menuCode == menu[j].code) {
return menu[j].text;
break;
}
}
}
void lcdBackLight(){
//human inout timeout to turn off lcd back light
if (humanInputActive == true) {
lcd.backlight();
} else {
lcd.noBacklight();
}
}