Skip to content
Merged
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
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int main()
button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
button_attach(&btn2, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
button_start(&btn1);

//make the timer invoking the button_ticks() interval 5ms.
Expand All @@ -116,11 +116,8 @@ int main()
{}
}

...
```



## 状态图

![states.png](states.png)
![states.png](states.png)
26 changes: 15 additions & 11 deletions multi_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static struct button* head_handle = NULL;

/**
* @brief Initializes the button struct handle.
* @param handle: the button handle strcut.
* @param pin_level: read the pin of the connet button level.
* @param handle: the button handle struct.
* @param pin_level: read the pin of the connected button level.
* @param active_level: pin pressed level.
* @retval None
*/
Expand All @@ -40,7 +40,7 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ

/**
* @brief Attach the button event callback function.
* @param handle: the button handle strcut.
* @param handle: the button handle struct.
* @param event: trigger event type.
* @param cb: callback function.
* @retval None
Expand All @@ -52,7 +52,7 @@ void button_attach(struct button* handle, PressEvent event, BtnCallback cb)

/**
* @brief Inquire the button event happen.
* @param handle: the button handle strcut.
* @param handle: the button handle struct.
* @retval button event.
*/
PressEvent get_button_event(struct button* handle)
Expand All @@ -62,10 +62,10 @@ PressEvent get_button_event(struct button* handle)

/**
* @brief button driver core function, driver state machine.
* @param handle: the button handle strcut.
* @param handle: the button handle struct.
* @retval None
*/
void button_handler(struct button* handle)
static void button_handler(struct button* handle)
{
uint8_t read_gpio_level = handle->hal_button_Level();

Expand All @@ -88,7 +88,7 @@ void button_handler(struct button* handle)
}
else
{
// leved not change ,counter reset.
// level not change ,counter reset.
handle->debounce_cnt = 0;
}

Expand Down Expand Up @@ -169,9 +169,9 @@ void button_handler(struct button* handle)
handle->state = 0;
}
}
else if(handle->ticks > SHORT_TICKS)
else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS
{
handle->state = 0;
handle->state = 1;
}
break;

Expand All @@ -192,12 +192,16 @@ void button_handler(struct button* handle)
handle->state = 0;
}
break;

default:
handle->state = 0; /* reset */
break;
}
}

/**
* @brief Start the button work, add the handle into work list.
* @param handle: target handle strcut.
* @param handle: target handle struct.
* @retval 0: succeed. -1: already exist.
*/
int button_start(struct button* handle)
Expand All @@ -222,7 +226,7 @@ int button_start(struct button* handle)

/**
* @brief Stop the button work, remove the handle off work list.
* @param handle: target handle strcut.
* @param handle: target handle struct.
* @retval None
*/
void button_stop(struct button* handle)
Expand Down
6 changes: 3 additions & 3 deletions multi_button.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef __MULTI_BUTTON_H_
#define __MULTI_BUTTON_H_

#include "stdint.h"
#include "string.h"
#include <stdint.h>
#include <string.h>

//According to your need to modify the constants.
#define TICKS_INTERVAL 5 //ms
#define DEBOUNCE_TICKS 3 //MAX 8
#define DEBOUNCE_TICKS 3 //MAX 7 (0 ~ 7)
#define SHORT_TICKS (300 / TICKS_INTERVAL)
#define LONG_TICKS (1000 / TICKS_INTERVAL)
#define LONG_HOLD_CYC (500 / TICKS_INTERVAL)
Expand Down