当前位置:网站首页>开源按键组件Multi_Button的使用,含测试工程
开源按键组件Multi_Button的使用,含测试工程
2022-04-23 17:44:00 【天外飞仙CUG】

之前的文章中介绍过按键的处理方法《多功能(单击、双击、长按)按键设计》,今天再来分享另外一种方式:MultiButton。
1.Multi_Button简介
MultiButton 是Github上的一个开源的按键处理组件,作者0x1abin。
GIthub地址:https://github.com/0x1abin/MultiButton

MultiButton一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。
按键支持的事件包括:

2.Multi_Button的使用
模块只有两个文件multi_button.c和multi_button.h,使用时,将.c文件添加到工程中。这里以STM32为例。
2.1.包含头文件#include"multi_button.h"
2.2.定义按键结构体和按键ID,这里定义了2个按键:
Button button1;
Button button2;
#define btn1_id 1
#define btn2_id 2
2.3.编写一个读取按键GPIO电平的函数:
uint8_t read_button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin);
break;
case btn2_id:
return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
break;
default:
return 0;
break;
}
}
2.4.初始化按键对象:
button_init(&button1, read_button_GPIO, 0, btn1_id);
button_init(&button2, read_button_GPIO, 0, btn2_id);
在button_init函数中:
第一个参数为2.2中定义的按键结构体指针。
第二个参数为绑定的2.3中编写的读取按键GPIO电平的函数。
第三个参数为按键的有效电平,0代表低电平有效,1代表高电平有效。
第四个参数为按键ID。
2.5.绑定按键回调函数:
button_attach(&button1, PRESS_DOWN, BTN_PRESS_DOWN_Handler);
button_attach(&button1, PRESS_UP, BTN_PRESS_UP_Handler);
button_attach(&button1, PRESS_REPEAT, BTN_PRESS_REPEAT_Handler);
button_attach(&button1, SINGLE_CLICK, BTN_SINGLE_Click_Handler);
button_attach(&button1, DOUBLE_CLICK, BTN_DOUBLE_Click_Handler);
button_attach(&button1, LONG_PRESS_START, BTN_LONG_PRESS_START_Handler);
button_attach(&button1, LONG_PRESS_HOLD, BTN_LONG_PRESS_HOLD_Handler);
button_attach(&button2, PRESS_DOWN, BTN_PRESS_DOWN_Handler);
button_attach(&button2, PRESS_UP, BTN_PRESS_UP_Handler);
button_attach(&button2, PRESS_REPEAT, BTN_PRESS_REPEAT_Handler);
button_attach(&button2, SINGLE_CLICK, BTN_SINGLE_Click_Handler);
button_attach(&button2, DOUBLE_CLICK, BTN_DOUBLE_Click_Handler);
button_attach(&button2, LONG_PRESS_START, BTN_LONG_PRESS_START_Handler);
button_attach(&button2, LONG_PRESS_HOLD, BTN_LONG_PRESS_HOLD_Handler);
这里绑定了所有的按键功能,用户可根据实际需求进行删减。不同的按键回调函数可以相同,以按键按下的回调函数为例,函数如下:
void BTN_PRESS_DOWN_Handler(void* btn)
{
Button *temp_button = (Button *)btn;
switch(temp_button->button_id)
{
case btn1_id:
printf("btn1 press down\r\n");
break;
case btn2_id:
printf("btn2 press down\r\n");
break;
default:
break;
}
}
2.6.调用启动函数:
button_start(&button1);
button_start(&button2);
2.7.最后,需要在一个定时任务中循环调用按键扫描函数:
void scan_key()
{
if(key_tick < TICKS_INTERVAL)return;
key_tick = 0;
button_ticks();
}
需要注意的是,按键的扫描周期、长按、短按、双击的时间定义可以在.h文件中修改:
//According to your need to modify the constants.
#define TICKS_INTERVAL 10 //ms
#define DEBOUNCE_TICKS 3 //MAX 8
#define SHORT_TICKS (300 /TICKS_INTERVAL)
#define LONG_TICKS (1000 /TICKS_INTERVAL)
最后来测试一下效果,按键的按下、弹起、单击、双击、长按等都能被检测到,用起来还是挺方便的。

这个开源按键模块的源代码只有200行左右,有兴趣的朋友可以自己研究一下。
测试工程链接:
链接:https://pan.baidu.com/s/1wiw5Ajoooc7WZFgwO87zqw
提取码:wola
推荐阅读:
欢迎关注公众号"嵌入式技术开发",大家可以后台给我留言沟通交流。如果觉得该公众号对你有所帮助,也欢迎推荐分享给其他人。
版权声明
本文为[天外飞仙CUG]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zhang062061/article/details/124358360
边栏推荐
- SystemVerilog(六)-变量
- Leak detection and vacancy filling (VIII)
- Construction of functions in C language programming
- 開期貨,開戶雲安全還是相信期貨公司的軟件?
- ECMAScript history
- Index: teach you index from zero basis to proficient use
- MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
- 394. 字符串解码-辅助栈
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- 386. Dictionary order (medium) - iteration - full arrangement
猜你喜欢

Summary of common SQL statements

2022制冷与空调设备运行操作判断题及答案

Why do some people say SCM is simple and I have to learn it so hard?

Matlab / Simulink simulation of double closed loop DC speed regulation system

干货 | 快速抽取缩略图是怎么练成的?

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

Use of five routing guards

2022年上海市安全员C证操作证考试题库及模拟考试

PC电脑使用无线网卡连接上手机热点,为什么不能上网

MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
随机推荐
Manually implement call, apply and bind functions
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
If you start from zero according to the frame
In embedded system, must the program code in flash be moved to ram to run?
48. Rotate image
Websocket (basic)
Read software engineering at Google (15)
122. 买卖股票的最佳时机 II-一次遍历
Construction of functions in C language programming
209. 长度最小的子数组-滑动窗口
587. 安装栅栏 / 剑指 Offer II 014. 字符串中的变位词
2022年茶艺师(初级)考试模拟100题及模拟考试
Add drag and drop function to El dialog
31. Next arrangement
MySQL advanced index [classification, performance analysis, use, design principles]
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Qt 修改UI没有生效
JS failed to change all variables and changed to the return method. Finally, the problem was solved
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
Kubernetes 服务发现 监控Endpoints