mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-04-01 05:17:13 +08:00
重构User_code目录结构:将文件组织到子文件夹中
主要更改: - 将所有BSP外设文件移动到独立子文件夹(can/, fdcan/, uart/等) - 将所有Component文件移动到独立子文件夹(pid/, filter/, cmd/等) - 将所有Device文件移动到独立子文件夹(dr16/, bmi088/等) - 更新代码生成器以支持新的文件夹结构 - 保持向后兼容性,支持从子文件夹或根目录加载模板 - 添加STRUCTURE.md文档说明新的目录结构 优势: ✅ 更好的代码组织和管理 ✅ 便于添加、删除、修改模板 ✅ 清晰的模块划分 ✅ 向后兼容现有结构
This commit is contained in:
106
assets/User_code/device/vofa/vofa.c
Normal file
106
assets/User_code/device/vofa/vofa.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "device/vofa.h"
|
||||
#include "bsp/uart.h"
|
||||
|
||||
/* USER INCLUDE BEGIN */
|
||||
|
||||
/* USER INCLUDE END */
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
||||
#define MAX_CHANNEL 64u // 根据实际最大通道数调整
|
||||
|
||||
#define JUSTFLOAT_TAIL 0x7F800000
|
||||
|
||||
/* USER DEFINE BEGIN */
|
||||
|
||||
/* USER DEFINE END */
|
||||
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* USER STRUCT BEGIN */
|
||||
|
||||
/* USER STRUCT END */
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static uint8_t vofa_tx_buf[sizeof(float) * MAX_CHANNEL + sizeof(uint32_t)];
|
||||
static VOFA_Protocol_t current_protocol = VOFA_PROTOCOL_FIREWATER; // 默认协议
|
||||
|
||||
/* Private function -------------------------------------------------------- */
|
||||
/* USER FUNCTION BEGIN */
|
||||
|
||||
/* USER FUNCTION END */
|
||||
|
||||
/************************ RawData *************************/
|
||||
void VOFA_RawData_Send(const char* data, bool dma) {
|
||||
BSP_UART_Transmit(BSP_UART_VOFA, (uint8_t*)data, strlen(data), dma);
|
||||
}
|
||||
|
||||
/************************ FireWater *************************/
|
||||
void VOFA_FireWater_Send(float *channels, uint8_t channel_count, bool dma)
|
||||
{
|
||||
if (channel_count == 0 || channel_count > MAX_CHANNEL)
|
||||
return;
|
||||
|
||||
char *buf = (char *)vofa_tx_buf;
|
||||
size_t len = 0;
|
||||
|
||||
for (uint8_t i = 0; i < channel_count; ++i) {
|
||||
len += snprintf(buf + len,
|
||||
sizeof(vofa_tx_buf) - len,
|
||||
"%s%.2f",
|
||||
(i ? "," : ""),
|
||||
channels[i]);
|
||||
}
|
||||
snprintf(buf + len, sizeof(vofa_tx_buf) - len, "\n");
|
||||
|
||||
BSP_UART_Transmit(BSP_UART_VOFA, vofa_tx_buf, strlen(buf), dma);
|
||||
}
|
||||
|
||||
/************************ JustFloat *************************/
|
||||
void VOFA_JustFloat_Send(float *channels, uint8_t channel_count, bool dma)
|
||||
{
|
||||
if (channel_count == 0 || channel_count > MAX_CHANNEL)
|
||||
return;
|
||||
memcpy(vofa_tx_buf, channels, channel_count * sizeof(float));
|
||||
|
||||
uint32_t tail = JUSTFLOAT_TAIL; // 0x7F800000
|
||||
memcpy(vofa_tx_buf + channel_count * sizeof(float), &tail, sizeof(tail));
|
||||
|
||||
BSP_UART_Transmit(BSP_UART_VOFA, vofa_tx_buf, channel_count * sizeof(float) + sizeof(tail), dma);
|
||||
}
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
int8_t VOFA_init(VOFA_Protocol_t protocol) {
|
||||
current_protocol = protocol;
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
int8_t VOFA_Send(float* channels, uint8_t channel_count, bool dma) {
|
||||
switch (current_protocol) {
|
||||
case VOFA_PROTOCOL_RAWDATA:
|
||||
{
|
||||
char data[256];
|
||||
if (channel_count >= 1) {
|
||||
sprintf(data, "Channel1: %.2f", channels[0]);
|
||||
if (channel_count >= 2) {
|
||||
sprintf(data + strlen(data), ", Channel2: %.2f", channels[1]);
|
||||
}
|
||||
strcat(data, "\n");
|
||||
VOFA_RawData_Send(data, dma);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case VOFA_PROTOCOL_FIREWATER:
|
||||
VOFA_FireWater_Send(channels, channel_count, dma);
|
||||
break;
|
||||
case VOFA_PROTOCOL_JUSTFLOAT:
|
||||
VOFA_JustFloat_Send(channels, channel_count, dma);
|
||||
break;
|
||||
default:
|
||||
return DEVICE_ERR;
|
||||
}
|
||||
return DEVICE_OK;
|
||||
}
|
||||
39
assets/User_code/device/vofa/vofa.h
Normal file
39
assets/User_code/device/vofa/vofa.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp/uart.h"
|
||||
#include "device/device.h"
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
|
||||
typedef enum {
|
||||
VOFA_PROTOCOL_RAWDATA,
|
||||
VOFA_PROTOCOL_FIREWATER,
|
||||
VOFA_PROTOCOL_JUSTFLOAT,
|
||||
} VOFA_Protocol_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化VOFA设备
|
||||
* @param protocol 设置通信协议
|
||||
* @return
|
||||
*/
|
||||
int8_t VOFA_init(VOFA_Protocol_t protocol);
|
||||
|
||||
/**
|
||||
* @brief 发送数据到VOFA
|
||||
* @param channels 要发送的通道数据
|
||||
* @param channel_count 通道数量
|
||||
* @param dma 是否使用DMA发送
|
||||
* @return
|
||||
*/
|
||||
int8_t VOFA_Send(float* channels, uint8_t channel_count, bool dma);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user