You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
//
// Created by iflyt on 2025/3/2.
//
#ifndef BUTTON_MANAGER_H
#define BUTTON_MANAGER_H
#include <stm32f4xx_hal.h>
#include <cmsis_os2.h>
// 按键和传感器引脚定义
#define SYSTEM_POWER_PIN GPIO_PIN_0
#define SYSTEM_POWER_PORT GPIOA
#define E_STOP_PIN GPIO_PIN_12
#define E_STOP_PORT GPIOF
#define X_ORIGIN_PIN GPIO_PIN_6
#define X_LIMIT_PIN GPIO_PIN_7
#define X_PORT GPIOB
#define Y_ORIGIN_PIN GPIO_PIN_9
#define Y_LIMIT_PIN GPIO_PIN_10
#define Y_PORT GPIOB
#define Z_ORIGIN_PIN GPIO_PIN_13
#define Z_LIMIT_PIN GPIO_PIN_14
#define Z_PORT GPIOB
#define PIN_COUNT 8
// 轴状态枚举
enum class AxisState { ORIGIN, ORIGIN_BOUNDARY, BETWEEN, LIMIT_BOUNDARY, LIMIT };
typedef struct { GPIO_TypeDef *port; uint16_t pin; GPIO_PinState activeLevel; // 有效电平
int debounceTime; // 消抖时间
} B_PinInfo;
// 按键类
class ButtonManager {
public: // 获取单例实例的静态方法
static ButtonManager* ins(); // 删除拷贝构造函数和赋值运算符,防止复制实例
ButtonManager(const ButtonManager&) = delete; ButtonManager& operator=(const ButtonManager&) = delete;
static void handleInterrupt(uint16_t pin);
bool isPinTriggered(uint16_t targetPin);
private: ButtonManager(); ~ButtonManager();
bool isPinStateTriggered(uint16_t pin, bool state);
static void handleKeyInterrupts(void* arg); void processKeyEvent(uint16_t pin, bool state); static GPIO_TypeDef* getPortByPin(uint16_t pin);
static int getPinIndex(uint16_t pin);
bool readPin(GPIO_TypeDef* port, uint16_t pin);
static int getPinDebounceTime(uint16_t pin);
private: static ButtonManager* instance; uint32_t debounceTime = 10; // 按键软件消抖时间
osMessageQueueId_t keyQueue; bool pinStates[PIN_COUNT]; // 引脚状态数组
static const uint16_t pins[];
// 初始化引脚信息数组
const static B_PinInfo pinInfos[8]; };
#ifdef __cplusplus
extern "C" { #endif
void EX_GPIO_Init(void); uint16_t getORIGINPin(uint32_t motor_index); uint16_t getAxisLimitPin(uint32_t motor_index);
#ifdef __cplusplus
} #endif
#endif //BUTTON_MANAGER_H
|