//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////                                                                                                      //////////////////
//////////////////   Actualment funcionant el menu inicial i les dues rutines de mode apagat i mode funcionament per    //////////////////
//////////////////   pantalla visualitzacio de noms de variables a mostrar amb modifficacions de totes les consignes.   //////////////////
//////////////////   Mode manual amb sortides activades i password d'entrada. 2 noves rutines per la mitja de les       //////////////////
//////////////////   temperatures                                          ultima modificació.   11 de març             //////////////////
//////////////////                                                                                                      //////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#include <16F877.h> 
#device adc = 10
#FUSES XT,NOWDT
#FUSES
#use delay (clock=4000000)
#include <LCD416portd.C>
#use fast_io(A)                           //Optimizamos E/S del PORTA
#use fast_io(B)                           //Optimizamos E/S del PORTB
#use fast_io(C)                           //Optimizamos E/S del PORTC
#include <math.h>   

// DECLARACIO DE FUNCIONS

void pass(void);
void comppass(void);
void modeauto(void);
void modeman (void);
void main (void);
void menu (void);
void menuman    (void);
void adentrades (void);
void entradatempint(void);
void entradatempext(void);

                                          //Variables on son guardats els valors
                                          //de les entrades analogiques.
char n,m;

int16 tint1,tint2,tint3;
int16 text1,text2;
float tempint,text;
float humit;
float vent;
float llum;
float co2;                            
float consti;                             //Variables de les consignes. Al realitzar modificacions en el LCD, es kedaran                                          
float conshum;                            //sempre guardades aki
float consco2;
float consllum;
int c1,c2,c3,c4,c5,c6,c7,c8,p;            //Variables del password
short c9;

//PROGRAMA PRINCIPAL

void main(){

char n;                                 //variables globals
n=0;
c9=0;
consti=25;
conshum=60;
consco2=0;
consllum=100;
c5=0;                                     //variables on possar pasword inicial
c6=0;
c7=0;
c8=0;

set_tris_A(0xFF);                         // portA com entrada
set_tris_b(0xFF);                         // portb com entrada
set_tris_c(0x00);                         // portc com sortida
setup_adc(adc_clock_internal);
setup_adc_ports(all_analog); 
output_low(pin_B0);                       //Sortides a 0
output_low(pin_B1);
output_low(pin_B2);
output_low(pin_B3);
output_low(pin_B4);

lcd_init();
lcd_gotoxy(2,1);
printf(lcd_putc,"L'HIVERNACLE");           
delay_ms(200); 
lcd_gotoxy(1,3);
printf(lcd_putc,"Albert sans");                
delay_ms(100);    
lcd_gotoxy(1,4);
printf(lcd_putc,"Oriol Font");          
delay_ms(200); 
lcd_putc('\f');



do{
   while (c9==0){
   pass();
   }
   do{
      modeauto ();
   }while(input(PIN_B2)==0 && c9==1);             
   Lcd_putc("\f");
      do{
         modeman();
      }while(input(PIN_B2)==0 && c9==1);
   Lcd_putc("\f");
   
} while(TRUE);                            //repetir el bucle sempre de manera que
                                          //sempre pasa de auto a manual i a l'inreves
                                          //si password correcte
}


//RUTINA DEL PASSWORD

void pass(void){
do{

printf(lcd_putc,"\fIntroduir clau");
lcd_gotoxy(1,3);
printf(lcd_putc,"%u%u%u%u",c1 c2 c3 c4);
delay_ms(20);

   if (input(PIN_B0)==1){
   delay_ms(30);
   p++;
   delay_ms(50);     
   }
   if (p==10){
   p=0;    
   }
   if (input(PIN_B5)==1){
      comppass();
   }

switch (p){
      case 0:
         if (input(PIN_B3)==1){
         delay_ms(20);
         c1++;
         }
         if (input(PIN_B4)==1){
         delay_ms(20);
         c1--;      
         }
         if (c1==10 || c1==255){
         c1=0;
         }
         break;
      case 1:
         if (input(PIN_B3)==1){
         delay_ms(20);
         c2++;
         }
         if (input(PIN_B4)==1){
         delay_ms(20);
         c2--;
         }
         if (c2==10 || c2==255){
         c2=0;
         }
         break;
      case 2:
         if (input(PIN_B3)==1){
         delay_ms(20);
         c3++;
         }
         if (input(PIN_B4)==1){
         delay_ms(20);
         c3--;
         }
         if (c3==10 || c3==255){
         c3=0;
         }
         break;
      case 3:
         if (input(PIN_B3)==1){
         delay_ms(20);
         c4++;
         }
         if (input(PIN_B4)==1){
         delay_ms(20);
         c4--;
         }
         if (c4==10 || c4==255){
         c4=0;
         }
         break;
      case 4:
         p=0;
         break;
     
}
}while (c9==0);
}

//RUTINA COMPARAR PASSWORD
void comppass(){                          
if ((c1==c5 && c2==c6 && c3==c7 && c4==c8)==1){      
   c9=1;
}
}


//RUTINA DE MODE MANUAL

void modeman (){


lcd_gotoxy(2,1);
printf(lcd_putc,"\fMode manual");
lcd_gotoxy(10,4);
printf(lcd_putc,"--> OK");
delay_ms(50);

if(input(PIN_B5)==1){                     //Pasar a menu principal si OK=1
      do{
      delay_ms(50);
      menuman();
      }while(input(pin_B2)==0);           //Realitzar rutina menu mentre no apretis
                                          //el boto de ON/OFF
   lcd_putc("\f");
}
}

//RUTINA DE MODE AUTO

void modeauto(void){


lcd_gotoxy(2,1);
printf(lcd_putc,"\fMode automatic");
lcd_gotoxy(10,4);
printf(lcd_putc,"--> OK");
delay_ms(50);


if(input(PIN_B5)==1){                     //Pasar a menu principal si OK=1
      do{
      delay_ms(50);
      menu();
      }while(input(pin_B2)==0);           //Realitzar rutina menu mentre no apretis
                                          //el boto de ON/OFF
   lcd_putc("\f");
}
}


//RUTINA ADQUISICIO D'ENTRADES

void adentrades (void){

set_adc_channel(2);         
delay_ms(10);   
humit=read_adc();

set_adc_channel(4);         
delay_ms(10);   
vent=read_adc();

set_adc_channel(5);         
delay_ms(10);   
llum=read_adc();

set_adc_channel(6);         
delay_ms(10);   
co2=read_adc();


}

void entradatempint(void){
set_adc_channel(0);         
delay_ms(10);   
tint1=read_adc();
delay_ms(10);   
tint2=read_adc();
delay_ms(10);   
tint3=read_adc();
}

void entradatempext(void){
set_adc_channel(1);
delay_ms(10);   
text1=read_adc();
delay_ms(10);   
text2=read_adc();
}

//RUTINA DE MENUS GENERALS COM LA TEMPERATURA INTERNA
//EXTERNA, HUMITAT, CO2, VELOCITAT VENT I LLUMINOSITAT

void menu(void){
do{
   if (input(PIN_B0)==1){
      delay_ms(30);
      n++;
      delay_ms(50);     
      }
   if (input(PIN_B1)==1){
      n=0;
      delay_ms(50);     
      }

switch (n){
      case 0:{
         float y,t,t1,t2,t3;                             //Conversio a tensio de l'entrada
         entradatempint();
         t1 =5.0 * (tint1 / 1024.0);              //formula de la conversio
         t2 =5.0 * (tint2 / 1024.0);
         t3 =5.0 * (tint3 / 1024.0);
         t=(t1+t2+t3)/3;                           //mitja de les 3 mesures
         y=t*100/6;
         
         t=y*10;                                //multiplicar valor per 10 
         y=floor(t);                            //truncar valor
         tempint=y/10;                          //dividir per 10
  

         printf(lcd_putc,"\fTemperatura");
         printf(lcd_putc,"\ninterna %1.1fC",tempint);
         lcd_gotoxy(1,4);
         printf(lcd_putc,"modificar -->");
         delay_ms(20);
            if (input(PIN_B5)==1){
               delay_ms(20);
                  do{  
                     printf(lcd_putc,"\fTemperatura");
                     printf(lcd_putc,"\ndesitjada");
                     lcd_gotoxy(1,4);
                     printf(lcd_putc,"%1.1f",consti);
                     delay_ms(50);
   
                        if (input(PIN_B3)==1){
                           delay_ms(20);
                           consti=consti+0.1;  
                           }
                        if (input(PIN_B4)==1){
                           delay_ms(20);
                           consti=consti+0.1;
                           }
      
               }while (input(PIN_B5)==0);
            }
         }
         break;
         
      case 1:{
         float y,t,t1,t2;      
         entradatempext();
         t1 =5.0 * (text1 / 1024.0);              //formula de la conversio
         t2 =5.0 * (text2 / 1024.0);
         t=(t1+t2)/2; 
         y =16*t-10;                     
   
         t=y*10;                                //multiplicar valor per 10
         y=floor(t);                            //truncar valor
         text=y/10;                             //dividir per 10
         printf(lcd_putc,"\fTemperatura");
         printf(lcd_putc,"\nexterna %04.1fC", text);
         }
         break;
         
      case 2:{
         printf(lcd_putc,"\fHumitat");     
         lcd_gotoxy(1,4);
         printf(lcd_putc,"modificar -->");
         delay_ms(50);
            if (input(PIN_B5)==1){
               delay_ms(20);
                  do{ 
                     printf(lcd_putc,"\fHumitat");
                     printf(lcd_putc,"\ndesitjada");
                     lcd_gotoxy(1,3);
                     printf(lcd_putc,"%1.1f",conshum);
                     delay_ms(50);
                        if (input(PIN_B3)==1){
                           delay_ms(20);
                           conshum=conshum+1;
                           }
                        if (input(PIN_B4)==1){
                           delay_ms(20);
                           conshum=conshum+1;
                        }
      
                  }while (input(PIN_B5)==0);
            }
         }
         break;
         
      case 3:{
         printf(lcd_putc,"\fVelocitat del"); 
         printf(lcd_putc,"\nvent");
         delay_ms(50);
         }
         break;
         
      case 4:{
         printf(lcd_putc,"\fLluminositat");  
         lcd_gotoxy(1,4);
         printf(lcd_putc,"modificar -->");
         delay_ms(50);
            if (input(PIN_B5)==1){
               delay_ms(20);
                  do{
                     printf(lcd_putc,"\fLluminositat");
                     printf(lcd_putc,"\ndesitjada");
                     lcd_gotoxy(1,4);
                     printf(lcd_putc,"%1.1f",consllum);
                     delay_ms(50);
                        if (input(PIN_B3)==1){
                           delay_ms(20);
                           consllum=consllum+10;
                        }
                        if (input(PIN_B4)==1){
                        delay_ms(20);
                        consllum=consllum+10;
                        }
                     
                  }while (input(PIN_B5)==0);
            }
         }
         break;
         
      case 5:{
         printf(lcd_putc,"\fNivell de");  
         printf(lcd_putc,"\nco2");
         lcd_gotoxy(1,4);
         printf(lcd_putc,"modificar -->");
         delay_ms(50); 
            if (input(PIN_B5)==1){
               delay_ms(20);
               do{
                  printf(lcd_putc,"\fCo2");
                  printf(lcd_putc,"\ndesitjat");
                  lcd_gotoxy(1,4);
                  printf(lcd_putc,"%1.1f",consco2);
                  delay_ms(50);
                     if (input(PIN_B3)==1){
                        delay_ms(20);
                        consco2=consco2+1;
                        }
                     if (input(PIN_B4)==1){
                        delay_ms(20);
                        consco2=consco2+1;
                        }
               }while (input(PIN_B5)==0);
            }
         }
         break;
         
      case 6:
         n=0;
         break;
}
}while (input(PIN_B2)==0);
}

//RUTINA MENU MANUAL

void menuman (){
do{
   if (input(PIN_B0)==1){
   delay_ms(30);
   m++;
   delay_ms(50);     
   }
   if (input(PIN_B1)==1){
   m=0;
   delay_ms(50);     
   }
switch (m){
      case 0:{
      printf(lcd_putc,"\fActivar");
      printf(lcd_putc,"\nventilador");
      lcd_gotoxy(10,4);
      printf(lcd_putc,"OK -->");
      output_low(PIN_C1);
         do{
            if (input(PIN_B5)==1){            
            output_high(PIN_C1);
            }
      }while (input(PIN_B5)==0 && input(PIN_B0)==0 && input(PIN_B1)==0 && input(PIN_B2)==0);
      delay_ms(20);
      }
      break;
         
      case 1:{
      printf(lcd_putc,"\fActivar");
      printf(lcd_putc,"\nescalfador"); 
      lcd_gotoxy(10,4);
      printf(lcd_putc,"OK -->");
      output_low(PIN_C4);
         do{
            if (input(PIN_B5)==1){
            output_high(PIN_C4);
            }
         }while (input(PIN_B5)==0 && input(PIN_B0)==0 && input(PIN_B1)==0 && input(PIN_B2)==0);
      delay_ms(20);
      }
      break;
         
      case 2:{
      printf(lcd_putc,"\fActivar");
      printf(lcd_putc,"\nlampades"); 
      lcd_gotoxy(10,4);
      printf(lcd_putc,"OK -->"); 
      output_low(PIN_C5);
         do{
            if (input(PIN_B5)==1){
            output_high(PIN_C5);
            }
         }while (input(PIN_B5)==0 && input(PIN_B0)==0 && input(PIN_B1)==0 && input(PIN_B2)==0);
      delay_ms(20);
      }
      break;
         
      case 3:{
         printf(lcd_putc,"\fActivar");
         printf(lcd_putc,"\nnebulitzadors");
         lcd_gotoxy(10,4);
         printf(lcd_putc,"OK -->"); 
         output_low(PIN_C3);
            do{
               if (input(PIN_B5)==1){
               output_high(PIN_C3);
               }
            }while (input(PIN_B5)==0 && input(PIN_B0)==0 && input(PIN_B1)==0 && input(PIN_B2)==0);
      delay_ms(20);
      }
      break;
         
      case 4:
         m=0;
         break;
}
}while (input(PIN_B2)==0);
}