Skip to content

sync upstream #11

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

Closed
wants to merge 7 commits into from
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MultiButton的作者是0x1abin, github地址: https://github.com/0x1abin/MultiBu
1.先申请一个按键结构

```c
struct Button button1;
struct button button1;
```
2.初始化按键对象,绑定按键的GPIO电平读取接口**read_button_pin()** ,后一个参数设置有效触发电平

Expand Down Expand Up @@ -46,20 +46,22 @@ while(1) {
MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理:

```c
struct Button {
uint16_t ticks;
uint8_t repeat: 4;
uint8_t event : 4;
uint8_t state : 3;
uint8_t debounce_cnt : 3;
uint8_t active_level : 1;
uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void);
BtnCallback cb[number_of_event];
struct Button* next;
struct button {
uint16_t ticks;
uint16_t short_ticks;
uint16_t long_ticks;
uint8_t repeat : 4;
uint8_t event : 4;
uint8_t state : 3;
uint8_t debounce_cnt : 3;
uint8_t active_level : 1;
uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void);
BtnCallback cb[number_of_event];
button_t next;
};
```
这样每个按键使用单向链表相连,依次进入 button_handler(struct Button* handle) 状态机处理,所以每个按键的状态彼此独立。
这样每个按键使用单向链表相连,依次进入 button_handler(button_t handle) 状态机处理,所以每个按键的状态彼此独立。


## 按键事件
Expand All @@ -78,9 +80,9 @@ LONG_PRESS_HOLD | 长按期间一直触发
## Examples

```c
#include "button.h"
#include <multi_button.h>

struct Button btn1;
struct button btn1;

uint8_t read_button1_GPIO()
{
Expand Down
4 changes: 2 additions & 2 deletions examples/event_async.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <rtthread.h>
#include <rtdevice.h>
#include "multi_button.h"
#include <multi_button.h>

static struct button btn;

Expand All @@ -15,7 +15,7 @@ void button_callback(void *btn)
{
uint32_t btn_event_val;

btn_event_val = get_button_event((struct button *)btn);
btn_event_val = get_button_event((button_t)btn);

switch(btn_event_val)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/event_inquire.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <rtthread.h>
#include <rtdevice.h>
#include "multi_button.h"
#include <multi_button.h>

static struct button btn;

Expand Down
65 changes: 46 additions & 19 deletions multi_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/
#include "multi_button.h"

#define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((button*)handle)
#define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((void*)handle)
#define PRESS_REPEAT_MAX_NUM 15 /*!< The maximum value of the repeat counter */

static struct button* head_handle = NULL;
static button_t head_handle = NULL;

/**
* @brief Initializes the button struct handle.
Expand All @@ -29,13 +30,15 @@ static struct button* head_handle = NULL;
* @param active_level: pin pressed level.
* @retval None
*/
void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level)
void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level)
{
memset(handle, 0, sizeof(struct button));
handle->event = (uint8_t)NONE_PRESS;
handle->hal_button_Level = pin_level;
handle->button_level = handle->hal_button_Level();
handle->button_level = !active_level;
handle->active_level = active_level;
handle->short_ticks = SHORT_TICKS;
handle->long_ticks = LONG_TICKS;
}

/**
Expand All @@ -45,17 +48,39 @@ void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t activ
* @param cb: callback function.
* @retval None
*/
void button_attach(struct button* handle, PressEvent event, BtnCallback cb)
void button_attach(button_t 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_set_short_ticks(button_t handle, uint16_t ticks)
{
handle->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_set_long_ticks(button_t handle, uint16_t ticks)
{
handle->long_ticks = ticks / TICKS_INTERVAL;
}

/**
* @brief Inquire the button event happen.
* @param handle: the button handle struct.
* @retval button event.
*/
PressEvent get_button_event(struct button* handle)
PressEvent get_button_event(button_t handle)
{
return (PressEvent)(handle->event);
}
Expand All @@ -65,7 +90,7 @@ PressEvent get_button_event(struct button* handle)
* @param handle: the button handle struct.
* @retval None
*/
static void button_handler(struct button* handle)
static void button_handler(button_t handle)
{
uint8_t read_gpio_level = handle->hal_button_Level();

Expand Down Expand Up @@ -117,9 +142,8 @@ 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->long_ticks)
{
handle->event = (uint8_t)LONG_PRESS_START;
EVENT_CB(LONG_PRESS_START);
Expand All @@ -132,12 +156,15 @@ static void button_handler(struct button* handle)
{
handle->event = (uint8_t)PRESS_DOWN;
EVENT_CB(PRESS_DOWN);
handle->repeat++;
if(handle->repeat != PRESS_REPEAT_MAX_NUM)
{
handle->repeat++;
}
EVENT_CB(PRESS_REPEAT);
handle->ticks = 0;
handle->state = 3;
}
else if(handle->ticks > SHORT_TICKS)
else if(handle->ticks > handle->short_ticks)
{
if(handle->repeat == 1)
{
Expand All @@ -159,7 +186,7 @@ static void button_handler(struct button* handle)
handle->event = (uint8_t)PRESS_UP;
EVENT_CB(PRESS_UP);

if(handle->ticks < SHORT_TICKS)
if(handle->ticks < handle->short_ticks)
{
handle->ticks = 0;
handle->state = 2;
Expand All @@ -169,7 +196,7 @@ static void button_handler(struct button* handle)
handle->state = 0;
}
}
else if(handle->ticks > SHORT_TICKS) // SHORT_TICKS < press down hold time < LONG_TICKS
else if(handle->ticks > handle->short_ticks) // SHORT_TICKS < press down hold time < LONG_TICKS
{
handle->state = 1;
}
Expand Down Expand Up @@ -204,9 +231,9 @@ static void button_handler(struct button* handle)
* @param handle: target handle struct.
* @retval 0: succeed. -1: already exist.
*/
int button_start(struct button* handle)
int button_start(button_t handle)
{
struct button* target = head_handle;
button_t target = head_handle;

while(target)
{
Expand All @@ -229,13 +256,13 @@ int button_start(struct button* handle)
* @param handle: target handle struct.
* @retval None
*/
void button_stop(struct button* handle)
void button_stop(button_t handle)
{
struct button** curr;
button_t* curr;

for(curr = &head_handle; *curr;)
{
struct button* entry = *curr;
button_t entry = *curr;

if (entry == handle)
{
Expand All @@ -256,7 +283,7 @@ void button_stop(struct button* handle)
*/
void button_ticks(void)
{
struct button* target;
button_t target;

for(target = head_handle; target != NULL; target = target->next)
{
Expand Down
22 changes: 14 additions & 8 deletions multi_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ typedef enum {
NONE_PRESS
}PressEvent;

typedef struct button {

typedef struct button *button_t;
struct button {
uint16_t ticks;
uint16_t short_ticks;
uint16_t long_ticks;
uint8_t repeat : 4;
uint8_t event : 4;
uint8_t state : 3;
Expand All @@ -35,19 +39,21 @@ typedef struct button {
uint8_t button_level : 1;
uint8_t (*hal_button_Level)(void);
BtnCallback cb[number_of_event];
struct button* next;
}button;
button_t next;
};

#ifdef __cplusplus
extern "C" {
#endif

void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level);
void button_attach(struct button* handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(struct button* handle);
int button_start(struct button* handle);
void button_stop(struct button* handle);
void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level);
void button_attach(button_t handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(button_t handle);
int button_start(button_t handle);
void button_stop(button_t handle);
void button_ticks(void);
void button_set_short_ticks(button_t handle, uint16_t ticks);
void button_set_long_ticks(button_t handle, uint16_t ticks);

#ifdef __cplusplus
}
Expand Down