#include "main.h"

#use fast_io(B)
#use fast_io(A)

#define Status_Moving         PIN_A1   //A kapu mozog
#define Status_Alarm          PIN_A0   //A kapu mozogni fog 
#define Status_Opening        PIN_A7   //Kapu nyitás 
#define Status_Closing        PIN_A6   //Kapu zárás
#define Status_Opened         PIN_B7   //A kapu nyitva van
#define Status_Full_Opened    PIN_B6   //A kapu teljesen nyitva van
#define Status_Full_Closed    PIN_B5   //A kapu teljesen zárva van
#define Status_Button         PIN_B4   //Gomb állapota

#define Input_Button          PIN_A3   //Gomb
#define Input_Close           PIN_A4   //Kapu zárva Reed
#define Input_Open            PIN_A5   //Kapu nyitva Reed

#define Motor_Dir             PIN_B2   //Motor irány
#define Motor_Move            PIN_B3   //Motor indítás


//Direction: 1 - Nyit; 0 - Zar
int1 direction, moving, button_released;

//PIC inicializalasa
void initPic() {
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   //IO iranyok beallitasa
   set_tris_b(0x00); //00000000
   set_tris_a(0x3C); //00111100
   
   //Minden kimenet alacsony szintre huzasa
   output_low(Motor_Move);   
   output_low(Motor_Dir);   
   output_low(Status_Button);   
   output_low(Status_Opening);
   output_low(Status_Closing);
   output_low(Status_Alarm);
   output_low(Status_Moving);
   output_low(Status_Alarm);   
   output_low(Status_Opened);   
   output_low(Status_Full_Opened);   
   output_low(Status_Full_Closed);   

}

//Motor inditasi procedura
void startMotor() {
   if ( moving )
      return;
      
   //Státuszok és vezérlés alaphelyzetbe állítása
   output_low(Status_Opening);
   output_low(Status_Closing);
   output_low(Status_Alarm);
   output_low(Status_Moving);
   output_low(Motor_Move);
   
   //Jelzem, hogy mozogni fog a kapu
   output_high(Status_Alarm);
   
   //Jelzem a mozgas iranyat
   if ( direction ) {
      output_high(Status_Opening);
   } else {
      output_high(Status_Closing);
   }
   //Irany beallitasa
   output_bit(Motor_Dir, direction);

   delay_ms(4000);

   //Jelzem, hogy a kapu mozog
   output_high(Status_Moving);
   delay_ms(1000);
   
   //Motor indítása
   output_high(Motor_Move);
   moving = 1;
}

void stopMotor() {
   if (!moving)
      return;
      
   //Motor megallitasa
   output_low(Motor_Move);
   delay_ms(100);
   //Energiatakarekossagbol rele elengedese
   output_low(Motor_Dir);
   
   //Visszajelzok kikapcsolasa
   output_low(Status_Opening);
   output_low(Status_Closing);
   output_low(Status_Alarm);
   output_low(Status_Moving);
   
   //forgas itany negalasa
   direction = !direction;
   moving = 0;
}
 
void main()
{
   initPic();
   moving = 0;
   button_released = true;
   
   do {
      output_bit(Status_Opened, input(Input_Close));       //Ha a záró reed nem jelez, akkor a kapu valamennyire nyitva van
      output_bit(Status_Full_Opened, !input(Input_Open));    //Nyitva reed jelzése
      output_bit(Status_Full_Closed, !input(Input_Close));   //Zárva reed jelzése
      output_bit(Status_Button, !input(Input_Button));

      //Gomb indit / megallit
      if ( !input(Input_Button) && button_released ) {
         button_released = false;
         
         if ( moving ) {
            stopMotor();
            delay_ms(300); //Prell mentesites
         } else {
            startMotor();
         }
      }
      
      if ( input(Input_Button) ) {
         button_released = true;
      }
      
      //Ha mozog a kapu akkor a vegallasokra figyelek
      if ( moving ) {
         //Zaro reed ha zaro iranyba mozgok
         if ( !input(Input_Close) && !direction ) {
            stopMotor();
         }

         //Nyito reed ha nyito iranyba mozgok
         if ( !input(Input_Open) && direction ) {
            stopMotor();
         }
      }
   } while( true);
}
