Skip to content

Commit b6ea6d2

Browse files
MRkuanmysterywolf
authored andcommitted
增加独立按键短按和长按时间控制
0x1abin/MultiButton#41
1 parent 443da0b commit b6ea6d2

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

multi_button.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ
3737
handle->hal_button_Level = pin_level;
3838
handle->button_level = !active_level;
3939
handle->active_level = active_level;
40+
handle->short_ticks = SHORT_TICKS;
41+
handle->long_ticks = LONG_TICKS;
4042
}
4143

4244
/**
@@ -51,6 +53,28 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
5153
handle->cb[event] = cb;
5254
}
5355

56+
/**
57+
* @brief Attach the button adjust ticks
58+
* @param handle: the button handle strcut.
59+
* @param ticks: judge short ticks(unit:ms)
60+
* @retval None
61+
*/
62+
void button_set_short_ticks(struct Button* handle, uint16_t ticks)
63+
{
64+
handle->short_ticks = ticks / TICKS_INTERVAL;
65+
}
66+
67+
/**
68+
* @brief Attach the button adjust long ticks
69+
* @param handle: the button handle strcut.
70+
* @param ticks: judge long ticks(unit:ms)
71+
* @retval None
72+
*/
73+
void button_set_long_ticks(struct Button* handle, uint16_t ticks)
74+
{
75+
handle->long_ticks = ticks / TICKS_INTERVAL;
76+
}
77+
5478
/**
5579
* @brief Inquire the button event happen.
5680
* @param handle: the button handle struct.
@@ -119,7 +143,7 @@ static void button_handler(struct button* handle)
119143
handle->ticks = 0;
120144
handle->state = 2;
121145
}
122-
else if(handle->ticks > LONG_TICKS)
146+
else if(handle->ticks > handle->long_ticks)
123147
{
124148
handle->event = (uint8_t)LONG_PRESS_START;
125149
EVENT_CB(LONG_PRESS_START);
@@ -140,7 +164,7 @@ static void button_handler(struct button* handle)
140164
handle->ticks = 0;
141165
handle->state = 3;
142166
}
143-
else if(handle->ticks > SHORT_TICKS)
167+
else if(handle->ticks > handle->short_ticks)
144168
{
145169
if(handle->repeat == 1)
146170
{
@@ -162,7 +186,7 @@ static void button_handler(struct button* handle)
162186
handle->event = (uint8_t)PRESS_UP;
163187
EVENT_CB(PRESS_UP);
164188

165-
if(handle->ticks < SHORT_TICKS)
189+
if(handle->ticks < handle->short_ticks)
166190
{
167191
handle->ticks = 0;
168192
handle->state = 2;
@@ -172,7 +196,7 @@ static void button_handler(struct button* handle)
172196
handle->state = 0;
173197
}
174198
}
175-
else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS
199+
else if(handle->ticks > handle->short_ticks) // SHORT_TICKS < press down hold time < LONG_TICKS
176200
{
177201
handle->state = 1;
178202
}

multi_button.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ typedef enum {
2727

2828
typedef struct button {
2929
uint16_t ticks;
30+
uint16_t short_ticks;
31+
uint16_t long_ticks;
3032
uint8_t repeat : 4;
3133
uint8_t event : 4;
3234
uint8_t state : 3;
@@ -48,6 +50,8 @@ PressEvent get_button_event(struct button* handle);
4850
int button_start(struct button* handle);
4951
void button_stop(struct button* handle);
5052
void button_ticks(void);
53+
void button_set_short_ticks(struct Button* handle, uint16_t ticks);
54+
void button_set_long_ticks(struct Button* handle, uint16_t ticks);
5155

5256
#ifdef __cplusplus
5357
}

0 commit comments

Comments
 (0)