新增 DebugSerialPort 支持,CommonCenter、CanMv 和 DfPlayer 改用智能指针
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Has been cancelled

This commit is contained in:
2025-05-12 13:17:44 +08:00
parent 21eec9089e
commit a7485fca40
5 changed files with 48 additions and 25 deletions

View File

@@ -4,6 +4,7 @@
#include "../Common/df_player.hpp" #include "../Common/df_player.hpp"
#include "../Common/ultrasonic.hpp" #include "../Common/ultrasonic.hpp"
#include "../config.hpp" #include "../config.hpp"
#include "Common/serial_port.hpp"
class CommonCenter { class CommonCenter {
public: public:
@@ -31,4 +32,14 @@ public:
} }
return *instance; 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;
}
}; };

View File

@@ -1,19 +1,18 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <memory>
#include "../Common/serial_port.hpp" #include "../Common/serial_port.hpp"
class CanMv { class CanMv {
public: public:
explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {} explicit CanMv(UART_HandleTypeDef* uart) : serialPort(std::make_unique<SerialPort>(uart, kLength, kTimeout)) {}
~CanMv() { ~CanMv() = default;
delete serialPort;
}
private: private:
static constexpr uint32_t kLength = 128; static constexpr uint32_t kLength = 128;
static constexpr uint32_t kTimeout = 10; static constexpr uint32_t kTimeout = 10;
SerialPort* serialPort; std::unique_ptr<SerialPort> serialPort;
}; };

View File

@@ -1,16 +1,15 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <memory>
#include "../Common/serial_port.hpp" #include "../Common/serial_port.hpp"
class DfPlayer { class DfPlayer {
public: public:
explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {} explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(std::make_unique<SerialPort>(uart, kLength, kTimeout)) {}
~DfPlayer() { ~DfPlayer() = default;
delete serialPort;
}
void Play(); void Play();
void Stop(); void Stop();
@@ -52,7 +51,7 @@ private:
static constexpr uint32_t kLength = 128; static constexpr uint32_t kLength = 128;
static constexpr uint32_t kTimeout = 10; static constexpr uint32_t kTimeout = 10;
SerialPort* serialPort; std::unique_ptr<SerialPort> serialPort;
void SendCommand(Command cmd, uint16_t param = 0); void SendCommand(Command cmd, uint16_t param = 0);

View File

@@ -2,35 +2,45 @@
#include "stm32h563xx.h" #include "stm32h563xx.h"
#include "Center/common_center.hpp" #include "Center/common_center.hpp"
#include "Common/serial_port.hpp"
#include "Helper/gpio_helper.hpp" #include "Helper/gpio_helper.hpp"
#include "Helper/delay_helper.h" #include "config.hpp"
void TerminateHandler() { 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) { while (true) {
led.Toggle(); led.Toggle();
DelayS(1); DelayS(1);
} }
} }
void Setup() { void GpioSetup() {
GpioHelper::EnableAllGpioPeripheral(); 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(); DelaySetup();
auto ultrasonic = CommonCenter::GetUltrasonic(); CommonSetup();
auto dfPlayer = CommonCenter::GetDfPlayer();
auto canMv = CommonCenter::GetCanMv();
} }
extern "C" void AppStart() { extern "C" void AppStart() {
Setup(); Setup();
auto ultrasonic = CommonCenter::GetUltrasonic(); auto& ultrasonic = CommonCenter::GetUltrasonic();
auto dfPlayer = CommonCenter::GetDfPlayer(); auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
auto canMv = CommonCenter::GetCanMv();
while (true) { while (true) {
// Example usage of the classes // Example usage of the classes
ultrasonic.GetDistance(); auto disatnce = ultrasonic.GetDistance();
dfPlayer.PlayTrack(1); debugSerialPort.WriteLineBlocking("Distance: %.2f cm", disatnce);
DelayS(1); // Delay for 1 second
DelayMs(1000); // Delay for 1 second
} }
} }

View File

@@ -12,7 +12,11 @@ struct Config {
uint32_t channel; uint32_t channel;
} kCaptureConfig = {{GPIOA, GPIO_PIN_0}, &htim7, TIM_CHANNEL_1}; } 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* 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};
}; };