/*
Ovladani domu rozvadec 1. n.p. pod schodama

Syntaxe prikazu:

<Roleta/Svetlo><#patro><#mistnost><Nord/Jih/Vychod/Zapad/A/B><Up/Down>
R2aNU - roleta, 2. n.p., mistnost 10, sever, nahoru
S12A  - svetlo, 1. n.p., mistnost 2, prostredni

Rozmisteni desek:

 [IN B]        [IN D]                         
         [CPU]    
 [IN A]  [CAN] [IN E] [IN F] 
  
*/

#include "main.h"

#case

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

#define S103JZ    input(PIN_B0)  // A Svetlo loznice jih vypinac u dveri
#define S103JN    input(PIN_B1)  // B -"- vypinac KAKL 
#define S103JV    input(PIN_B2)  // C -"- vypinac mama
#define R103VZU   input(PIN_B3)  // D Roleta loznice vypinac u dveri
#define R103VZD   input(PIN_B4)  // E
#define S110AZ    input(PIN_B5)  // F Vypinac koupelna sprcha
#define S107AN    input(PIN_B6)  // G Svetlo satna vypinac do chodby
#define S109AJ    input(PIN_B7)  // H 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)  // A Svetlo bunecka severni vypinac bliz u dveri
#define S101AJ    input(PIN_D1)  // B Svetlo bunecka jizni vypinac
#define S105VV    input(PIN_D2)  // C Vypinac nad chlebarnou
#define S105NZ    input(PIN_D3)  // D Svetlo kuchyne sever vypinac zapadni
#define S105ZZ    input(PIN_D4)  // E Svetlo kuchyne zapad vypinac zapadni
#define S102AN    input(PIN_D5)  // F Svetlo chodba vypinac dole u vchodu
#define S110AV    input(PIN_D6)  // G Vypinac koupelna u dveri
#define S100AV    input(PIN_D7)  // H Svetlo prujezd venkovni vypinac bunecka sever dal od dveri

#define R105JJD   input(PIN_E0)  // A Roleta kuchyn u varice vypinac u dveri do trucovnu u kliky
#define R105JJU   input(PIN_E1)  // B
#define R103VND   input(PIN_E2)  // C Roleta loznice vypinac KAKL
#define R103VNU   input(PIN_E3)  // D
#define R103VVD   input(PIN_E4)  // E Roleta loznice vypinac mama
#define R103VVU   input(PIN_E5)  // F
#define S105AV    input(PIN_E6)  // G Kuchyne hlavni svetlo vypinac u dveri na terasu
#define S102AJ    input(PIN_E7)  // H Svetlo chodba vypinac dole u schodu

#define S106AV    input(PIN_F0)  // A Hlavni svetlo trucovna vypinac u dveri do kuchyne
#define R106JVD   input(PIN_F1)  // B Roleta trucovna jih vypinac u dveri do kuchyne 
#define R106JVU   input(PIN_F2)  // C
#define R106ZVD   input(PIN_F3)  // D Roleta trucovna zapad vypinac u dveri do kuchyne
#define R106ZVU   input(PIN_F4)  // E
#define S106AN    input(PIN_F5)  // F Hlavni svetlo trucovna vypinac u dveri do satny
#define S107AJ    input(PIN_F6)  // G Svetlo satna vypinac u trucovny
#define S109AN    input(PIN_F7)  // H Svetlo technologicka mistnost vypinac u dveri do prujezdu

/*
#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 s105AV;
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 s107AN;
int1 s109AJ;
int1 s101AN;
int1 s101AJ;
int1 s105VV;
int1 s105NZ;
int1 s105ZZ;
int1 s102AN;
int1 s102AJ;
int1 s110AV;
int1 s110AZ;
int1 r106JVU;
int1 r106JVD;
int1 r106ZVU;
int1 r106ZVD;
int1 s106AV;
int1 s106AN;
int1 s107AJ;
int1 s109AN;
int1 s100AV;
int1 r105JJD;
int1 r105JJU;
int1 r103VND;
int1 r103VNU;
int1 r103VVD;
int1 r103VVU;

#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);

   {
      int n;
      
      for(n=0;n<10;n++)
      {
         output_low(LED1); // Indikace restartu (pockame na nabiti kondenzatoru ve vstupnich obvodech)
         delay_ms(100);   
         output_high(LED1);  
         delay_ms(100);
         restart_wdt();
      }
   }


   s105AN=S105AN;      // Poznamenej vychozi stav vypinacu
   s105AV=S105AV;
   r110NJU=R110NJU;
   r110NJD=R110NJD;
   s104AZ=S104AZ;
   r104VZU=R104VZU;
   r104VZD=R104VZD;
   r104JZU=R104JZU;
   r104JZD=R104JZD;
   s103JZ=S103JZ;
   s103JN=S103JN;
   s103JV=S103JV;
   r103VZU=R103VZU;
   r103VZD=R103VZD;
   s107AN=S107AN;
   s109AJ=S109AJ;
   s101AN=S101AN;
   s101AJ=S101AJ;
   s105VV=S105VV;
   s105NZ=S105NZ;
   s105ZZ=S105ZZ;
   s102AN=S102AN;
   s102AJ=S102AJ;
   s110AV=S110AV;
   s110AZ=S110AZ;
   r106JVU=R106JVU;
   r106JVD=R106JVD;
   r106ZVU=R106ZVU;
   r106ZVD=R106ZVD;
   s106AV=S106AV;
   s106AN=S106AN;
   s107AJ=S107AJ;
   s109AN=S109AN;
   s100AV=S100AV;
   r105JJD=R105JJD;
   r105JJU=R105JJU;
   r103VND=R103VND;
   r103VNU=R103VNU;
   r103VVD=R103VVD;
   r103VVU=R103VVU;
 
   while(TRUE)
   {
      if(s105AN!=S105AN) {s105AN=S105AN; send(S15A);}
      if(s105AV!=S105AV) {s105AV=S105AV; 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(r103VND!=R103VND) {r103VND=R103VND; send(R13VD);}
      if(r103VNU!=R103VNU) {r103VNU=R103VNU; send(R13VU);}
      if(r103VVD!=R103VVD) {r103VVD=R103VVD; send(R13VD);}
      if(r103VVU!=R103VVU) {r103VVU=R103VVU; send(R13VU);}
  
      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(s102AJ!=S102AJ) {s102AJ=S102AJ; send(S12A);}
      if(s110AV!=S110AV) {s110AV=S110AV; send(S1aA);}
      if(s110AZ!=S110AZ) {s110AZ=S110AZ; send(S1aB);}

      if(r106JVU!=R106JVU) {r106JVU=R106JVU; send(R16JU);}
      if(r106JVD!=R106JVD) {r106JVD=R106JVD; send(R16JD);}
      if(r106ZVU!=R106ZVU) {r106ZVU=R106ZVU; send(R16ZU);}
      if(r106ZVD!=R106ZVD) {r106ZVD=R106ZVD; send(R16ZD);}
      if(s106AV!=S106AV) {s106AV=S106AV; send(S16A);}
      if(s106AN!=S106AN) {s106AN=S106AN; send(S16A);}
      if(s107AJ!=S107AJ) {s107AJ=S107AJ; send(S17A);}
      if(s109AN!=S109AN) {s109AN=S109AN; send(S19A);}
      if(s100AV!=S100AV) {s100AV=S100AV; send(S10A);}

      if(r105JJD!=R105JJD) {r105JJD=R105JJD; send(R15JD);}
      if(r105JJU!=R105JJU) {r105JJU=R105JJU; send(R15JU);}

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