diff --git a/README.md b/README.md index 44ea367..aff2417 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ MultiButton的作者是0x1abin, github地址: https://github.com/0x1abin/MultiBu 1.先申请一个按键结构 ```c -struct Button button1; +struct button button1; ``` 2.初始化按键对象,绑定按键的GPIO电平读取接口**read_button_pin()** ,后一个参数设置有效触发电平 @@ -46,20 +46,22 @@ while(1) { MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理: ```c -struct Button { - uint16_t ticks; - uint8_t repeat: 4; - uint8_t event : 4; - uint8_t state : 3; - uint8_t debounce_cnt : 3; - uint8_t active_level : 1; - uint8_t button_level : 1; - uint8_t (*hal_button_Level)(void); - BtnCallback cb[number_of_event]; - struct Button* next; +struct button { + uint16_t ticks; + uint16_t short_ticks; + uint16_t long_ticks; + uint8_t repeat : 4; + uint8_t event : 4; + uint8_t state : 3; + uint8_t debounce_cnt : 3; + uint8_t active_level : 1; + uint8_t button_level : 1; + uint8_t (*hal_button_Level)(void); + BtnCallback cb[number_of_event]; + button_t next; }; ``` -这样每个按键使用单向链表相连,依次进入 button_handler(struct Button* handle) 状态机处理,所以每个按键的状态彼此独立。 +这样每个按键使用单向链表相连,依次进入 button_handler(button_t handle) 状态机处理,所以每个按键的状态彼此独立。 ## 按键事件 @@ -78,9 +80,9 @@ LONG_PRESS_HOLD | 长按期间一直触发 ## Examples ```c -#include "button.h" +#include -struct Button btn1; +struct button btn1; uint8_t read_button1_GPIO() { diff --git a/examples/event_async.c b/examples/event_async.c index c14068b..83ecb4f 100644 --- a/examples/event_async.c +++ b/examples/event_async.c @@ -1,6 +1,6 @@ #include #include -#include "multi_button.h" +#include static struct button btn; @@ -15,7 +15,7 @@ void button_callback(void *btn) { uint32_t btn_event_val; - btn_event_val = get_button_event((struct button *)btn); + btn_event_val = get_button_event((button_t)btn); switch(btn_event_val) { diff --git a/examples/event_inquire.c b/examples/event_inquire.c index a84eaed..bcf6d72 100644 --- a/examples/event_inquire.c +++ b/examples/event_inquire.c @@ -1,6 +1,6 @@ #include #include -#include "multi_button.h" +#include static struct button btn; diff --git a/multi_button.c b/multi_button.c index 9d47afd..5fadc55 100644 --- a/multi_button.c +++ b/multi_button.c @@ -18,9 +18,10 @@ */ #include "multi_button.h" -#define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((button*)handle) +#define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((void*)handle) +#define PRESS_REPEAT_MAX_NUM 15 /*!< The maximum value of the repeat counter */ -static struct button* head_handle = NULL; +static button_t head_handle = NULL; /** * @brief Initializes the button struct handle. @@ -29,13 +30,15 @@ static struct button* head_handle = NULL; * @param active_level: pin pressed level. * @retval None */ -void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level) +void button_init(button_t handle, uint8_t(*pin_level)(void), 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(); + handle->button_level = !active_level; handle->active_level = active_level; + handle->short_ticks = SHORT_TICKS; + handle->long_ticks = LONG_TICKS; } /** @@ -45,17 +48,39 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ * @param cb: callback function. * @retval None */ -void button_attach(struct button* handle, PressEvent event, BtnCallback cb) +void button_attach(button_t handle, PressEvent event, BtnCallback cb) { handle->cb[event] = cb; } +/** + * @brief Attach the button adjust ticks + * @param handle: the button handle strcut. + * @param ticks: judge short ticks(unit:ms) + * @retval None + */ +void button_set_short_ticks(button_t handle, uint16_t ticks) +{ + handle->short_ticks = ticks / TICKS_INTERVAL; +} + +/** + * @brief Attach the button adjust long ticks + * @param handle: the button handle strcut. + * @param ticks: judge long ticks(unit:ms) + * @retval None + */ +void button_set_long_ticks(button_t handle, uint16_t ticks) +{ + handle->long_ticks = ticks / TICKS_INTERVAL; +} + /** * @brief Inquire the button event happen. * @param handle: the button handle struct. * @retval button event. */ -PressEvent get_button_event(struct button* handle) +PressEvent get_button_event(button_t handle) { return (PressEvent)(handle->event); } @@ -65,7 +90,7 @@ PressEvent get_button_event(struct button* handle) * @param handle: the button handle struct. * @retval None */ -static void button_handler(struct button* handle) +static void button_handler(button_t handle) { uint8_t read_gpio_level = handle->hal_button_Level(); @@ -117,9 +142,8 @@ static void button_handler(struct button* handle) EVENT_CB(PRESS_UP); handle->ticks = 0; handle->state = 2; - } - else if(handle->ticks > LONG_TICKS) + else if(handle->ticks > handle->long_ticks) { handle->event = (uint8_t)LONG_PRESS_START; EVENT_CB(LONG_PRESS_START); @@ -132,12 +156,15 @@ static void button_handler(struct button* handle) { handle->event = (uint8_t)PRESS_DOWN; EVENT_CB(PRESS_DOWN); - handle->repeat++; + if(handle->repeat != PRESS_REPEAT_MAX_NUM) + { + handle->repeat++; + } EVENT_CB(PRESS_REPEAT); handle->ticks = 0; handle->state = 3; } - else if(handle->ticks > SHORT_TICKS) + else if(handle->ticks > handle->short_ticks) { if(handle->repeat == 1) { @@ -159,7 +186,7 @@ static void button_handler(struct button* handle) handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); - if(handle->ticks < SHORT_TICKS) + if(handle->ticks < handle->short_ticks) { handle->ticks = 0; handle->state = 2; @@ -169,7 +196,7 @@ static void button_handler(struct button* handle) handle->state = 0; } } - else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS + else if(handle->ticks > handle->short_ticks) // SHORT_TICKS < press down hold time < LONG_TICKS { handle->state = 1; } @@ -204,9 +231,9 @@ static void button_handler(struct button* handle) * @param handle: target handle struct. * @retval 0: succeed. -1: already exist. */ -int button_start(struct button* handle) +int button_start(button_t handle) { - struct button* target = head_handle; + button_t target = head_handle; while(target) { @@ -229,13 +256,13 @@ int button_start(struct button* handle) * @param handle: target handle struct. * @retval None */ -void button_stop(struct button* handle) +void button_stop(button_t handle) { - struct button** curr; + button_t* curr; for(curr = &head_handle; *curr;) { - struct button* entry = *curr; + button_t entry = *curr; if (entry == handle) { @@ -256,7 +283,7 @@ void button_stop(struct button* handle) */ void button_ticks(void) { - struct button* target; + button_t target; for(target = head_handle; target != NULL; target = target->next) { diff --git a/multi_button.h b/multi_button.h index 02aa33d..851a5c5 100644 --- a/multi_button.h +++ b/multi_button.h @@ -25,8 +25,12 @@ typedef enum { NONE_PRESS }PressEvent; -typedef struct button { + +typedef struct button *button_t; +struct button { uint16_t ticks; + uint16_t short_ticks; + uint16_t long_ticks; uint8_t repeat : 4; uint8_t event : 4; uint8_t state : 3; @@ -35,19 +39,21 @@ typedef struct button { uint8_t button_level : 1; uint8_t (*hal_button_Level)(void); BtnCallback cb[number_of_event]; - struct button* next; -}button; + button_t next; +}; #ifdef __cplusplus extern "C" { #endif -void button_init(struct button* handle, uint8_t(*pin_level)(void), 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); -void button_stop(struct button* handle); +void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level); +void button_attach(button_t handle, PressEvent event, BtnCallback cb); +PressEvent get_button_event(button_t handle); +int button_start(button_t handle); +void button_stop(button_t handle); void button_ticks(void); +void button_set_short_ticks(button_t handle, uint16_t ticks); +void button_set_long_ticks(button_t handle, uint16_t ticks); #ifdef __cplusplus }