N32L406x+MultiButton+Mdk5移植教程

MultiButton

这里首先感谢作者

MultiButton: Multibutton是一个精巧的多按键事件处理 (gitee.com)

简介

MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。

注意:该框架仅仅可以检测按键这种慢速的输入,不可以检测频率很高的脉冲输入

使用方法

1.先申请一个按键结构

struct Button button1;

2.初始化按键对象,绑定按键的GPIO电平读取接口read_button_pin() ,后一个参数设置有效触发电平

button_init(&button1, read_button_pin, 0, 0);

3.注册按键事件

button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
...

4.启动按键

button_start(&button1);

5.设置一个5ms间隔的定时器循环调用后台处理函数

while(1) {
    ...
    if(timer_ticks == 5) {
        timer_ticks = 0;
	
        button_ticks();
    }
}

特性

MultiButton 使用C语言实现,基于面向对象方式设计思路,每个按键对象单独用一份数据结构管理:

struct Button {
	uint16_t ticks;
	uint8_t  repeat: 4;
	uint8_t  event : 4;
	uint8_t  state : 3;
	uint8_t  debounce_cnt : 3;
	uint8_t  active_level : 1;
	uint8_t  button_level : 1;
	uint8_t  button_id;
	uint8_t  (*hal_button_Level)(uint8_t  button_id_);
	BtnCallback  cb[number_of_event];
	struct Button* next;
};

这样每个按键使用单向链表相连,依次进入 button_handler(struct Button* handle) 状态机处理,所以每个按键的状态彼此独立。

按键事件

事件说明
PRESS_DOWN按键按下,每次按下都触发
PRESS_UP按键弹起,每次松开都触发
PRESS_REPEAT重复按下触发,变量repeat计数连击次数
SINGLE_CLICK单击按键事件
DOUBLE_CLICK双击按键事件
LONG_PRESS_START达到长按时间阈值时触发一次
LONG_PRESS_HOLD长按期间一直触发

参数配置

参数内容在multi_button.h文件中

#define TICKS_INTERVAL 5				   // ms	每一个tick时间 也就是 button_ticks()调用间隔
#define DEBOUNCE_TICKS 3				   // MAX 7 (0 ~ 7) //双击间隔时间
#define SHORT_TICKS (300 / TICKS_INTERVAL) // 短按时间
#define LONG_TICKS (2000 / TICKS_INTERVAL) // 长按的时间

官方Examples

#include "button.h"

unit8_t btn1_id = 0;

struct Button btn1;

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(B1_GPIO_Port, B1_Pin);
			break;

		default:
			return 0;
			break;
	}
}
void BTN1_PRESS_DOWN_Handler(void* btn)
{
	//do something...
}

void BTN1_PRESS_UP_Handler(void* btn)
{
	//do something...
}

...

int main()
{
	button_init(&btn1, read_button_GPIO, 0, btn1_id);
	button_attach(&btn1, PRESS_DOWN,       BTN1_PRESS_DOWN_Handler);
	button_attach(&btn1, PRESS_UP,         BTN1_PRESS_UP_Handler);
	button_attach(&btn1, PRESS_REPEAT,     BTN1_PRESS_REPEAT_Handler);
	button_attach(&btn1, SINGLE_CLICK,     BTN1_SINGLE_Click_Handler);
	button_attach(&btn1, DOUBLE_CLICK,     BTN1_DOUBLE_Click_Handler);
	button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
	button_attach(&btn1, LONG_PRESS_HOLD,  BTN1_LONG_PRESS_HOLD_Handler);
	button_start(&btn1);

	//创建一个超时为5ms的周期定时器,这个周期定时器调用button_ticks函数
	__timer_start(button_ticks, 0, 5);

	while(1)
	{}
}

移植教程

1.复制文件到工程middle目录

在这里插入图片描述

2.添加文件到工程

在这里插入图片描述

添加头文件

…\middle\MultiButton

在这里插入图片描述

3.使用MutiButton

  • 定义一个button按键

    static struct Button button1;

  • 初始化按键

    这里需要提供一个按键状态检测函数

    button_init(&button1, bsp_read_key, 0, K_KEY_0);

  • 绑定事件

    button_attach(&button1, SINGLE_CLICK, btn_callback_clink);

    button_attach(&button1, DOUBLE_CLICK, btn_callback_double_clink);

    button_attach(&button1, LONG_PRESS_START, btn_callback_long_press);

  • 启动按键检测

    button_start(&button1);

  • 调用按键处理循环

    这里需要MCU提供一个时间戳

    button_ticks();

/*
 * @Author: car12
 * @Date: 2024-07-30 11:56:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-07-30 12:07:25
 * @Description: 请填写简介
 */
#include "MutiButtonApp.h"
#include "multi_button.h"
#include "key/bsp_key.h"
#include "stdio.h"
static struct Button button1;
void btn_callback_clink(void *data)
{
    printf("%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);

}

void btn_callback_long_press(void *data)
{
	 printf("%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);

}
void btn_callback_double_clink(void *data)
{
	 printf("%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);

}

void MutiButtonAppInit(void)
{
    button_init(&button1, bsp_read_key, 0, K_KEY_0);
    button_attach(&button1, SINGLE_CLICK, btn_callback_clink);
	button_attach(&button1, DOUBLE_CLICK,     btn_callback_double_clink);
    button_attach(&button1, LONG_PRESS_START, btn_callback_long_press);
    button_start(&button1);
}

void MutiButtonProcess(uint32_t tick)
{
   button_ticks();
}

这里创建了一个线程间隔5ms调用button prcess

 void  AppTaskStart (void *p_arg)
{
	
	multi_timer_start(&g_timer1_, 1000, fun_multi_timer_callback , NULL);
	MutiButtonAppInit();
    while (1) 
    {
        vTaskDelay(5/portTICK_RATE_MS);
		multi_timer_yield();
		MutiButtonProcess(1);
    }
}

在这里插入图片描述

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值