259 lines
8.3 KiB
C
259 lines
8.3 KiB
C
#include "key.h"
|
||
#include "main.h"
|
||
|
||
int Key_State[3]; // 使用一个三维数组描述K1-K3的状态,1为按下,0为抬起,例:[1 0 0]->[K1按下 K2抬起 K3抬起]
|
||
int Key_Mode[3]; // 模式0:按住执行,抬起停止;
|
||
// 模式1:只要按下就执行,不考虑抬起;
|
||
// 模式2:完成(按下+抬起)的动作才执行。
|
||
// K1, K2, K3三者模式独立
|
||
int Key_Busy[3] = {0, 0, 0};
|
||
|
||
void Key_Init(void) // 按键状态&模式初始化
|
||
{
|
||
Key_State[0] = 0;
|
||
Key_State[1] = 0;
|
||
Key_State[2] = 1;
|
||
Key_Mode[0] = 0;
|
||
Key_Mode[1] = 2;
|
||
Key_Mode[2] = 2;
|
||
}
|
||
|
||
void Key_Scan(void) // 扫描K1-K3的状态
|
||
{
|
||
int i = 0;
|
||
if (Key_State[2] == 0)//K3使能K1,K2 begin
|
||
{
|
||
/*********************************** K1 begin **************************************/
|
||
// mode 0 begin
|
||
if (Key_Mode[0] == 0)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
|
||
{
|
||
Key_State[0] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1)
|
||
{
|
||
Key_State[0] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 0 end
|
||
|
||
// mode 1 begin
|
||
if (Key_Mode[0] == 1)
|
||
{
|
||
if ((HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0) && (Key_Busy[0] == 0))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
|
||
{
|
||
Key_State[0] = !Key_State[0];
|
||
Key_Busy[0] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1)
|
||
{
|
||
Key_Busy[0] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 1 end
|
||
|
||
// mode 2 begin
|
||
if (Key_Mode[0] == 2)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
|
||
{
|
||
Key_Busy[0] = 1; // 完成按下
|
||
}
|
||
}
|
||
if ((HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1) && (Key_Busy[0] == 1))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 1)
|
||
{
|
||
// 完成抬起
|
||
Key_State[0] = !Key_State[0];
|
||
Key_Busy[0] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 2 end
|
||
/*********************************** K1 end **************************************/
|
||
|
||
/*********************************** K2 begin **************************************/
|
||
// mode 0 begin
|
||
if (Key_Mode[1] == 0)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
|
||
{
|
||
Key_State[1] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1)
|
||
{
|
||
Key_State[1] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 0 end
|
||
|
||
// mode 1 begin
|
||
if (Key_Mode[1] == 1)
|
||
{
|
||
if ((HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0) && (Key_Busy[1] == 0))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
|
||
{
|
||
Key_State[1] = !Key_State[1];
|
||
Key_Busy[1] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1)
|
||
{
|
||
Key_Busy[1] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 1 end
|
||
|
||
// mode 2 begin
|
||
if (Key_Mode[1] == 2)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
|
||
{
|
||
Key_Busy[1] = 1; // 完成按下
|
||
}
|
||
}
|
||
if ((HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1) && (Key_Busy[1] == 1))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 1)
|
||
{
|
||
// 完成抬起
|
||
Key_Mode[0] = (Key_Mode[0] + 1) * ((Key_Mode[0] + 1) < 3); // 使用K2切换K1模式
|
||
Key_State[1] = !Key_State[1];
|
||
Key_Busy[1] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 2 end
|
||
/*********************************** K2 end **************************************/
|
||
}//K3使能K1,K2 begin
|
||
|
||
/*********************************** K3 begin **************************************/
|
||
// mode 0 begin
|
||
if (Key_Mode[2] == 0)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
|
||
{
|
||
Key_State[2] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1)
|
||
{
|
||
Key_State[2] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 0 end
|
||
|
||
// mode 1 begin
|
||
if (Key_Mode[2] == 1)
|
||
{
|
||
if ((HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0) && (Key_Busy[2] == 0))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
|
||
{
|
||
Key_State[2] = !Key_State[2];
|
||
Key_Busy[2] = 1;
|
||
}
|
||
}
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 抬起延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1)
|
||
{
|
||
Key_Busy[2] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 1 end
|
||
|
||
// mode 2 begin
|
||
if (Key_Mode[2] == 2)
|
||
{
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
|
||
{
|
||
Key_Busy[2] = 1; // 完成按下
|
||
}
|
||
}
|
||
if ((HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1) && (Key_Busy[2] == 1))
|
||
{
|
||
for (i = 0; i < 10000; i++)
|
||
; // 按下延时防抖
|
||
if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 1)
|
||
{
|
||
// 完成抬起
|
||
Key_State[2] = !Key_State[2];
|
||
Key_Busy[2] = 0;
|
||
}
|
||
}
|
||
}
|
||
// mode 2 end
|
||
/*********************************** K3 end **************************************/
|
||
}
|