generated from Template/H563ZI-HAL-CMake-Template
新增 Hc05 类,支持 HC-05 蓝牙模块的串口通信,增加串口设置波特率函数
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 19m59s
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 19m59s
This commit is contained in:
43
Core/App/Common/hc05.hpp
Normal file
43
Core/App/Common/hc05.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#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;
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user