diff --git a/README.md b/README.md index ea55082..c74d19b 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); +button_init(&button1, read_button_pin, 0, 0); ``` 3.注册按键事件 @@ -52,7 +52,8 @@ struct Button { uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; - uint8_t (*hal_button_Level)(void); + uint8_t button_id; + uint8_t (*hal_button_Level)(uint8_t button_id_); BtnCallback cb[number_of_event]; struct Button* next; }; @@ -78,11 +79,23 @@ LONG_PRESS_HOLD | 长按期间一直触发 ```c #include "button.h" +unit8_t btn1_id = 0; + struct Button btn1; -uint8_t read_button1_GPIO() +uint8_t read_button_GPIO(uint8_t button_id) { - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); + // 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; + } } void BTN1_PRESS_DOWN_Handler(void* btn) { @@ -98,7 +111,7 @@ void BTN1_PRESS_UP_Handler(void* btn) int main() { - button_init(&btn1, read_button1_GPIO, 0); + button_init(&btn1, read_button_GPIO, 0, btn1_id); 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 d774069..2c9f267 100644 --- a/examples/example_callback.c +++ b/examples/example_callback.c @@ -1,22 +1,36 @@ #include "multi_button.h" +enum Button_IDs { + btn1_id, + btn2_id, +}; + struct Button btn1; struct Button btn2; -uint8_t read_button1_GPIO() +uint8_t read_button_GPIO(uint8_t button_id) { - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); -} + // 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; -uint8_t read_button2_GPIO() -{ - return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin); + case btn2_id: + return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin); + break; + + default: + return 0; + break; + } } int main() { - button_init(&btn1, read_button1_GPIO, 0); - button_init(&btn2, read_button2_GPIO, 0); + button_init(&btn1, read_button_GPIO, 0, btn1_id); + button_init(&btn2, read_button_GPIO, 0, btn2_id); button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler); button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler); @@ -54,5 +68,3 @@ 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 054cd72..162b357 100644 --- a/examples/example_poll.c +++ b/examples/example_poll.c @@ -1,10 +1,21 @@ #include "multi_button.h" +unit8_t btn1_id = 0; struct Button btn1; -uint8_t read_button1_GPIO() +uint8_t read_button_GPIO(uint8_t button_id) { - return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); + // 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; + } } @@ -12,7 +23,7 @@ int main() { static uint8_t btn1_event_val; - button_init(&btn1, read_button1_GPIO, 0); + button_init(&btn1, read_button_GPIO, 0, btn1_id); button_start(&btn1); //make the timer invoking the button_ticks() interval 5ms. diff --git a/multi_button.c b/multi_button.c index 762c797..878885d 100644 --- a/multi_button.c +++ b/multi_button.c @@ -15,15 +15,17 @@ 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) +void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level, uint8_t button_id) { 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(); + handle->button_level = handle->hal_button_Level(button_id); handle->active_level = active_level; + handle->button_id = button_id; } /** @@ -55,7 +57,7 @@ PressEvent get_button_event(struct Button* handle) */ void button_handler(struct Button* handle) { - uint8_t read_gpio_level = handle->hal_button_Level(); + uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id); //ticks counter working.. if((handle->state) > 0) handle->ticks++; diff --git a/multi_button.h b/multi_button.h index 6cf54d7..b066ade 100644 --- a/multi_button.h +++ b/multi_button.h @@ -38,7 +38,8 @@ typedef struct Button { uint8_t debounce_cnt : 3; uint8_t active_level : 1; uint8_t button_level : 1; - uint8_t (*hal_button_Level)(void); + uint8_t button_id; + uint8_t (*hal_button_Level)(uint8_t button_id_); BtnCallback cb[number_of_event]; struct Button* next; }Button; @@ -47,7 +48,7 @@ typedef struct Button { extern "C" { #endif -void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level); +void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level, uint8_t button_id); void button_attach(struct Button* handle, PressEvent event, BtnCallback cb); PressEvent get_button_event(struct Button* handle); int button_start(struct Button* handle);