当前位置:网站首页>开源按键组件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
边栏推荐
- 102. Sequence traversal of binary tree
- Hcip fifth experiment
- JVM class loading mechanism
- JS failed to change all variables and changed to the return method. Finally, the problem was solved
- 402. Remove K digits - greedy
- JS implementation private attribute
- 402. 移掉 K 位数字-贪心
- 394. String decoding - auxiliary stack
- Future usage details
- In embedded system, must the program code in flash be moved to ram to run?
猜你喜欢

Halo 开源项目学习(二):实体类与数据表

为什么有些人说单片机简单,我学起来这么吃力?

In embedded system, must the program code in flash be moved to ram to run?

Index: teach you index from zero basis to proficient use

嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?

For the space occupation of the software, please refer to the installation directory

JS parsing and execution process

01 - get to know the advantages of sketch sketch

双闭环直流调速系统matlab/simulink仿真

Tdan over half
随机推荐
Sword finger offer 22 The penultimate node in the linked list - speed pointer
41. 缺失的第一个正数
[simple understanding of database]
Websocket (basic)
92. Reverse linked list II byte skipping high frequency question
QT modification UI does not take effect
JS interview question: FN call. call. call. Call (FN2) parsing
Node template engine (EJS, art template)
给 el-dialog 增加拖拽功能
The system cannot be started after AHCI is enabled
常用SQL语句总结
STM32 entry development board choose wildfire or punctual atom?
2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
EasymodbusTCP之clientexample解析
How to change input into text
Add drag and drop function to El dialog
JS implementation private attribute
Index: teach you index from zero basis to proficient use
uni-app黑马优购项目学习记录(下)
Collection of common SQL statements