Skip to content

增加独立按键短按和长按时间控制 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions multi_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions multi_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down