110VCGQ/project/revamp_addr/user/Src/user_key.c

452 lines
11 KiB
C
Raw Normal View History

2024-11-18 10:09:39 +08:00
/*
* key.c
*
* Created on: Sep 25, 2022
* Author: wyf
*/
/*
* key.c
*
* Created on: 2022623
* Author: wyf
*/
#include "user.h"
#include "user_key.h"
#define KEY_COUNT 5 /* 实体按键个数 */
/* 依次定义GPIO */
typedef struct
{
GPIO_TypeDef* gpio;
uint16_t pin;
uint8_t ActiveLevel; /* 激活电平 */
}X_GPIO_T;
/* GPIO和PIN定义 */
static const X_GPIO_T s_gpio_list[KEY_COUNT] = {
{Key_UP_GPIO_Port, Key_UP_Pin, 0},
{KEY_Down_GPIO_Port, KEY_Down_Pin, 0},
{Key_OK_GPIO_Port, Key_OK_Pin, 0},
{Key_Reset_GPIO_Port,Key_Reset_Pin, 0},
{KEY_PWR_GPIO_Port,KEY_PWR_Pin, 1},
};
/* 定义一个宏函数简化后续代码
GPIO引脚是否有效按下
*/
static KEY_T s_tBtn[KEY_COUNT] = {0};
static KEY_FIFO_T s_tKey; /* 按键FIFO变量,结构体 */
static void bsp_InitKeyVar(void);
static void bsp_DetectKey(uint8_t i);
/*
*********************************************************************************************************
* : KeyPinActive
* :
* :
* : 1 (0
*********************************************************************************************************
*/
static uint8_t KeyPinActive(uint8_t _id)
{
uint8_t level;
if ((s_gpio_list[_id].gpio->IDR & s_gpio_list[_id].pin) == 0)
{
level = 0;
}
else
{
level = 1;
}
if (level == s_gpio_list[_id].ActiveLevel)
{
return 1;
}
else
{
return 0;
}
}
/*
*********************************************************************************************************
* : IsKeyDownFunc
* :
* :
* : 1 (0
*********************************************************************************************************
*/
static uint8_t IsKeyDownFunc(uint8_t _id)
{
/* 实体单键 */
if (_id < KEY_COUNT)
{
if (KeyPinActive(_id))
{
return 1;
}
}
return 0;
}
/*
*********************************************************************************************************
* : bsp_InitKey
* : . bsp_Init()
* :
* :
*********************************************************************************************************
*/
void Init_Key(void)
{
bsp_InitKeyVar(); /* 初始化按键变量 */
}
/*
*********************************************************************************************************
* : bsp_InitKeyVar
* :
* :
* :
*********************************************************************************************************
*/
static void bsp_InitKeyVar(void)
{
uint8_t i;
/* 对按键FIFO读写指针清零 */
s_tKey.Read = 0;
s_tKey.Write = 0;
s_tKey.Read2 = 0;
/* 给每个按键结构体成员变量赋一组缺省值 */
for (i = 0; i < KEY_COUNT; i++)
{
s_tBtn[i].LongTime = KEY_LONG_TIME; /* 长按时间 0 表示不检测长按键事件 */
s_tBtn[i].Count = KEY_FILTER_TIME / 2; /* 计数器设置为滤波时间的一半 */
s_tBtn[i].State = 0; /* 按键缺省状态0为未按下 */
s_tBtn[i].RepeatSpeed = 4; /* 按键连发的速度0表示不支持连发 */
s_tBtn[i].RepeatCount = 0; /* 连发计数器 */
}
/* 如果需要单独更改某个按键的参数,可以在此单独重新赋值 */
}
/*
*********************************************************************************************************
* : bsp_PutKey
* : 1FIFO缓冲区
* : _KeyCode :
* :
*********************************************************************************************************
*/
void bsp_PutKey(uint8_t _KeyCode)
{
s_tKey.Buf[s_tKey.Write] = _KeyCode;
if (++s_tKey.Write >= KEY_FIFO_SIZE)
{
s_tKey.Write = 0;
}
}
/*
*********************************************************************************************************
* : bsp_GetKey
* : FIFO缓冲区读取一个键值
* :
* :
*********************************************************************************************************
*/
uint8_t bsp_GetKey(void)
{
uint8_t ret;
if (s_tKey.Read == s_tKey.Write)
{
return KEY_NONE;
}
else
{
ret = s_tKey.Buf[s_tKey.Read];
if (++s_tKey.Read >= KEY_FIFO_SIZE)
{
s_tKey.Read = 0;
}
return ret;
}
}
/*
*********************************************************************************************************
* : bsp_GetKey2
* : FIFO缓冲区读取一个键值
* :
* :
*********************************************************************************************************
*/
uint8_t bsp_GetKey2(void)
{
uint8_t ret;
if (s_tKey.Read2 == s_tKey.Write)
{
return KEY_NONE;
}
else
{
ret = s_tKey.Buf[s_tKey.Read2];
if (++s_tKey.Read2 >= KEY_FIFO_SIZE)
{
s_tKey.Read2 = 0;
}
return ret;
}
}
/*
*********************************************************************************************************
* : bsp_GetKeyState
* :
* : _ucKeyID : ID0
* : 1 0
*********************************************************************************************************
*/
uint8_t bsp_GetKeyState(KEY_ID_E _ucKeyID)
{
return s_tBtn[_ucKeyID].State;
}
/*
*********************************************************************************************************
* : bsp_SetKeyParam
* :
* _ucKeyID : ID0
* _LongTime :
* _RepeatSpeed :
* :
*********************************************************************************************************
*/
void bsp_SetKeyParam(uint8_t _ucKeyID, uint16_t _LongTime, uint8_t _RepeatSpeed)
{
s_tBtn[_ucKeyID].LongTime = _LongTime; /* 长按时间 0 表示不检测长按键事件 */
s_tBtn[_ucKeyID].RepeatSpeed = _RepeatSpeed; /* 按键连发的速度0表示不支持连发 */
s_tBtn[_ucKeyID].RepeatCount = 0; /* 连发计数器 */
}
/*
*********************************************************************************************************
* : bsp_ClearKey
* : FIFO缓冲区
*
* :
*********************************************************************************************************
*/
void bsp_ClearKey(void)
{
s_tKey.Read = s_tKey.Write;
}
/*
*********************************************************************************************************
* : bsp_DetectKey
* :
* : IO的id 0
* :
*********************************************************************************************************
*/
static void bsp_DetectKey(uint8_t i)
{
KEY_T *pBtn;
pBtn = &s_tBtn[i];
if (IsKeyDownFunc(i))
{
if (pBtn->Count < KEY_FILTER_TIME)
{
pBtn->Count = KEY_FILTER_TIME;
}
else if(pBtn->Count < 2 * KEY_FILTER_TIME)
{
pBtn->Count++;
}
else
{
if (pBtn->State == 0)
{
pBtn->State = 1;
/* 发送按钮按下的消息 */
bsp_PutKey((uint8_t)(3 * i + 1));
}
if (pBtn->LongTime > 0)
{
if (pBtn->LongCount < pBtn->LongTime)
{
/* 发送按钮持续按下的消息 */
if (++pBtn->LongCount == pBtn->LongTime)
{
/* 键值放入按键FIFO */
bsp_PutKey((uint8_t)(3 * i + 3));
}
}
else
{
if (pBtn->RepeatSpeed > 0)
{
if (++pBtn->RepeatCount >= pBtn->RepeatSpeed)
{
pBtn->RepeatCount = 0;
/* 常按键后每隔10ms发送1个按键 */
bsp_PutKey((uint8_t)(3 * i + 1));
}
}
}
}
}
}
else
{
if(pBtn->Count > KEY_FILTER_TIME)
{
pBtn->Count = KEY_FILTER_TIME;
}
else if(pBtn->Count != 0)
{
pBtn->Count--;
}
else
{
if (pBtn->State == 1)
{
pBtn->State = 0;
/* 发送按钮弹起的消息 */
bsp_PutKey((uint8_t)(3 * i + 2));
}
}
pBtn->LongCount = 0;
pBtn->RepeatCount = 0;
}
}
/*
*********************************************************************************************************
* : bsp_DetectFastIO
* : IO. 1ms刷新一次
* : IO的id 0
* :
*********************************************************************************************************
*/
static void bsp_DetectFastIO(uint8_t i)
{
KEY_T *pBtn;
pBtn = &s_tBtn[i];
if (IsKeyDownFunc(i))
{
if (pBtn->State == 0)
{
pBtn->State = 1;
/* 发送按钮按下的消息 */
bsp_PutKey((uint8_t)(3 * i + 1));
}
if (pBtn->LongTime > 0)
{
if (pBtn->LongCount < pBtn->LongTime)
{
/* 发送按钮持续按下的消息 */
if (++pBtn->LongCount == pBtn->LongTime)
{
/* 键值放入按键FIFO */
bsp_PutKey((uint8_t)(3 * i + 3));
}
}
else
{
if (pBtn->RepeatSpeed > 0)
{
if (++pBtn->RepeatCount >= pBtn->RepeatSpeed)
{
pBtn->RepeatCount = 0;
/* 常按键后每隔10ms发送1个按键 */
bsp_PutKey((uint8_t)(3 * i + 1));
}
}
}
}
}
else
{
if (pBtn->State == 1)
{
pBtn->State = 0;
/* 发送按钮弹起的消息 */
bsp_PutKey((uint8_t)(3 * i + 2));
}
pBtn->LongCount = 0;
pBtn->RepeatCount = 0;
}
}
/*
*********************************************************************************************************
* : bsp_KeyScan10ms
* : systick中断周期性的调用10ms一次
* :
* :
*********************************************************************************************************
*/
void bsp_KeyScan10ms(void)
{
uint8_t i;
for (i = 0; i < KEY_COUNT; i++)
{
bsp_DetectKey(i);
}
}
/*
*********************************************************************************************************
* : bsp_KeyScan1ms
* : systick中断周期性的调用1ms一次.
* :
* :
*********************************************************************************************************
*/
void bsp_KeyScan1ms(void)
{
uint8_t i;
for (i = 0; i < KEY_COUNT; i++)
{
bsp_DetectFastIO(i);
}
}