Compare commits

8 Commits

Author SHA1 Message Date
chauyinn
be081086cb 优化 App 类,重构 Setup 和 AppStart 函数,清理未使用的代码,增强可读性
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 20m6s
2025-05-10 19:49:39 +08:00
chauyin
e9e08375fd 关闭 ThreadX
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Has been cancelled
2025-05-10 19:45:55 +08:00
chauyinn
5beaf15efd 新增 DfPlayer 和 CanMv 类,优化 UART 读写方法,更新 CommonCenter 以支持新类实例化
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Has been cancelled
2025-05-10 19:38:20 +08:00
chauyinn
60b49d24c3 重构 SerialPort 类,移除静态成员,改为实例成员,优化 UART 读写方法 2025-05-10 18:58:32 +08:00
chauyinn
3f2e7bf019 新增 CommonCenter 类和 Config 结构体,提供超声波测距功能的配置和实例化支持
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 20m2s
2025-05-10 18:23:47 +08:00
chauyinn
b9a9165c71 新增 Ultrasonic 类和 TaskHelper 类,提供超声波测距功能及任务等待辅助方法 2025-05-10 18:23:21 +08:00
chauyinn
92e9fc4864 更新 CMakeLists.txt,使用 find_package 查找 Python3,并更新 MCUVersionGen.py 脚本的执行命令
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Failing after 16m22s
2025-05-08 23:22:12 +08:00
chauyin
559760be0c 开启 ThreadX
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 25m8s
2025-05-07 22:53:45 +08:00
11 changed files with 41 additions and 224 deletions

View File

@@ -2,17 +2,11 @@
#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"
class CommonCenter {
public:
CommonCenter() = delete;
CommonCenter(const CommonCenter&) = delete;
CommonCenter& operator=(const CommonCenter&) = delete;
static Ultrasonic& GetUltrasonic() {
static Ultrasonic* instance = nullptr;
if (instance == nullptr) {
@@ -37,23 +31,4 @@ public:
}
return *instance;
}
static Hc05& GetHc05() {
static Hc05* instance = nullptr;
if (instance == nullptr) {
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;
}
};

View File

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

View File

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

View File

@@ -1,47 +0,0 @@
#pragma once
#include <cerrno>
#include <cstdint>
#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> 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<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
~Hc05() = default;
Hc05(const Hc05&) = delete;
Hc05& operator=(const Hc05&) = delete;
Hc05(Hc05&&) = delete;
Hc05& operator=(Hc05&&) = delete;
Response SendCommand() {
Response data;
serialPort->WriteBytesBlocking(reinterpret_cast<const uint8_t*>(&kCommand), sizeof(kCommand));
serialPort->ReadBytesBlocking(reinterpret_cast<uint8_t*>(&data), sizeof(data));
return data;
}
};

View File

@@ -1,36 +0,0 @@
#pragma once
#include "../Helper/delay_helper.h"
#include "stm32h5xx_hal_gpio.h"
#include <cstdint>
#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);
}
~Key() = default;
Key(const Key&) = delete;
Key& operator=(const Key&) = delete;
Key(Key&&) = delete;
Key& operator=(Key&&) = delete;
bool IsPressed() {
auto reading = gpio.Read();
if (reading != normalStatus) {
DelayMs(kDebounceDelay);
reading = gpio.Read();
if (reading != normalStatus) {
return true;
}
}
return false;
}
};

View File

@@ -1,7 +1,6 @@
#pragma once
#include "main.h"
#include "stm32h5xx_hal_uart.h"
#include <cstdarg>
#include <cstdint>
#include <cstring>
@@ -14,35 +13,23 @@ private:
uint32_t timeout;
std::unique_ptr<char[]> buffer;
uint8_t ReadByteForce() {
return handle->Instance->RDR;
}
public:
SerialPort(UART_HandleTypeDef* uart, uint32_t baudRate, uint32_t length, uint32_t timeout)
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {
SetBaudRate(baudRate);
}
SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout)
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {}
~SerialPort() = default;
SerialPort(const SerialPort&) = delete;
SerialPort& operator=(const SerialPort&) = delete;
SerialPort(SerialPort&&) = delete;
SerialPort& operator=(SerialPort&&) = delete;
void SetBaudRate(uint32_t baudRate) {
HAL_UART_DeInit(handle);
handle->Init.BaudRate = baudRate;
HAL_UART_Init(handle);
uint8_t ReadByte() {
return handle->Instance->RDR;
}
std::string ReadLine() {
std::string result;
result.reserve(128);
char c;
ReadByteForce();
ReadByte();
while (true) {
auto r = HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&c), sizeof(c), timeout);
auto r = HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&c), sizeof(c), timeout);
if (r != HAL_OK) {
throw std::runtime_error("UART receive error");
}

View File

@@ -19,12 +19,6 @@ public:
captureFlag = GetCaptureFlag(channel);
}
~Ultrasonic() = default;
Ultrasonic(const Ultrasonic&) = delete;
Ultrasonic& operator=(const Ultrasonic&) = delete;
Ultrasonic(Ultrasonic&&) = delete;
Ultrasonic& operator=(Ultrasonic&&) = delete;
double GetDistance(uint32_t time = 1) {
double distance = 0;
for (uint32_t cnt = 0; cnt < time; ++cnt) {
@@ -52,7 +46,6 @@ private:
double MeasureEchoTime() {
timer->Instance->CNT = 0;
uint32_t data[2];
__HAL_TIM_CLEAR_FLAG(timer, captureFlag);
HAL_TIM_IC_Start(timer, channel);
try {
for (auto& value : data) {

View File

@@ -6,12 +6,6 @@
class GpioHelper {
public:
GpioHelper() = delete;
GpioHelper(const GpioHelper&) = delete;
GpioHelper& operator=(const GpioHelper&) = delete;
GpioHelper(GpioHelper&&) = delete;
GpioHelper& operator=(GpioHelper&&) = delete;
struct Gpio {
GPIO_TypeDef* port;
uint16_t pin;
@@ -62,8 +56,7 @@ public:
return {port, pin};
}
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull,
const std::optional<GPIO_PinState> state = std::nullopt) {
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, const GPIO_PinState state) {
return GpioInit(gpio.port, gpio.pin, mode, pull, state);
}

View File

@@ -4,15 +4,16 @@
#include <cstdint>
#include <stdexcept>
namespace TaskHelper {
class TaskHelper {
public:
template <typename Func>
static void WaitFor(Func func, uint32_t timeoutMilliseconds) {
static void WaitFor(Func func, int timeoutMilliseconds) {
auto start = HAL_GetTick();
while (!func()) {
uint32_t now = HAL_GetTick();
if ((now - start) > timeoutMilliseconds) {
if ((now - start) > static_cast<uint32_t>(timeoutMilliseconds)) {
throw std::runtime_error("Operation timed out");
}
}
}
}; // namespace TaskHelper
};

View File

@@ -1,65 +1,36 @@
#include "Helper/delay_helper.h"
#include "stm32h563xx.h"
#include <string>
#include "Center/common_center.hpp"
#include "Common/serial_port.hpp"
#include "Helper/gpio_helper.hpp"
#include "config.hpp"
#include "Helper/delay_helper.h"
void TerminateHandler() {
auto led = Config::kLed;
auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
std::string message;
std::exception_ptr exceptionPtr = std::current_exception();
if (exceptionPtr) {
try {
std::rethrow_exception(exceptionPtr);
} catch (const std::exception& e) {
message = e.what();
} catch (...) {
message = "Unknown exception type";
}
} else {
message = "No active exception, but terminate() was called";
}
auto led = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
while (true) {
debugSerialPort.WriteLineBlocking("FATAL ERROR: %s", message.c_str());
led.Toggle();
DelayS(1);
}
}
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();
CommonCenter::GetHc05();
}
void Setup() {
GpioSetup();
GpioHelper::EnableAllGpioPeripheral();
DelaySetup();
CommonSetup();
std::set_terminate(TerminateHandler);
auto ultrasonic = CommonCenter::GetUltrasonic();
auto dfPlayer = CommonCenter::GetDfPlayer();
auto canMv = CommonCenter::GetCanMv();
}
extern "C" void AppStart() {
Setup();
auto& ultrasonic = CommonCenter::GetUltrasonic();
auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
auto ultrasonic = CommonCenter::GetUltrasonic();
auto dfPlayer = CommonCenter::GetDfPlayer();
auto canMv = CommonCenter::GetCanMv();
while (true) {
// Example usage of the classes
auto disatnce = ultrasonic.GetDistance();
debugSerialPort.WriteLineBlocking("Distance: %.2f cm", disatnce);
DelayS(1); // Delay for 1 second
ultrasonic.GetDistance();
dfPlayer.PlayTrack(1);
DelayMs(1000); // Delay for 1 second
}
}

View File

@@ -6,23 +6,13 @@
#include "./Helper/gpio_helper.hpp"
struct Config {
Config() = delete;
Config(const Config&) = delete;
Config& operator=(const Config&) = delete;
static inline struct {
GpioHelper::Gpio trigger;
TIM_HandleTypeDef* timer;
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* kHc05Uart = &hcom_uart[COM1];
static inline GpioHelper::Gpio kLed = {GPIOB, GPIO_PIN_0};
static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1];
};