基质喷涂
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.

99 lines
2.2 KiB

  1. //
  2. // Created by iflyt on 2025/3/2.
  3. //
  4. #ifndef BUTTON_MANAGER_H
  5. #define BUTTON_MANAGER_H
  6. #include <stm32f4xx_hal.h>
  7. #include <cmsis_os2.h>
  8. // 按键和传感器引脚定义
  9. #define SYSTEM_POWER_PIN GPIO_PIN_0
  10. #define SYSTEM_POWER_PORT GPIOA
  11. #define E_STOP_PIN GPIO_PIN_12
  12. #define E_STOP_PORT GPIOF
  13. #define X_ORIGIN_PIN GPIO_PIN_6
  14. #define X_LIMIT_PIN GPIO_PIN_7
  15. #define X_PORT GPIOB
  16. #define Y_ORIGIN_PIN GPIO_PIN_9
  17. #define Y_LIMIT_PIN GPIO_PIN_10
  18. #define Y_PORT GPIOB
  19. #define Z_ORIGIN_PIN GPIO_PIN_13
  20. #define Z_LIMIT_PIN GPIO_PIN_14
  21. #define Z_PORT GPIOB
  22. #define PIN_COUNT 8
  23. // 轴状态枚举
  24. enum class AxisState {
  25. ORIGIN,
  26. ORIGIN_BOUNDARY,
  27. BETWEEN,
  28. LIMIT_BOUNDARY,
  29. LIMIT
  30. };
  31. typedef struct {
  32. GPIO_TypeDef *port;
  33. uint16_t pin;
  34. GPIO_PinState activeLevel; // 有效电平
  35. int debounceTime; // 消抖时间
  36. } B_PinInfo;
  37. // 按键类
  38. class ButtonManager {
  39. public:
  40. // 获取单例实例的静态方法
  41. static ButtonManager* ins();
  42. // 删除拷贝构造函数和赋值运算符,防止复制实例
  43. ButtonManager(const ButtonManager&) = delete;
  44. ButtonManager& operator=(const ButtonManager&) = delete;
  45. static void handleInterrupt(uint16_t pin);
  46. bool isPinTriggered(uint16_t targetPin);
  47. private:
  48. ButtonManager();
  49. ~ButtonManager();
  50. bool isPinStateTriggered(uint16_t pin, bool state);
  51. static void handleKeyInterrupts(void* arg);
  52. void processKeyEvent(uint16_t pin, bool state);
  53. static GPIO_TypeDef* getPortByPin(uint16_t pin);
  54. static int getPinIndex(uint16_t pin);
  55. bool readPin(GPIO_TypeDef* port, uint16_t pin);
  56. static int getPinDebounceTime(uint16_t pin);
  57. private:
  58. static ButtonManager* instance;
  59. uint32_t debounceTime = 10; // 按键软件消抖时间
  60. osMessageQueueId_t keyQueue;
  61. bool pinStates[PIN_COUNT]; // 引脚状态数组
  62. static const uint16_t pins[];
  63. // 初始化引脚信息数组
  64. const static B_PinInfo pinInfos[8];
  65. };
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. void EX_GPIO_Init(void);
  70. uint16_t getORIGINPin(uint32_t motor_index);
  71. uint16_t getAxisLimitPin(uint32_t motor_index);
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif //BUTTON_MANAGER_H