HW_Lib/lib/key/inc/key.h

104 lines
3.6 KiB
C
Raw Normal View History

2024-06-22 14:51:58 +00:00
#ifndef HW_LIB_SIM_KEY_H
2024-06-22 03:08:31 +00:00
#define HW_LIB_KEY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
2024-06-22 03:08:31 +00:00
#define KEY_TICKS_INTERVAL 5 // 定时器间隔时间,单位为毫秒
2024-06-22 14:51:58 +00:00
#define DEBOUNCE_TICKS 0 // 按键去抖动计数阈值最大为70 ~ 7
2024-06-22 03:08:31 +00:00
#define SHORT_TICKS (300 / KEY_TICKS_INTERVAL) // 短按阈值,单位为定时器间隔的倍数
#define LONG_TICKS (1000 / KEY_TICKS_INTERVAL) // 长按阈值,单位为定时器间隔的倍数
#define KEY_STOP_FREE 1 // 按键停止释放状态标识
typedef struct Key_List Key_t; // 定义结构体Key_t为Key_List的别名
typedef void (*Key_Callback_t)(Key_t *key); // 定义函数指针类型Key_Callback_t接受一个Key_t结构体指针参数
typedef enum {
KEY_PRESS_DOWN = 0, // 按下事件
KEY_PRESS_UP, // 弹起事件
KEY_PRESS_REPEAT, // 重复按下事件
KEY_SINGLE_CLICK, // 单击事件
KEY_DOUBLE_CLICK, // 双击事件
KEY_LONG_PRESS_START, // 长按开始事件
KEY_LONG_PRESS_HOLD, // 长按保持事件
number_of_event, // 事件总数
KEY_ALL_EVENT, // 所有事件
NONE_PRESS // 无按键事件
} PressEvent; // 定义枚举类型PressEvent表示按键事件类型
struct Key_List {
Key_t *next; // 指向下一个按键结构体的指针
uint16_t ticks; // 计时器
uint8_t repeat: 4; // 按键重复次数占4位
uint8_t event: 4; // 当前按键事件类型占4位
uint8_t state: 3; // 按键状态占3位
uint8_t debounce_cnt: 3; // 按键去抖计数占3位
uint8_t active_level: 1; // 按键激活电平占1位
uint8_t key_level: 1; // 当前按键电平占1位
uint8_t key_id; // 按键ID
2024-06-23 06:19:19 +00:00
uint8_t (*hal_read_pin)(uint8_t key_id_); // 函数指针,用于获取按键电平
2024-06-22 03:08:31 +00:00
Key_Callback_t cb[number_of_event]; // 按键事件回调函数数组
};
/**
2024-06-23 06:19:19 +00:00
* @brief
* @param key: []
* @param key_id: [] ID
* @param active_level: []
* @param read_pin: []
2024-06-22 03:08:31 +00:00
* @return void
2024-06-23 06:19:19 +00:00
* @example key_init(&my_key, 1, ACTIVE_HIGH, read_key_pin_func);
**/
void key_init(Key_t *key, uint8_t key_id, uint8_t active_level, uint8_t(*read_pin)(uint8_t));
2024-06-22 03:08:31 +00:00
/**
2024-06-23 06:14:35 +00:00
* @brief
* @param key: []
2024-06-22 03:08:31 +00:00
* @param event: []
2024-06-23 06:14:35 +00:00
* @param cb: []
* @param start: []
2024-06-22 03:08:31 +00:00
* @return void
2024-06-23 06:14:35 +00:00
* @example key_attach(&my_key, PRESS_EVENT_LONG_PRESS, key_callback_func, true);
**/
void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb, bool start);
2024-06-22 03:08:31 +00:00
/**
* @brief
* @param key: []
* @return
* @example PressEvent current_event = get_key_event(&my_key);
*/
PressEvent get_key_event(Key_t *key);
/**
* @brief
* @param key: []
* @return 0
* @example int result = key_start(&my_key);
*/
int key_start(Key_t *key);
/**
* @brief
* @param key: []
* @return void
* @example key_stop(&my_key);
*/
void key_stop(Key_t *key);
/**
* @brief
* @return void
* @example key_ticks();
*/
void key_ticks(void);
#ifdef __cplusplus
}
#endif
2024-06-22 14:51:58 +00:00
#endif //HW_LIB_SIM_KEY_H