#include "common.h"






HSKey Key[4];





void KeyInit(void)
{


	Key[KEY_SELECT].longPressTimeOne = 2;

	Key[KEY_UP].longPressTimeKeep = 2;



	//Key[KEY_COUNTER].longPressTimeKeep = 4;

}




//50ms key polling
void ReadKEY(void)
{	  



	if( SystemTimer.TIM_KEY < 45 )
		return;

	SystemTimer.TIM_KEY = 0;




//////////////////////////////////////////////////////////////////////////////////////////
	if( nrf_gpio_pin_read(BSP_BUTTON_2) == false  )
	{

		if( Key[KEY_SELECT].softRelease == 1 )
		{
			return;
		}

		
		if( Key[KEY_SELECT].onFlag == 0 )
		{
#ifdef ENABLE_USART
			//printf("KEY_NUMUP\r\n");
#endif			
			KeySelectPressed();
		}else{

			if( Key[KEY_SELECT].longPressTimeKeep != 0 )
			{
				if( Key[KEY_SELECT].pressedSec >= Key[KEY_SELECT].longPressTimeKeep )
				{
					KeySelectLongPressedKeep();
				}
			}

			
			if( Key[KEY_SELECT].pressTime%11 == 0 )	// count up 1 sec 
			{
#ifdef ENABLE_USART
				//printf("KEY_NUMUP Press Time_%d\r\n", (unsigned int)Key[0].pressedSec);
#endif				
				Key[KEY_SELECT].pressedSec++;

				if( Key[KEY_SELECT].longPressTimeOne != 0 )
				{
					if( Key[KEY_SELECT].pressedSec >= Key[KEY_SELECT].longPressTimeOne )
					{
						
						KeySelectLongPressedOne();
						Key[KEY_SELECT].softRelease = 1;
					}
				}				
			}
		}
		Key[KEY_SELECT].onFlag = 1;
		Key[KEY_SELECT].pressTime++;

	}else{
		if( Key[KEY_SELECT].onFlag == 1 )
		{
			//printf("KEY_NUMUP_%d\r\n", (unsigned int)Key[0].pressTime);
			KeySelectReleased();
			
		}
		Key[KEY_SELECT].pressTime = 0;
		Key[KEY_SELECT].onFlag = 0;
		Key[KEY_SELECT].pressedSec = 0;
		Key[KEY_SELECT].softRelease = 0;
		Key[KEY_SELECT].prevTime = 0;

	}



//////////////////////////////////////////////////////////////////////////////////////////
	if( nrf_gpio_pin_read(BSP_BUTTON_1) == false )
	{

		if( Key[KEY_UP].softRelease == 1 )
		{
			return;
		}

		
		if( Key[KEY_UP].onFlag == 0 )
		{
#ifdef ENABLE_USART
			//printf("KEY_NUMUP\r\n");
#endif			
			KeyUpPressed();
		}else{

			if( Key[KEY_UP].longPressTimeKeep != 0 )
			{
				if( Key[KEY_UP].pressedSec >= Key[KEY_UP].longPressTimeKeep )
				{
					KeyUpLongPressedKeep();
				}
			}

			
			if( Key[KEY_UP].pressTime%11 == 0 )	// count up 1 sec 
			{
#ifdef ENABLE_USART
				//printf("KEY_NUMUP Press Time_%d\r\n", (unsigned int)Key[0].pressedSec);
#endif				
				Key[KEY_UP].pressedSec++;

				if( Key[KEY_UP].longPressTimeOne != 0 )
				{
					if( Key[KEY_UP].pressedSec >= Key[KEY_UP].longPressTimeOne )
					{
						
						KeyUpLongPressedOne();
						Key[KEY_UP].softRelease = 1;
					}
				}				
			}
		}
		Key[KEY_UP].onFlag = 1;
		Key[KEY_UP].pressTime++;

	}else{
		if( Key[KEY_UP].onFlag == 1 )
		{
			//printf("KEY_NUMUP_%d\r\n", (unsigned int)Key[0].pressTime);
			KeyUpReleased();
			
		}
		Key[KEY_UP].pressTime = 0;
		Key[KEY_UP].onFlag = 0;
		Key[KEY_UP].pressedSec = 0;
		Key[KEY_UP].softRelease = 0;
		Key[KEY_UP].prevTime = 0;

	}












}



void KeySelectPressed(void)
{
	DBGPrint("KeySelectPressed\r\n" );
}

void KeySelectReleased(void)
{
	DBGPrint("KeySelectReleased\r\n" );
}

void KeySelectLongPressedOne(void)
{
	DBGPrint("KeySelectLongPressedOne\r\n" );	
}

void KeySelectLongPressedKeep(void)
{
	DBGPrint("KeySelectLongPressedKeep\r\n" );
}


void KeyUpPressed(void)
{
	DBGPrint("KeyUpPressed\r\n" );
}

void KeyUpReleased(void)
{
	DBGPrint("KeyUpReleased\r\n" );
}

void KeyUpLongPressedOne(void)
{
	DBGPrint("KeyUpLongPressedOne\r\n" );	
}

void KeyUpLongPressedKeep(void)
{
	DBGPrint("KeyUpLongPressedKeep\r\n" );
}









