当前位置:网站首页>Open source key component multi_ Button use, including test engineering
Open source key component multi_ Button use, including test engineering
2022-04-23 17:45:00 【Tianwaifeixian CUG】

In the previous article, I introduced the processing method of keys 《 multi-function ( single click 、 double-click 、 Long press ) Key design 》, Let's share another way today :MultiButton.
1.Multi_Button brief introduction
MultiButton yes Github An open source key processing component on , author 0x1abin.
GIthub Address :https://github.com/0x1abin/MultiButton

MultiButton A small and easy-to-use event driven key driver module , Unlimited expansion of keys , The callback asynchronous processing of key events can simplify your program structure , Remove redundant key processing hard coding , Make your key business logic clearer .
Key supported events include :

2.Multi_Button Use
The module has only two files multi_button.c and multi_button.h, When using , take .c Add files to the project . Here we use STM32 For example .
2.1. Include header file #include"multi_button.h"
2.2. Define key structure and key ID, It's defined here 2 A button :
Button button1;
Button button2;
#define btn1_id 1
#define btn2_id 2
2.3. Write a read key GPIO As a function of level :
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. Initialize the key object :
button_init(&button1, read_button_GPIO, 0, btn1_id);
button_init(&button2, read_button_GPIO, 0, btn2_id);
stay button_init Function :
The first parameter is zero 2.2 Key structure pointer defined in .
The second parameter is bound 2.3 Read keys written in GPIO As a function of level .
The third parameter is the effective level of the key ,0 Indicates that the low level is active ,1 Represents high level active .
The fourth parameter is the key ID.
2.5. Bind key callback function :
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);
All key functions are bound here , Users can delete according to actual needs . Different key callback functions can be the same , Take the callback function of pressing the key as an example , Function as follows :
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. Call the startup function :
button_start(&button1);
button_start(&button2);
2.7. Last , You need to call the key scanning function in a timed task :
void scan_key()
{
if(key_tick < TICKS_INTERVAL)return;
key_tick = 0;
button_ticks();
}
It should be noted that , Scan cycle of the key 、 Long press 、 Short press 、 Double click the time definition in .h Modification in file :
//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)
Finally, let's test the effect , Press the key 、 Spring up 、 single click 、 double-click 、 Long press, etc. can be detected , It's very convenient to use .

The source code of this open source key module is only 200 Row or so , Interested friends can do their own research .
Test engineering link :
link :https://pan.baidu.com/s/1wiw5Ajoooc7WZFgwO87zqw
Extraction code :wola
Recommended reading :
ITM Mechanism - You can... Without serial port printf
multi-function ( single click 、 double-click 、 Long press ) Key design
How to measure the running time of single chip microcomputer program ?
How to check the variable waveform when debugging the program ? Look here
Welcome to the official account " Development of Embedded Technology ", You can leave a message for me backstage . If you think the official account is helpful to you , Also welcome to recommend and share with others .
版权声明
本文为[Tianwaifeixian CUG]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231744338976.html
边栏推荐
- MySQL advanced index [classification, performance analysis, use, design principles]
- Uniapp custom search box adaptation applet alignment capsule
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- JVM class loading mechanism
- 【Appium】通过设计关键字驱动文件来编写脚本
- 超分之TDAN
- 31. Next arrangement
- 列表的使用-增删改查
- JS parsing and execution process
- Leak detection and vacancy filling (VIII)
猜你喜欢
随机推荐
In ancient Egypt and Greece, what base system was used in mathematics
Hcip fifth experiment
Indexes and views in MySQL
为什么有些人说单片机简单,我学起来这么吃力?
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!!!)
1217_使用SCons生成目标文件
SiteServer CMS5. 0 Usage Summary
2022年流动式起重机司机国家题库模拟考试平台操作
QT modification UI does not take effect
開期貨,開戶雲安全還是相信期貨公司的軟件?
On the problem of V-IF display and hiding
Future 用法详解
92. Reverse linked list II byte skipping high frequency question
Excel quickly and automatically fills the contents of a row on a blank cell
给 el-dialog 增加拖拽功能
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
This point in JS
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
STM32 entry development board choose wildfire or punctual atom?








