#include "includes.h"

extern tsEDK 	EDK;
extern tsAppCB  appCB;


	
/******************************************************************************
// CheckInput1 - pouze filtrace vstupu IN1
******************************************************************************/
void FilterInput1(tUInt _state)
{ 

	EDK.input1.lastState = EDK.input1.state;					
    EDK.input1.state = _state;									
	

   
	if (!EDK.input1.lastState && EDK.input1.state)			
	 {
	  EDK.io_flag |= FLAG_INPUT1_UPDOWN;							
	  EDK.counter.input1 = 0;								
	 }
   
	if (EDK.input1.lastState && !EDK.input1.state)				
	 {
	  EDK.io_flag &= ~FLAG_INPUT1_UPDOWN;						
	  EDK.counter.input1 = EDK_PRESET_INPUT1_TIME;				
	 }


 	
  if (EDK.io_flag & FLAG_INPUT1_UPDOWN)						
  { 
   if (EDK.counter.input1 < EDK_PRESET_INPUT1_TIME)
    {
	  EDK.counter.input1++;			 							
	}
	 
   if (EDK.counter.input1 == EDK_PRESET_INPUT1_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT1;						
	}
   }
   else															
    {
	  if (EDK.counter.input1)									
	   {
		 EDK.counter.input1--;
	   }
	   
	  if (!EDK.counter.input1)									
	   {														
          EDK.input.state &= ~EDK_IN_INPUT1;					
	   }
	 }

}


	
/******************************************************************************
// CheckInput2 - detekce a filtrace vstupu IN2
******************************************************************************/
void FilterInput2(tUInt _state)
{ 

	EDK.input2.lastState = EDK.input2.state;					
    EDK.input2.state = _state;									


	if (!EDK.input2.lastState && EDK.input2.state)				
	 {
	  EDK.io_flag |= FLAG_INPUT2_UPDOWN;						
	  EDK.counter.input2 = 0;									
	 }
   
	if (EDK.input2.lastState && !EDK.input2.state)				
	 {
	  EDK.io_flag &= ~FLAG_INPUT2_UPDOWN;						
	  EDK.counter.input2 = EDK_PRESET_INPUT2_TIME;				
	 }


  	
  if (EDK.io_flag & FLAG_INPUT2_UPDOWN)							
  { 
   if (EDK.counter.input2 < EDK_PRESET_INPUT2_TIME)
    {
	  EDK.counter.input2++;			 							
	}
	 
   if (EDK.counter.input2 == EDK_PRESET_INPUT2_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT2;						
	}
   }
   else															
    {
	  if (EDK.counter.input2)									
	   {
		 EDK.counter.input2--;
	   }
	   
	  if (!EDK.counter.input2)									
	   {														
         EDK.input.state &= ~EDK_IN_INPUT2;						
	   }
	 }

}





	
/******************************************************************************
// CheckInput3 - detekce a filtrace vstupu IN3
******************************************************************************/
void FilterInput3(tUInt _state)
{ 
// nacteni pinu IN3
	EDK.input3.lastState = EDK.input3.state;					
    EDK.input3.state = _state;									

    
	if (!EDK.input3.lastState && EDK.input3.state)				
	 {
	  EDK.ext_io_flag |= FLAG_INPUT3_UPDOWN;							
	  EDK.counter.input3 = 0;									
	 }
   
	if (EDK.input3.lastState && !EDK.input3.state)				
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT3_UPDOWN;						
	  EDK.counter.input3 = EDK_PRESET_INPUT3_TIME;				
	 }



  if (EDK.ext_io_flag & FLAG_INPUT3_UPDOWN)							
  { 
   if (EDK.counter.input3 < EDK_PRESET_INPUT3_TIME)
    {
	  EDK.counter.input3++;			 						
	}
	 
   if (EDK.counter.input3 == EDK_PRESET_INPUT3_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT3;						
	}
   }
   else															
    {
	  if (EDK.counter.input3)									
	   {
		 EDK.counter.input3--;
	   }
	   
	  if (!EDK.counter.input3)									
	   {														
         EDK.input.state &= ~EDK_IN_INPUT3;						
	   }
	 }

}




	
/******************************************************************************
// CheckInput4 - detekce a filtrace vstupu IN4
******************************************************************************/
void FilterInput4(tUInt _state)
{ 
// nacteni pinu IN4
	EDK.input4.lastState = EDK.input4.state;					
    EDK.input4.state = _state;									

    
	if (!EDK.input4.lastState && EDK.input4.state)				
	 {
	  EDK.ext_io_flag |= FLAG_INPUT4_UPDOWN;							
	  EDK.counter.input4 = 0;									
	 }
   
	if (EDK.input4.lastState && !EDK.input4.state)				
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT4_UPDOWN;							
	  EDK.counter.input4 = EDK_PRESET_INPUT4_TIME;				
	 }


	
  if (EDK.ext_io_flag & FLAG_INPUT4_UPDOWN)							
  { 
   if (EDK.counter.input4 < EDK_PRESET_INPUT4_TIME)
    {
	  EDK.counter.input4++;			 							
	}
	 
   if (EDK.counter.input4 == EDK_PRESET_INPUT4_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT4;					
	}
   }
   else														
    {
	  if (EDK.counter.input4)								
	   {
		 EDK.counter.input4--;
	   }
	   
	  if (!EDK.counter.input4)									 
	   {														
         EDK.input.state &= ~EDK_IN_INPUT4;						
	   }
	 }

}


	
/******************************************************************************
// CheckInput5 - detekce a filtrace vstupu IN5
******************************************************************************/
void FilterInput5(tUInt _state)
{ 
// nacteni pinu IN5
	EDK.input5.lastState = EDK.input5.state;					
    EDK.input5.state = _state;								 

   
	if (!EDK.input5.lastState && EDK.input5.state)			
	 {
	  EDK.ext_io_flag |= FLAG_INPUT5_UPDOWN;				
	  EDK.counter.input5 = 0;								
	 }
   
	if (EDK.input5.lastState && !EDK.input5.state)			
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT5_UPDOWN;				
	  EDK.counter.input5 = EDK_PRESET_MUX_INPUT_TIME;		
	 }



  if (EDK.ext_io_flag & FLAG_INPUT5_UPDOWN)							
  { 
   if (EDK.counter.input5 < EDK_PRESET_MUX_INPUT_TIME)
    {
	  EDK.counter.input5++;			 							
	}
	 
   if (EDK.counter.input5 == EDK_PRESET_MUX_INPUT_TIME)  		
    {															
	   EDK.input.state |= EDK_IN_INPUT5;						
	}
   }
   else															
    {
	  if (EDK.counter.input5)									
	   {
		 EDK.counter.input5--;
	   }
	   
	  if (!EDK.counter.input5)									 
	   {														
         EDK.input.state &= ~EDK_IN_INPUT5;						 
	   }
	 }

}



	
/******************************************************************************
// CheckInput6 - detekce a filtrace vstupu IN6
******************************************************************************/
void FilterInput6(tUInt _state)
{ 
// nacteni pinu IN6
	EDK.input6.lastState = EDK.input6.state;					
    EDK.input6.state = _state;									

    
	if (!EDK.input6.lastState && EDK.input6.state)				
	 {
	  EDK.ext_io_flag |= FLAG_INPUT6_UPDOWN;							
	  EDK.counter.input6 = 0;									
	 }
   
	if (EDK.input6.lastState && !EDK.input6.state)			
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT6_UPDOWN;							
	  EDK.counter.input6 = EDK_PRESET_MUX_INPUT_TIME;				
	 }


 	
  if (EDK.ext_io_flag & FLAG_INPUT6_UPDOWN)							
  { 
   if (EDK.counter.input6 < EDK_PRESET_MUX_INPUT_TIME)
    {
	  EDK.counter.input6++;			 							
	}
	 
   if (EDK.counter.input6 == EDK_PRESET_MUX_INPUT_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT6;						
	}
   }
   else														
    {
	  if (EDK.counter.input6)									
	   {
		 EDK.counter.input6--;
	   }
	   
	  if (!EDK.counter.input6)									
	   {														
         EDK.input.state &= ~EDK_IN_INPUT6;						
	   }
	 }

}



	
/******************************************************************************
// CheckInput7 - detekce a filtrace vstupu IN7
******************************************************************************/
void FilterInput7(tUInt _state)
{ 

	EDK.input7.lastState = EDK.input7.state;					
    EDK.input7.state = _state;									

    
	if (!EDK.input7.lastState && EDK.input7.state)				
	 {
	  EDK.ext_io_flag |= FLAG_INPUT7_UPDOWN;							
	  EDK.counter.input7 = 0;									
	 }
   
	if (EDK.input7.lastState && !EDK.input7.state)				
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT7_UPDOWN;							
	  EDK.counter.input7 = EDK_PRESET_MUX_INPUT_TIME;				
	 }


  if (EDK.ext_io_flag & FLAG_INPUT7_UPDOWN)							
  { 
   if (EDK.counter.input7 < EDK_PRESET_MUX_INPUT_TIME)
    {
	  EDK.counter.input7++;			 							
	}
	 
   if (EDK.counter.input7 == EDK_PRESET_MUX_INPUT_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT7;						
	}
   }
   else															
    {
	  if (EDK.counter.input7)									
	   {
		 EDK.counter.input7--;
	   }
	   
	  if (!EDK.counter.input7)									 
	   {														
         EDK.input.state &= ~EDK_IN_INPUT7;						
	   }
	 }

}




	
/******************************************************************************
// CheckInput8 - detekce a filtrace vstupu IN8
******************************************************************************/
void FilterInput8(tUInt _state)
{ 
// nacteni pinu IN8
	EDK.input8.lastState = EDK.input8.state;					
    EDK.input8.state = _state;									

  
	if (!EDK.input8.lastState && EDK.input8.state)				
	 {
	  EDK.ext_io_flag |= FLAG_INPUT8_UPDOWN;							
	  EDK.counter.input8 = 0;								
	 }
   
	if (EDK.input8.lastState && !EDK.input8.state)				
	 {
	  EDK.ext_io_flag &= ~FLAG_INPUT8_UPDOWN;							
	  EDK.counter.input8 = EDK_PRESET_MUX_INPUT_TIME;				
	 }



  if (EDK.ext_io_flag & FLAG_INPUT8_UPDOWN)							
  { 
   if (EDK.counter.input8 < EDK_PRESET_MUX_INPUT_TIME)
    {
	  EDK.counter.input8++;			 							
	}
	 
   if (EDK.counter.input8 == EDK_PRESET_MUX_INPUT_TIME)  			
    {															
	   EDK.input.state |= EDK_IN_INPUT8;						
	}
   }
   else															
    {
	  if (EDK.counter.input8)									
	   {
		 EDK.counter.input8--;
	   }
	   
	  if (!EDK.counter.input8)									
	   {														
         EDK.input.state &= ~EDK_IN_INPUT8;						
	   }
	 }

}






/******************************************************************************
//	UpdateInputStateRS GR-EXT8 
//
//  nacitani vstupu musi byt provedeno v rutine SyncOutput() 
******************************************************************************/
void UpdateInputStateRS(void)
{
	GUINTRECORD   PT_ADR;
    tUInt	 curState;
  	  

 if ( EDK.input.state != EDK.input.lastState)					
  {
    EDK.input.change = EDK.input.state ^ EDK.input.lastState;	

  //******************************
  // zmena vstupu IN1
  //******************************
  if (EDK.input.change & EDK_IN_INPUT1)						
   {
					
	  {	  												
		PT_ADR.X = 0x01;									
		curState = (EDK.input.state & EDK_IN_INPUT1)?1:0;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
	  }
	}

  //******************************
  // zmena vstupu IN2
  //******************************
  if (EDK.input.change & EDK_IN_INPUT2)
   { 
     if (!(EDK.input.state & EDK_IN_READER2))			
	  {														
		PT_ADR.X = 0x02;									
		curState = (EDK.input.state & EDK_IN_INPUT2)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
	  }
   }

  //******************************
  // zmena vstupu IN3
  //******************************
  if (EDK.input.change & EDK_IN_INPUT3)
   { 
		PT_ADR.X = 0x03;									
		curState = (EDK.input.state & EDK_IN_INPUT3)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }
   
  //******************************
  // zmena vstupu IN4
  //******************************
  if (EDK.input.change & EDK_IN_INPUT4)
   { 
		PT_ADR.X = 0x04;								
		curState = (EDK.input.state & EDK_IN_INPUT4)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }

  //******************************
  // zmena vstupu IN5
  //******************************
  if (EDK.input.change & EDK_IN_INPUT5)
   { 
		PT_ADR.X = 0x05;								
		curState = (EDK.input.state & EDK_IN_INPUT5)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }


  //******************************
  // zmena vstupu IN6
  //******************************
  if (EDK.input.change & EDK_IN_INPUT6)
   { 
		PT_ADR.X = 0x06;									
		curState = (EDK.input.state & EDK_IN_INPUT6)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }


  //******************************
  // zmena vstupu IN7
  //******************************
  if (EDK.input.change & EDK_IN_INPUT7)
   { 
		PT_ADR.X = 0x07;									
		curState = (EDK.input.state & EDK_IN_INPUT7)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }


  //******************************
  // zmena vstupu IN8
  //******************************
  if (EDK.input.change & EDK_IN_INPUT8)
   { 
		PT_ADR.X = 0x08;									
		curState = (EDK.input.state & EDK_IN_INPUT8)?0:1;	
		AddReq(PRIORITY_SYS_MESSAGE | ADD_MULTIPLE | PROCESS_SCI_STATE_CHANGE, curState, (tUInt *)&PT_ADR.X);			
   }

   
  


    EDK.input.lastState = EDK.input.state;		
	 										
 }	

}





/******************************************************************************
//	UpdateInputStateEDK
//
//  aktualizace stavu LED+BUZZER pro varianty WIEGAND a EDK
//  LED a BUZZER ovlada terminal vstupy IN1+IN2
******************************************************************************/
void UpdateInputStateEDK(void)
{
  //******************************
  // zmena vstupu IN1 
  //******************************
	if (EDK.input.state & EDK_IN_INPUT1)	// BUZZER sleduje stav vstupu IN1
	 {  									// spousti se pri zname karte
	   EDK.output |=  EDK_OUT_BUZZER;
	 }
	else  
	 {  
       EDK.output &= ~EDK_OUT_BUZZER;
	 }

  //******************************
  // zmena vstupu IN2
  //******************************
  if (EDK.input.state & EDK_IN_INPUT2)		// LED GREEN sleduje stav vstupu IN2
   {  										// znama karta
     EDK.output |=  EDK_OUT_LED_GRN;
     EDK.output &= ~EDK_OUT_LED_RED;
   }
  else  
   {  
  	 if (!EDK.counter.led_time)				// pokud vlastni casovac uz dobehl
	  {
       EDK.output |=  EDK_OUT_LED_RED;		// u cervene LED ma prednost vlastni casovac
	  }
       EDK.output &= ~EDK_OUT_LED_GRN;
   }
}



/******************************************************************************
//	GetActiveAnCount
//
//  funkce vraci pocet aktivnich analogovych vstupu
******************************************************************************/
tUInt GetActiveAnCount(void)
{
  tUInt activeIoCount;
  
  activeIoCount=0;
  
   #if defined USE_THERMO
    if (appCB.sensor.presence_state)
	 {
	   activeIoCount++;
	 }
   #endif

   
   #if defined USE_VOLT
	activeIoCount++;
   #endif


 return (activeIoCount);
								   
}



/******************************************************************************
//	GetActiveInput
//
//  funkce vraci index/masku aktivniho vstupu
******************************************************************************/
tUInt GetActiveIoCount(void)
{
  tUInt activeIoCount;
  
  activeIoCount=0;
  
  // proto spocitame pocet aktivnich IO, a podle toho se rozhodne co dal
  if (EDK.output & EDK_OUT_RELAY) activeIoCount++;
 
  if (EDK.input.state & EDK_IN_INPUT1) activeIoCount++;										 

  if (!(EDK.input.state & EDK_IN_INPUT2)) activeIoCount++;
 

  if (EDK.input.state & EDK_IN_EHS)
   {
     if(EDK.input.state & EDK_IN_TAMPER)
      {
 	    activeIoCount++;
	  }		 
   }

 return (activeIoCount);
								   
}


/******************************************************************************
//	GetFirstLastSign
//
//  funkce vraci priznak FIRST/LAST/NONE pro nastaveni hlavickz paketu
******************************************************************************/										   
tUInt GetFirstLastSign( tUInt total, tUInt actual)
{
  if (total == 0) return (0);						// pro pripad ze by to sem presto proslo
  
  
  if (total==1)										// pouze jeden paket, nastavit oba bity
   {
     return (PAC_FLAG_FIRST|PAC_FLAG_LAST);
   }
  else												// vice nez jeden paket
   {
     if (actual == total) return (PAC_FLAG_FIRST);	// prvni paket
	 else if (actual==1)  return (PAC_FLAG_LAST);	// posledni paket
	 else return (0);								// pro pakety mezi, nenastavovat nic
   }
  
}

	
													 										 
									
