- Growing up’s a funny thing. Sneaks up on you.
长大是件很有趣的事,不经意间就发生了。
一、概括
-
项目的仓库地址:https://github.com/0x1abin/MultiButton
0x1abin大佬的超精简的软件定时器multi_timer已经让人眼前一亮,如今这个按键组件MultiButton更令我折服。把按键的各种事件(按下触发、按下弹出触发、单击、双击、长按等)用状态机非常perfect的实现了。该组件兼具按键的消抖处理,亲测效果非常不错。同时,该组件非常易于添加到项目的代码里,只需要简单的几步操作。
说着说着,膝盖又疼了。
二、使用和代码分析
发现两篇文章把这个组件的使用和代码分析都讲得蛮清楚了,没兴趣看github上的源码,可以有兴趣看一下下面的文章:
三、项目源代码和个人的注释
multi_button.h
/*
* Copyright (c) 2016 Zibin Zheng <[email protected]>
* All rights reserved
*/
#ifndef _MULTI_BUTTON_H_
#define _MULTI_BUTTON_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 SHORT_TICKS (300 /TICKS_INTERVAL)
#define LONG_TICKS (1000 /TICKS_INTERVAL)
typedef void (*BtnCallback)(void*);
typedef enum {
PRESS_DOWN = 0, // 按键按下,每次按下都触发
PRESS_UP, // 按键弹起,每次松开都触发
PRESS_REPEAT, // 重复按下触发,变量repeat计数连击次数
SINGLE_CLICK, // 单击按键事件
DOUBLE_CLICK, // 双击按键事件
LONG_RRESS_START, // 达到长按时间阈值时触发一次
LONG_PRESS_HOLD, // 长按期间一直触发
number_of_event, // 事件种类数
NONE_PRESS
}PressEvent;
typedef 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;
}Button;
#ifdef __cplusplus
extern "C" {
#endif
void button_init(struct Button* handle, uint8_t(*pin_level)(), 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_ticks(void);
#ifdef __cplusplus
}
#endif
#endif
multi_button.c
/*
* Copyright (c) 2016 Zibin Zheng <[email protected]>
* All rights reserved
*/
#include "multi_button.h"
#define EVENT_CB(ev) if(handle->cb[ev]) handle->cb[ev]((Button*)handle) // 执行回调函数
//button handle list head.
static struct Button* head_handle = NULL;
/**
* @brief Initializes the button struct handle.
* @param handle: the button handle strcut.
* @param pin_level: read the HAL GPIO of the connet button level.
* @param active_level: pressed GPIO level.
* @retval None
*/
void button_init(struct Button* handle, uint8_t(*pin_level)(), 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->active_level = active_level;
}
/**
* @brief Attach the button event callback function.
* @param handle: the button handle strcut.
* @param event: trigger event type.
* @param cb: callback function.
* @retval None
*/
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb)
{
handle->cb[event] = cb; // 将事件类型与回调函数绑定
}
/**
* @brief Inquire the button event happen.
* @param handle: the button handle strcut.
* @retval button event.
*/
// 返回按键事件类型
PressEvent get_button_event(struct Button* handle)
{
return (PressEvent)(handle->event);
}
/**
* @brief Button driver core function, driver state machine.
* @param handle: the button handle strcut.
* @retval None
*/
void button_handler(struct Button* handle)
{
uint8_t read_gpio_level = handle->hal_button_Level();
//ticks counter working..
if((handle->state) > 0) handle->ticks++;
/*------------button debounce handle---------------*/
if(read_gpio_level != handle->button_level) { //not equal to prev one
//continue read 3 times same new level change
if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {
handle->button_level = read_gpio_level;
handle->debounce_cnt = 0;
}
} else { //leved not change ,counter reset.
handle->debounce_cnt = 0;
}
/*-----------------State machine-------------------*/
switch (handle->state) {
case 0:
if(handle->button_level == handle->active_level) { //start press down
handle->event = (uint8_t)PRESS_DOWN;
EVENT_CB(PRESS_DOWN); // 跳转到回调函数
handle->ticks = 0;
handle->repeat = 1;
handle->state = 1;
} else {
handle->event = (uint8_t)NONE_PRESS;
}
break;
case 1:
if(handle->button_level != handle->active_level) { //released press up
handle->event = (uint8_t)PRESS_UP;
EVENT_CB(PRESS_UP); // 跳转到回调函数
handle->ticks = 0;
handle->state = 2;
} else if(handle->ticks > LONG_TICKS) {
handle->event = (uint8_t)LONG_RRESS_START;
EVENT_CB(LONG_RRESS_START); // 长按
handle->state = 5;
}
break;
case 2:
if(handle->button_level == handle->active_level) { //press down again
handle->event = (uint8_t)PRESS_DOWN;
EVENT_CB(PRESS_DOWN);
handle->repeat++;
// 若将所有的按键事件回调函数写在同一个函数里,需要添加这一句(该回调函数内是靠handle->event来判断怎么处理)
// 除非你不需要处理PRESS_REPEAT
handle->event = (uint8_t)PRESS_REPEAT;
EVENT_CB(PRESS_REPEAT); // repeat hit
handle->ticks = 0;
handle->state = 3;
} else if(handle->ticks > SHORT_TICKS) { //released timeout
if(handle->repeat == 1) {
handle->event = (uint8_t)SINGLE_CLICK;
EVENT_CB(SINGLE_CLICK);
} else if(handle->repeat == 2) {
handle->event = (uint8_t)DOUBLE_CLICK;
EVENT_CB(DOUBLE_CLICK); // repeat hit
}
handle->state = 0;
}
break;
case 3:
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) {
handle->ticks = 0;
handle->state = 2; //repeat press
} else {
handle->state = 0;
}
}
break;
case 5:
if(handle->button_level == handle->active_level) {
//continue hold trigger
handle->event = (uint8_t)LONG_PRESS_HOLD;
EVENT_CB(LONG_PRESS_HOLD);
} else { //releasd
handle->event = (uint8_t)PRESS_UP;
EVENT_CB(PRESS_UP);
handle->state = 0; //reset
}
break;
}
}
/**
* @brief Start the button work, add the handle into work list.
* @param handle: target handle strcut.
* @retval 0: succeed. -1: already exist.
*/
// 将按键结构体插入扫描队列
int button_start(struct Button* handle)
{
struct Button* target = head_handle;
while(target) {
if(target == handle) return -1; //already exist.
target = target->next;
}
handle->next = head_handle;
head_handle = handle;
return 0;
}
/**
* @brief Stop the button work, remove the handle off work list.
* @param handle: target handle strcut.
* @retval None
*/
// 将按键结构体脱离扫描队列
void button_stop(struct Button* handle)
{
struct Button** curr;
for(curr = &head_handle; *curr; ) {
struct Button* entry = *curr;
if (entry == handle) {
*curr = entry->next;
// free(entry);
} else
curr = &entry->next;
}
}
/**
* @brief background ticks, timer repeat invoking interval 5ms.
* @param None.
* @retval None
*/
// 将其放在周期定时执行的函数里,每一个周期扫描一次各个按键是否有事件发生
void button_ticks()
{
struct Button* target;
for(target=head_handle; target; target=target->next) {
button_handler(target);
}
}