#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);  

enum{
    MENUPPAL,
    MENULED1,
    MENULED2,
    MENULED3
};

#define led1  23
#define led2  19
#define led3  18
#define BTN_UP        34
#define BTN_DOWN      35
#define BTN_SELECT    32

int op=MENUPPAL,opSelect=1, clearFlag = 1;

void menuPPAL();
void menuLED1();
void menuLED2();
void menuLED3();


void setup() {
  
  pinMode(34, INPUT);
  pinMode(35, INPUT);
  pinMode(32, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  lcd.init();
  lcd.backlight();



  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  lcd.print("Sensor1: ");
}

void loop() {
  
  switch(op){

      case MENUPPAL: menuPPAL(); break;
      case MENULED1: menuLED1(); break;

  }
  if(clearFlag == 1){
          lcd.clear();
          clearFlag = 0;
  }


  delay(2000); // this speeds up the simulation
}

void menuPPAL(){
     
     
     lcd.setCursor(5,0); lcd.print("MENU PPAL");
     lcd.setCursor(0,1); lcd.print("1.LED1    2.LED2    ");
     lcd.setCursor(0,2); lcd.print("3.LED3    OP:       ");
     lcd.setCursor(13,2); lcd.print(opSelect);

     if( digitalRead(BTN_UP) == HIGH ){
          while( digitalRead(BTN_UP) == HIGH );
          opSelect++;
          if(opSelect>3)
             opSelect = 1;
     }

      if( digitalRead(BTN_DOWN) == HIGH ){
          while( digitalRead(BTN_DOWN) == HIGH );
          opSelect--;
          if(opSelect<1)
             opSelect = 3;
     }

     if( digitalRead(BTN_SELECT) == HIGH ){
          while( digitalRead(BTN_SELECT) == HIGH );
          
          switch(opSelect){

              case MENULED1: op=MENULED1;  break;
              case MENULED2: op=MENULED2; break;
              case MENULED3: op=MENULED3; break;

          }
          clearFlag= 1;
          lcd.clear();
     }

     switch(opSelect){
        case 1: lcd.setCursor(6,1); lcd.print("<");
                lcd.setCursor(16,1); lcd.print(" ");
                lcd.setCursor(6,2); lcd.print(" ");
                break;
        case 2: lcd.setCursor(6,1); lcd.print(" ");
                lcd.setCursor(16,1); lcd.print("<");
                lcd.setCursor(6,2); lcd.print(" ");
                break;
        case 3: lcd.setCursor(6,1); lcd.print(" ");
                lcd.setCursor(16,1); lcd.print(" ");
                lcd.setCursor(6,2); lcd.print("<");
                break;

     } 

}
void menuLED1(){
     lcd.setCursor(5,0); lcd.print("MENU LED1");
     lcd.setCursor(0,1); lcd.print("1.ON    2.OFF    ");
     lcd.setCursor(0,2); lcd.print("3.BACK    OP:       ");
     lcd.setCursor(13,2); lcd.print(opSelect); 

    if( digitalRead(BTN_UP) == HIGH ){
          while( digitalRead(BTN_UP) == HIGH );
          opSelect++;
          if(opSelect>3)
             opSelect = 1;
     }

      if( digitalRead(BTN_DOWN) == HIGH ){
          while( digitalRead(BTN_DOWN) == HIGH );
          opSelect--;
          if(opSelect<1)
             opSelect = 3;
     }

     if( digitalRead(BTN_SELECT) == HIGH ){
          while( digitalRead(BTN_SELECT) == HIGH );
          
          switch(opSelect){

              case 1: digitalWrite(led1, HIGH);  break;
              case 2: digitalWrite(led1, LOW); break;
              case 3: op=MENUPPAL; break;

          }
          clearFlag= 1;
          lcd.clear();
     } 

    switch(opSelect){
        case 1: lcd.setCursor(6,1); lcd.print("<");
                lcd.setCursor(16,1); lcd.print(" ");
                lcd.setCursor(6,2); lcd.print(" ");
                break;
        case 2: lcd.setCursor(6,1); lcd.print(" ");
                lcd.setCursor(16,1); lcd.print("<");
                lcd.setCursor(6,2); lcd.print(" ");
                break;
        case 3: lcd.setCursor(6,1); lcd.print(" ");
                lcd.setCursor(16,1); lcd.print(" ");
                lcd.setCursor(6,2); lcd.print("<");
                break;

     }
    

}
void menuLED2(){

}
void menuLED3(){

}