From 456ec801520fb860c6649e59d8f311b306df1e6c Mon Sep 17 00:00:00 2001 From: KUAN <1062220953@qq.com> Date: Tue, 26 Dec 2023 17:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8B=AC=E7=AB=8B=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E7=9F=AD=E6=8C=89=E5=92=8C=E9=95=BF=E6=8C=89=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 ca366f7..7de9167 100644 --- a/multi_button.c +++ b/multi_button.c @@ -29,6 +29,8 @@ void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t ac handle->button_level = handle->hal_button_Level(button_id); handle->active_level = active_level; handle->button_id = button_id; + handle->judge_short_ticks = SHORT_TICKS; + handle->judge_long_ticks = LONG_TICKS; } /** @@ -43,6 +45,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_judge_short_ticks_attach(struct Button* handle, uint16_t ticks) +{ + handle->judge_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_judge_long_ticks_attach(struct Button* handle, uint16_t ticks) +{ + handle->judge_long_ticks = ticks/TICKS_INTERVAL; +} + /** * @brief Inquire the button event happen. * @param handle: the button handle struct. @@ -96,7 +120,7 @@ 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->judge_long_ticks) { handle->event = (uint8_t)LONG_PRESS_START; EVENT_CB(LONG_PRESS_START); handle->state = 5; @@ -113,7 +137,7 @@ static void button_handler(struct Button* handle) EVENT_CB(PRESS_REPEAT); // repeat hit handle->ticks = 0; handle->state = 3; - } else if(handle->ticks > SHORT_TICKS) { //released timeout + } else if(handle->ticks > handle->judge_short_ticks) { //released timeout if(handle->repeat == 1) { handle->event = (uint8_t)SINGLE_CLICK; EVENT_CB(SINGLE_CLICK); @@ -129,13 +153,13 @@ static void button_handler(struct Button* handle) if(handle->button_level != handle->active_level) { //released press up handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); - if(handle->ticks < SHORT_TICKS) { + if(handle->ticks < handle->judge_short_ticks) { handle->ticks = 0; handle->state = 2; //repeat press } else { handle->state = 0; } - } else if(handle->ticks > SHORT_TICKS) { // SHORT_TICKS < press down hold time < LONG_TICKS + } else if(handle->ticks > handle->judge_short_ticks) { // SHORT_TICKS < press down hold time < LONG_TICKS handle->state = 1; } break; diff --git a/multi_button.h b/multi_button.h index 2974168..bb2aa07 100644 --- a/multi_button.h +++ b/multi_button.h @@ -32,6 +32,8 @@ typedef enum { typedef struct Button { uint16_t ticks; + uint16_t judge_short_ticks; + uint16_t judge_long_ticks; uint8_t repeat : 4; uint8_t event : 4; uint8_t state : 3; @@ -50,6 +52,8 @@ extern "C" { void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id); void button_attach(struct Button* handle, PressEvent event, BtnCallback cb); +void button_judge_short_ticks_attach(struct Button* handle, uint16_t ticks); +void button_judge_long_ticks_attach(struct Button* handle, uint16_t ticks); PressEvent get_button_event(struct Button* handle); int button_start(struct Button* handle); void button_stop(struct Button* handle);