当前位置:网站首页>开源按键组件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
边栏推荐
- Use of five routing guards
- Construction of functions in C language programming
- Land cover / use data product download
- 1217_使用SCons生成目标文件
- 2022年上海市安全员C证操作证考试题库及模拟考试
- Generating access keys using JSON webtoken
- Kubernetes service discovery monitoring endpoints
- In ancient Egypt and Greece, what base system was used in mathematics
- MySQL advanced index [classification, performance analysis, use, design principles]
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
猜你喜欢
MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
uni-app黑马优购项目学习记录(下)
stm32入门开发板选野火还是正点原子呢?
Gets the time range of the current week
Kubernetes 服务发现 监控Endpoints
470. 用 Rand7() 实现 Rand10()
索引:手把手教你索引从零基础到精通使用
Why do some people say SCM is simple and I have to learn it so hard?
高德地图搜索、拖拽 查询地址
Halo open source project learning (II): entity classes and data tables
随机推荐
node中,如何手动实现触发垃圾回收机制
102. Sequence traversal of binary tree
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
2021长城杯WP
HCIP第五次实验
Qt 修改UI没有生效
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Comparison between xtask and kotlin coroutine
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
1217_ Generating target files using scons
QT modification UI does not take effect
Type judgment in [untitled] JS
386. 字典序排数(中等)-迭代-全排列
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Read software engineering at Google (15)
Use of todesk remote control software
209. 长度最小的子数组-滑动窗口
01-初识sketch-sketch优势