UP 优化key库

main
JiXieShi 2024-06-23 14:19:19 +08:00
parent 58afcd9d81
commit 3a889f16dd
4 changed files with 32 additions and 39 deletions

View File

@ -40,29 +40,22 @@ void Key_Call(Key_t *key) {
void Test_Key() { void Test_Key() {
Key_t k1, k2, k3, k4, k5, k6, ks; Key_t k1, k2, k3, k4, k5, k6, ks;
key_init(&k1, SIM_Key_UP, 1, SIM_KEY_UP); key_init(&k1, SIM_KEY_UP, 1, SIM_Key_UP);
key_init(&k2, SIM_Key_DOWN, 1, SIM_KEY_DOWN); key_init(&k2, SIM_KEY_DOWN, 1, SIM_Key_DOWN);
key_init(&k3, SIM_Key_LEFT, 1, SIM_KEY_LEFT); key_init(&k3, SIM_KEY_LEFT, 1, SIM_Key_LEFT);
key_init(&k4, SIM_Key_RIGHT, 1, SIM_KEY_RIGHT); key_init(&k4, SIM_KEY_RIGHT, 1, SIM_Key_RIGHT);
key_init(&k5, SIM_Key_ENABLE, 1, SIM_KEY_ENABLE); key_init(&k5, SIM_KEY_ENABLE, 1, SIM_Key_ENABLE);
key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); key_init(&k6, SIM_KEY_SET, 1, SIM_Key_SET);
// key_init(&ks,Key_Pin,1,7); // key_init(&ks,Key_Pin,1,7);
key_attach(&k1, KEY_ALL_EVENT, Key_Call); key_attach(&k1, KEY_ALL_EVENT, Key_Call, true);
key_attach(&k2, KEY_ALL_EVENT, Key_Call); key_attach(&k2, KEY_ALL_EVENT, Key_Call, true);
key_attach(&k3, KEY_ALL_EVENT, Key_Call); key_attach(&k3, KEY_ALL_EVENT, Key_Call, true);
key_attach(&k4, KEY_ALL_EVENT, Key_Call); key_attach(&k4, KEY_ALL_EVENT, Key_Call, true);
key_attach(&k5, KEY_ALL_EVENT, Key_Call); key_attach(&k5, KEY_ALL_EVENT, Key_Call, true);
key_attach(&k6, KEY_ALL_EVENT, Key_Call); key_attach(&k6, KEY_ALL_EVENT, Key_Call, true);
// key_attach(&ks, KEY_ALL_EVENT, Key_Call); // key_attach(&ks, KEY_ALL_EVENT, Key_Call,true);
key_start(&k1);
key_start(&k2);
key_start(&k3);
key_start(&k4);
key_start(&k5);
key_start(&k6);
// key_start(&ks);
while (1) { while (1) {
// 每5ms调用一次key_ticks函数 // 每5ms调用一次key_ticks函数
key_ticks(); key_ticks();

View File

@ -69,13 +69,13 @@ void Key_Call_E(Key_t *key) {
void Get_Key(void *pVoid) { void Get_Key(void *pVoid) {
Key_t k1, k2, k3, k4, k5, k6, k7; Key_t k1, k2, k3, k4, k5, k6, k7;
key_init(&k1, SIM_Key_UP, 1, SIM_KEY_UP); key_init(&k1, SIM_KEY_UP, 1, SIM_Key_UP);
key_init(&k2, SIM_Key_DOWN, 1, SIM_KEY_DOWN); key_init(&k2, SIM_KEY_DOWN, 1, SIM_Key_DOWN);
key_init(&k3, SIM_Key_LEFT, 1, SIM_KEY_LEFT); key_init(&k3, SIM_KEY_LEFT, 1, SIM_Key_LEFT);
key_init(&k4, SIM_Key_RIGHT, 1, SIM_KEY_RIGHT); key_init(&k4, SIM_KEY_RIGHT, 1, SIM_Key_RIGHT);
key_init(&k5, SIM_Key_ENABLE, 1, SIM_KEY_ENABLE); key_init(&k5, SIM_KEY_ENABLE, 1, SIM_Key_ENABLE);
key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); key_init(&k6, SIM_KEY_SET, 1, SIM_Key_SET);
key_init(&k7, SIM_Key_RESET, 1, SIM_KEY_RESET); key_init(&k7, SIM_KEY_RESET, 1, SIM_Key_RESET);
key_attach(&k1, KEY_ALL_EVENT, Key_Call_E, true); key_attach(&k1, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true); key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true);

View File

@ -40,20 +40,20 @@ struct Key_List {
uint8_t active_level: 1; // 按键激活电平占1位 uint8_t active_level: 1; // 按键激活电平占1位
uint8_t key_level: 1; // 当前按键电平占1位 uint8_t key_level: 1; // 当前按键电平占1位
uint8_t key_id; // 按键ID uint8_t key_id; // 按键ID
uint8_t (*hal_key_Level)(uint8_t key_id_); // 函数指针,用于获取按键电平 uint8_t (*hal_read_pin)(uint8_t key_id_); // 函数指针,用于获取按键电平
Key_Callback_t cb[number_of_event]; // 按键事件回调函数数组 Key_Callback_t cb[number_of_event]; // 按键事件回调函数数组
}; };
/** /**
* @brief * @brief
* @param key: [] * @param key: []
* @param pin_level: [] * @param key_id: [] ID
* @param active_level: [] * @param active_level: []
* @param key_id: [] ID * @param read_pin: []
* @return void * @return void
* @example key_init(&my_key, get_pin_level, ACTIVE_HIGH, 1); * @example key_init(&my_key, 1, ACTIVE_HIGH, read_key_pin_func);
*/ **/
void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t key_id); void key_init(Key_t *key, uint8_t key_id, uint8_t active_level, uint8_t(*read_pin)(uint8_t));
/** /**
* @brief * @brief

View File

@ -11,10 +11,10 @@ static Key_t *head_key = NULL;
static void key_state_switch(Key_t *key); static void key_state_switch(Key_t *key);
void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t key_id) { void key_init(Key_t *key, uint8_t key_id, uint8_t active_level, uint8_t(*read_pin)(uint8_t)) {
memset(key, 0, sizeof(Key_t)); memset(key, 0, sizeof(Key_t));
key->event = (uint8_t) NONE_PRESS; key->event = (uint8_t) NONE_PRESS;
key->hal_key_Level = pin_level; key->hal_read_pin = read_pin;
key->key_level = !active_level; key->key_level = !active_level;
key->active_level = active_level; key->active_level = active_level;
key->key_id = key_id; key->key_id = key_id;
@ -40,7 +40,7 @@ PressEvent get_key_event(Key_t *key) {
static void key_state_switch(Key_t *key) { static void key_state_switch(Key_t *key) {
// 读取按键的GPIO电平 // 读取按键的GPIO电平
uint8_t read_gpio_level = key->hal_key_Level(key->key_id); uint8_t read_gpio_level = key->hal_read_pin(key->key_id);
// 如果按键状态大于0则增加ticks计数 // 如果按键状态大于0则增加ticks计数
if ((key->state) > 0) key->ticks++; if ((key->state) > 0) key->ticks++;