From 3e5fb48a6b775bcff750433f7a9e1291d5965422 Mon Sep 17 00:00:00 2001 From: Riggin Date: Wed, 16 Aug 2023 18:26:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8B=BC=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20&&=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1,将EVENT_CB(ev)宏函数中的handle参数强制类型转换为BtnCallback类型定义一致,而不是在函数调用时通过传递参数做隐性的类型转换。 2,#include 改用<>,引入系统头文件。 --- multi_button.c | 4 ++-- multi_button.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/multi_button.c b/multi_button.c index d1861e3..51febe6 100644 --- a/multi_button.c +++ b/multi_button.c @@ -5,7 +5,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) //button handle list head. static struct Button* head_handle = NULL; @@ -142,7 +142,7 @@ static void button_handler(struct Button* handle) //continue hold trigger handle->event = (uint8_t)LONG_PRESS_HOLD; EVENT_CB(LONG_PRESS_HOLD); - } else { //releasd + } else { //released handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); handle->state = 0; //reset diff --git a/multi_button.h b/multi_button.h index a5bfc99..2974168 100644 --- a/multi_button.h +++ b/multi_button.h @@ -6,8 +6,8 @@ #ifndef _MULTI_BUTTON_H_ #define _MULTI_BUTTON_H_ -#include "stdint.h" -#include "string.h" +#include +#include //According to your need to modify the constants. #define TICKS_INTERVAL 5 //ms From 2e68340382fe07e687595303c900f667d544d53f Mon Sep 17 00:00:00 2001 From: Riggin Date: Wed, 16 Aug 2023 18:45:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8C=89=E9=94=AE?= =?UTF-8?q?=E2=80=9C=E5=A4=9A=E6=AC=A1=E6=8C=89=E4=B8=8B=E2=80=9D=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=88=B0=E2=80=9C=E9=95=BF=E6=8C=89=E2=80=9D=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=97=B6=E4=BC=9A=E5=A4=9A=E8=A7=A6=E5=8F=91=E4=B8=80?= =?UTF-8?q?=E6=AC=A1PRESS=5FDOWN=E4=BA=8B=E4=BB=B6=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1,修改状态机切换的状态。 2,修改错误的代码注释。 --- multi_button.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/multi_button.c b/multi_button.c index 51febe6..bf4037d 100644 --- a/multi_button.c +++ b/multi_button.c @@ -132,8 +132,8 @@ static void button_handler(struct Button* handle) } else { handle->state = 0; } - } else if(handle->ticks > SHORT_TICKS) { // long press up - handle->state = 0; + } else if(handle->ticks > SHORT_TICKS) { // SHORT_TICKS < press down hold time < LONG_TICKS + handle->state = 1; } break; From b5e193f55d038999fd1bbf95c4891bff5bb0ce98 Mon Sep 17 00:00:00 2001 From: Riggin Date: Wed, 16 Aug 2023 18:59:33 +0800 Subject: [PATCH 3/3] =?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 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/multi_button.c b/multi_button.c index bf4037d..ca366f7 100644 --- a/multi_button.c +++ b/multi_button.c @@ -6,6 +6,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 */ //button handle list head. static struct Button* head_handle = NULL; @@ -106,7 +107,9 @@ static void button_handler(struct Button* handle) if(handle->button_level == handle->active_level) { //press down again 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); // repeat hit handle->ticks = 0; handle->state = 3;