From a7485fca408d97a7bffbe4daabb82a15d204f9f2 Mon Sep 17 00:00:00 2001 From: chauyin Date: Mon, 12 May 2025 13:17:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20DebugSerialPort=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8CCommonCenter=E3=80=81CanMv=20?= =?UTF-8?q?=E5=92=8C=20DfPlayer=20=E6=94=B9=E7=94=A8=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/App/Center/common_center.hpp | 11 ++++++++++ Core/App/Common/can_mv.hpp | 9 ++++---- Core/App/Common/df_player.hpp | 9 ++++---- Core/App/app.cpp | 36 ++++++++++++++++++++----------- Core/App/config.hpp | 8 +++++-- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/Core/App/Center/common_center.hpp b/Core/App/Center/common_center.hpp index cd2fa17..e5e1b73 100644 --- a/Core/App/Center/common_center.hpp +++ b/Core/App/Center/common_center.hpp @@ -4,6 +4,7 @@ #include "../Common/df_player.hpp" #include "../Common/ultrasonic.hpp" #include "../config.hpp" +#include "Common/serial_port.hpp" class CommonCenter { public: @@ -31,4 +32,14 @@ public: } return *instance; } + + static SerialPort& GetDebugSerialPort() { + static constexpr auto kLength = 256; + static constexpr auto kTimeout = 1000; + static SerialPort* instance = nullptr; + if (instance == nullptr) { + instance = new SerialPort(Config::kDebugUart, kLength, kTimeout); + } + return *instance; + } }; diff --git a/Core/App/Common/can_mv.hpp b/Core/App/Common/can_mv.hpp index 684914d..cc224b0 100644 --- a/Core/App/Common/can_mv.hpp +++ b/Core/App/Common/can_mv.hpp @@ -1,19 +1,18 @@ #pragma once #include +#include #include "../Common/serial_port.hpp" class CanMv { public: - explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {} + explicit CanMv(UART_HandleTypeDef* uart) : serialPort(std::make_unique(uart, kLength, kTimeout)) {} - ~CanMv() { - delete serialPort; - } + ~CanMv() = default; private: static constexpr uint32_t kLength = 128; static constexpr uint32_t kTimeout = 10; - SerialPort* serialPort; + std::unique_ptr serialPort; }; diff --git a/Core/App/Common/df_player.hpp b/Core/App/Common/df_player.hpp index 7559239..3a752d8 100644 --- a/Core/App/Common/df_player.hpp +++ b/Core/App/Common/df_player.hpp @@ -1,16 +1,15 @@ #pragma once #include +#include #include "../Common/serial_port.hpp" class DfPlayer { public: - explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {} + explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(std::make_unique(uart, kLength, kTimeout)) {} - ~DfPlayer() { - delete serialPort; - } + ~DfPlayer() = default; void Play(); void Stop(); @@ -52,7 +51,7 @@ private: static constexpr uint32_t kLength = 128; static constexpr uint32_t kTimeout = 10; - SerialPort* serialPort; + std::unique_ptr serialPort; void SendCommand(Command cmd, uint16_t param = 0); diff --git a/Core/App/app.cpp b/Core/App/app.cpp index 9240eac..4a96a09 100644 --- a/Core/App/app.cpp +++ b/Core/App/app.cpp @@ -2,35 +2,45 @@ #include "stm32h563xx.h" #include "Center/common_center.hpp" +#include "Common/serial_port.hpp" #include "Helper/gpio_helper.hpp" -#include "Helper/delay_helper.h" +#include "config.hpp" + void TerminateHandler() { - auto led = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET); + auto led = Config::kLed; while (true) { led.Toggle(); DelayS(1); } } -void Setup() { +void GpioSetup() { GpioHelper::EnableAllGpioPeripheral(); + GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_RESET); +} + +void CommonSetup() { + CommonCenter::GetUltrasonic(); + CommonCenter::GetDfPlayer(); + CommonCenter::GetCanMv(); + CommonCenter::GetDebugSerialPort(); +} + +void Setup() { + GpioSetup(); DelaySetup(); - auto ultrasonic = CommonCenter::GetUltrasonic(); - auto dfPlayer = CommonCenter::GetDfPlayer(); - auto canMv = CommonCenter::GetCanMv(); + CommonSetup(); } extern "C" void AppStart() { Setup(); - auto ultrasonic = CommonCenter::GetUltrasonic(); - auto dfPlayer = CommonCenter::GetDfPlayer(); - auto canMv = CommonCenter::GetCanMv(); + auto& ultrasonic = CommonCenter::GetUltrasonic(); + auto& debugSerialPort = CommonCenter::GetDebugSerialPort(); while (true) { // Example usage of the classes - ultrasonic.GetDistance(); - dfPlayer.PlayTrack(1); - - DelayMs(1000); // Delay for 1 second + auto disatnce = ultrasonic.GetDistance(); + debugSerialPort.WriteLineBlocking("Distance: %.2f cm", disatnce); + DelayS(1); // Delay for 1 second } } diff --git a/Core/App/config.hpp b/Core/App/config.hpp index 17664d3..ac6e389 100644 --- a/Core/App/config.hpp +++ b/Core/App/config.hpp @@ -12,7 +12,11 @@ struct Config { uint32_t channel; } kCaptureConfig = {{GPIOA, GPIO_PIN_0}, &htim7, TIM_CHANNEL_1}; + static inline UART_HandleTypeDef* kDebugUart = &hcom_uart[COM1]; + static inline UART_HandleTypeDef* kDfPlayerUart = &hcom_uart[COM1]; - - static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1]; + + static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1]; + + static inline GpioHelper::Gpio kLed = {GPIOB, GPIO_PIN_0}; };