mirror of
https://github.com/goldenfishs/MRobot.git
synced 2025-05-02 23:40:54 +08:00
bht添加bsp/key_gpio
This commit is contained in:
parent
1c17339484
commit
f67878cce7
BIN
User/.DS_Store
vendored
Normal file
BIN
User/.DS_Store
vendored
Normal file
Binary file not shown.
48
User/bsp/key_gpio.c
Normal file
48
User/bsp/key_gpio.c
Normal file
@ -0,0 +1,48 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "key_gpio.h"
|
||||
#include "bsp.h"
|
||||
#include <gpio.h>
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
|
||||
static uint32_t key_stats; // 使用位掩码记录每个通道的状态,最多支持32LED
|
||||
/* 按键配置表(根据实际硬件修改) */
|
||||
static const BSP_Key_Config_t KEY_CONFIGS[] = {
|
||||
{GPIOA, GPIO_PIN_7, GPIO_PIN_SET}, // KEY1按下时电平为高
|
||||
{GPIOA, GPIO_PIN_9, GPIO_PIN_SET}, // KEY2按下时电平为低
|
||||
// 添加更多按键...
|
||||
};
|
||||
|
||||
#define KEY_COUNT (sizeof(KEY_CONFIGS)/sizeof(KEY_CONFIGS[0])
|
||||
|
||||
|
||||
//读取按键状态(带消抖)
|
||||
int8_t BSP_Key_Read(BSP_Key_Channel_t ch) {
|
||||
static uint32_t last_press_time[BSP_KEY_COUNT] = {0}; //上次按下时间
|
||||
const uint32_t debounce_ms = 20; //按键消抖时间
|
||||
const uint32_t long_press_ms = 2000; //按键长按时间
|
||||
|
||||
if(ch >= BSP_KEY_COUNT) return BSP_KEY_RELEASED ;
|
||||
|
||||
const BSP_Key_Config_t *cfg = &KEY_CONFIGS[ch];
|
||||
GPIO_PinState state = HAL_GPIO_ReadPin(cfg->port, cfg->pin);
|
||||
|
||||
if(state == cfg->active_level) {
|
||||
uint32_t now = HAL_GetTick(); //用于记录按键按下时间(这里比较state是为了方便适应不同有效电平做出修改的,也可以改成直接检测电平高低)
|
||||
|
||||
//消抖检测(只有按下超过20ms才被认为按下)
|
||||
if((now - last_press_time[ch]) > debounce_ms) {
|
||||
//长按检测(只有被按下超过2000ms才被认为是长按,根据实际情况可做出修改)
|
||||
if((now - last_press_time[ch]) > long_press_ms) {
|
||||
return BSP_KEY_LONG_PRESS;
|
||||
}
|
||||
return BSP_KEY_PRESSED;
|
||||
}
|
||||
} else {
|
||||
last_press_time[ch] = HAL_GetTick();
|
||||
}
|
||||
return BSP_KEY_RELEASED;
|
||||
}
|
36
User/bsp/key_gpio.h
Normal file
36
User/bsp/key_gpio.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
#include "main.h"
|
||||
//#include "key_gpio.h"
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
|
||||
/* KEY按键状态,设置用 */
|
||||
typedef enum
|
||||
{
|
||||
BSP_KEY_RELEASED, //按键释放
|
||||
BSP_KEY_PRESSED, //按键按下
|
||||
BSP_KEY_LONG_PRESS, //按键长按
|
||||
} BSP_KEY_Status_t;
|
||||
|
||||
|
||||
/* 按键通道定义 */
|
||||
typedef enum {
|
||||
BSP_KEY_1,
|
||||
BSP_KEY_2,
|
||||
/* 可根据需要扩展 */
|
||||
BSP_KEY_COUNT
|
||||
} BSP_Key_Channel_t;
|
||||
|
||||
|
||||
/* 按键硬件配置结构体 */
|
||||
typedef struct {
|
||||
GPIO_TypeDef *port; // GPIO端口
|
||||
uint16_t pin; // 引脚编号
|
||||
uint8_t active_level; // 有效电平(GPIO_PIN_SET/RESET)
|
||||
} BSP_Key_Config_t;
|
||||
|
||||
int8_t BSP_Key_Read(BSP_Key_Channel_t ch);
|
Loading…
Reference in New Issue
Block a user