update key

This commit is contained in:
不吃油炸鸡
2025-08-12 18:44:02 +08:00
parent 12b5d9e142
commit dd2176335c
2 changed files with 67 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
#include "key.h" #include "key.h"
#include "cmsis_os.h"
void Key_Port_Init(void) void Key_Port_Init(void)
{ {
@@ -8,29 +9,54 @@ void Key_Port_Init(void)
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA0 */ /*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = KEY1_PIN; GPIO_InitStruct.Pin = KEYB_PIN | KEYY_PIN | KEYL_PIN | KEYR_PIN | KEYN_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEY1_PORT, &GPIO_InitStruct); HAL_GPIO_Init(KEYB_PORT, &GPIO_InitStruct);
} }
// mode: 0 - sigle press, 1 - long press
uint8_t KeyScan(uint8_t mode) uint8_t KeyScan(uint8_t mode)
{ {
static uint8_t key_up = 1; static uint8_t key_up = 1;
uint8_t keyvalue=0; static uint8_t key_down = 0;
if(mode) key_up = 1; uint8_t keyvalue = 0;
if( key_up && (!KEY1))
if(mode)
{ {
HAL_Delay(3);//ensure the key is down key_up = 1;
if(!KEY1) keyvalue = 1; key_down = 0;
if(keyvalue) key_up = 0;
} }
else
if( key_up && (!KEYB || !KEYY || !KEYL || !KEYR || !KEYN) )
{ {
HAL_Delay(3);//ensure the key is up osDelay(3); // ensure the key pressed
if(KEY1) if(!KEYB)
key_down = KEYB_NUM;
else if(!KEYY)
key_down = KEYY_NUM;
else if(!KEYL)
key_down = KEYL_NUM;
else if(!KEYR)
key_down = KEYR_NUM;
else if(!KEYN)
key_down = KEYN_NUM;
if(key_down)
key_up = 0;
}
// 松开才有效
if ( key_down && KEYB && KEYY && KEYL && KEYR && KEYN )
{
osDelay(3);//ensure the key
if(KEYB && KEYY && KEYL && KEYR && KEYN)
{
key_up = 1; key_up = 1;
keyvalue = key_down;
key_down = 0;
}
} }
return keyvalue; return keyvalue;
} }

View File

@@ -7,21 +7,42 @@ extern "C" {
#include "main.h" #include "main.h"
//KEY1 // boot, yes, right, left, no
#define KEY1_PORT GPIOA #define KEYB_NUM 1
#define KEY1_PIN GPIO_PIN_0 #define KEYY_NUM 2
#define KEY1 HAL_GPIO_ReadPin(KEY1_PORT,KEY1_PIN) #define KEYL_NUM 3
#define KEYR_NUM 4
#define KEYN_NUM 5
//KEY2 //KEYB
#define KEY2 0 #define KEYB_PORT GPIOA
#define KEYB_PIN GPIO_PIN_0
#define KEYB HAL_GPIO_ReadPin(KEYB_PORT,KEYB_PIN)
//KEYY
#define KEYY_PORT GPIOA
#define KEYY_PIN GPIO_PIN_1
#define KEYY HAL_GPIO_ReadPin(KEYY_PORT,KEYY_PIN)
//KEYL
#define KEYL_PORT GPIOA
#define KEYL_PIN GPIO_PIN_2
#define KEYL HAL_GPIO_ReadPin(KEYL_PORT,KEYL_PIN)
//KEYR
#define KEYR_PORT GPIOA
#define KEYR_PIN GPIO_PIN_3
#define KEYR HAL_GPIO_ReadPin(KEYR_PORT,KEYR_PIN)
//KEYN
#define KEYN_PORT GPIOA
#define KEYN_PIN GPIO_PIN_4
#define KEYN HAL_GPIO_ReadPin(KEYN_PORT,KEYN_PIN)
void Key_Port_Init(void); void Key_Port_Init(void);
void Key_Interrupt_Callback(void);
uint8_t KeyScan(uint8_t mode); uint8_t KeyScan(uint8_t mode);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif