Skip to content

Commit

Permalink
[fix] 修复 combos 单次的歧义,愿意是想用作连击,但与组合歧义,因此替换为 multiple click
Browse files Browse the repository at this point in the history
Signed-off-by: zhaojuntao <[email protected]>
  • Loading branch information
murphyzhao committed Dec 27, 2019
1 parent 292c8ad commit 5356ee8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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 |
Expand All @@ -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 毫秒
Expand Down
32 changes: 16 additions & 16 deletions flexible_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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 ?
Expand Down Expand Up @@ -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)
{
Expand All @@ -255,23 +255,23 @@ 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;
target->scan_cnt = 0;
}
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 ?
Expand Down
12 changes: 6 additions & 6 deletions flexible_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/

Expand All @@ -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*);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion flexible_button_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5356ee8

Please sign in to comment.