#include "D:\Purga.h"
#define use_portb_lcd true
#include <lcd.c>

#Byte Puerto_C = 0x07      // Puerto de Entrada C

#bit ENTER = 0x07.0        // Boton ENTER
#bit UP = 0x07.1           // Boton UP
#bit DOWN = 0x07.2         // Boton DOWN

#Byte Puerto_D = 0x08      // Puerto de Salida D
int i,j=0,flag = 0;


void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);


//******************************************************************************
//***********************Configuracion de los puertos***************************
//******************************************************************************

   set_tris_c(255);        // Puerto C como entrada para los botones
   set_tris_d(0);          // Puerto D como salida para las valvulas y bomba
   Puerto_D = 0;
//******************************************************************************
//******************************************************************************
//******************************************************************************

//******************************************************************************
//****************** Mensaje de Sistema de Limpieza ****************************
//******************************************************************************
   
INICIO:
   lcd_init();
   printf(lcd_putc,"SISTEMA SOLVENTE\nPRESIONE ENTER");
   
//******************************************************************************
//******************************************************************************
//******************************************************************************

   do{
      if(ENTER == 1)       //  Presion Enter ?
      {
       do{
           delay_ms(20);      // Eliminar rebotes
           if(Puerto_C == 0)  // Esperamos que suelte Enter
           {
            printf(lcd_putc,"\fINGRESANDO");
            delay_ms(500);
            printf(lcd_putc,".");
            delay_ms(500);
            printf(lcd_putc,".");
            delay_ms(500);
            printf(lcd_putc,".");
            delay_ms(500);
            printf(lcd_putc,"\fLIMP. AUTOMATICA\nLIMP. MANUAL");
            flag=1;           // variable para q salga del while
           }
         }while(flag<1);
      }
     }while(flag<1);
     
//******************************************************************************
//******************* Seleccin Manual / Automtico ****************************
//******************************************************************************    
SELECCION:
flag = 0;
   do{
      if(ENTER == 1)       // Ingres Limpieza Automtica
      {
       do{
          delay_ms(20);
          if(ENTER == 0)   // Solto Enter
          {
           printf(lcd_putc,"\fCONFIGURE TIEMPO");           
           goto LIMPIEZA_AUTO;
           //flag=1;
          }          
         }while(true);
      }
      else if(DOWN == 1)  // Seleccion Limpieza Manual, mas no Ingres a ella
      {
         // Aqui tiene 2 Opciones: Volver a subir o presionar Enter
         if(UP == 1) // Presion UP Seleccion Limpieza Automtica, mas no ingres a ella
         {
          do {
              delay_ms(20);
              if(UP == 0)      // Solto UP
              {
               printf(lcd_putc,"\fLimp. Automtica\nLimp. Manual");
               goto SELECCION;
              }
            }while(true);
      }
      else if(ENTER == 1)  // Ingres a Limpieza Manual
      {
          do {
              if(ENTER == 0) // Solt Enter
              {
               printf(lcd_putc,"\fNegro\nCyan"); //La secuencia debe continuar con los demas colores
              }
             }while(true); 
      }
       
     
      }
     }while(flag<1);
     goto INICIO;
     


//******************************************************************************
//**********************Funcion de limpieza automtica**************************
//******************************************************************************

LIMPIEZA_AUTO:

   delay_ms(900);
   printf(lcd_putc,"\f15 SEGUNDOS\n30 SEGUNDOS");
      
    do{
      if(ENTER==1)      // Ingres a 15 Segundos
      {
       do{
         if(ENTER == 0) // Solt Enter
         {
         j=15;
         goto LIMPIEZA;        
         }
         }while(true);
      }
      }while(true);
   

LIMPIEZA:

   Puerto_D = 129;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   Puerto_D = 130;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   Puerto_D = 132;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   Puerto_D = 136;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   Puerto_D = 144;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   Puerto_D = 160;
      for(i=1;i<=j;i++)        // j da el tiempo de retardo
   {
      delay_ms(500);
      delay_ms(500);
   }
   j=0;
   goto INICIO;
}