From 5356ee8cc5781c14fe0915df001abc9b30d619c0 Mon Sep 17 00:00:00 2001 From: zhaojuntao Date: Fri, 27 Dec 2019 15:12:15 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8D=20combos=20=E5=8D=95?= =?UTF-8?q?=E6=AC=A1=E7=9A=84=E6=AD=A7=E4=B9=89=EF=BC=8C=E6=84=BF=E6=84=8F?= =?UTF-8?q?=E6=98=AF=E6=83=B3=E7=94=A8=E4=BD=9C=E8=BF=9E=E5=87=BB=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E4=B8=8E=E7=BB=84=E5=90=88=E6=AD=A7=E4=B9=89=EF=BC=8C?= =?UTF-8?q?=E5=9B=A0=E6=AD=A4=E6=9B=BF=E6=8D=A2=E4=B8=BA=20multiple=20clic?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaojuntao --- README.md | 8 ++++---- flexible_button.c | 32 ++++++++++++++++---------------- flexible_button.h | 12 ++++++------ flexible_button_demo.c | 2 +- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5dfac8f..49925f1 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ static void common_btn_evt_cb(void *arg) { flex_button_t *btn = (flex_button_t *)arg; - rt_kprintf("id: [%d - %s] event: [%d - %30s] combos: %d\n", + rt_kprintf("id: [%d - %s] event: [%d - %30s] repeat: %d\n", btn->id, enum_btn_id_string[btn->id], btn->event, enum_event_string[btn->event], btn->click_cnt); @@ -194,7 +194,7 @@ typedef struct flex_button uint16_t scan_cnt; uint16_t click_cnt; - uint16_t max_combos_click_solt; + uint16_t max_multiple_clicks_interval; uint16_t debounce_tick; uint16_t short_press_start_tick; @@ -215,7 +215,7 @@ typedef struct flex_button | 3 | cb | 是 | 设置按键事件回调,用于应用层对按键事件的分类处理 | | 4 | scan_cnt | 否 | 用于记录扫描次数,按键按下是开始从零计数 | | 5 | click_cnt | 否 | 记录单击次数,用于判定单击、连击 | -| 6 | max_combos_click_solt | 是 | 连击间隙,用于判定是否结束连击计数,有默认值 `MAX_COMBOS_CLICK_SOLT` | +| 6 | max_multiple_clicks_interval | 是 | 连击间隙,用于判定是否结束连击计数,有默认值 `MAX_MULTIPLE_CLICKS_INTERVAL` | | 7 | debounce_tick | 否 | 消抖时间,暂未使用,依靠扫描间隙进行消抖 | | 8 | short_press_start_tick | 是 | 设置短按事件触发的起始 tick | | 9 | long_press_start_tick | 是 | 设置长按事件触发的起始 tick | @@ -225,7 +225,7 @@ typedef struct flex_button | 13 | event | 否 | 用于记录当前按键事件 | | 14 | status | 否 | 用于记录当前按键的状态,用于内部状态机 | -注意,在使用 `max_combos_click_solt`、`debounce_tick`、`short_press_start_tick`、`long_press_start_tick`、`long_hold_start_tick` 的时候,注意需要使用宏 `**FLEX_MS_TO_SCAN_CNT(ms)**` 将毫秒值转换为扫描次数。因为按键库基于扫描次数运转。示例如下: +注意,在使用 `max_multiple_clicks_interval`、`debounce_tick`、`short_press_start_tick`、`long_press_start_tick`、`long_hold_start_tick` 的时候,注意需要使用宏 `**FLEX_MS_TO_SCAN_CNT(ms)**` 将毫秒值转换为扫描次数。因为按键库基于扫描次数运转。示例如下: ``` user_button[1].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500); // 1500 毫秒 diff --git a/flexible_button.c b/flexible_button.c index b9b338c..7cbcdd8 100644 --- a/flexible_button.c +++ b/flexible_button.c @@ -24,7 +24,7 @@ * Date Author Notes * 2018-09-29 MurphyZhao First add * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account - * 2019-12-26 MurphyZhao Refactor code and implement combos + * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks * */ @@ -54,7 +54,7 @@ enum FLEX_BTN_STAGE { FLEX_BTN_STAGE_DEFAULT = 0, FLEX_BTN_STAGE_DOWN = 1, - FLEX_BTN_STAGE_COMBOS = 2 + FLEX_BTN_STAGE_MULTIPLE_CLICK = 2 }; typedef uint32_t btn_type_t; @@ -103,7 +103,7 @@ int8_t flex_button_register(flex_button_t *button) { if(curr == button) { - return -1; //already exist. + return -1; /* already exist. */ } curr = curr->next; } @@ -117,7 +117,7 @@ int8_t flex_button_register(flex_button_t *button) button->event = FLEX_BTN_PRESS_NONE; button->scan_cnt = 0; button->click_cnt = 0; - button->max_combos_click_solt = MAX_COMBOS_CLICK_SOLT; + button->max_multiple_clicks_interval = MAX_MULTIPLE_CLICKS_INTERVAL; btn_head = button; /** @@ -179,8 +179,8 @@ static void flex_button_process(void) switch (target->status) { - case FLEX_BTN_STAGE_DEFAULT: // stage: default(button up) - if (BTN_IS_PRESSED(i)) // is pressed + case FLEX_BTN_STAGE_DEFAULT: /* stage: default(button up) */ + if (BTN_IS_PRESSED(i)) /* is pressed */ { target->scan_cnt = 0; target->click_cnt = 0; @@ -196,12 +196,12 @@ static void flex_button_process(void) } break; - case FLEX_BTN_STAGE_DOWN: // stage: button down - if (BTN_IS_PRESSED(i)) // is pressed + case FLEX_BTN_STAGE_DOWN: /* stage: button down */ + if (BTN_IS_PRESSED(i)) /* is pressed */ { - if (target->click_cnt > 0) // combos + if (target->click_cnt > 0) /* multiple click */ { - if (target->scan_cnt > target->max_combos_click_solt) + if (target->scan_cnt > target->max_multiple_clicks_interval) { EVENT_SET_AND_EXEC_CB(target, target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ? @@ -236,7 +236,7 @@ static void flex_button_process(void) } } } - else // is up + else /* button up */ { if (target->scan_cnt >= target->long_hold_start_tick) { @@ -255,15 +255,15 @@ static void flex_button_process(void) } else { - /* swtich to combos stage */ - target->status = FLEX_BTN_STAGE_COMBOS; + /* swtich to multiple click stage */ + target->status = FLEX_BTN_STAGE_MULTIPLE_CLICK; target->click_cnt ++; } } break; - case FLEX_BTN_STAGE_COMBOS: // stage: combos - if (BTN_IS_PRESSED(i)) // is pressed + case FLEX_BTN_STAGE_MULTIPLE_CLICK: /* stage: multiple click */ + if (BTN_IS_PRESSED(i)) /* is pressed */ { /* swtich to button down stage */ target->status = FLEX_BTN_STAGE_DOWN; @@ -271,7 +271,7 @@ static void flex_button_process(void) } else { - if (target->scan_cnt > target->max_combos_click_solt) + if (target->scan_cnt > target->max_multiple_clicks_interval) { EVENT_SET_AND_EXEC_CB(target, target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ? diff --git a/flexible_button.h b/flexible_button.h index bd0b6a6..595c8bb 100644 --- a/flexible_button.h +++ b/flexible_button.h @@ -24,7 +24,7 @@ * Date Author Notes * 2018-09-29 MurphyZhao First add * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account - * 2019-12-26 MurphyZhao Refactor code and implement combos + * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks * */ @@ -36,8 +36,8 @@ #define FLEX_BTN_SCAN_FREQ_HZ 50 // How often flex_button_scan () is called #define FLEX_MS_TO_SCAN_CNT(ms) (ms / (1000 / FLEX_BTN_SCAN_FREQ_HZ)) -/* Combos slot, default 300ms */ -#define MAX_COMBOS_CLICK_SOLT (FLEX_MS_TO_SCAN_CNT(300)) +/* Multiple clicks interval, default 300ms */ +#define MAX_MULTIPLE_CLICKS_INTERVAL (FLEX_MS_TO_SCAN_CNT(300)) typedef void (*flex_button_response_callback)(void*); @@ -81,8 +81,8 @@ typedef enum * Internal use, user read-only. * Number of button clicks * - * @member max_combos_click_solt - * Combo slot. Default 'MAX_COMBOS_CLICK_SOLT'. + * @member max_multiple_clicks_interval + * Multiple click interval. Default 'MAX_MULTIPLE_CLICKS_INTERVAL'. * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts. * * @member debounce_tick @@ -128,7 +128,7 @@ typedef struct flex_button uint16_t scan_cnt; uint16_t click_cnt; - uint16_t max_combos_click_solt; + uint16_t max_multiple_clicks_interval; uint16_t debounce_tick; uint16_t short_press_start_tick; diff --git a/flexible_button_demo.c b/flexible_button_demo.c index dacf48b..d7e5954 100644 --- a/flexible_button_demo.c +++ b/flexible_button_demo.c @@ -114,7 +114,7 @@ static void common_btn_evt_cb(void *arg) { flex_button_t *btn = (flex_button_t *)arg; - rt_kprintf("id: [%d - %s] event: [%d - %30s] combos: %d\n", + rt_kprintf("id: [%d - %s] event: [%d - %30s] repeat: %d\n", btn->id, enum_btn_id_string[btn->id], btn->event, enum_event_string[btn->event], btn->click_cnt);