#include <LiquidMenu.h>

//#include <LiquidCrystal.h>

// int led_seleccionado = 0;
bool mode = false;
int x = 0;
TaskHandle_t Tarea0;

int Ciclo = 0;
int Ciclos = 0;
int Ciclos_1 = 0;
int Ciclos_2 = 0;
int cont_1 = 0;
int cont_2 = 0;
int cont_3 = 0;
//int cont = 0;
int cantidad;
//unsigned long tiempo;
//unsigned long tx=0;
int veces = 0;
boolean Giro = true;
boolean a = false;
//unsigned long timer;
unsigned long Pasos = 0;

//Driver Motor Step
const int steppin = 33;
const int dirpin = 32;

//Display 16x2
//LiquidCrystal_I2C lcd(0x27, 16, 2);
const int RS = 23, EN = 22, D4 = 5, D5 = 18, D6 = 19, D7 = 21;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
byte UP[8] = {
  0b00000100,
  0b00001110,
  0b00010101,
  0b00000100,
  0b00000100,
  0b00000100,
  0b00000100,
  0b00000000,
};

byte G1[8]= {
  0b00011,
  0b00100,
  0b01000,
  0b11100,
  0b01000,
  0b00000,
  0b00100,
  0b00011,
};

byte G2[8] = {
  0b11000,
  0b00100,
  0b00010,
  0b00010,
  0b00010,
  0b00010,
  0b00100,
  0b11000,
};


String DER = "----->";
String IZQ = "<-----";
String SENT = DER;

const int Pulsa = 4;

//ENCODER
const int outputA = 15; // CLK
const int outputB = 2; // DT
const int SW = 16;
int incremento = 0;
int incrementos = 0;
int aState;
int aLastState;


//LiquidMenu
//Pantalla 1
LiquidLine linea1(1, 0, "Volumen");
LiquidLine linea2(1, 1, "Encender");
//LiquidLine linea3(1, 0, "Ciclos?");
LiquidLine linea3(1, 0, "Opciones");
//LiquidLine linea4(1, 1, "Hola a todos");
LiquidScreen pantalla1(linea1,linea2,linea3);

//Pantalla linea 1 -> Pantalla 2
LiquidLine linea1_2(1, 0, "Cantidad");
LiquidLine linea2_2(1, 1, "Ciclos?");
LiquidLine linea3_2(1, 0, "Atras");
LiquidScreen pantalla2(linea1_2,linea2_2,linea3_2);

//Pantalla linea 2 -> Pantalla 3
LiquidLine linea1_3(1, 0, "----->");
LiquidLine linea2_3(1, 1, "<-----");
LiquidLine linea3_3(1, 0, "Atras");
LiquidScreen pantalla3(linea1_3,linea2_3,linea3_3);

//Pantalla linea 4 -> Pantalla 4
LiquidLine linea1_4(1, 0, "Opcion 1");
LiquidLine linea2_4(1, 1, "Opcion 2");
LiquidLine linea3_4(1, 1, "Atras");
LiquidScreen pantalla4(linea1_4,linea2_4,linea3_4);


LiquidMenu menu(lcd,pantalla1,pantalla2,pantalla3,pantalla4);

void setup() {

  Serial.begin(115200);
  pinMode(Pulsa, INPUT_PULLUP);
  pinMode(SW,INPUT_PULLUP);
  pinMode(outputA, INPUT);
  pinMode(outputB, INPUT);
  pinMode(steppin, OUTPUT);
  pinMode(dirpin, OUTPUT);

  xTaskCreatePinnedToCore(Motor, "Tarea_0", 1000, NULL, 1, &Tarea0, 0);

  lcd.begin(16, 2);
  lcd.createChar (1, UP);

  linea1.set_focusPosition(Position::LEFT); 
  linea2.set_focusPosition(Position::LEFT); 
  linea3.set_focusPosition(Position::LEFT); 
   
  linea1.attach_function(1, fn_volumen); 
  linea2.attach_function(1, fn_encender);
  linea3.attach_function(1, fn_opciones);
  
  menu.add_screen(pantalla1);
  
//---------------------------------------------

  linea1_2.set_focusPosition(Position::LEFT); 
  linea2_2.set_focusPosition(Position::LEFT); 
  linea3_2.set_focusPosition(Position::LEFT); 
  
  linea1_2.attach_function(1, fn_cantidad);
  linea2_2.attach_function(1, fn_ciclos); 
  linea3_2.attach_function(1, fn_atras);
   
  menu.add_screen(pantalla2);

//---------------------------------------------

  linea1_3.set_focusPosition(Position::LEFT); 
  linea2_3.set_focusPosition(Position::LEFT);
  linea3_3.set_focusPosition(Position::LEFT); 
   
  linea1_3.attach_function(1, fn_onR); 
  linea2_3.attach_function(1, fn_onL);
  linea3_3.attach_function(1, fn_atras); 
    
  menu.add_screen(pantalla3);

//---------------------------------------------

  linea1_4.set_focusPosition(Position::LEFT); 
  linea2_4.set_focusPosition(Position::LEFT); 
  linea3_4.set_focusPosition(Position::LEFT); 
   
  linea1_4.attach_function(1, fn_opcion1); 
  linea2_4.attach_function(1, fn_opcion2);
  linea3_4.attach_function(1, fn_atras);

  menu.add_screen(pantalla4);

//---------------------------------------------
  pantalla1.set_displayLineCount(2);
  pantalla2.set_displayLineCount(2);
  pantalla3.set_displayLineCount(2);
  pantalla4.set_displayLineCount(2);

  menu.set_focusedLine(0);

  menu.update();

}

void loop() {

  selectOption();

  atrass();

  aState = digitalRead(outputA);

  if (aState != aLastState){ 
  
    if (digitalRead(outputB) != aState) { 
      
      incremento++;
      
      if(incremento>1){

        incremento = 0;
        menu.switch_focus(false);

      }
  
    } 
    
    else {
      
      incremento++;

      if(incremento>1){

        incremento = 0;
        menu.switch_focus(true);

      }
      
    }

      menu.update();
      aLastState = aState;

  }


}


//Funciones:::::
void selectOption(){

  if(digitalRead(SW) == LOW){
      menu.call_function(1);
      delay(500);

  }
}


void fn_volumen(){

  menu.change_screen(2);
  menu.set_focusedLine(0);

}


void fn_encender(){

  menu.change_screen(3);
  menu.set_focusedLine(0);

}


void fn_opciones(){

  menu.change_screen(4);
  menu.set_focusedLine(0);

}

void fn_off(){

  menu.change_screen(1);
  menu.set_focusedLine(0);

}

void fn_atras(){

  menu.change_screen(1);
  menu.set_focusedLine(0);

}


void fn_cantidad(){

  Ciclos = 1;
  a = true;
  fn_ciclos();

}

void Motor(void *parameter){
 
  while(1==1){

    while(mode == true || digitalRead(Pulsa) == LOW){

      digitalWrite(dirpin, Giro);

      for(x = 0; x < Pasos; x++){

        digitalWrite(steppin, HIGH); 
        digitalWrite(steppin, LOW); 
        delayMicroseconds(5000);
 
        if(digitalRead(Pulsa) == LOW){
            
            break;
            atrass();
              
        }
      }

      break;

    }

    mode = false;
    vTaskDelay(50);

  }
}


void fn_onR(){

  delay(200);
  Giro = true;
  Pasos = 4294967295;

  while(digitalRead(SW) == HIGH){

    mode = true;
    Serial.println(mode);
  
  }

  mode = false;
  Pasos = 0;
  menu.change_screen(3);
  menu.set_focusedLine(0);

}

void fn_onL(){

  delay(200);
  Giro = false;
  Pasos = 4294967295;

  while(digitalRead(SW) == HIGH){

    mode = true;
    Serial.println(mode);

 }

  mode = false;
  Pasos = 0;
  menu.change_screen(3);
  menu.set_focusedLine(1);
   
}    

void fn_ciclos(){

  delay(500);
  lcd.clear();

  while(digitalRead(SW)==1){

    if(digitalRead(Pulsa) == LOW){

      break;
      atrass();

    }

    lcd.setCursor(0, 0);
    lcd.print("Cant. => ");
    lcd.setCursor(14, 0);
    lcd.print("ml ");
    lcd.setCursor(12, 0);
    lcd.print(cont_1);
    lcd.setCursor(12, 1);
    lcd.write((byte)1);
    lcd.setCursor(11, 0);
    lcd.print(cont_2);
    lcd.setCursor(10, 0);
    lcd.print(cont_3);
    aState = digitalRead(outputA);

    if (aState != aLastState){
      
      if (digitalRead(outputB) != aState) {

        incremento++;

        if(incremento>1){

          incremento = 0;
          cont_1--;

        }

      } 
      else {

        incremento++;

        if(incremento>1){

          incremento = 0;
          cont_1++;

        }
      }

      aLastState = aState;
      //delay(100);
      lcd.clear();

    }
  
    if(cont_1 < 0){
      cont_1 = 0;

    }

    if(cont_1 > 9){
        cont_1 = 9;

    }
  }
    
  if (!digitalRead(SW)==1) {

    delay(250);
    lcd.clear();

    while(digitalRead(SW)==1){


      if(digitalRead(Pulsa) == LOW){

        break;
        atrass();

      }

      lcd.setCursor(0, 0);
      lcd.print("Cant. => ");
      lcd.setCursor(14, 0);
      lcd.print("ml ");
      lcd.setCursor(12, 0);
      lcd.print(cont_1);
      lcd.setCursor(11, 0);
      lcd.print(cont_2);
      lcd.setCursor(11, 1);
      lcd.write((byte)1);
      lcd.setCursor(10, 0);
      lcd.print(cont_3);
      aState = digitalRead(outputA); 

      if (aState != aLastState){

        if (digitalRead(outputB) != aState) {

            incremento++;

          if(incremento>1){

            incremento = 0;
            cont_2--;

          }
        } 

        else {

          incremento++;

          if(incremento>1){
            incremento = 0;
            cont_2++;

          }
        }

        aLastState = aState;
        //delay(100);
        lcd.clear();

      }

      if(cont_2 < 0){
        cont_2 = 0;

      }

      if(cont_2 > 9){
        cont_2 = 9;

      }
    }
  }

  if (!digitalRead(SW)==1) {

    delay(250);
    lcd.clear();

    while(digitalRead(SW)==1 || digitalRead(Pulsa) == LOW){

      if(digitalRead(Pulsa) == LOW){

        break;
        atrass();

      }


      lcd.setCursor(0, 0);
      lcd.print("Cant. => ");
      lcd.setCursor(14, 0);
      lcd.print("ml ");
      lcd.setCursor(12, 0);
      lcd.print(cont_1);
      lcd.setCursor(11, 0);
      lcd.print(cont_2);
      lcd.setCursor(10, 0);
      lcd.print(cont_3);
      lcd.setCursor(10, 1);
      lcd.write((byte)1);
      aState = digitalRead(outputA);

      if (aState != aLastState){

        if (digitalRead(outputB) != aState) { 

          incremento++;

          if(incremento>1){
            incremento = 0;
            cont_3--;

          }
        } 

        else {

          incremento++;

          if(incremento>1){
            incremento = 0;
            cont_3++;

          }
        }

        aLastState = aState;
        //delay(100);
        lcd.clear();

      }

      if(cont_3 < 0){
        cont_3 = 0;

      }

      if(cont_3 > 9){
        cont_3 = 9;

      }
    }
  }

  if (!digitalRead(SW)==1) {

    delay(250);
    lcd.clear();

    while(digitalRead(SW)==1 || digitalRead(Pulsa) == LOW){

      if(digitalRead(Pulsa) == LOW){

        break;
        atrass();

      }

      lcd.setCursor(0, 0);
      lcd.print("Cant. => ");
      lcd.setCursor(14, 0);
      lcd.print("ml ");
      lcd.setCursor(12, 0);
      lcd.print(cont_1);
      lcd.setCursor(11, 0);
      lcd.print(cont_2);
      lcd.setCursor(10, 0);
      lcd.print(cont_3);
      lcd.setCursor(5, 1);
      lcd.print(SENT);
      aState = digitalRead(outputA);

      if (aState != aLastState){

        if (digitalRead(outputB) != aState) {   

          incremento++;

          if(incremento>1){

            incremento = 0;
            SENT = DER;
              
          }
        } 
          
        else {

          incremento++;

          if(incremento>1){

            incremento = 0;
            SENT = IZQ;

          }
        }

        aLastState = aState;
        lcd.clear();
        lcd.setCursor(5, 1);
        lcd.print(SENT);

        if(SENT == IZQ){
          Giro = false; 

        }

        else{
          Giro = true;

        }
      }
    }
  }

  delay(250);
  lcd.clear();

    while(digitalRead(SW)==1 || digitalRead(Pulsa) == LOW){

      if(digitalRead(Pulsa) == LOW){

        break;
        atrass();
        
      }

      lcd.setCursor(0, 0);
      lcd.print("Ciclos => ");
      lcd.setCursor(12, 0);
      lcd.print(Ciclos_1);
      lcd.setCursor(11, 0);
      lcd.print(Ciclos_2);
      aState = digitalRead(outputA);

      if (aState != aLastState){

        if (digitalRead(outputB) != aState) {   

          incremento++;

          if(incremento>1){

            incremento = 0;
            Ciclos_1--;

          }
        } 
        
        else {

          incremento++;

          if(incremento>1){

            incremento = 0;
            Ciclos_1++;

          }
        }

        if(Ciclos_1 < 0){
          Ciclos_1 = 0;

        }

        if(Ciclos_1 > 9){
            Ciclos_1 = 9;

        }
      }
    }

      while(digitalRead(SW)==1 || digitalRead(Pulsa) == LOW){

        if(digitalRead(Pulsa) == LOW){

          break;
          atrass();
          
        }

        lcd.setCursor(0, 0);
        lcd.print("Ciclos => ");
        lcd.setCursor(12, 0);
        lcd.print(Ciclos_1);
        lcd.setCursor(11, 0);
        lcd.print(Ciclos_2);
        aState = digitalRead(outputA);

        if (aState != aLastState){

          if (digitalRead(outputB) != aState) {   

            incremento++;

            if(incremento>1){

              incremento = 0;
              Ciclos_2--;

            }
          } 
          
          else {

            incremento++;

            if(incremento>1){

              incremento = 0;
              Ciclos_2++;

            }
          }

          if(Ciclos_2 < 0){
            Ciclos_2 = 0;

          }

          if(Ciclos_2 > 9){
              Ciclos_2 = 9;

          }
        }

        if (!digitalRead(SW)==1) {

          Pasos = (cont_1 + 10*cont_2 + 100*cont_3);
          mode = true;
          lcd.clear();

          if(a == false){

            while(mode==true){

              lcd.setCursor(0, 0);
              lcd.print("Cant = ");
              lcd.setCursor(8, 0);
              lcd.print(Pasos);
              lcd.setCursor(12, 0);
              lcd.print("C=");
              lcd.setCursor(14,0);
              lcd.print(Ciclos);
              lcd.setCursor(0, 1);
              lcd.print("Progr = ");
              lcd.setCursor(8, 1);
              lcd.print(x);
              lcd.setCursor(12, 1);
              lcd.print("C=");
              lcd.setCursor(14,0);
              lcd.print(Ciclo);
              Serial.println(String(Pasos) + "  " + String(x));

            }
          }

          else{

            while(mode==true){

              lcd.setCursor(0, 0);
              lcd.print("Cant = ");
              lcd.setCursor(8, 0);
              lcd.print(Pasos);
              lcd.setCursor(0, 1);
              lcd.print("Progr = ");
              lcd.setCursor(8, 1);
              lcd.print(x);
              lcd.setCursor(12, 1);
              Serial.println(String(Pasos) + "  " + String(x));

            }

          }
          
          Ciclo = 0;
          mode = false;
          a = false;
          lcd.clear();

        }
      }
}





void fn_opcion1(){
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print(cantidad);
  lcd.setCursor(0, 1);
  lcd.print(millis() / 1000);
  delay(2000);
}


void fn_opcion2(){
  lcd.print("hello, world!");
  lcd.setCursor(0, 1);
  lcd.print(millis() / 1000);
  delay(2000);
}

void atrass(){

  if(digitalRead(Pulsa) == LOW){

    Pasos = 0;
    menu.change_screen(1);
    menu.set_focusedLine(0);
    digitalWrite(steppin,LOW);

    
  }
}

A4988