freertos开发空气检测仪之输入子系统结构体设计
本篇文章带来本篇带来空气检测仪项目之输入子系统结构体设计。
在这个项目中,有使用一个按键,仿照高手代码进行编程,抽象对应的结构体如下
input_system.h
#ifndef __INPUT_SYSTEM_H
#define __INPUT_SYSTEM_H
#ifndef NULL
#define NULL (void *)0
#endif
#define TIME_T int
#define INPUT_BUF_LEN 20
/* 事件类型 */
typedef enum
{
INPUT_EVENT_TYPE_KEY,
INPUT_EVENT_TYPE_TOUCH,
INPUT_EVENT_TYPE_NET,
INPUT_EVENT_TYPE_STDIO
} INPUT_EVENT_TYPE;
/* 按键状态*/
typedef enum
{
KEY_STATE_PRESSED, /* 按下 */
KEY_STATE_RELEASED, /* 弹起 */
KEY_STATE_LONG_PRESS, /* 长按 */
KEY_STATE_LONG_RELEASED, /* 长按弹起 */
KEY_STATE_REPEAT, /* 长按连发 */
KEY_STATE_DOUBLE_CLICK, /* 双击 */
KEY_STATE_MULTI_CLICK /* 多击 */
} KEY_STATE;
/* 输入事件结构体扩展 */
typedef struct InputEvent {
TIME_T time; /* 事件时间戳 */
INPUT_EVENT_TYPE eType; /* 事件类型 */
/* 通用事件数据 */
union {
/* 按键事件数据 */
struct {
int iKey; /* 按键代码 */
KEY_STATE eState; /* 按键状态 */
int iDuration; /* 持续时间(ms),用于长按判断 */
int iClickCount; /* 点击次数,用于多击判断 */
} key;
/* 触摸事件数据 */
struct {
int iX;
int iY;
int iPressure;
} touch;
/* 网络事件数据 */
struct {
int iEventCode;
char strData[INPUT_BUF_LEN];
} net;
/* 标准输入事件数据 */
struct {
char strInput[INPUT_BUF_LEN];
} stdio;
} data;
} InputEvent, *PInputEvent;
typedef struct InputDevice
{
char *name;
int (*GetInputEvent)(PInputEvent ptInputEvent);
int (*DeviceInit)(void);
int (*DeviceExit)(void);
struct InputDevice *pNext;
} InputDevice, *PInputDevice;
/**********************************************************************
* 函数名称: AddInputDevices
* 功能描述: 注册多个输入设备
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
***********************************************************************/
void AddInputDevices(void);
/**********************************************************************
* 函数名称: InitInputDevices
* 功能描述: 初始化所有的输入设备
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
***********************************************************************/
void InitInputDevices(void);
/**********************************************************************
* 函数名称: InputDeviceRegister
* 功能描述: 注册一个输入设备
* 输入参数: ptInputDevice-输入设备
* 输出参数: 无
* 返 回 值: 无
***********************************************************************/
void InputDeviceRegister(PInputDevice ptInputDevice);
#endif /* __INPUT_SYSTEM_H */
使用共用体设计
在上述的代码片段中,使用了union共用体进行设计,主要原因是节省内存空间,
输入事件在某个时刻只能是一种类型: 不可能同时是按键事件和触摸事件
不可能同时是网络事件和标准输入事件 这种互斥性非常适合使用共用体,因为同一时间只需要存储一种事件的数据。
总结
本篇文件设计了对应的输入事件设计,进行代码的规划整合,下一篇文章将带来InputEvent的按键示例。
本文完!!