You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.9 KiB
138 lines
4.9 KiB
|
3 days ago
|
/******************************************************************************
|
||
|
|
|
||
|
|
版权所有 (C), 2018-2099, Radkil
|
||
|
|
|
||
|
|
******************************************************************************
|
||
|
|
文 件 名 : button.h
|
||
|
|
版 本 号 : 初稿
|
||
|
|
作 者 : Radkil
|
||
|
|
生成日期 : 2026年3月25日星期三
|
||
|
|
最近修改 :
|
||
|
|
功能描述 : button.c 的头文件
|
||
|
|
|
||
|
|
修改历史 :
|
||
|
|
1.日 期 : 2026年3月25日星期三
|
||
|
|
作 者 : Radkil
|
||
|
|
修改内容 : 创建文件
|
||
|
|
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 外部变量说明 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 外部函数原型说明 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 内部函数原型说明 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 全局变量 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 模块级变量 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 常量定义 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
/*----------------------------------------------*
|
||
|
|
* 宏定义 *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
#ifndef __BUTTON_H__
|
||
|
|
#define __BUTTON_H__
|
||
|
|
|
||
|
|
|
||
|
|
#ifdef __cplusplus
|
||
|
|
#if __cplusplus
|
||
|
|
extern "C"{
|
||
|
|
#endif
|
||
|
|
#endif /* __cplusplus */
|
||
|
|
|
||
|
|
|
||
|
|
/*==============================================*
|
||
|
|
* include header files *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
#include <stdint.h>
|
||
|
|
#include "slist.h"
|
||
|
|
#include "common.h"
|
||
|
|
|
||
|
|
/*==============================================*
|
||
|
|
* constants or macros define *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
typedef enum {
|
||
|
|
EVENT_PRESS_DOWN = 0, /**< 按下事件 */
|
||
|
|
EVENT_HOLD, /**< 持续按下有效事件 */
|
||
|
|
EVENT_PRESS_UP, /**< 弹起事件 */
|
||
|
|
EVENT_CLICK, /**< 点击事件 */
|
||
|
|
EVENT_NUM /**< 事件总数目 */
|
||
|
|
}rd_BtnEvent;
|
||
|
|
|
||
|
|
typedef enum {
|
||
|
|
STATE_NONE_PRESS = 0, /**< 未按下状态 */
|
||
|
|
STATE_CHECK_PRESS, /**< 抖动检查状态 */
|
||
|
|
STATE_PRESS_DOWN, /**< 按下状态 */
|
||
|
|
STATE_PRESS_HOLD, /**< 持续按下状态 */
|
||
|
|
STATE_PRESS_UP, /**< 弹起状态 */
|
||
|
|
}rd_BtnState;
|
||
|
|
|
||
|
|
typedef struct T_btn_t rd_btn_t; /**< Agile Button 结构体 */
|
||
|
|
typedef int (*cbk_InPut)(void);
|
||
|
|
|
||
|
|
struct T_btn_t
|
||
|
|
{
|
||
|
|
uint8_t m_iActive; /**< 激活标志 */
|
||
|
|
uint8_t m_iRepeatCnt; /**< 按键重按计数 */
|
||
|
|
uint8_t m_iEliminationTime; /**< 按键消抖时间(单位ms,默认15ms) */
|
||
|
|
rd_BtnEvent m_tEvent; /**< 按键对象事件 */
|
||
|
|
rd_BtnState m_tState; /**< 按键对象状态 */
|
||
|
|
uint32_t m_iHoldTime; /**< 按键按下持续时间(单位ms) */
|
||
|
|
uint32_t m_iPrevHoldTime; /**< 缓存 hold_time 变量 */
|
||
|
|
uint32_t m_iHoldCycleTime; /**< 按键按下后持续调用回调函数的周期(单位ms,默认1s) */
|
||
|
|
uint32_t m_iTimeout; /**< 超时时间 */
|
||
|
|
int m_iActiveLogic; /**< 有效电平(PIN_HIGH/PIN_LOW) */
|
||
|
|
void (*m_pEventCbk[EVENT_NUM])(rd_btn_t *pBtn);/**< 按键对象事件回调函数 */
|
||
|
|
rd_slist_t m_tSlist; /**< 单向链表节点 */
|
||
|
|
};
|
||
|
|
|
||
|
|
#define RD_BUTTON_EVENT_CBK(btn, event) \
|
||
|
|
do { \
|
||
|
|
if (btn->m_pEventCbk[event]) { \
|
||
|
|
btn->m_pEventCbk[event](btn); \
|
||
|
|
} \
|
||
|
|
} while (0)
|
||
|
|
|
||
|
|
/*==============================================*
|
||
|
|
* project-wide global variables *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/*==============================================*
|
||
|
|
* routines' or functions' implementations *
|
||
|
|
*----------------------------------------------*/
|
||
|
|
rd_btn_t *rd_BtnCreate(int _iActiveLogic);
|
||
|
|
int rd_BtnDelete(rd_btn_t *_pBtn);
|
||
|
|
int rd_BtnStart(rd_btn_t *_pBtn);
|
||
|
|
int rd_BtnStop(rd_btn_t *_pBtn);
|
||
|
|
int rd_BtnSetEliminationTime(rd_btn_t *_pBtn, uint8_t _iEliminationTime);
|
||
|
|
int rd_BtnSetHoldCycleTime(rd_btn_t *_pBtn, uint32_t _iHoldCycleTime);
|
||
|
|
int rd_BtnSetEventCbk(rd_btn_t *_pBtn, rd_BtnEvent _eEvent, void (*_pEventCbk)(rd_btn_t *pBtn));
|
||
|
|
void rd_BtnProcess(cbk_InPut _pFunc);
|
||
|
|
|
||
|
|
|
||
|
|
#ifdef __cplusplus
|
||
|
|
#if __cplusplus
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
#endif /* __cplusplus */
|
||
|
|
|
||
|
|
|
||
|
|
#endif /* __BUTTON_H__ */
|