本文主要是介绍分享一款嵌入式开源按键框架代码工程MultiButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1 工程简介
2 工程代码分析
3 工程代码应用
4 思考
1 工程简介
MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块。
Github地址:https://github.com/0x1abin/MultiButton
这个项目非常精简,只有两个文件:
(1)可无限扩展按键;
(2)按键事件的回调异步处理方式可以简化程序结构,去除冗余的按键处理硬编码,让按键业务逻辑更清晰。
通过此工程可以学习到以下知识点:
(1)按键各种类型事件
(2)状态机的思想
(3)单向链表语法
MultiButton 支持如下的按键事件:
MultiButton 的按键状态机软件流程图:
2 工程代码分析
注:在使用源码工程时稍微修改了两个点,后续贴出完整代码,有需要查看修改的同学可以和将源码工程下载后进行对比,修改的点如下:
(1)将按键时间的相关参数通过接口定义进行初始化
(2)修改长按期间一直触发事件为真正的长按定时触发
在头文件multi_button.h中:
(1)定义了按键时间相关参数
(2)定义了按键的事件类型
(3)定义按键链表结构体,这里使用了位域操作,解决字节的存储空间问题
#ifndef _MULTI_BUTTON_H_
#define _MULTI_BUTTON_H_#include <stdint.h>
#include <string.h>typedef struct ButtonPara {uint8_t ticks_interval;uint8_t debounce_ticks;uint16_t short_ticks;uint16_t long_ticks;
}ButtonPara;typedef void (*BtnCallback)(void*);typedef enum {PRESS_DOWN = 0,PRESS_UP,PRESS_REPEAT,SINGLE_CLICK,DOUBLE_CLICK,LONG_PRESS_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 button_id;uint8_t (*hal_button_Level)(uint8_t button_id_);BtnCallback cb[number_of_event];struct Button* next;
}Button;#ifdef __cplusplus
extern "C" {
#endif
void button_para_init(struct ButtonPara para);
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);
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中:
(1)对按键时间参数进行初始化
(2)对按键对象结构体进行初始化,化成员包括按键句柄,绑定GPIO电平读取函数,设置有效触发电平
(3)初始化按键完成之后,进行按键绑定操作,将绑定按键结构体成员,按键触发事件,按键回调函数
(4)按键启动:也就是将按键加入链表当中,启动按键。这里选择的插入方式是头部插入法,在链表的头部插入按键节点。效率高,时间复杂度为O(1)
(5)按键删除,将按键从当前链表中删除。使用到了二级指针删除一个按键元素。与之前的多定时器删除方法相同
(6)按键滴答函数,每间隔Nms触发一次按键事件,驱动状态机运行。
(7)读取当前引脚的状态,获取按键当前属于哪种状态
(8)按键处理核心函数,驱动状态机
#include "multi_button.h"#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 ButtonPara buttonpara;
//button handle list head.
static struct Button* head_handle = NULL;static void button_handler(struct Button* handle);void button_para_init(struct ButtonPara para)
{buttonpara.ticks_interval = para.ticks_interval;buttonpara.debounce_ticks = para.debounce_ticks;buttonpara.short_ticks = para.short_ticks;buttonpara.long_ticks = para.long_ticks;
}
/*** @brief Initializes the button struct handle.* @param handle: the button handle struct.* @param pin_level: read the HAL GPIO of the connected button level.* @param active_level: pressed GPIO level.* @param button_id: the button id.* @retval None*/
void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id)
{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(button_id);handle->active_level = active_level;handle->button_id = button_id;
}/*** @brief Attach the button event callback function.* @param handle: the button handle struct.* @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 struct.* @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 struct.* @retval None*/
static void button_handler(struct Button* handle)
{uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id);//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 changeif(++(handle->debounce_cnt) >= buttonpara.debounce_ticks) {handle->button_level = read_gpio_level;handle->debounce_cnt = 0;}} else { //level not change ,counter reset.handle->debounce_cnt = 0;}/*-----------------State machine-------------------*/switch (handle->state) {case 0:if(handle->button_level == handle->active_level) { //start press downhandle->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 uphandle->event = (uint8_t)PRESS_UP;EVENT_CB(PRESS_UP);handle->ticks = 0;handle->state = 2;} else if(handle->ticks > buttonpara.long_ticks) {handle->event = (uint8_t)LONG_PRESS_START;EVENT_CB(LONG_PRESS_START);handle->state = 5;}break;case 2:if(handle->button_level == handle->active_level) { //press down againhandle->event = (uint8_t)PRESS_DOWN;EVENT_CB(PRESS_DOWN);if(handle->repeat != PRESS_REPEAT_MAX_NUM) {handle->repeat++;}EVENT_CB(PRESS_REPEAT); // repeat hithandle->ticks = 0;handle->state = 3;} else if(handle->ticks > buttonpara.short_ticks) { //released timeoutif(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 uphandle->event = (uint8_t)PRESS_UP;EVENT_CB(PRESS_UP);if(handle->ticks < buttonpara.short_ticks) {handle->ticks = 0;handle->state = 2; //repeat press} else {handle->state = 0;}} else if(handle->ticks > buttonpara.short_ticks) { // SHORT_TICKS < press down hold time < LONG_TICKShandle->state = 1;}break;case 5:if(handle->button_level == handle->active_level) {//continue hold triggerif(handle->ticks > buttonpara.long_ticks) {handle->event = (uint8_t)LONG_PRESS_HOLD;EVENT_CB(LONG_PRESS_HOLD);handle->ticks = 0;}} else { //releasedhandle->event = (uint8_t)PRESS_UP;EVENT_CB(PRESS_UP);handle->state = 0; //reset}break;default:handle->state = 0; //resetbreak;}
}/*** @brief Start the button work, add the handle into work list.* @param handle: target handle struct.* @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 struct.* @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);return;//glacier add 2021-8-18} else {curr = &entry->next;}}
}/*** @brief background ticks, timer repeat invoking interval 5ms.* @param None.* @retval None*/
void button_ticks(void)
{struct Button* target;for(target=head_handle; target; target=target->next) {button_handler(target);}
}
3 工程代码应用
以在freertos中应用中为例,包括:
(1)按键对象的定义及时间参数定义
(2)按键回调函数包括读取按键电平函数和各按键事件处理函数的编写
(2)按键初始化操作及启动按键功能
(4)在while(1)中添加按键滴答函数
#define TICKS_INTERVAL 5 //按键状态轮询周期,单位ms
#define DEBOUNCE_TICKS 3 //MAX 7 (0 ~ 7) 去抖时间次数,此为15ms/TICKS_INTERVAL=3次
#define SHORT_TICKS (300 /TICKS_INTERVAL) //短按时间次数,300ms/TICKS_INTERVAL
#define LONG_TICKS (2000 /TICKS_INTERVAL) //长按时间次数,2000ms/TICKS_INTERVALenum Button_IDs {btn1_id,btn2_id,btn3_id,
};
struct ButtonPara btnpara = {TICKS_INTERVAL, DEBOUNCE_TICKS, SHORT_TICKS, LONG_TICKS};
struct Button btn1;
struct Button btn2;
struct Button btn3;//According to your need to modify the constants.uint8_t read_button_GPIO(uint8_t button_id)
{// you can share the GPIO read function with multiple Buttonsswitch(button_id){case btn1_id:return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);case btn2_id:return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);case btn3_id:return GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2);default:return 0;}
}void BTN1_PRESS_DOWN_Handler(void* btn)
{printf("BTN1_PRESS_DOWN_Handler!\r\n");
}void BTN1_PRESS_UP_Handler(void* btn)
{printf("BTN1_PRESS_UP_Handler!\r\n");
}void BTN1_PRESS_REPEAT_Handler(void* btn)
{printf("BTN1_PRESS_REPEAT_Handler, repeatcount = %d!\r\n",btn1.repeat);
}void BTN1_SINGLE_Click_Handler(void* btn)
{printf("BTN1_SINGLE_Click_Handler!\r\n");
}void BTN1_DOUBLE_Click_Handler(void* btn)
{printf("BTN1_DOUBLE_Click_Handler!\r\n");
}void BTN1_LONG_PRESS_START_Handler(void* btn)
{printf("BTN1_LONG_PRESS_START_Handler!\r\n");
}void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{printf("BTN1_LONG_PRESS_HOLD_Handler!\r\n");
}void BTN2_SINGLE_Click_Handler(void* btn)
{printf("BTN2_SINGLE_Click_Handler!\r\n");
}void BTN2_DOUBLE_Click_Handler(void* btn)
{printf("BTN2_DOUBLE_Click_Handler!\r\n");
}void BTN3_LONG_PRESS_START_Handler(void* btn)
{printf("BTN3_LONG_PRESS_START_Handler!\r\n");
}void BTN3_LONG_PRESS_HOLD_Handler(void* btn)
{printf("BTN3_LONG_PRESS_HOLD_Handler!\r\n");
}int main(void)
{ button_para_init(btnpara);button_init(&btn1, read_button_GPIO, 0, btn1_id);button_init(&btn2, read_button_GPIO, 0, btn2_id);button_init(&btn3, read_button_GPIO, 0, btn3_id);button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);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(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);button_attach(&btn2, PRESS_REPEAT, BTN2_PRESS_REPEAT_Handler);button_attach(&btn2, SINGLE_CLICK, BTN2_SINGLE_Click_Handler);button_attach(&btn2, DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler);button_attach(&btn3, LONG_PRESS_START, BTN3_LONG_PRESS_START_Handler);button_attach(&btn3, LONG_PRESS_HOLD, BTN3_LONG_PRESS_HOLD_Handler);button_start(&btn1);button_start(&btn2);button_start(&btn3);xTaskCreate((TaskFunction_t )key_task, (const char* )"key_task", (uint16_t )KEY_STK_SIZE, (void* )NULL, (UBaseType_t )KEY_TASK_PRIO, (TaskHandle_t* )&KeyTask_Handler); vTaskStartScheduler();
}void key_task(void *pvParameters)
{while(1){button_ticks();vTaskDelay(5); //5ms周期轮询}
}
4 思考
使用中有如下问题值得思考:
(1)组合键和矩阵按键如何实现?
在函数uint8_t read_button_GPIO(uint8_t button_id)中进行组合键和矩阵按键返回值的自定义
(2)多个按键时,按键参数进行区分?
去抖时间,短按时间,长按时间可以放在一个数组中区分,各个按键有各自的参数。
(3)按键事件较多的情况时,需要多个绑定的事件函数?
按键事件函数可以统一放在一个数组中进行初始化注册。
↓↓↓更多技术内容和书籍资料获取,入群技术交流敬请关注“明解嵌入式”↓↓↓
这篇关于分享一款嵌入式开源按键框架代码工程MultiButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!