#include "main.h"

#case

#define S105AN    input(PIN_A0)  // Kuchyne hlavni svetlo severni vypinac
#define R110NJU   input(PIN_A1)  // Roleta koupelna
#define R110NJD   input(PIN_A2)  
#define S104AZ    input(PIN_A3)  // Svetlo laborator vypinac u dveri
#define R104VZU   input(PIN_A4)  // Roleta laborator vychod
#define R104VZD   input(PIN_A5)
#define R104JZU   input(PIN_A6)  // Roleta laborator jih
#define R104JZD   input(PIN_A7)

#define S103JZ    input(PIN_B0)  // Svetlo loznice jih vypinac u dveri
#define S103JN    input(PIN_B1)  // -"- vypinac KAKL 
#define S103JV    input(PIN_B2)  // -"- vypinac mama
#define R103VZU   input(PIN_B3)  // Roleta loznice vypinac u dveri
#define R103VZD   input(PIN_B4)
#define S106AV    input(PIN_B5)  // Hlavni svetlo trucovna vypinac u dveri do kuchyne
#define S107AN    input(PIN_B6)  // Svetlo satna vypinac do chodby
#define S109AJ    input(PIN_B7)  // Svetlo tech. mistnost vypinac do chodby

#define OS101A    PIN_C0   // Spinac svetla v bunecce prizemi
#define OS105V    PIN_C1   // Svetlo nad chlebarnou
#define OS105N    PIN_C2   // Severni svetlo kuchyne

//#define PIN_C3  31763
//#define PIN_C4  31764
//#define PIN_C5  31765

//#define PIN_C6  31766 //TX
//#define PIN_C7  31767 //RX

#define S101AN    input(PIN_D0)  // Hlavni svetlo bunecka severni vypinac bliz u dveri
#define S101AJ    input(PIN_D1)  // Hlavni svetlo bunecka jizni vypinac
#define S105VV    input(PIN_D2)  // Vypinac nad chlebarnou
#define S105NZ    input(PIN_D3)  // Svetlo kuchyne sever vypinac zapadni
#define S105ZZ    input(PIN_D4)  // Svetlo kuchyne zapad vypinac zapadni
#define S102AN    input(PIN_D5)  // Vypinac chodba dole u schodu
#define S110AN    input(PIN_D6)  // Vypinac koupelna sever
#define S108AV    input(PIN_D7)  // Dvere do spajzu

/*
#define PIN_E0  31776
#define PIN_E1  31777
#define PIN_E2  31778
#define PIN_E3  31779
#define PIN_E4  31780
#define PIN_E5  31781
#define PIN_E6  31782
#define PIN_E7  31783

#define PIN_F0  31784
#define PIN_F1  31785
#define PIN_F2  31786
#define PIN_F3  31787
#define PIN_F4  31788
#define PIN_F5  31789
#define PIN_F6  31790
#define PIN_F7  31791

#define PIN_G0  31792
#define PIN_G1  31793
#define PIN_G2  31794
#define PIN_G3  31795
#define PIN_G4  31796
#define PIN_G5  31797

#define PIN_H0  31800
#define PIN_H1  31801
#define PIN_H2  31802
#define PIN_H3  31803
#define PIN_H4  31804
#define PIN_H5  31805
#define PIN_H6  31806
#define PIN_H7  31807

#define PIN_J0  31808
#define PIN_J1  31809
#define PIN_J2  31810
#define PIN_J3  31811
#define PIN_J4  31812
#define PIN_J5  31813
#define PIN_J6  31814
#define PIN_J7  31815
*/

#define LED1 PIN_J7  
#define LED2 PIN_J6 

#define ON(port)  output_low(port);
#define OFF(port) output_high(port);

int1 s105AN;      // Predchozi stav vypinacu
int1 r110NJU;
int1 r110NJD;
int1 s104AZ;
int1 r104VZU;
int1 r104VZD;
int1 r104JZU;
int1 r104JZD;
int1 s103JZ;
int1 s103JN;
int1 s103JV;
int1 r103VZU;
int1 r103VZD;
int1 s106AV;
int1 s107AN;
int1 s109AJ;
int1 s101AN;
int1 s101AJ;
int1 s105VV;
int1 s105NZ;
int1 s105ZZ;
int1 s102AN;
int1 s110AN;
int1 s108AV;

#define send(s) \
{\
   printf("%s\r\n",#s);\
   output_toggle(LED1);\
   delay_ms(100);\  
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   OFF(OS101A);

   s105AN=S105AN;      // Poznamenej vychozi stav vypinacu
   r110NJU=R110NJU;
   r110NJD=R110NJD;
   s104AZ=S104AZ;
   r104VZU=R104VZU;
   r104VZD=R104VZD;
   r104JZU=R104JZU;
   r104JZD=R104JZD;
   s103JZ=S103JZ;
   s103JN=S103JN;
   s103JV=S103JV;
   r103VZU=R103VZU;
   r103VZD=R103VZD;
   s106AV=S106AV;
   s107AN=S107AN;
   s109AJ=S109AJ;
   s101AN=S101AN;
   s101AJ=S101AJ;
   s105VV=S105VV;
   s105NZ=S105NZ;
   s105ZZ=S105ZZ;
   s102AN=S102AN;
   s110AN=S110AN;
   s108AV=S108AV;

   while(TRUE)
   {
      if(s105AN!=S105AN) {s105AN=S105AN; send(S15A);}
      if(r110NJU!=R110NJU) {r110NJU=R110NJU; send(R1aNU);}
      if(r110NJD!=R110NJD) {r110NJD=R110NJD; send(R1aND);}

      if(!S105VV) {output_low(OS105V);} else {output_high(OS105V);}
      if(!S105NZ) {output_low(OS105N);} else {output_high(OS105N);}
      if(s105ZZ!=S105ZZ) {s105ZZ=S105ZZ; send(S15Z);}
      
      if(s104AZ!=S104AZ) {s104AZ=S104AZ; send(S14A);}
      if(r104VZU!=R104VZU) {r104VZU=R104VZU; send(R14VU);}
      if(r104VZD!=R104VZD) {r104VZD=R104VZD; send(R14VD);}
      if(r104JZU!=R104JZU) {r104JZU=R104JZU; send(R14JU);}
      if(r104JZD!=R104JZD) {r104JZD=R104JZD; send(R14JD);}
      
      if(s103JZ!=S103JZ) {s103JZ=S103JZ; send(S13A);}
      if(s103JN!=S103JN) {s103JN=S103JN; send(S13A);}
      if(s103JV!=S103JV) {s103JV=S103JV; send(S13A);}
      if(r103VZU!=R103VZU) {r103VZU=R103VZU; send(R13VU);}
      if(r103VZD!=R103VZD) {r103VZD=R103VZD; send(R13VD);}
  
      if(s106AV!=S106AV) {s106AV=S106AV; send(S16A);}
      if(s107AN!=S107AN) {s107AN=S107AN; send(S17A);}
      if(s109AJ!=S109AJ) {s109AJ=S109AJ; send(S19A);}
      
      if(s101AN!=S101AN) {s101AN=S101AN; output_toggle(OS101A); delay_ms(100);}
      if(s101AJ!=S101AJ) {s101AJ=S101AJ; output_toggle(OS101A); delay_ms(100);}
      
      if(s102AN!=S102AN) {s102AN=S102AN; send(S12A);}
      if(s110AN!=S110AN) {s110AN=S110AN; send(S1aA);}
      if(s108AV!=S108AV) {s108AV=S108AV; send(S18A);}

      if(get_timer0()>65000) {output_toggle(LED2); set_timer0(0);}
   }
}