From 06fe54226c2d70fe1a70905e0a472812d9c138d9 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 25 Feb 2024 15:52:42 -0500 Subject: [PATCH 1/7] =?UTF-8?q?=E5=B0=86EVENT=5FCB(ev)=E5=AE=8F=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=B8=AD=E7=9A=84handle=E5=8F=82=E6=95=B0=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E4=B8=BABtnCallba?= =?UTF-8?q?ck=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89=E4=B8=80=E8=87=B4?= =?UTF-8?q?=EF=BC=8C=E8=80=8C=E4=B8=8D=E6=98=AF=E5=9C=A8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=97=B6=E9=80=9A=E8=BF=87=E4=BC=A0=E9=80=92?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=81=9A=E9=9A=90=E6=80=A7=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BD=AC=E6=8D=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- multi_button.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multi_button.c b/multi_button.c index 9d47afd..2457712 100644 --- a/multi_button.c +++ b/multi_button.c @@ -18,7 +18,7 @@ */ #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) static struct button* head_handle = NULL; From 80b2b48cde29f2eeeafe711b5836c7b10e30ddbc Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 25 Feb 2024 15:53:17 -0500 Subject: [PATCH 2/7] format code --- multi_button.c | 1 - 1 file changed, 1 deletion(-) diff --git a/multi_button.c b/multi_button.c index 2457712..3ac6dfa 100644 --- a/multi_button.c +++ b/multi_button.c @@ -117,7 +117,6 @@ static void button_handler(struct button* handle) EVENT_CB(PRESS_UP); handle->ticks = 0; handle->state = 2; - } else if(handle->ticks > LONG_TICKS) { From 5d00b96a85df1720da67ec078cb4b4630ed8b44e Mon Sep 17 00:00:00 2001 From: Riggin Date: Wed, 16 Aug 2023 18:59:33 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9E=E5=87=BB17?= =?UTF-8?q?=E6=AC=A1=E6=88=9618=E6=AC=A1=E5=90=8E=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E6=8C=89=E9=92=AE=EF=BC=8C=E4=BC=9A=E5=BC=82=E5=B8=B8=E8=A7=A6?= =?UTF-8?q?=E5=8F=91SINGLE=5FCLICK=E6=88=96DOUBLE=5FCLICK=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1,连击n次(n >= 3)后释放都应该看作PRESS_REPEAT事件,不可触发SINGLE_CLICK或DOUBLE_CLICK事件。 2,连击时限制repeat的最大值为(2^4 - 1)防止溢出。 --- multi_button.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/multi_button.c b/multi_button.c index 3ac6dfa..49924ce 100644 --- a/multi_button.c +++ b/multi_button.c @@ -19,6 +19,7 @@ #include "multi_button.h" #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; @@ -131,7 +132,10 @@ 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; From 443da0baf71ea4f20ad3bf0fd2e0f0e2c2434959 Mon Sep 17 00:00:00 2001 From: pony <1027287419@qq.com> Date: Wed, 24 Jan 2024 23:12:04 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=9C=A8=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=97=B6=E7=BB=93=E6=9E=84=E4=BD=93=E4=B8=AD=E7=9A=84button=5F?= =?UTF-8?q?level=E5=8F=98=E9=87=8F=E7=9B=B4=E6=8E=A5=E7=94=B1=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E7=94=B5=E5=B9=B3=E6=8E=A5=E5=8F=A3=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=AF=BC=E8=87=B4=E4=B8=8A?= =?UTF-8?q?=E7=94=B5=E7=9E=AC=E9=97=B4=E8=AF=AF=E8=AF=BB=E7=94=B5=E5=B9=B3?= =?UTF-8?q?=EF=BC=8C=E4=BB=8E=E8=80=8C=E5=AF=BC=E8=87=B4=E8=AF=AF=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/0x1abin/MultiButton/pull/42 --- multi_button.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multi_button.c b/multi_button.c index 49924ce..de8c26d 100644 --- a/multi_button.c +++ b/multi_button.c @@ -35,7 +35,7 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ 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; } From fea3dc3c5637e7616a8cbb8c28c66fe6ddffa456 Mon Sep 17 00:00:00 2001 From: KUAN <1062220953@qq.com> Date: Tue, 26 Dec 2023 17:29:52 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E7=9F=AD=E6=8C=89=E5=92=8C=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/0x1abin/MultiButton/pull/41 --- multi_button.c | 32 ++++++++++++++++++++++++++++---- multi_button.h | 4 ++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/multi_button.c b/multi_button.c index de8c26d..e401979 100644 --- a/multi_button.c +++ b/multi_button.c @@ -37,6 +37,8 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ handle->hal_button_Level = pin_level; handle->button_level = !active_level; handle->active_level = active_level; + handle->short_ticks = SHORT_TICKS; + handle->long_ticks = LONG_TICKS; } /** @@ -51,6 +53,28 @@ void button_attach(struct button* 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(struct Button* 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(struct Button* handle, uint16_t ticks) +{ + handle->long_ticks = ticks / TICKS_INTERVAL; +} + /** * @brief Inquire the button event happen. * @param handle: the button handle struct. @@ -119,7 +143,7 @@ static void button_handler(struct button* handle) 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); @@ -140,7 +164,7 @@ static void button_handler(struct button* handle) handle->ticks = 0; handle->state = 3; } - else if(handle->ticks > SHORT_TICKS) + else if(handle->ticks > handle->short_ticks) { if(handle->repeat == 1) { @@ -162,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; @@ -172,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; } diff --git a/multi_button.h b/multi_button.h index 02aa33d..790d3f2 100644 --- a/multi_button.h +++ b/multi_button.h @@ -27,6 +27,8 @@ typedef enum { typedef 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; @@ -48,6 +50,8 @@ PressEvent get_button_event(struct button* handle); int button_start(struct button* handle); void button_stop(struct button* handle); void button_ticks(void); +void button_set_short_ticks(struct Button* handle, uint16_t ticks); +void button_set_long_ticks(struct Button* handle, uint16_t ticks); #ifdef __cplusplus } From 992984f7ca927859e010465dbfb080fd5da1318f Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 7 Apr 2024 22:31:14 -0400 Subject: [PATCH 6/7] fix typo: Button -> button --- README.md | 10 +++++----- multi_button.c | 4 ++-- multi_button.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 44ea367..4d6b34d 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,7 +46,7 @@ while(1) { MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理: ```c -struct Button { +struct button { uint16_t ticks; uint8_t repeat: 4; uint8_t event : 4; @@ -56,10 +56,10 @@ struct Button { uint8_t button_level : 1; uint8_t (*hal_button_Level)(void); BtnCallback cb[number_of_event]; - struct Button* next; + struct button* next; }; ``` -这样每个按键使用单向链表相连,依次进入 button_handler(struct Button* handle) 状态机处理,所以每个按键的状态彼此独立。 +这样每个按键使用单向链表相连,依次进入 button_handler(struct button* handle) 状态机处理,所以每个按键的状态彼此独立。 ## 按键事件 @@ -80,7 +80,7 @@ LONG_PRESS_HOLD | 长按期间一直触发 ```c #include "button.h" -struct Button btn1; +struct button btn1; uint8_t read_button1_GPIO() { diff --git a/multi_button.c b/multi_button.c index e401979..c47a82d 100644 --- a/multi_button.c +++ b/multi_button.c @@ -59,7 +59,7 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb) * @param ticks: judge short ticks(unit:ms) * @retval None */ -void button_set_short_ticks(struct Button* handle, uint16_t ticks) +void button_set_short_ticks(struct button* handle, uint16_t ticks) { handle->short_ticks = ticks / TICKS_INTERVAL; } @@ -70,7 +70,7 @@ void button_set_short_ticks(struct Button* handle, uint16_t ticks) * @param ticks: judge long ticks(unit:ms) * @retval None */ -void button_set_long_ticks(struct Button* handle, uint16_t ticks) +void button_set_long_ticks(struct button* handle, uint16_t ticks) { handle->long_ticks = ticks / TICKS_INTERVAL; } diff --git a/multi_button.h b/multi_button.h index 790d3f2..e298ba2 100644 --- a/multi_button.h +++ b/multi_button.h @@ -50,8 +50,8 @@ PressEvent get_button_event(struct button* handle); int button_start(struct button* handle); void button_stop(struct button* handle); void button_ticks(void); -void button_set_short_ticks(struct Button* handle, uint16_t ticks); -void button_set_long_ticks(struct Button* handle, uint16_t ticks); +void button_set_short_ticks(struct button* handle, uint16_t ticks); +void button_set_long_ticks(struct button* handle, uint16_t ticks); #ifdef __cplusplus } From 3a0524f535d3a1c1c3ac85376500ca758cb26d86 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 7 Apr 2024 22:39:17 -0400 Subject: [PATCH 7/7] use button_t to replace struct button * --- README.md | 26 ++++++++++++++------------ examples/event_async.c | 4 ++-- examples/event_inquire.c | 2 +- multi_button.c | 26 +++++++++++++------------- multi_button.h | 22 ++++++++++++---------- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 4d6b34d..aff2417 100644 --- a/README.md +++ b/README.md @@ -47,19 +47,21 @@ 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; + 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,7 +80,7 @@ LONG_PRESS_HOLD | 长按期间一直触发 ## Examples ```c -#include "button.h" +#include struct button btn1; 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 c47a82d..5fadc55 100644 --- a/multi_button.c +++ b/multi_button.c @@ -21,7 +21,7 @@ #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. @@ -30,7 +30,7 @@ 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; @@ -48,7 +48,7 @@ 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; } @@ -59,7 +59,7 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb) * @param ticks: judge short ticks(unit:ms) * @retval None */ -void button_set_short_ticks(struct button* handle, uint16_t ticks) +void button_set_short_ticks(button_t handle, uint16_t ticks) { handle->short_ticks = ticks / TICKS_INTERVAL; } @@ -70,7 +70,7 @@ void button_set_short_ticks(struct button* handle, uint16_t ticks) * @param ticks: judge long ticks(unit:ms) * @retval None */ -void button_set_long_ticks(struct button* handle, uint16_t ticks) +void button_set_long_ticks(button_t handle, uint16_t ticks) { handle->long_ticks = ticks / TICKS_INTERVAL; } @@ -80,7 +80,7 @@ void button_set_long_ticks(struct button* handle, uint16_t ticks) * @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); } @@ -90,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(); @@ -231,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) { @@ -256,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) { @@ -283,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 e298ba2..851a5c5 100644 --- a/multi_button.h +++ b/multi_button.h @@ -25,7 +25,9 @@ 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; @@ -37,21 +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(struct button* handle, uint16_t ticks); -void button_set_long_ticks(struct button* handle, uint16_t ticks); +void button_set_short_ticks(button_t handle, uint16_t ticks); +void button_set_long_ticks(button_t handle, uint16_t ticks); #ifdef __cplusplus }