高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-8-14
|
发表于 2016-9-18 10:17:57
|
显示全部楼层
/*******************************************************************************
* Function Name : Get_Key
* Description : Read the KEY state
* Input : None
* Output : None
* Return : uint8_t KEY state
* Attention : None
*******************************************************************************/
uint8_t Get_Key(void)
{
uint8_t ReadKey;
if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT,GPIO_KEY1_PIN))
{
ReadKey |= PRESS_KEY1;
}
if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT,GPIO_KEY2_PIN))
{
ReadKey |= PRESS_KEY2;
}
if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT,GPIO_KEY3_PIN))
{
ReadKey |= PRESS_KEY3;
}
return ReadKey;
}
/*******************************************************************************
* Function Name : ReadKeyValue
* Description : Read the KEY value
* Input : None
* Output : None
* Return : uint8_t KEY value
* Attention : None
*******************************************************************************/
uint8_t ReadKeyValue(void)
{
static uint8_t Key_Check;
static uint8_t Key_State;
static uint16_t Key_LongCheck;
static uint8_t Key_Prev = 0; //上一次按键
uint8_t Key_press;
uint8_t Key_return = 0;
//KeyCountTime 1MS+1 按键消抖10MS
if(KeyCountTime >= 10)
{
KeyCountTime = 0;
Key_Check = 1;
}
//按键消抖10MS后开始处理
if(Key_Check == 1)
{
Key_Check = 0;
Key_press = Get_Key();//再次读取按键值
switch (Key_State)
{
case 0:
if(Key_press != 0)//有按键值
{
Key_Prev = Key_press;//将值赋予缓存区(上一次按键)
Key_State = 1;
}
break;
case 1:
if(Key_press == Key_Prev)
{
Key_State = 2;
Key_return= Key_Prev | KEY_DOWN;
}
else //按键抬起,是抖动,不响应按键
{
Key_State = 0;
}
break;
case 2:
if(Key_press != Key_Prev)
{
Key_State = 0;
Key_LongCheck = 0;
Key_return = Key_Prev | KEY_UP;
return Key_return;
}
if(Key_press == Key_Prev)
{
Key_LongCheck++;
if(Key_LongCheck >= 100) //长按2S
{
Key_LongCheck = 0;
Key_State = 3;
Key_return= Key_press | KEY_LONG;
return Key_return;
}
}
break;
case 3:
if(Key_press != Key_Prev)
{
Key_State = 0;
}
break;
}
}
return NO_KEY;
}
/*******************************************************************************
* Function Name : KEY_Handle
* Description : Key processing function
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void KEY_Handle(void)
{
uint8_t Key_return =0;
Key_return = ReadKeyValue();
if(Key_return & KEY_UP)
{
if(Key_return & PRESS_KEY1)
{
printf("KEY1 PRESS\r\n");
LED_RGB_Control(0,0,10);
Motor_status(0);
keys(1);
}
if(Key_return & PRESS_KEY2)
{
printf("KEY2 PRESS\r\n");
LED_RGB_Control(0,10,0);
Motor_status(10);
}
if(Key_return & PRESS_KEY3)
{
printf("KEY3 PRESS\r\n");
LED_RGB_Control(10,0,00);
DHT11_Read_Data(&Device_ReadStruct.Temperature, &Device_ReadStruct.Humidity);
printf("Temperature =%d;Humidity = %d " ,Device_ReadStruct.Temperature,Device_ReadStruct.Humidity);
Motor_status(5);
}
}
if(Key_return & KEY_LONG)
{
if(Key_return & PRESS_KEY1)
{
KEY_LongHandle(PRESS_KEY1);
printf("KEY1 Long PRESS\r\n");
printf("M2W RESET WIFI\r\n");
}
if(Key_return & PRESS_KEY2)
{
KEY_LongHandle(PRESS_KEY2);
printf("KEY2 Long PRESS\r\n");
printf("M2W Config WIFI\r\n");
}
if(Key_return & PRESS_KEY3)
{
printf("KEY3 Long PRESS\r\n");
}
}
} |
|