#include <ESP32Time.h> //To handle ESP32 RTC (Real Time Clock)
#include <Wire.h> //To communicate with I2C
#include <LiquidCrystal_I2C.h> // To handle LCD display
#include <ezButton.h>
// Configure LCD screen size
#define LCD_ROWS 4
#define LCD_COLS 20
LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
// Configure butons
ezButton upButton(14);
ezButton downButton(13);
ezButton enterButton(12);
ezButton backButton(27);
//byte firstElementToDisplay = 0; // Line of menu element displayed on 1st. row
//byte menuLevelToDisplay = 0; // Level of menu to display
// To store the n (2 or 4) lines to actually display at any given time
String menuActuallyDisplayed[4] = {
"",
"",
"",
""
};
//ESP32Time RTC (Real Time Clock);
ESP32Time rtc(21600); // offset in seconds GMT+6
const int menuRows = 18; // Rows in menu structure
const int menuCols = 3; // Cols in menu structure
// Define the Menu of Level 1
const String menu[18][3] = {
"Reloj", "", "",
"..", "Configurar Fecha", "",
"..", "Configurar Hora", "",
"..", "Formato", "",
"Alarmas", "", "",
"..", "Configurar", "",
"..", "Desactivar", "",
"Adelantar toma", "", "",
"Preferencias", "", "",
"..", "Volumen", "",
"..", "Vibración", "",
"Notificaciones", "", "",
"..", "Usuarios", "",
"..", "Tipos", "",
"..", "..", "WhatsApp",
"..", "..", "email",
"..", "Activar/Desactivar", "",
"Reportes", "", ""
};
// Down arrow (↓) icon
byte downArrow[8] = {
0b00100, // *
0b00100, // *
0b00100, // *
0b00100, // *
0b00100, // *
0b10101, // * * *
0b01110, // ***
0b00100 // *
};
// Up arrow (↑) icon
byte upArrow[8] = {
0b00100, // *
0b01110, // ***
0b10101, // * * *
0b00100, // *
0b00100, // *
0b00100, // *
0b00100, // *
0b00100 // *
};
// Cursor icon. Defaults to right arrow (→)
uint8_t cursorIcon = 0x7E;
byte lcdLine = 0; // Counter for storing text in menuActuallyDisplayed[] array
byte cursorRow = 0;
byte cursorRow_new =0;
byte i = 0;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.createChar(0, upArrow);
lcd.createChar(1, downArrow);
upButton.setDebounceTime(50); //upButton.loop();
downButton.setDebounceTime(50); //downButton.loop();
enterButton.setDebounceTime(50); //enterButton.loop();
backButton.setDebounceTime(50); //backButton.loop();
//rtc.setTime(25, 50, 11, 06, 02, 1970); // 06th Feb 1970 11:50:25
/*
pinMode(upButton, INPUT_PULLDOWN);
pinMode(downButton, INPUT_PULLDOWN);
pinMode(enterButton, INPUT_PULLDOWN);
pinMode(backButton, INPUT_PULLDOWN);
*/
updateMenu(0,0); //Params: firstElementToDisplay, menuLevelToDisplay
}
void loop() {
//print_time();
/*
while (i<1) {
updateMenu(0,0); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(1,1); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(4,0); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(5,1); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(7,0); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(9,1); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(12,1); //Params: firstElementToDisplay, menuLevelToDisplay
updateMenu(14,2); //Params: firstElementToDisplay, menuLevelToDisplay
i++;
}
*/
upButton.loop();
downButton.loop();
enterButton.loop();
backButton.loop();
//delay(1000);
//if (digitalRead(downButton) == 1) {
//Serial.println(downButton.isPressed());
if (downButton.isPressed()) {
cursorRow_new++;
validateCursorRow(cursorRow, cursorRow_new);
}
}
// --------------------------------------------------------------
void updateMenu(byte firstElementToDisplay, byte menuLevelToDisplay) {
byte i = 0; // Counter for going through the menu
menuActuallyDisplayed[0] = "";
menuActuallyDisplayed[1] = "";
menuActuallyDisplayed[2] = "";
menuActuallyDisplayed[3] = "";
String text = "";
i = firstElementToDisplay;
while (i<menuRows and lcdLine<4) {
if (menuLevelToDisplay == 0) {
if (menu[i][menuLevelToDisplay] != "..") {
menuActuallyDisplayed[lcdLine] = menu[i][menuLevelToDisplay];
lcdLine++;
}
} else {
if (menu[i][menuLevelToDisplay-1] == ".." and menu[i][menuLevelToDisplay] != "..") {
menuActuallyDisplayed[lcdLine] = menu[i][menuLevelToDisplay];
lcdLine++;
} else {
break;
}
}
i++;
}
lcd.clear();
lcd.write(cursorIcon);
//lcd.write(byte(0));
//lcd.write(byte(1));
lcd.setCursor(1,0); lcd.println(menuActuallyDisplayed[0]);
lcd.setCursor(1,1); lcd.println(menuActuallyDisplayed[1]);
lcd.setCursor(1,2); lcd.println(menuActuallyDisplayed[2]);
lcd.setCursor(1,3); lcd.println(menuActuallyDisplayed[3]);
//Serial.println("lcdLine --> " + String(lcdLine));
if (lcdLine == 4) { //All 4 rows have text
lcd.setCursor(19,0); lcd.write(byte(0)); //upArrow
lcd.setCursor(19,3); lcd.write(byte(1)); //downArrow
}
}
void validateCursorRow(byte row, byte row_new){
//Serial.println("row --> " + String(row));
Serial.println("lcdLine --> " + String(lcdLine));
if (row_new>(lcdLine-1)) {
row_new=lcdLine-1;
} else {
lcd.setCursor(0,row);
lcd.print(" ");
lcd.setCursor(0,row_new);
lcd.write(cursorIcon);
cursorRow = row_new;
cursorRow_new = cursorRow_new;
Serial.println("cursorRow --> " + String(cursorRow));
Serial.println("cursorRow_new --> " + String(cursorRow_new));
}
}