From 17d298c782e1a90b8ba2ad9ab6b06a03e889b5d2 Mon Sep 17 00:00:00 2001 From: chauyin Date: Mon, 12 May 2025 21:57:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Hc05=20=E7=B1=BB=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20HC-05=20=E8=93=9D=E7=89=99=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E4=B8=B2=E5=8F=A3=E9=80=9A=E4=BF=A1=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=B2=E5=8F=A3=E8=AE=BE=E7=BD=AE=E6=B3=A2?= =?UTF-8?q?=E7=89=B9=E7=8E=87=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/App/Center/common_center.hpp | 20 ++++++++++---- Core/App/Common/can_mv.hpp | 8 +++--- Core/App/Common/df_player.hpp | 8 +++--- Core/App/Common/hc05.hpp | 43 +++++++++++++++++++++++++++++++ Core/App/Common/key.hpp | 31 ++++++++++++++++++++++ Core/App/Common/serial_port.hpp | 21 ++++++++++----- Core/App/Helper/gpio_helper.hpp | 3 ++- Core/App/app.cpp | 1 + Core/App/config.hpp | 2 ++ 9 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 Core/App/Common/hc05.hpp create mode 100644 Core/App/Common/key.hpp diff --git a/Core/App/Center/common_center.hpp b/Core/App/Center/common_center.hpp index e5e1b73..8bb5357 100644 --- a/Core/App/Center/common_center.hpp +++ b/Core/App/Center/common_center.hpp @@ -2,6 +2,7 @@ #include "../Common/can_mv.hpp" #include "../Common/df_player.hpp" +#include "../Common/hc05.hpp" #include "../Common/ultrasonic.hpp" #include "../config.hpp" #include "Common/serial_port.hpp" @@ -33,12 +34,21 @@ public: return *instance; } - static SerialPort& GetDebugSerialPort() { - static constexpr auto kLength = 256; - static constexpr auto kTimeout = 1000; - static SerialPort* instance = nullptr; + static Hc05& GetHc05() { + static Hc05* instance = nullptr; if (instance == nullptr) { - instance = new SerialPort(Config::kDebugUart, kLength, kTimeout); + instance = new Hc05(Config::kHc05Uart); + } + return *instance; + } + + static SerialPort& GetDebugSerialPort() { + static constexpr auto kBaudRate = 115200; + static constexpr auto kLength = 256; + static constexpr auto kTimeout = 1000; + static SerialPort* instance = nullptr; + if (instance == nullptr) { + instance = new SerialPort(Config::kDebugUart, kBaudRate, kLength, kTimeout); } return *instance; } diff --git a/Core/App/Common/can_mv.hpp b/Core/App/Common/can_mv.hpp index cc224b0..4bd515a 100644 --- a/Core/App/Common/can_mv.hpp +++ b/Core/App/Common/can_mv.hpp @@ -7,12 +7,14 @@ class CanMv { public: - explicit CanMv(UART_HandleTypeDef* uart) : serialPort(std::make_unique(uart, kLength, kTimeout)) {} + explicit CanMv(UART_HandleTypeDef* uart) + : serialPort(std::make_unique(uart, kBaudRate, kLength, kTimeout)) {} ~CanMv() = default; private: - static constexpr uint32_t kLength = 128; - static constexpr uint32_t kTimeout = 10; + static constexpr uint32_t kBaudRate = 115200; + static constexpr uint32_t kLength = 128; + static constexpr uint32_t kTimeout = 10; std::unique_ptr serialPort; }; diff --git a/Core/App/Common/df_player.hpp b/Core/App/Common/df_player.hpp index 3a752d8..5d78c39 100644 --- a/Core/App/Common/df_player.hpp +++ b/Core/App/Common/df_player.hpp @@ -7,7 +7,8 @@ class DfPlayer { public: - explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(std::make_unique(uart, kLength, kTimeout)) {} + explicit DfPlayer(UART_HandleTypeDef* uart) + : serialPort(std::make_unique(uart, kBaudRate, kLength, kTimeout)) {} ~DfPlayer() = default; @@ -49,8 +50,9 @@ private: kQueryFlashCurrentTrack = 0x4D, }; - static constexpr uint32_t kLength = 128; - static constexpr uint32_t kTimeout = 10; + static constexpr uint32_t kBaudRate = 9600; + static constexpr uint32_t kLength = 128; + static constexpr uint32_t kTimeout = 10; std::unique_ptr serialPort; void SendCommand(Command cmd, uint16_t param = 0); diff --git a/Core/App/Common/hc05.hpp b/Core/App/Common/hc05.hpp new file mode 100644 index 0000000..94157c8 --- /dev/null +++ b/Core/App/Common/hc05.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +#include "../Common/serial_port.hpp" + +class Hc05 { +private: + static constexpr uint32_t kBaudRate = 9600; + static constexpr uint32_t kLength = 128; + static constexpr uint32_t kTimeout = 10; + std::unique_ptr serialPort; + + static constexpr uint8_t kCommand = 0xAA; + +public: + // App 返回单个字节 Command 结果 + enum class Response : uint8_t { + kForward, // 前进 + kBackward, // 后退 + kTurnLeft, // 左转 + kTurnRight, // 右转 + kForwardLeft, // 左前方 + kForwardRight, // 右前方 + kBackwardLeft, // 左后方 + kBackwardRight, // 右后方 + kTurnAround, // 掉头/180度转向 + kStop, // 停止,到达目的地 + }; + + explicit Hc05(UART_HandleTypeDef* uart) + : serialPort(std::make_unique(uart, kBaudRate, kLength, kTimeout)) {} + + ~Hc05() = default; + + Response SendCommand() { + Response data; + serialPort->WriteBytesBlocking(reinterpret_cast(&kCommand), sizeof(kCommand)); + serialPort->ReadBytesBlocking(reinterpret_cast(&data), sizeof(data)); + return data; + } +}; diff --git a/Core/App/Common/key.hpp b/Core/App/Common/key.hpp new file mode 100644 index 0000000..eec1a1c --- /dev/null +++ b/Core/App/Common/key.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "../Helper/delay_helper.h" +#include "stm32h5xx_hal_gpio.h" +#include + +#include "../Helper/gpio_helper.hpp" + +class Key { +private: + static constexpr uint32_t kDebounceDelay = 20; + GpioHelper::Gpio gpio; + GPIO_PinState normalStatus; + +public: + explicit Key(GpioHelper::Gpio gpio, GPIO_PinState normalStatus) : gpio(gpio), normalStatus(normalStatus) { + GpioHelper::GpioInit(gpio, GPIO_MODE_INPUT, normalStatus ? GPIO_PULLUP : GPIO_PULLDOWN); + } + + bool IsPressed() { + auto reading = gpio.Read(); + if (reading != normalStatus) { + DelayMs(kDebounceDelay); + reading = gpio.Read(); + if (reading != normalStatus) { + return true; + } + } + return false; + } +}; diff --git a/Core/App/Common/serial_port.hpp b/Core/App/Common/serial_port.hpp index 0119eb1..7896b54 100644 --- a/Core/App/Common/serial_port.hpp +++ b/Core/App/Common/serial_port.hpp @@ -1,6 +1,7 @@ #pragma once #include "main.h" +#include "stm32h5xx_hal_uart.h" #include #include #include @@ -13,23 +14,31 @@ private: uint32_t timeout; std::unique_ptr buffer; + uint8_t ReadByteForce() { + return handle->Instance->RDR; + } + public: - SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout) - : handle(uart), timeout(timeout), buffer(std::make_unique(length)) {} + SerialPort(UART_HandleTypeDef* uart, uint32_t baudRate, uint32_t length, uint32_t timeout) + : handle(uart), timeout(timeout), buffer(std::make_unique(length)) { + SetBaudRate(baudRate); + } ~SerialPort() = default; - uint8_t ReadByte() { - return handle->Instance->RDR; + void SetBaudRate(uint32_t baudRate) { + HAL_UART_DeInit(handle); + handle->Init.BaudRate = baudRate; + HAL_UART_Init(handle); } std::string ReadLine() { std::string result; result.reserve(128); char c; - ReadByte(); + ReadByteForce(); while (true) { - auto r = HAL_UART_Receive(handle, reinterpret_cast(&c), sizeof(c), timeout); + auto r = HAL_UART_Receive(handle, reinterpret_cast(&c), sizeof(c), timeout); if (r != HAL_OK) { throw std::runtime_error("UART receive error"); } diff --git a/Core/App/Helper/gpio_helper.hpp b/Core/App/Helper/gpio_helper.hpp index 51b5f01..8b5e7d5 100644 --- a/Core/App/Helper/gpio_helper.hpp +++ b/Core/App/Helper/gpio_helper.hpp @@ -56,7 +56,8 @@ public: return {port, pin}; } - static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, const GPIO_PinState state) { + static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, + const std::optional state = std::nullopt) { return GpioInit(gpio.port, gpio.pin, mode, pull, state); } diff --git a/Core/App/app.cpp b/Core/App/app.cpp index 3a0f079..06f08ee 100644 --- a/Core/App/app.cpp +++ b/Core/App/app.cpp @@ -42,6 +42,7 @@ void CommonSetup() { CommonCenter::GetDfPlayer(); CommonCenter::GetCanMv(); CommonCenter::GetDebugSerialPort(); + CommonCenter::GetHc05(); } void Setup() { diff --git a/Core/App/config.hpp b/Core/App/config.hpp index ac6e389..89a1a13 100644 --- a/Core/App/config.hpp +++ b/Core/App/config.hpp @@ -18,5 +18,7 @@ struct Config { static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1]; + static inline UART_HandleTypeDef* kHc05Uart = &hcom_uart[COM1]; + static inline GpioHelper::Gpio kLed = {GPIOB, GPIO_PIN_0}; };