当前位置:网站首页>开源按键组件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
边栏推荐
- 01 - get to know the advantages of sketch sketch
- Index: teach you index from zero basis to proficient use
- 122. The best time to buy and sell stocks II - one-time traversal
- 209. Minimum length subarray - sliding window
- 31. 下一个排列
- [simple understanding of database]
- 102. Sequence traversal of binary tree
- Type judgment in [untitled] JS
- Read software engineering at Google (15)
- 48. Rotate image
猜你喜欢
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Chrome浏览器的跨域设置----包含新老版本两种设置
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
2021 Great Wall Cup WP
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
470. 用 Rand7() 实现 Rand10()
Use of five routing guards
常用SQL语句总结
Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)
EasymodbusTCP之clientexample解析
随机推荐
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
为什么有些人说单片机简单,我学起来这么吃力?
394. String decoding - auxiliary stack
This point in JS
Solution of Navicat connecting Oracle library is not loaded
QT modification UI does not take effect
双指针进阶--leetcode题目--盛最多水的容器
[appium] write scripts by designing Keyword Driven files
Header built-in object
XTask与Kotlin Coroutine的使用对比
EasymodbusTCP之clientexample解析
Index: teach you index from zero basis to proficient use
102. 二叉树的层序遍历
超分之TDAN
ES6 new method
土地覆盖/利用数据产品下载
Construction of functions in C language programming
干货 | 快速抽取缩略图是怎么练成的?
Gets the time range of the current week
Entity Framework core captures database changes