110VCGQ/project/revamp_addr/user/Inc/user_key.h
2024-11-18 10:09:39 +08:00

118 lines
2.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* user_key.h
*
* Created on: 2023年10月31日
* Author: wyf
*/
#ifndef USER_INC_USER_KEY_H_
#define USER_INC_USER_KEY_H_
#include "user.h"
#define KEY_UP_DOWN KEY_1_DOWN
#define KEY_UP_UP KEY_1_UP
#define KEY_UP_LONG KEY_1_LONG
#define KEY_DOWN_DOWN KEY_2_DOWN
#define KEY_DOWN_UP KEY_2_UP
#define KEY_DOWN_LONG KEY_2_LONG
#define KEY_OK_DOWN KEY_3_DOWN
#define KEY_OK_UP KEY_3_UP
#define KEY_OK_LONG KEY_3_LONG
#define KEY_RESET_DOWN KEY_4_DOWN
#define KEY_RESET_UP KEY_4_UP
#define KEY_RESET_LONG KEY_4_LONG
#define KEY_PWR_DOWN KEY_5_DOWN
#define KEY_PWR_UP KEY_5_UP
#define KEY_PWR_LONG KEY_5_LONG
/* 按键ID, 主要用于bsp_KeyState()函数的入口参数 */
typedef enum
{
KID_START = 0,
KID_STOP,
KID_CODER,
}KEY_ID_E;
/*
按键滤波时间50ms, 单位10ms。
只有连续检测到50ms状态不变才认为有效包括弹起和按下两种事件
即使按键电路不做硬件滤波,该滤波机制也可以保证可靠地检测到按键事件
*/
#define KEY_FILTER_TIME 5
#define KEY_LONG_TIME 100 /* 单位10ms 持续1秒认为长按事件 */
/*
每个按键对应1个全局的结构体变量。
*/
typedef struct
{
/* 下面是一个函数指针,指向判断按键手否按下的函数 */
uint8_t (*IsKeyDownFunc)(void); /* 按键按下的判断函数,1表示按下 */
uint8_t Count; /* 滤波器计数器 */
uint16_t LongCount; /* 长按计数器 */
uint16_t LongTime; /* 按键按下持续时间, 0表示不检测长按 */
uint8_t State; /* 按键当前状态(按下还是弹起) */
uint8_t RepeatSpeed; /* 连续按键周期 */
uint8_t RepeatCount; /* 连续按键计数器 */
}KEY_T;
typedef enum
{
KEY_NONE = 0, /* 0 表示按键事件 */
KEY_1_DOWN, /* 1键按下 */
KEY_1_UP, /* 1键弹起 */
KEY_1_LONG, /* 1键长按 */
KEY_2_DOWN, /* 2键按下 */
KEY_2_UP, /* 2键弹起 */
KEY_2_LONG, /* 2键长按 */
KEY_3_DOWN, /* 2键按下 */
KEY_3_UP, /* 2键弹起 */
KEY_3_LONG, /* 2键长按 */
KEY_4_DOWN, /* 2键按下 */
KEY_4_UP, /* 2键弹起 */
KEY_4_LONG, /* 2键长按 */
KEY_5_DOWN, /* 2键按下 */
KEY_5_UP, /* 2键弹起 */
KEY_5_LONG, /* 2键长按 */
}KEY_ENUM;
/* 按键FIFO用到变量 */
#define KEY_FIFO_SIZE 10
typedef struct
{
uint8_t Buf[KEY_FIFO_SIZE]; /* 键值缓冲区 */
uint8_t Read; /* 缓冲区读指针1 */
uint8_t Write; /* 缓冲区写指针 */
uint8_t Read2; /* 缓冲区读指针2 */
}KEY_FIFO_T;
/* 供外部调用的函数声明 */
void Init_Key(void);
void bsp_KeyScan10ms(void);
void bsp_PutKey(uint8_t _KeyCode);
uint8_t bsp_GetKey(void);
uint8_t bsp_GetKey2(void);
uint8_t bsp_GetKeyState(KEY_ID_E _ucKeyID);
void bsp_SetKeyParam(uint8_t _ucKeyID, uint16_t _LongTime, uint8_t _RepeatSpeed);
void bsp_ClearKey(void);
void bsp_KeyScan1ms(void);
#endif /* USER_INC_USER_KEY_H_ */