//***************************** P R S T ***************************************/
//****************** PRISTUPOVY REGISTRACNI SYSTEM ****************************/
//***************************** main.c ****************************************/
//*****************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include "main.h"
#include "stm32f10x_conf.h"
#include "common.h"

/*TEST DEFINES*/
//#define TEST_ACCUCHARGE
//#define TEST_BACKUPOWER
/* Global variables ---------------------------------------------------------*/

ErrorStatus HSEStartUpStatus;
volatile int delay_time;
volatile unsigned int tick1ms;
unsigned char ledstat=0;
unsigned char RFID_Rx_Buffer[13];

/* Private function prototypes -----------------------------------------------*/
void Delay(volatile uint32_t nCount);
void Delay_ms(int ms);


int main(void)
{
	uint8_t k,util,ActiveTimeout;

	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //Switch off JTAG
	GPIO_ResetBits(EXT_OUT1_PORT,EXT_OUT1_PIN);				//Switch off binary outputs
	GPIO_ResetBits(EXT_OUT2_PORT,EXT_OUT2_PIN);
	Clock_Init();
	Delay_ms(1000);
	HW_Init();
	KEYB_Init();
	Delay_ms(100);
	BEEP_ON();
	Delay_ms(1000);
	BEEP_OFF();
	RED_LED_BLFAST();
	Delay_ms(4000);
	FPR_ON();
	GREEN_LED_BLSLOW();

	KEYB_SetMode(keybmode_alpha);

#ifdef TEST_ACCUCHARGE
	//********************* TEST NABIJENI AKUMULATORU ************************

	while(1){GPIO_ResetBits(BATT_POWER_EN_PORT,BATT_POWER_EN_PIN);
	if(!(GPIO_ReadInputDataBit(BATT_CHRG_ST1_PORT,BATT_CHRG_ST1_PIN))){
		RED_LED_ON();
	}
	else {
		RED_LED_OFF();
	}

	if(!(GPIO_ReadInputDataBit(BATT_CHRG_ST2_PORT,BATT_CHRG_ST2_PIN))){
		GREEN_LED_ON();
	}
	else {
		GREEN_LED_OFF();
	}

	}
#endif

#ifdef TEST_BACKUPOWER
	//*************************** TEST ZALOZNIHO ZDROJE **********************
	while(1){
		Delay_ms(500);
		GPIO_SetBits(BATT_MES_EN_PORT,BATT_MES_EN_PIN);
		Delay_ms(500);
		GPIO_ResetBits(BATT_MES_EN_PORT,BATT_MES_EN_PIN);
	if(STATUS_GET(STAT_BATTERY_POWERED)){RED_LED_ON();GREEN_LED_OFF();}
	else{GREEN_LED_ON();RED_LED_OFF();}

	}
#endif

// -------------- INFINITE LOOP -------------------
while (1)
{
//-------------- ASYNCHRONOUS STEP ----------------
	if(Host_Com_Stat){
		k=Host_Com_Stat;
		if(k==1){
			Host_Process();
		}
		else{
			Host_Send_Err(ERR_HOST_PACKET,k);
		}
		HOST_PACKET_PROCESSED();
	}


//-------------- SYNCHROUNOUS STEP 500 ms -----------
	if(tick1ms < 500) continue;
	tick1ms=0;

	if(!(STATUS_GET(STAT_PIR_SIG | STAT_TERMINAL_ACTIVE)))
		continue;
	if(STATUS_GET(STAT_PIR_SIG)){
		if(STATUS_GET(STAT_TERMINAL_ACTIVE)) ActiveTimeout=5;
		else{
			FPR_ON();
			AMP_ON();
			Host_Send_ActiveMode(ENABLE);
			GREEN_LED_BLFAST();
			ActiveTimeout=0;
			STATUS_SET(STAT_TERMINAL_ACTIVE);
		}
		STATUS_RESET(STAT_PIR_SIG);
	}

	else if(ActiveTimeout++<5){
		continue;
	}
	else if(((STATUS_GET(STAT_BATTERY_POWERED)) && (ActiveTimeout>(ACTIVE_TIMEOUT_BATTERY*2)))||(ActiveTimeout>(ACTIVE_TIMEOUT_MAINS*2))){
		STATUS_RESET(STAT_TERMINAL_ACTIVE);
		FPR_OFF();
		if(!(STATUS_GET(STAT_AMP_PERMANENT_ACTIVE)))
			AMP_OFF();
		Host_Send_ActiveMode(DISABLE);
		GREEN_LED_BLSLOW();
	}

//----------------------------- *TERMINAL IN ACTIVE MODE* ---------------------
		RED_LED_TOGGLE();
		BEEP_OFF_S();
//------------------------------ FINGRPRINT POLLING ---------------------------
		k=fpr_CheckFinger();
		if(k==0x01){
			Host_Send_Err(ERR_FPR,k);
		}
		if(k) util=0;
		else if((util++)==2){
			util=3;
			BEEP_ON();

			k=fpr_GetImage();
			if(k){
				Host_Send_Err(ERR_FPR,k);
			}

			k=fpr_GenTemplet(BUFFER_A);
			if(k){
				Host_Send_Err(ERR_FPR,k);
			}

			if(!fpr_Search(BUFFER_A,1,255)){
				Host_Send_ScannedFPR((uint16_t)(FPR_Buffer[FOUND_ID_L] | (FPR_Buffer[FOUND_ID_H]<<8)));
			}
			else{
				Host_Send_ScannedFPR(HOSTOUT_FPR_NOTFOUND);
				}
			BEEP_OFF();
		}
//------------------------------ KEYBOARD POLLING ----------------------------
		k=KEYB_ReadKey();
		if(k){
			Host_Send_PressedKey(k);
			BEEP_ON();
		}
//------------------------------ RFID CARD POLLING ---------------------------
		if(STATUS_GET(STAT_RFID_READ)){
			BEEP_ON();
			Host_Send_ReadRFID(RFID_Rx_Buffer);
			STATUS_RESET(STAT_RFID_READ);
		}
//-------------------------------- BATTERY CHECK -----------------------------
		if(STATUS_GET(STAT_BATTERY_POWERED)){
			BATT_MEAS_ON();
			Delay_ms(5);
			if(BATT_VOLTAGE_READ()<BATT_WARNING_LEVEL){
				Host_Send_PWRChange(POWER_BATTERY_LOW);
				Delay_ms(2000);
				GPIO_ResetBits(BATT_POWER_EN_PORT,BATT_POWER_EN_PIN);
			}
			BATT_MEAS_OFF();
		}
}}

void Delay_ms(int ms)
{
 delay_time=ms;
 while(delay_time!=0);
}

