/*
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)  // 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)  // 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 S    input(PIN_B5)  // F 
#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)  // 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 S   input(PIN_E0)  // A
//#define S   input(PIN_E1)  // B
//#define S   input(PIN_E2)  // C
//#define S   input(PIN_E3)  // D
//#define S   input(PIN_E4)  // E
//#define S   input(PIN_E5)  // F
//#define S   input(PIN_E6)  // G
//#define S   input(PIN_E7)  // H

#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 S107AJ    input(PIN_F5)  // F Svetlo satna vypinac u trucovny
#define S109AN    input(PIN_F6)  // G Svetlo technologicka mistnost vypinac do prujezdu
#define S106AN    input(PIN_F7)  // H Hlavni svetlo trucovna vypinac u dveri do satny


/*
#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 s107AN;
int1 s109AJ;
int1 s101AN;
int1 s101AJ;
int1 s105VV;
int1 s105NZ;
int1 s105ZZ;
int1 s102AN;
int1 s110AN;
int1 s108AV;
int1 r106JVU;
int1 r106JVD;
int1 r106ZVU;
int1 r106ZVD;
int1 s106AV;
int1 s106AN;
int1 s107AJ;
int1 s109AN;

#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
   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;
   s110AN=S110AN;
   s108AV=S108AV;
   r106JVU=R106JVU;
   r106JVD=R106JVD;
   r106ZVU=R106ZVU;
   r106ZVD=R106ZVD;
   s106AV=S106AV;
   s106AN=S106AN;
   s107AJ=S107AJ;
   s109AN=S109AN;

   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(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(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(get_timer0()>65000) {output_toggle(LED2); set_timer0(0);}
   }
}