【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
#ifndef _COMM_KEY_H_
#define _COMM_KEY_H_
#include "soft_timer.h"
/*与按键电路有关,当按键按下为低电平时定义COMM_KEY_DOWN为0,否则交换定义*/
#define COMM_KEY_UP 1
#define COMM_KEY_DOWN 0
typedef int(*key_callback_func)(void*);
typedef enum {
COMM_KEY_IDLE_STATUS =4,
COMM_KEY_DOWN_STATUS =0,
COMM_KEY_DOWN_SURE_STATUS =5,
COMM_KEY_UP_STATUS =6,
COMM_KEY_UP_SURE_STATUS =1,
COMM_KEY_SHORT_PRESS =2,
COMM_KEY_LONG_PRESS =3
}comm_key_status;
typedef struct{
/*获取按键状态*/
key_callback_func key_status_get_func;
/*短按回调函数*/
key_callback_func key_short_press_func;
/*长按回调函数*/
key_callback_func key_long_press_func;
/*短按时间*/
unsigned int short_press_time;
/*长按时间*/
unsigned int long_press_time;
/*消抖时间*/
unsigned int shake_time;
/*消抖计时器*/
TICK shake_timer;
/*按下计时器*/
TICK key_timer;
/*按键状态*/
comm_key_status key_status;
/*按键是否已在使用*/
u8 key_using_flag;
}keyInfo;
void key_InfoInit(void);
u8 key_Delete(keyInfo* key);
void key_HandleTask(void);
keyInfo* key_Create(key_callback_func keystatus,
key_callback_func shortfunc,
key_callback_func longfunc,
u32 shorttime,
u32 longtime,
u32 shaketime//消抖时间,一般为15ms
);
#endif // _COMM_KEY_H_
#include "comm_key.h"
#include "stdlib.h"
#include "board.h"
#define MAX_KEY_NUM 5
keyInfo KeyArray[MAX_KEY_NUM];
typedef enum{
COMM_KEY_UNUSING=0,
COMM_KEY_INUSING=1,
}key_Use;
typedef enum {
COMM_KEY_OK=0,
COMM_KEY_NO_THIS=1,
COMM_KEY_PRAR_ERR=2
}func_return;
void key_InfoInit(void)
{
u8 i=0;
for(i=0;i<MAX_KEY_NUM;i++)
{
KeyArray[i].key_long_press_func=NULL;
KeyArray[i].key_short_press_func=NULL;
KeyArray[i].key_status=COMM_KEY_IDLE_STATUS;
KeyArray[i].key_status_get_func=NULL;
KeyArray[i].long_press_time=0;
KeyArray[i].shake_time=0;
KeyArray[i].short_press_time=0;
KeyArray[i].key_using_flag=COMM_KEY_UNUSING;
}
}
keyInfo* key_Create(key_callback_func keystatus,
key_callback_func shortfunc,
key_callback_func longfunc,
u32 shorttime,
u32 longtime,
u32 shaketime//消抖时间,一般为15ms
)
{
u8 i=0;
/*获取按键状态的函数一定不能为空*/
if(keystatus==NULL) return NULL;
for(i=0;i<MAX_KEY_NUM;i++)
{
if(KeyArray[i].key_using_flag==COMM_KEY_UNUSING)
{
KeyArray[i].key_using_flag=COMM_KEY_INUSING;
KeyArray[i].key_long_press_func=longfunc;
KeyArray[i].key_short_press_func=shortfunc;
KeyArray[i].key_status_get_func=keystatus;
KeyArray[i].short_press_time=shorttime;
KeyArray[i].long_press_time=longtime;
KeyArray[i].shake_time=shaketime;
return &KeyArray[i];
}
}
return NULL;
}
u8 key_Delete(keyInfo* key)
{
u8 i=0;
/*指针不能为空*/
if(key==NULL)
{
mprintf(LOG_ERROR,"按键参数为空!!!\r\n");
return COMM_KEY_PRAR_ERR;
}
for(i=0;i<MAX_KEY_NUM;i++)
{
if(&KeyArray[i]==key)
{
KeyArray[i].key_using_flag=COMM_KEY_UNUSING;
KeyArray[i].key_long_press_func=NULL;
KeyArray[i].key_short_press_func=NULL;
KeyArray[i].key_status_get_func=NULL;
return COMM_KEY_OK;
}
}
mprintf(LOG_ERROR,"无此按键!!!\r\n");
/*无此按键*/
return COMM_KEY_NO_THIS;
}
void key_HandleTask(void)
{
u8 i=0;
for(i=0;i<MAX_KEY_NUM;i++)
{
if(KeyArray[i].key_using_flag==COMM_KEY_INUSING)
{
GET_TICK(KeyArray[i].shake_timer.tick_temp);
if((KeyArray[i].shake_timer.tick_temp-KeyArray[i].shake_timer.tick_begin)>KeyArray[i].shake_time)
{
switch (KeyArray[i].key_status)
{
case COMM_KEY_IDLE_STATUS:
if(COMM_KEY_DOWN==KeyArray[i].key_status_get_func(NULL))
{
KeyArray[i].key_status=COMM_KEY_DOWN_SURE_STATUS;
/*开始去抖动*/
GET_TICK(KeyArray[i].shake_timer.tick_begin);
}
break;
case COMM_KEY_DOWN_SURE_STATUS:
if(COMM_KEY_DOWN==KeyArray[i].key_status_get_func(NULL))
{
KeyArray[i].key_status=COMM_KEY_UP_STATUS;
/*开始计时按下时间*/
GET_TICK(KeyArray[i].key_timer.tick_begin);
}
else
{
KeyArray[i].key_status=COMM_KEY_IDLE_STATUS;
}
break;
case COMM_KEY_UP_STATUS:
if(COMM_KEY_UP==KeyArray[i].key_status_get_func(NULL))
{
KeyArray[i].key_status=COMM_KEY_UP_STATUS;
GET_TICK(KeyArray[i].key_timer.tick_temp);
if((KeyArray[i].key_timer.tick_temp-KeyArray[i].key_timer.tick_begin)>KeyArray[i].long_press_time)
{
if(KeyArray[i].key_long_press_func!=NULL)
{
KeyArray[i].key_long_press_func(NULL);
}
else
{
mprintf(LOG_INFO,"无长按回调函数,按键index:%d。\r\n",i);
}
}
else
{
if(KeyArray[i].key_short_press_func!=NULL)
{
KeyArray[i].key_short_press_func(NULL);
}
else
{
mprintf(LOG_INFO,"无短按回调函数,按键index:%d。\r\n",i);
}
}
KeyArray[i].key_status=COMM_KEY_IDLE_STATUS;
}
break;
default:
break;
}
}
}
}
}
来源:oschina
链接:https://my.oschina.net/u/3538381/blog/3148934