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 "cmsis_os.h"
void Key_Port_Init(void)
{
@@ -8,29 +9,54 @@ void Key_Port_Init(void)
__HAL_RCC_GPIOA_CLK_ENABLE();
/*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.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)
{
static uint8_t key_up = 1;
uint8_t keyvalue=0;
if(mode) key_up = 1;
if( key_up && (!KEY1))
static uint8_t key_down = 0;
uint8_t keyvalue = 0;
if(mode)
{
HAL_Delay(3);//ensure the key is down
if(!KEY1) keyvalue = 1;
if(keyvalue) key_up = 0;
key_up = 1;
key_down = 0;
}
else
if( key_up && (!KEYB || !KEYY || !KEYL || !KEYR || !KEYN) )
{
HAL_Delay(3);//ensure the key is up
if(KEY1)
osDelay(3); // ensure the key pressed
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;
keyvalue = key_down;
key_down = 0;
}
}
return keyvalue;
}