/**
	************************************************************
	************************************************************
	************************************************************
	*	ļ 	key.c
	*
	*	ߣ 		ż
	*
	*	ڣ 		2016-11-23
	*
	*	汾 		V1.0
	*
	*	˵ 		IOʼж
	*
	*	޸ļ¼	
	************************************************************
	************************************************************
	************************************************************
**/

//ͷļ
#include "key.h"

//Ӳ
#include "delay.h"


/*
************************************************************
*	ƣ	Key_Init
*
*	ܣ	IOʼ
*
*	ڲ	
*
*	ز	
*
*	˵		SW2-PD2		SW3-PC11	SW4-PC12	SW5-PC13	
*				Ϊ͵ƽ		ͷΪߵƽ
************************************************************
*/
void Key_Init(void)
{

	GPIO_InitTypeDef gpio_initstruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);	//GPIOCGPIODʱ
	
	gpio_initstruct.GPIO_Mode = GPIO_Mode_IPU;									//Ϊģʽ
	gpio_initstruct.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;			//ʼPin111213
	gpio_initstruct.GPIO_Speed = GPIO_Speed_50MHz;								//صƵ
	GPIO_Init(GPIOC, &gpio_initstruct);											//ʼGPIOC
	
	gpio_initstruct.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOD, &gpio_initstruct);											//ʼGPIOD

}

/*
************************************************************
*	ƣ	KeyScan
*
*	ܣ	ƽɨ
*
*	ڲ	GPIOXҪɨGPIO	NUMGPIOڵı
*
*	ز	IOƽ״̬
*
*	˵		
************************************************************
*/
_Bool KeyScan(GPIO_TypeDef* GPIOX, unsigned int NUM)
{
	
	if(GPIOX == GPIOC)
	{
		if(!GPIO_ReadInputDataBit(GPIOC, NUM))	//  Ϊ
		{
			return KEYDOWN;
		}
		else									//  Ϊ
		{
			return KEYUP;
		}
	}
	else if(GPIOX == GPIOD)
	{
		if(!GPIO_ReadInputDataBit(GPIOD, NUM))	//  Ϊ
		{
			return KEYDOWN;
		}
		else									//  Ϊ
		{
			return KEYUP;
		}
	}
	
	return KEYUP;								//ĬϷذͷ
	
}

/*
************************************************************
*	ƣ	Keyboard
*
*	ܣ	ܼ
*
*	ڲ	
*
*	ز	
*
*	˵		ֵ˫
************************************************************
*/
unsigned char Keyboard(void)
{
	
	static unsigned char keyStatus = 0;
	
	if(KeyScan(GPIOC, KEY0) == KEYDOWN)
	{
		keyStatus |= 0x01;							//KEY0
		
		while(KeyScan(GPIOC, KEY0) != KEYUP)		//ȴͷ
			return KEYNONE;
	}
	else
	{
		if(keyStatus & 0x01)
		{
			keyStatus &= 0xfe;						//KEY0ͷ
			return KEY0DOWN;
		}
	}
	
	if(KeyScan(GPIOC, KEY1) == KEYDOWN)
	{
		keyStatus |= 0x02;							//KEY1
		
		while(KeyScan(GPIOC, KEY1) != KEYUP)		//ȴͷ
			return KEYNONE;
	}
	else
	{
		if(keyStatus & 0x02)
		{
			keyStatus &= 0xfd;						//KEY1ͷ
			return KEY1DOWN;
		}
	}
	
	if(KeyScan(GPIOC, KEY2) == KEYDOWN)
	{
		keyStatus |= 0x04;							//KEY2
		
		while(KeyScan(GPIOC, KEY2) != KEYUP)		//ȴͷ
			return KEYNONE;
	}
	else
	{
		if(keyStatus & 0x04)
		{
			keyStatus &= 0xfb;						//KEY2ͷ
			return KEY2DOWN;
		}
	}
	
	if(KeyScan(GPIOD, KEY3) == KEYDOWN)
	{
		keyStatus |= 0x08;							//KEY3
		
		while(KeyScan(GPIOD, KEY3) != KEYUP)		//ȴͷ
			return KEYNONE;
	}
	else
	{
		if(keyStatus & 0x08)
		{
			keyStatus &= 0xf7;						//KEY3ͷ
			return KEY3DOWN;
		}
	}
	
	return KEYNONE;
	
}
