generated from Template/H563ZI-HAL-CMake-Template
新增 DfPlayer 和 CanMv 类,优化 UART 读写方法,更新 CommonCenter 以支持新类实例化
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Has been cancelled
Some checks failed
Build and Upload Artifact / build and upload-artifact (push) Has been cancelled
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Common/can_mv.hpp"
|
||||||
|
#include "../Common/df_player.hpp"
|
||||||
#include "../Common/ultrasonic.hpp"
|
#include "../Common/ultrasonic.hpp"
|
||||||
#include "../config.hpp"
|
#include "../config.hpp"
|
||||||
|
|
||||||
@@ -13,4 +15,20 @@ public:
|
|||||||
}
|
}
|
||||||
return *instance;
|
return *instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DfPlayer& GetDfPlayer() {
|
||||||
|
static DfPlayer* instance = nullptr;
|
||||||
|
if (instance == nullptr) {
|
||||||
|
instance = new DfPlayer(Config::kDfPlayerUart);
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CanMv& GetCanMv() {
|
||||||
|
static CanMv* instance = nullptr;
|
||||||
|
if (instance == nullptr) {
|
||||||
|
instance = new CanMv(Config::kCanMvUart);
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
19
Core/App/Common/can_mv.hpp
Normal file
19
Core/App/Common/can_mv.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
|
class CanMv {
|
||||||
|
public:
|
||||||
|
explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
|
|
||||||
|
~CanMv() {
|
||||||
|
delete serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr uint32_t kLength = 128;
|
||||||
|
static constexpr uint32_t kTimeout = 10;
|
||||||
|
SerialPort* serialPort;
|
||||||
|
};
|
||||||
41
Core/App/Common/df_player.cpp
Normal file
41
Core/App/Common/df_player.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "df_player.hpp"
|
||||||
|
|
||||||
|
void DfPlayer::SendCommand(Command cmd, uint16_t param) {
|
||||||
|
uint8_t buffer[10];
|
||||||
|
buffer[0] = kStartByte;
|
||||||
|
buffer[1] = kVersion;
|
||||||
|
buffer[2] = 0x06;
|
||||||
|
buffer[3] = static_cast<uint8_t>(cmd);
|
||||||
|
buffer[4] = kFeedback;
|
||||||
|
buffer[5] = (param >> 8) & 0xFF;
|
||||||
|
buffer[6] = param & 0xFF;
|
||||||
|
|
||||||
|
uint16_t checksum = 0xFFFF - (kVersion + 0x06 + static_cast<uint8_t>(cmd) + kFeedback + buffer[5] + buffer[6]) + 1;
|
||||||
|
buffer[7] = (checksum >> 8) & 0xFF;
|
||||||
|
buffer[8] = checksum & 0xFF;
|
||||||
|
buffer[9] = kEndByte;
|
||||||
|
|
||||||
|
serialPort->WriteBytesBlocking(buffer, sizeof(buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::Play() {
|
||||||
|
SendCommand(Command::kPlay);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::Stop() {
|
||||||
|
SendCommand(Command::kStop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::SetVolume(uint8_t volume) {
|
||||||
|
if (volume > 30) {
|
||||||
|
volume = 30;
|
||||||
|
}
|
||||||
|
SendCommand(Command::kSetVolume, volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::PlayTrack(uint16_t track) {
|
||||||
|
if (track < 1 || track > 2999) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SendCommand(Command::kPlayTrack, track);
|
||||||
|
}
|
||||||
63
Core/App/Common/df_player.hpp
Normal file
63
Core/App/Common/df_player.hpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
|
class DfPlayer {
|
||||||
|
public:
|
||||||
|
explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
|
|
||||||
|
~DfPlayer() {
|
||||||
|
delete serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Play();
|
||||||
|
void Stop();
|
||||||
|
void SetVolume(uint8_t volume);
|
||||||
|
void PlayTrack(uint16_t track);
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class Command : uint8_t {
|
||||||
|
kNext = 0x01,
|
||||||
|
kPrevious = 0x02,
|
||||||
|
kPlayTrack = 0x03,
|
||||||
|
kVolumeUp = 0x04,
|
||||||
|
kVolumeDown = 0x05,
|
||||||
|
kSetVolume = 0x06,
|
||||||
|
kSetEQ = 0x07,
|
||||||
|
kSingleLoopPlayTrack = 0x08,
|
||||||
|
kSetPlaybackDevice = 0x09,
|
||||||
|
kEnterSleep = 0x0A,
|
||||||
|
kResetModule = 0x0C,
|
||||||
|
kPlay = 0x0D,
|
||||||
|
kPause = 0x0E,
|
||||||
|
kPlayFolderTrack = 0x0F,
|
||||||
|
kSetAmplification = 0x10,
|
||||||
|
kLoopAll = 0x11,
|
||||||
|
kPlayMp3FolderTrack = 0x12,
|
||||||
|
kInsertAd = 0x13,
|
||||||
|
kStopAdPlayBackground = 0x15,
|
||||||
|
kStop = 0x16,
|
||||||
|
kQueryStatus = 0x42,
|
||||||
|
kQueryVolume = 0x43,
|
||||||
|
kQueryEQ = 0x44,
|
||||||
|
kQueryUDiskTotalFiles = 0x47,
|
||||||
|
kQueryTFCardTotalFiles = 0x48,
|
||||||
|
kQueryFlashTotalFiles = 0x49,
|
||||||
|
kQueryUDiskCurrentTrack = 0x4B,
|
||||||
|
kQueryTFCardCurrentTrack = 0x4C,
|
||||||
|
kQueryFlashCurrentTrack = 0x4D,
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr uint32_t kLength = 128;
|
||||||
|
static constexpr uint32_t kTimeout = 10;
|
||||||
|
SerialPort* serialPort;
|
||||||
|
|
||||||
|
void SendCommand(Command cmd, uint16_t param = 0);
|
||||||
|
|
||||||
|
static constexpr uint8_t kStartByte = 0x7E;
|
||||||
|
static constexpr uint8_t kVersion = 0xFF;
|
||||||
|
static constexpr uint8_t kFeedback = 0x00;
|
||||||
|
static constexpr uint8_t kEndByte = 0xEF;
|
||||||
|
};
|
||||||
@@ -29,7 +29,10 @@ public:
|
|||||||
char c;
|
char c;
|
||||||
ReadByte();
|
ReadByte();
|
||||||
while (true) {
|
while (true) {
|
||||||
HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&c), sizeof(c), HAL_MAX_DELAY);
|
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");
|
||||||
|
}
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
if (!result.empty() && result.back() == '\r') {
|
if (!result.empty() && result.back() == '\r') {
|
||||||
result.pop_back();
|
result.pop_back();
|
||||||
@@ -41,6 +44,13 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReadBytesBlocking(uint8_t* data, size_t size) {
|
||||||
|
auto r = HAL_UART_Receive(handle, data, size, timeout);
|
||||||
|
if (r != HAL_OK) {
|
||||||
|
throw std::runtime_error("UART receive error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
||||||
HAL_UART_Transmit(handle, const_cast<uint8_t*>(data), size, timeout);
|
HAL_UART_Transmit(handle, const_cast<uint8_t*>(data), size, timeout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ void Setup() {
|
|||||||
GpioHelper::EnableAllGpioPeripheral();
|
GpioHelper::EnableAllGpioPeripheral();
|
||||||
DelaySetup();
|
DelaySetup();
|
||||||
auto ultrasonic = CommonCenter::GetUltrasonic();
|
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||||
|
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||||
|
auto canMv = CommonCenter::GetCanMv();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void AppStart() {
|
extern "C" void AppStart() {
|
||||||
|
|||||||
@@ -11,4 +11,8 @@ struct Config {
|
|||||||
TIM_HandleTypeDef* timer;
|
TIM_HandleTypeDef* timer;
|
||||||
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* kDfPlayerUart = &hcom_uart[COM1];
|
||||||
|
|
||||||
|
static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user