当前位置: 首页 > news >正文

按键状态机

原工程地址:https://github.com/candylife9/state_machine_example

视频:C语言之状态机编程_02_状态机使用案例分析_哔哩哔哩_bilibili

我觉得讲的挺好的。

来自豆包封装的通用接口

头文件

/*** @file key_state_machine.h* @brief 通用按键状态机接口*/#ifndef KEY_STATE_MACHINE_H
#define KEY_STATE_MACHINE_H#include <stdint.h>
#include <stdbool.h>/** 按键状态枚举 */
typedef enum {KEY_STATE_IDLE,           // 空闲状态KEY_STATE_PRESS_DEBOUNCE, // 按下消抖状态KEY_STATE_SHORT_PRESS,    // 短按状态KEY_STATE_LONG_PRESS,     // 长按状态KEY_STATE_RELEASE_DEBOUNCE // 释放消抖状态
} KeyState;/** 按键事件回调函数类型 */
typedef void (*KeyEventCallback)(void* context, uint8_t key_id, bool is_long_press);/** 按键配置结构体 */
typedef struct {uint8_t key_id;                  // 按键IDuint32_t debounce_time_ms;       // 消抖时间(ms)uint32_t long_press_threshold_ms; // 长按阈值(ms)uint32_t long_press_interval_ms;  // 长按连续触发间隔(ms)bool active_low;                 // 是否低电平有效bool (*read_pin)(void);          // 读取引脚电平的函数指针KeyEventCallback event_callback; // 事件回调函数void* callback_context;          // 回调函数上下文
} KeyConfig;/** 按键状态机实例结构体 */
typedef struct {const KeyConfig* config;         // 按键配置KeyState state;                  // 当前状态uint8_t debounce_counter;        // 消抖计数器uint32_t check_time;             // 检测时间戳uint32_t old_time;               // 旧时间戳bool press_detected;             // 短按检测标志bool long_press_detected;        // 长按检测标志uint32_t long_press_trigger_time; // 长按触发时间
} KeyStateMachine;/*** @brief 初始化按键状态机* @param key 按键状态机实例指针* @param config 按键配置指针*/
void Key_Init(KeyStateMachine* key, const KeyConfig* config);/*** @brief 按键状态机处理函数,需定期调用* @param key 按键状态机实例指针*/
void Key_Process(KeyStateMachine* key);/*** @brief 获取按键当前状态* @param key 按键状态机实例指针* @return 当前状态*/
KeyState Key_GetState(const KeyStateMachine* key);/*** @brief 检查按键是否被按下(短按或长按)* @param key 按键状态机实例指针* @return true: 按下, false: 未按下*/
bool Key_IsPressed(const KeyStateMachine* key);/*** @brief 检查按键是否处于长按状态* @param key 按键状态机实例指针* @return true: 长按, false: 非长按*/
bool Key_IsLongPressed(const KeyStateMachine* key);#endif // KEY_STATE_MACHINE_H

C文件 

/*** @file key_state_machine.c* @brief 通用按键状态机实现*/#include "key_state_machine.h"
#include "stm32fxxx_hal.h" // 假设使用STM32系列,需要包含HAL库头文件/*** @brief 判断引脚电平是否为有效电平* @param key 按键状态机实例指针* @return true: 有效电平, false: 无效电平*/
static bool IsKeyLevelActive(const KeyStateMachine* key) {bool pin_state = key->config->read_pin();return (key->config->active_low) ? (pin_state == false) : (pin_state == true);
}void Key_Init(KeyStateMachine* key, const KeyConfig* config) {if (!key || !config) return;key->config = config;key->state = KEY_STATE_IDLE;key->debounce_counter = 0;key->check_time = HAL_GetTick();key->old_time = HAL_GetTick();key->press_detected = false;key->long_press_detected = false;key->long_press_trigger_time = 0;
}void Key_Process(KeyStateMachine* key) {if (!key || !key->config) return;uint32_t current_time = HAL_GetTick();switch (key->state) {case KEY_STATE_IDLE:if (current_time - key->old_time > 1000) {// 空闲时,每间隔1秒输出等待按键提示信息(可通过回调函数实现)if (key->config->event_callback) {key->config->event_callback(key->config->callback_context, key->config->key_id, false);}key->old_time = current_time;}if (IsKeyLevelActive(key)) {// 检测到有效电平,开始消抖key->check_time = current_time;key->debounce_counter = 5; // 默认5次检测key->state = KEY_STATE_PRESS_DEBOUNCE;}break;case KEY_STATE_PRESS_DEBOUNCE:if (key->debounce_counter > 0) {if (current_time - key->check_time > key->config->debounce_time_ms) {if (!IsKeyLevelActive(key)) {// 检测到无效电平,抖动,返回空闲状态key->old_time = current_time;key->state = KEY_STATE_IDLE;} else {// 仍然是有效电平,继续消抖key->check_time = current_time;key->debounce_counter--;}}} else {// 消抖完成,确认按下key->press_detected = true;key->old_time = current_time;key->long_press_trigger_time = current_time;key->state = KEY_STATE_SHORT_PRESS;// 触发短按事件回调if (key->config->event_callback) {key->config->event_callback(key->config->callback_context, key->config->key_id, false);}}break;case KEY_STATE_SHORT_PRESS:key->press_detected = false; // 重置短按标志if (current_time - key->old_time > key->config->long_press_threshold_ms) {// 达到长按阈值,进入长按状态key->long_press_detected = true;key->old_time = current_time;key->state = KEY_STATE_LONG_PRESS;// 触发长按事件回调if (key->config->event_callback) {key->config->event_callback(key->config->callback_context, key->config->key_id, true);}} else if (!IsKeyLevelActive(key)) {// 短按状态下检测到释放,开始释放消抖key->check_time = current_time;key->debounce_counter = 5;key->state = KEY_STATE_RELEASE_DEBOUNCE;}break;case KEY_STATE_LONG_PRESS:if (current_time - key->old_time > key->config->long_press_interval_ms) {// 长按连续触发key->old_time = current_time;// 触发长按连续事件回调if (key->config->event_callback) {key->config->event_callback(key->config->callback_context, key->config->key_id, true);}}if (!IsKeyLevelActive(key)) {// 长按状态下检测到释放,开始释放消抖key->check_time = current_time;key->debounce_counter = 5;key->state = KEY_STATE_RELEASE_DEBOUNCE;}break;case KEY_STATE_RELEASE_DEBOUNCE:if (key->debounce_counter > 0) {if (current_time - key->check_time > key->config->debounce_time_ms) {if (IsKeyLevelActive(key)) {// 检测到有效电平,抖动,返回之前的状态key->state = (key->long_press_detected) ? KEY_STATE_LONG_PRESS : KEY_STATE_SHORT_PRESS;} else {// 仍然是无效电平,继续消抖key->check_time = current_time;key->debounce_counter--;}}} else {// 消抖完成,确认释放key->long_press_detected = false;key->old_time = current_time;key->state = KEY_STATE_IDLE;}break;default:key->state = KEY_STATE_IDLE;break;}
}KeyState Key_GetState(const KeyStateMachine* key) {return (key) ? key->state : KEY_STATE_IDLE;
}bool Key_IsPressed(const KeyStateMachine* key) {if (!key) return false;KeyState state = key->state;return (state == KEY_STATE_SHORT_PRESS || state == KEY_STATE_LONG_PRESS);
}bool Key_IsLongPressed(const KeyStateMachine* key) {return (key && key->state == KEY_STATE_LONG_PRESS);
}

http://www.xdnf.cn/news/661501.html

相关文章:

  • FFmpeg 4.3 H265 二十二.3,avformat_open_input 支持打开的协议
  • 07-多线程案例-任务调度
  • NoteGen 如何使用 AI 进行记录
  • set和map简单模拟实现
  • TCP 三次握手过程详解
  • 【Java学习笔记】抽象类
  • 时间的基本概念及相关技术
  • 通用寄存器 专用寄存器
  • 大模型训练中的GPU作用解析
  • 项目三 - 任务8:实现词频统计功能
  • 基于Geotools的Worldpop世界人口tif解析-以中国2020年数据为例
  • 北京大学肖臻老师《区块链技术与应用》公开课:02-BTC-密码学原理
  • Excel快捷键大全
  • 深入理解Java装饰器模式:动态扩展对象功能的优雅之道
  • USB设备状态
  • pyhton基础【5】循环
  • uniapp 小说成品源码
  • Python爬虫实战:研究Selenium框架相关技术
  • NAT、代理服务、内网穿透
  • Python训练营打卡Day37
  • 经典文献阅读之--RT-Grasp(通过MLLM进行推理调优的机器人抓取)
  • 如何设计ES的冷热数据分离架构?Elasticsearch 集群如何实现高可用?如何避免脑裂问题?如果出现脑裂如何恢复?
  • 6.1 Q1|广州医科大学GBD发文 | 良性前列腺增生与合并症之间的相关性
  • mysql ACID 原理
  • OpenCV CUDA模块图像过滤------创建一个 Sobel 滤波器函数createSobelFilter()
  • 高并发下使用防重表做防重案例
  • Linux 常用操作步骤
  • ubantu给github配置ssh
  • Unity—lua基础语法
  • MyBatis-Plus 中 的动态SQL 片段(sqlSegment)讲解