generated from Template/H563ZI-HAL-CMake-Template
All checks were successful
Build and Upload Artifact / build and upload-artifact (push) Successful in 19m59s
44 lines
1.2 KiB
C++
44 lines
1.2 KiB
C++
#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;
|
|
}
|
|
};
|