STM32之按键驱动的使用和自定义(MultiButton)

原始Github地址

Github地址

修改后

调整内容

  1. 将宏定义转换成配置结构体

头文件

#ifndef _MULTI_BUTTON_H_
#define _MULTI_BUTTON_H_#include "stdint.h"
#include "string.h"//According to your need to modify the constants.
//#define TICKS_INTERVAL    5	//ms
//#define DEBOUNCE_TICKS    3	//MAX 8
//#define SHORT_TICKS       (300 /TICKS_INTERVAL)
//#define LONG_TICKS        (1000 /TICKS_INTERVAL)#define BUTTON_DEFAULT_CONF {5,3,60,200}typedef void (*BtnCallback)(void *);typedef enum {PRESS_DOWN = 0,PRESS_UP,PRESS_REPEAT,SINGLE_CLICK,DOUBLE_CLICK,LONG_PRESS_START,LONG_PRESS_HOLD,number_of_event,NONE_PRESS
} PressEvent;typedef 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 (*hal_button_Level)(void);BtnCallback cb[number_of_event];struct Button *next;
} Button;
struct Button_Conf {uint8_t interval_ticks; /*定时执行次数*/uint8_t debounce_ticks; /*消抖次数*/uint16_t short_press_timeout_ticks; /*短按超时次数*/uint16_t long_press_timeout_ticks; /*长按超时次数*/
};
#ifdef __cplusplus
extern "C" {
#endifvoid button_init(struct Button *handle, uint8_t(*pin_level)(), uint8_t active_level);void button_attach(struct Button *handle, PressEvent event, BtnCallback cb);PressEvent get_button_event(struct Button *handle);int button_start(struct Button *handle, struct Button_Conf *conf);void button_stop(struct Button *handle);void button_ticks(void);#ifdef __cplusplus
}
#endif#endif

源文件

/** Copyright (c) 2016 Zibin Zheng <znbin@qq.com>* All rights reserved*/#include "multi_button.h"#define EVENT_CB(ev)   if(handle->cb[ev])handle->cb[ev]((Button*)handle)//button handle list head.
static struct Button *head_handle = NULL;
static struct Button_Conf *button_conf = NULL;/*** @brief  Initializes the button struct handle.* @param  handle:     按钮句柄.* @param  pin_level: 读取io状态回调.* @param  active_level: 按下时电平.* @retval None*/
void button_init(struct Button *handle, uint8_t(*pin_level)(), uint8_t active_level) {memset(handle, 0, sizeof(struct Button));handle->event = (uint8_t) NONE_PRESS;handle->hal_button_Level = pin_level;handle->button_level = handle->hal_button_Level();handle->active_level = active_level;
}/*** @brief  Attach the button event callback function.* @param  handle: the button handle strcut.* @param  event: trigger event type.* @param  cb: callback function.* @retval None*/
void button_attach(struct Button *handle, PressEvent event, BtnCallback cb) {handle->cb[event] = cb;
}/*** @brief  Inquire the button event happen.* @param  handle: the button handle strcut.* @retval button event.*/
PressEvent get_button_event(struct Button *handle) {return (PressEvent) (handle->event);
}/*** @brief  按钮驱动核心功能,驱动状态机。* @param  handle: the button handle strcut.* @retval None*/
void button_handler(struct Button *handle) {uint8_t read_gpio_level = handle->hal_button_Level();//ticks counter working..if ((handle->state) > 0) handle->ticks++;/*------------button debounce handle---------------*/if (read_gpio_level != handle->button_level) { //not equal to prev one//continue read 3 times same new level changeif (++(handle->debounce_cnt) >= button_conf->debounce_ticks) {handle->button_level = read_gpio_level;handle->debounce_cnt = 0;}} else { //leved not change ,counter reset.handle->debounce_cnt = 0;}/*-----------------State machine-------------------*/switch (handle->state) {case 0:if (handle->button_level == handle->active_level) {    //start press downhandle->event = (uint8_t) PRESS_DOWN;EVENT_CB(PRESS_DOWN);handle->ticks = 0;handle->repeat = 1;handle->state = 1;} else {handle->event = (uint8_t) NONE_PRESS;}break;case 1:if (handle->button_level != handle->active_level) { //released press uphandle->event = (uint8_t) PRESS_UP;EVENT_CB(PRESS_UP);handle->ticks = 0;handle->state = 2;} else if (handle->ticks > button_conf->long_press_timeout_ticks) {handle->event = (uint8_t) LONG_PRESS_START;EVENT_CB(LONG_PRESS_START);handle->state = 5;}break;case 2:if (handle->button_level == handle->active_level) { //press down againhandle->event = (uint8_t) PRESS_DOWN;EVENT_CB(PRESS_DOWN);handle->repeat++;EVENT_CB(PRESS_REPEAT); // repeat hithandle->ticks = 0;handle->state = 3;} else if (handle->ticks > button_conf->short_press_timeout_ticks) { //released timeoutif (handle->repeat == 1) {handle->event = (uint8_t) SINGLE_CLICK;EVENT_CB(SINGLE_CLICK);} else if (handle->repeat == 2) {handle->event = (uint8_t) DOUBLE_CLICK;EVENT_CB(DOUBLE_CLICK); // repeat hit}handle->state = 0;}break;case 3:if (handle->button_level != handle->active_level) { //released press uphandle->event = (uint8_t) PRESS_UP;EVENT_CB(PRESS_UP);if (handle->ticks < button_conf->short_press_timeout_ticks) {handle->ticks = 0;handle->state = 2; //repeat press} else {handle->state = 0;}} else if (handle->ticks > button_conf->short_press_timeout_ticks) { // long press uphandle->state = 0;}break;case 5:if (handle->button_level == handle->active_level) {//continue hold triggerhandle->event = (uint8_t) LONG_PRESS_HOLD;EVENT_CB(LONG_PRESS_HOLD);} else { //releasdhandle->event = (uint8_t) PRESS_UP;EVENT_CB(PRESS_UP);handle->state = 0; //reset}break;}
}/*** @brief  Start the button work, add the handle into work list.* @param  handle: target handle strcut.* @param  conf: 对应配置* @retval 0: succeed. -1: already exist.*/
int button_start(struct Button *handle, struct Button_Conf *conf) {struct Button *target = head_handle;button_conf = conf;while (target) {if (target == handle) return -1;    //already exist.target = target->next;}handle->next = head_handle;head_handle = handle;return 0;
}/*** @brief  Stop the button work, remove the handle off work list.* @param  handle: target handle strcut.* @retval None*/
void button_stop(struct Button *handle) {struct Button **curr;for (curr = &head_handle; *curr;) {struct Button *entry = *curr;if (entry == handle) {*curr = entry->next;
//			free(entry);return;//glacier add 2021-8-18} elsecurr = &entry->next;}
}/*** @brief  background ticks, timer repeat invoking interval 5ms.* @param  None.* @retval None*/
void button_ticks() {struct Button *target;if (button_conf == NULL)return;for (target = head_handle; target; target = target->next) {button_handler(target);}
}

测试

驱动使用

/*******************************************************************************Copyright (c) [scl]。保留所有权利。@brief 此为按键配置******************************************************************************/
#include "app_conf.h"#define DBG_ENABLE
#define DBG_SECTION_NAME "btn_cnf"
#define DBG_LEVEL DBG_LOG#include "sys_dbg.h"
#include "dr_button.h"
#include "multi_button.h"struct Button button; /*按钮结构体*/
struct Button_Conf btn_cnf = BUTTON_DEFAULT_CONF;
#define BUTTON_PIN stm_port_define(A,0)static uint8_t read_button_pin() {return stm_pin_read(BUTTON_PIN);
}static void btn_cb(void *arg) {struct Button *handle = (struct Button *) arg;PressEvent event = get_button_event(handle);switch (event) {case PRESS_DOWN:LOG_D("BUTTON EVENT:PRESS_DOWN");break;case PRESS_UP:LOG_D("BUTTON EVENT:PRESS_UP");break;case PRESS_REPEAT:LOG_D("BUTTON EVENT:PRESS_REPEAT");break;case SINGLE_CLICK:LOG_D("BUTTON EVENT:SINGLE_CLICK");break;case DOUBLE_CLICK:LOG_D("BUTTON EVENT:DOUBLE_CLICK");break;case LONG_PRESS_START:LOG_D("BUTTON EVENT:LONG_PRESS_START");break;case LONG_PRESS_HOLD:LOG_D("BUTTON EVENT:LONG_PRESS_HOLD");break;case number_of_event:LOG_D("BUTTON EVENT:number_of_event");break;case NONE_PRESS:LOG_D("BUTTON EVENT:NONE_PRESS");break;}}static void btn_cnf_pre_init() {btn_cnf.debounce_ticks = 4;// 初始化button_init(&button, read_button_pin, GPIO_PIN_SET);// 注册按键事件
//    button_attach(&button, SINGLE_CLICK, btn_cb);
//    button_attach(&button, DOUBLE_CLICK, btn_cb);
//    button_attach(&button, LONG_PRESS_START, btn_cb);
//    button_attach(&button, LONG_PRESS_HOLD, btn_cb);
//    button_attach(&button, PRESS_REPEAT, btn_cb);button_attach(&button, PRESS_DOWN, btn_cb);button_attach(&button, PRESS_UP, btn_cb);//启动按键button_start(&button, &btn_cnf);
}sys_pre_init_export(btn_cnf, btn_cnf_pre_init);static void btn_cnf_init() {stm32_pin_mode(BUTTON_PIN, pin_mode_input);
}sys_init_export(btn_cnf, btn_cnf_init);static void btn_cnf_after_init() {}sys_after_init_export(btn_cnf, btn_cnf_after_init);

执行循环逻辑 (在RTOS的定时器中执行)

/*******************************************************************************Copyright (c) [scl]。保留所有权利。******************************************************************************/
#include "app_task.h"#if OS_TIMER_LED
#define DBG_ENABLE /*日志是否启用*/
#define DBG_SECTION_NAME "led" /*日志模块名*/
#define DBG_LEVEL DBG_LOG /*日志等级*/#include "sys_dbg.h"
#include "dr_led.h"
#include "multi_button.h"
/****************************************内部文件使用宏定义*************************************/
#define led_TIMER_TRIGGER 10 /*定时触发:ms(单位)*/
/****************************************外部变量引入****************************************************/
/****************************************变量定义****************************************************/
osTimerId led_timer;/***************************************************************************************************/static void led_run(void const *argument);static void led_timer_create() {osTimerDef(led_timer, led_run);led_timer = osTimerCreate(osTimer(led_timer), osTimerPeriodic, NULL);osTimerStart(led_timer, led_TIMER_TRIGGER);
}OS_TIMERS_EXPORT(led, led_timer_create);/****************************************业务逻辑****************************************************/static void led_run(void const *argument) {
//    LED_Toggle(LED_G);button_ticks(); // 执行按键循环检测
}#endif

测试结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/28556.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis实战案例19-Redis解决主从一致性问题

主节点&#xff08;Master&#xff09;“写操作”&#xff1a; 接收并响应客户端的读写请求。持久化数据到磁盘&#xff08;根据配置可以选择使用RDB快照或者AOF日志&#xff09;。将自己的写操作同步给所有的从节点。处理发布/订阅&#xff08;Pub/Sub&#xff09;模式中的发…

AUTOSAR CP标准的RTE和BSW各模块的设计及开发工作

AUTOSAR&#xff08;Automotive Open System Architecture&#xff09;是一种开放的汽车电子系统架构标准&#xff0c;旨在提供一种统一的软件架构&#xff0c;以实现汽车电子系统的模块化和可重用性。 AUTOSAR标准中的两个重要模块是RTE&#xff08;Runtime Environment&…

微服务Day2——Nacos注册中心入门

Nacos注册中心 1、Nacos简介 国内公司一般都推崇阿里巴巴的技术&#xff0c;比如注册中心&#xff0c;SpringCloudAlibaba也推出了一个名为Nacos的注册中心。 2、Mac安装 进入Nacos官网下载安装包 http://nacos.io/zh-cn/ Github仓库地址 下载解压后进入nacos/bin目录下 …

Docker——认识Docker 常用命令 Linux中安装docker 常见问题及其解决

目录 引出Docker是啥&#xff1f;Docker是啥&#xff1f;Docker VS 虚拟机1.特性优势2.资源优势 Docker的架构Docker常用命令&#xff08;0&#xff09;docker run&#xff08;1&#xff09;docker ps&#xff08;2&#xff09;docker stop 容器名称&#xff08;3&#xff09;…

大二毕设.2-自研Spring框架

目录 项目描述&#xff1a; 基本演示 提取标记类 IOC容器的装载 IOC容器的操作 DI依赖注入 Aspect排序 AOP MVC 功能实现讲解 项目描述&#xff1a; 为了更好地学习 Spring 的核心&#xff0c;参考 Spring 源码实现的一个简易框架当前已实现 IOC&#xff0c;DI依赖注…

学习react,复制一个civitai(C站)-更新3

更新内容 优化了一下加载速度 图片列表 初步更新了199张图片&#xff0c;大部分都有stable diffusion 的prompts。 可以直接复制到AI绘画里面使用。 先来看看效果图吧&#xff1a; 我还是挺喜欢这种砌砖流布局 技术点 同样使用了砌墙瀑布流布局:masonry js 安装方法 npm …

2023-07-10 linux IIO子系统使用学习,在TI 的ads1015驱动里面看到相关使用,故花点时间进行简单的学习,入门级别,纪录点滴。

一、Linux IIO&#xff08;Industrial I/O&#xff09;架构是Linux内核提供的一种用于支持各种类型传感器和数据采集设备的子系统&#xff0c;包括温度、压力、湿度、加速度、光度等多种传感器。 二、这个就是ads1015的驱动&#xff0c;里面用到iio子系统。 ti-ads1015.c adc…

qiankun框架vue3主应用和子应用生产环境打包部署nginx

首先下载nginx,进行最小化配置 用vscode 打开nginx.conf文件 在http模块的server模块里进行配置 listen 字段监听端口号 http的默认端口号是80(nginx的端口号可以随便写) server_name字段 是ip地址 lochhost就是127.0.0.1 lacation 字段 是在浏览器的地址栏http协议ip地址…

【es报错】:

报错信息 reasonrequest [/test1] contains unrecognized parameter: [include_type_name] 解决方法 统一导入client下的包&#xff1a;

Leetcode-每日一题【1019.链表中的下一个更大结点】

题目 给定一个长度为 n 的链表 head 对于列表中的每个节点&#xff0c;查找下一个 更大节点 的值。也就是说&#xff0c;对于每个节点&#xff0c;找到它旁边的第一个节点的值&#xff0c;这个节点的值 严格大于 它的值。 返回一个整数数组 answer &#xff0c;其中 answer[…

「车型分析」控制系统典型应用车型 —— 停车机器人

如今&#xff0c;城市可用土地的日益稀缺&#xff08;城市化&#xff09;和汽车使用数量的增加&#xff08;机动化&#xff09;,为了可持续性发展和其他生活质量问题相结合&#xff0c;由此孕育出来了一种自动停车系统。停车机器人凭借其灵活、高效、标准化的停车模式&#xff…

Springboot + Vue 上传Word、PDF文档并保留内部格式

因为业务需求&#xff0c;上传Word文件需要编辑&#xff0c;但如何使用Blob方式&#xff0c;在数据库里存文件&#xff0c;就会造成格式消失。所以修改思路&#xff1a;上传文件到服务器本地&#xff0c;保证数据存储的完整性。 前端 <el-upload class"upload-demo&quo…