From 71c11b00de8bdad14ad13b6bb4c161ab764a81d0 Mon Sep 17 00:00:00 2001 From: IceBreak Date: Mon, 22 Aug 2022 11:29:40 +0800 Subject: [PATCH] fix: drop button id func implement --- README.md | 23 +++++------------------ examples/example_callback.c | 29 +++++++++++------------------ examples/example_poll.c | 27 ++++++++++----------------- multi_button.c | 12 ++++-------- multi_button.h | 5 ++--- 5 files changed, 32 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index c74d19b..ea55082 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ struct Button button1; 2.初始化按键对象,绑定按键的GPIO电平读取接口**read_button_pin()** ,后一个参数设置有效触发电平 ```c -button_init(&button1, read_button_pin, 0, 0); +button_init(&button1, read_button_pin, 0); ``` 3.注册按键事件 @@ -52,8 +52,7 @@ struct Button { uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; - uint8_t button_id; - uint8_t (*hal_button_Level)(uint8_t button_id_); + uint8_t (*hal_button_Level)(void); BtnCallback cb[number_of_event]; struct Button* next; }; @@ -79,23 +78,11 @@ LONG_PRESS_HOLD | 长按期间一直触发 ```c #include "button.h" -unit8_t btn1_id = 0; - struct Button btn1; -uint8_t read_button_GPIO(uint8_t button_id) +uint8_t read_button1_GPIO() { - // you can share the GPIO read function with multiple Buttons - switch(button_id) - { - case btn1_id: - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); - break; - - default: - return 0; - break; - } + return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); } void BTN1_PRESS_DOWN_Handler(void* btn) { @@ -111,7 +98,7 @@ void BTN1_PRESS_UP_Handler(void* btn) int main() { - button_init(&btn1, read_button_GPIO, 0, btn1_id); + button_init(&btn1, read_button1_GPIO, 0); button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler); button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler); button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler); diff --git a/examples/example_callback.c b/examples/example_callback.c index 99ffff2..d774069 100644 --- a/examples/example_callback.c +++ b/examples/example_callback.c @@ -1,31 +1,22 @@ #include "multi_button.h" -enum Button_IDs { - btn1_id, - btn2_id, -}; - struct Button btn1; struct Button btn2; -uint8_t read_button_GPIO(uint8_t button_id) +uint8_t read_button1_GPIO() { - // you can share the GPIO read function with multiple Buttons - switch(button_id) - { - case btn1_id: - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); - case btn2_id: - return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin); - default: - return 0; - } + return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); +} + +uint8_t read_button2_GPIO() +{ + return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin); } int main() { - button_init(&btn1, read_button_GPIO, 0, btn1_id); - button_init(&btn2, read_button_GPIO, 0, btn2_id); + button_init(&btn1, read_button1_GPIO, 0); + button_init(&btn2, read_button2_GPIO, 0); button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler); button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler); @@ -63,3 +54,5 @@ void BTN1_PRESS_UP_Handler(void* btn) { //do something... } + +... \ No newline at end of file diff --git a/examples/example_poll.c b/examples/example_poll.c index eb06029..054cd72 100644 --- a/examples/example_poll.c +++ b/examples/example_poll.c @@ -1,37 +1,29 @@ #include "multi_button.h" -unit8_t btn1_id = 0; struct Button btn1; -uint8_t read_button_GPIO(uint8_t button_id) +uint8_t read_button1_GPIO() { - // you can share the GPIO read function with multiple Buttons - switch(button_id) - { - case btn1_id: - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); - default: - return 0; - } + return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); } int main() { static uint8_t btn1_event_val; - - button_init(&btn1, read_button_GPIO, 0, btn1_id); + + button_init(&btn1, read_button1_GPIO, 0); button_start(&btn1); - + //make the timer invoking the button_ticks() interval 5ms. //This function is implemented by yourself. - __timer_start(button_ticks, 0, 5); - - while(1) + __timer_start(button_ticks, 0, 5); + + while(1) { if(btn1_event_val != get_button_event(&btn1)) { btn1_event_val = get_button_event(&btn1); - + if(btn1_event_val == PRESS_DOWN) { //do something } else if(btn1_event_val == PRESS_UP) { @@ -42,3 +34,4 @@ int main() } } } + diff --git a/multi_button.c b/multi_button.c index 1061e68..762c797 100644 --- a/multi_button.c +++ b/multi_button.c @@ -15,17 +15,15 @@ static struct Button* head_handle = NULL; * @param handle: the button handle strcut. * @param pin_level: read the HAL GPIO of the connet button level. * @param active_level: pressed GPIO level. - * @param button_id: the button id. * @retval None */ -void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level, uint8_t button_id) +void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level) { memset(handle, 0, sizeof(struct Button)); handle->event = (uint8_t)NONE_PRESS; handle->hal_button_Level = pin_level; - handle->button_level = handle->hal_button_Level(button_id); + handle->button_level = handle->hal_button_Level(); handle->active_level = active_level; - handle->button_id = button_id; } /** @@ -57,7 +55,7 @@ PressEvent get_button_event(struct Button* handle) */ void button_handler(struct Button* handle) { - uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id); + uint8_t read_gpio_level = handle->hal_button_Level(); //ticks counter working.. if((handle->state) > 0) handle->ticks++; @@ -148,9 +146,6 @@ void button_handler(struct Button* handle) handle->state = 0; //reset } break; - default: - handle->state = 0; //reset - break; } } @@ -202,3 +197,4 @@ void button_ticks() button_handler(target); } } + diff --git a/multi_button.h b/multi_button.h index b066ade..6cf54d7 100644 --- a/multi_button.h +++ b/multi_button.h @@ -38,8 +38,7 @@ typedef struct Button { uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; - uint8_t button_id; - uint8_t (*hal_button_Level)(uint8_t button_id_); + uint8_t (*hal_button_Level)(void); BtnCallback cb[number_of_event]; struct Button* next; }Button; @@ -48,7 +47,7 @@ typedef struct Button { extern "C" { #endif -void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level, uint8_t button_id); +void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level); void button_attach(struct Button* handle, PressEvent event, BtnCallback cb); PressEvent get_button_event(struct Button* handle); int button_start(struct Button* handle);