当前位置:网站首页>开源按键组件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
边栏推荐
- SiteServer CMS5. 0 Usage Summary
- [difference between Oracle and MySQL]
- Allowed latency and side output
- Matlab / Simulink simulation of double closed loop DC speed regulation system
- 92. Reverse linked list II byte skipping high frequency question
- Double pointer advanced -- leetcode title -- container with the most water
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- JS failed to change all variables and changed to the return method. Finally, the problem was solved
- 102. 二叉树的层序遍历
- Change Oracle to MySQL
猜你喜欢

uni-app黑马优购项目学习记录(下)

Double pointer advanced -- leetcode title -- container with the most water

Hcip fifth experiment

Chrome浏览器的跨域设置----包含新老版本两种设置

440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question

JVM类加载机制
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

SystemVerilog(六)-变量

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

SystemVerilog (VI) - variable
随机推荐
209. 长度最小的子数组-滑动窗口
Use of five routing guards
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
索引:手把手教你索引从零基础到精通使用
Comparison between xtask and kotlin coroutine
开期货,开户云安全还是相信期货公司的软件?
Websocket (basic)
41. The first missing positive number
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
Open futures, open an account, cloud security or trust the software of futures companies?
Land cover / use data product download
Model problems of stock in and stock out and inventory system
uni-app黑马优购项目学习记录(下)
Dry goods | how to extract thumbnails quickly?
JVM类加载机制
SystemVerilog (VI) - variable
Kubernetes 服务发现 监控Endpoints
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
剑指 Offer 22. 链表中倒数第k个节点-快慢指针