#include "includes.h"

extern tsEDK 	EDK;
extern tsAppCB  appCB;



/************************************************************************************/
// SetJumperFunctionRS
// prirazeni funkce jumperu pro verzi RS
// jelikoz se s definici bitu v EDK.input.state muze hybat, pridana samostatna
// promena EDK.hwspec_low ktera drzi stav propojek a technologii ctecky, lze pak vycist prikazem STV
/************************************************************************************/
void SetJumperFunctionRS( tUInt *anvalue)
{
	//***********************
	// vyhodnoceni JP3+JP4
	//***********************	
	if (anvalue[0] > JP_HI_LIMIT)				// JP3 ani JP4 neosazeno
	 {
	     EDK.input.state &= ~EDK_IN_BUZZER;
	     EDK.input.state &= ~EDK_IN_EHS;
		 EDK.hwspec_low &= ~HWSPEC_LOW_JP3;		// pro predani stavu jumperu na dotaz
		 EDK.hwspec_low &= ~HWSPEC_LOW_JP4;
	 }
	
	else if (anvalue[0] > JP2_LOW_LIMIT)		// osazen jen jumper JP4
	 {
	   EDK.input.state |= EDK_IN_EHS;
	   EDK.hwspec_low |= HWSPEC_LOW_JP4;
	 }

	else if (anvalue[0] > JP1_LOW_LIMIT)		// osazen jen jumper JP3
	 {
	   EDK.input.state |= EDK_IN_BUZZER;
	   EDK.hwspec_low |= HWSPEC_LOW_JP3;
	 }

	else if (anvalue[1] > JP12_LOW_LIMIT)		// osazen jumper JP3+JP4
	 {
	   EDK.input.state |= EDK_IN_BUZZER;
	   EDK.input.state |= EDK_IN_EHS;
	   EDK.hwspec_low |= HWSPEC_LOW_JP3;
	   EDK.hwspec_low |= HWSPEC_LOW_JP4;
	 }
	  

	//***********************
	// vyhodnoceni JP1+JP2
	//***********************
	if (anvalue[1] > JP_HI_LIMIT)				// JP1 ani JP2 neosazeno
	 {
	   EDK.input.state &= ~EDK_IN_READER2;
	   EDK.input.state &= ~EDK_IN_RELE_INV;
	   EDK.hwspec_low &= ~HWSPEC_LOW_JP1;		// pro predani stavu jumperu na dotaz
	   EDK.hwspec_low &= ~HWSPEC_LOW_JP2;
	 }
	else if (anvalue[1] > JP2_LOW_LIMIT)		// osazen jen jumper JP2
	 {
	   EDK.input.state |= EDK_IN_RELE_INV;				
	   EDK.hwspec_low |= HWSPEC_LOW_JP2;
	 }

	 else if (anvalue[1] > JP1_LOW_LIMIT)		// osazen jen jumper JP1
	  {
	    EDK.input.state |= EDK_IN_READER2;
	    EDK.hwspec_low |= HWSPEC_LOW_JP1;
	  }

	 else if (anvalue[1] > JP12_LOW_LIMIT)		// osazen jumper JP1+JP2
	  {
	    EDK.input.state |= EDK_IN_READER2;
	    EDK.input.state |= EDK_IN_RELE_INV;
	    EDK.hwspec_low |= HWSPEC_LOW_JP1;
	    EDK.hwspec_low |= HWSPEC_LOW_JP2;		
	  }
}


/************************************************************************************/
// SetJumperFunctionEDK
// prirazeni funkce jumperu pro verzi EDK
/************************************************************************************/
void SetJumperFunctionEDK( tUInt *anvalue)
{
	
	//***********************
	// vyhodnoceni JP3+JP4
	//***********************	
	if (anvalue[0] > JP_HI_LIMIT)				// JP3 ani JP4 neosazeno
	 {
	     EDK.input.state &= ~EDK_IN_W26;		// JP3
												// JP4 nema funkci 
	 }
	
	else if (anvalue[0] > JP2_LOW_LIMIT)		// osazen jen jumper JP4
	 {
	   											// JP4 nema funkci
	 }

	else if (anvalue[0] > JP1_LOW_LIMIT)		// osazen jen jumper JP3
	 {
	   EDK.input.state |= EDK_IN_W26;
	 }

	else if (anvalue[1] > JP12_LOW_LIMIT)		// osazen jumper JP3+JP4
	 {
	   EDK.input.state |= EDK_IN_W26;			// JP3
	   											// JP4 nema funkci
	 }
	  

	//***********************
	// vyhodnoceni JP1+JP2
	//***********************
	if (anvalue[1] > JP_HI_LIMIT)				// JP1 ani JP2 neosazeno
	 {
	   EDK.input.state &= ~EDK_IN_READER2;		// JP1
	   EDK.input.state &= ~EDK_IN_WGOUT;		// JP2
	 }
	else if (anvalue[1] > JP2_LOW_LIMIT)		// osazen jen jumper JP2
	 {
	   EDK.input.state |= EDK_IN_WGOUT;				
	 }

	 else if (anvalue[1] > JP1_LOW_LIMIT)		// osazen jen jumper JP1
	  {
	    EDK.input.state |= EDK_IN_READER2;
	  }

	 else if (anvalue[1] > JP12_LOW_LIMIT)		// osazen jumper JP1+JP2
	  {
	    EDK.input.state |= EDK_IN_READER2;
	    EDK.input.state |= EDK_IN_WGOUT;
	  }

}
	
	
/************************************************************************************/
// ReadConfigJumpers
// nacteni konfiguracnich propojek, nacitani reseno pomoci 2 kanalu ADC - AN0 a AN4
/************************************************************************************/
void ReadConfigJumpers(void)
{
 tUInt temp;


	//***********************
	// mereni kanalu AN0
	//***********************
	adinsel.byte = (adinsel.byte & 0xF0) | AN_CH0;		// set channel AN0
	adst = 1;   						   				// start conversion (one-shot-mode)
	asm ("nop");
	asm ("nop");
    while(adst);						            	// wait until completed
    temp = ad0.word;									// prvni vzorek zahodit

	adst = 1;   						   				// start conversion (one-shot-mode)
	asm ("nop");
	asm ("nop");
	while(adst);						            	// wait until completed
    appCB.adc.buffer[0] = 0x03ff & ad0.word;			// conversion result of AN0


	//***********************
	// mereni kanalu AN4
	//***********************
	adinsel.byte = (adinsel.byte & 0xF0) | AN_CH4; 		// set channel AN4
    adst = 1;   						   				// start conversion (one-shot-mode)
	asm ("nop");
	asm ("nop");
	while(adst);						            	// wait until completed
    temp = ad4.word;									// prvni vzorek zahodit

	adst = 1;   						   				// start conversion (one-shot-mode)
	asm ("nop");
	asm ("nop");
    while(adst);						            	// wait until completed
    appCB.adc.buffer[1] = 0x03ff & ad4.word;			// conversion result of AN4


// prirazeni funkce jumperum	
	#ifdef INTERFACE_RS				

	  SetJumperFunctionRS( (tUInt*)&appCB.adc.buffer[0]);

    #elif INTERFACE_EDK

	  SetJumperFunctionEDK( (tUInt*)&appCB.adc.buffer[0]);

	#endif	  

	

}

