#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; Hc05(const Hc05&) = delete; Hc05& operator=(const Hc05&) = delete; Hc05(Hc05&&) = delete; Hc05& operator=(Hc05&&) = delete; Response SendCommand() { Response data; serialPort->WriteBytesBlocking(reinterpret_cast(&kCommand), sizeof(kCommand)); serialPort->ReadBytesBlocking(reinterpret_cast(&data), sizeof(data)); return data; } };