mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-04-01 05:17:13 +08:00
修改了can
This commit is contained in:
77
assets/User_code/device/vofa.c
Normal file
77
assets/User_code/device/vofa.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "vofa.h"
|
||||
#include "bsp/uart.h"
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
||||
//#define PROTOCOL_RAWDATA
|
||||
#define PROTOCOL_FIREWATER
|
||||
//#define PROTOCOL_JUSTFLOAT
|
||||
|
||||
#define MAX_CHANNEL 64u // 根据实际最大通道数调整
|
||||
|
||||
#define JUSTFLOAT_TAIL 0x7F800000
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static uint8_t vofa_tx_buf[sizeof(float) * MAX_CHANNEL + sizeof(uint32_t)];
|
||||
|
||||
/* Private function -------------------------------------------------------- */
|
||||
|
||||
/************************ 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 ------------------------------------------------------- */
|
||||
init8_t VOFA_Send(float* channels, uint8_t channel_count, bool dma) {
|
||||
#ifdef PROTOCOL_RAWDATA
|
||||
sprintf(vofa_tx_buf, "Channel1:%.2f,Channel2:%.2f\n", channels[0],channels[1]);
|
||||
VOFA_RawData_Send(vofa_tx_buf, dma);
|
||||
#elif defined(PROTOCOL_FIREWATER)
|
||||
VOFA_FireWater_Send(channels, channel_count, dma);
|
||||
#elif defined(PROTOCOL_JUSTFLOAT)
|
||||
VOFA_JustFloat_Send(channels, channel_count, dma);
|
||||
#else
|
||||
// 默认使用RawData协议
|
||||
char data[256];
|
||||
sprintf(data, "Channel1: %.2f, Channel2: %.2f\n", channels[0], channels[1]);
|
||||
VOFA_RawData_Send(data, dma);
|
||||
#endif
|
||||
return DEVICE_OK;
|
||||
}
|
||||
39
assets/User_code/device/vofa.h
Normal file
39
assets/User_code/device/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