diff --git a/demo/key/test.c b/demo/key/test.c index 0b4cba0..8ef53de 100644 --- a/demo/key/test.c +++ b/demo/key/test.c @@ -40,29 +40,22 @@ void Key_Call(Key_t *key) { void Test_Key() { Key_t k1, k2, k3, k4, k5, k6, ks; - key_init(&k1, SIM_Key_UP, 1, SIM_KEY_UP); - key_init(&k2, SIM_Key_DOWN, 1, SIM_KEY_DOWN); - key_init(&k3, SIM_Key_LEFT, 1, SIM_KEY_LEFT); - key_init(&k4, SIM_Key_RIGHT, 1, SIM_KEY_RIGHT); - key_init(&k5, SIM_Key_ENABLE, 1, SIM_KEY_ENABLE); - key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); + key_init(&k1, SIM_KEY_UP, 1, SIM_Key_UP); + key_init(&k2, SIM_KEY_DOWN, 1, SIM_Key_DOWN); + key_init(&k3, SIM_KEY_LEFT, 1, SIM_Key_LEFT); + key_init(&k4, SIM_KEY_RIGHT, 1, SIM_Key_RIGHT); + key_init(&k5, SIM_KEY_ENABLE, 1, SIM_Key_ENABLE); + key_init(&k6, SIM_KEY_SET, 1, SIM_Key_SET); // key_init(&ks,Key_Pin,1,7); - key_attach(&k1, KEY_ALL_EVENT, Key_Call); - key_attach(&k2, KEY_ALL_EVENT, Key_Call); - key_attach(&k3, KEY_ALL_EVENT, Key_Call); - key_attach(&k4, KEY_ALL_EVENT, Key_Call); - key_attach(&k5, KEY_ALL_EVENT, Key_Call); - key_attach(&k6, KEY_ALL_EVENT, Key_Call); -// key_attach(&ks, KEY_ALL_EVENT, Key_Call); + key_attach(&k1, KEY_ALL_EVENT, Key_Call, true); + key_attach(&k2, KEY_ALL_EVENT, Key_Call, true); + key_attach(&k3, KEY_ALL_EVENT, Key_Call, true); + key_attach(&k4, KEY_ALL_EVENT, Key_Call, true); + key_attach(&k5, KEY_ALL_EVENT, Key_Call, true); + key_attach(&k6, KEY_ALL_EVENT, Key_Call, true); +// 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) { // 每5ms调用一次key_ticks函数 key_ticks(); diff --git a/demo/oled/test.c b/demo/oled/test.c index 9d75329..9b37147 100644 --- a/demo/oled/test.c +++ b/demo/oled/test.c @@ -69,13 +69,13 @@ void Key_Call_E(Key_t *key) { void Get_Key(void *pVoid) { Key_t k1, k2, k3, k4, k5, k6, k7; - key_init(&k1, SIM_Key_UP, 1, SIM_KEY_UP); - key_init(&k2, SIM_Key_DOWN, 1, SIM_KEY_DOWN); - key_init(&k3, SIM_Key_LEFT, 1, SIM_KEY_LEFT); - key_init(&k4, SIM_Key_RIGHT, 1, SIM_KEY_RIGHT); - key_init(&k5, SIM_Key_ENABLE, 1, SIM_KEY_ENABLE); - key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); - key_init(&k7, SIM_Key_RESET, 1, SIM_KEY_RESET); + key_init(&k1, SIM_KEY_UP, 1, SIM_Key_UP); + key_init(&k2, SIM_KEY_DOWN, 1, SIM_Key_DOWN); + key_init(&k3, SIM_KEY_LEFT, 1, SIM_Key_LEFT); + key_init(&k4, SIM_KEY_RIGHT, 1, SIM_Key_RIGHT); + key_init(&k5, SIM_KEY_ENABLE, 1, SIM_Key_ENABLE); + key_init(&k6, SIM_KEY_SET, 1, SIM_Key_SET); + key_init(&k7, SIM_KEY_RESET, 1, SIM_Key_RESET); key_attach(&k1, KEY_ALL_EVENT, Key_Call_E, true); key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true); diff --git a/lib/inc/key/key.h b/lib/inc/key/key.h index 1c5d73b..a23a5fe 100644 --- a/lib/inc/key/key.h +++ b/lib/inc/key/key.h @@ -40,20 +40,20 @@ struct Key_List { uint8_t active_level: 1; // 按键激活电平,占1位 uint8_t key_level: 1; // 当前按键电平,占1位 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]; // 按键事件回调函数数组 }; /** - * @brief 初始化按键模块 - * @param key: [输入] 指向按键结构体的指针 - * @param pin_level: [输入] 获取按键电平的回调函数 - * @param active_level: [输入] 激活电平 - * @param key_id: [输入] 按键ID + * @brief 初始化按键结构体 + * @param key: [输入] 按键结构体指针 + * @param key_id: [输入] 按键ID + * @param active_level: [输入] 激活电平 + * @param read_pin: [输入] 读取按键状态的函数指针 * @return void - * @example key_init(&my_key, get_pin_level, ACTIVE_HIGH, 1); - */ -void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t key_id); + * @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)); /** * @brief 为按键设置回调函数 diff --git a/lib/src/key/key.cpp b/lib/src/key/key.cpp index 5338bb1..27739fd 100644 --- a/lib/src/key/key.cpp +++ b/lib/src/key/key.cpp @@ -11,10 +11,10 @@ static Key_t *head_key = NULL; 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)); key->event = (uint8_t) NONE_PRESS; - key->hal_key_Level = pin_level; + key->hal_read_pin = read_pin; key->key_level = !active_level; key->active_level = active_level; key->key_id = key_id; @@ -40,7 +40,7 @@ PressEvent get_key_event(Key_t *key) { static void key_state_switch(Key_t *key) { // 读取按键的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计数 if ((key->state) > 0) key->ticks++;