Files
VSMD/src/main.cpp

124 lines
4.3 KiB
C++
Raw Normal View History

2025-06-27 10:03:05 +08:00
#include <ArduinoJson.h>
#include "IRIS_Method.h"
2025-06-30 13:27:25 +08:00
#include "vsmd_parser.h"
2025-07-08 21:15:48 +08:00
#include "VSMD_command_handler.h"
#include "bluetooth_handler.h" // 添加蓝牙模块
2025-06-27 10:03:05 +08:00
2025-06-30 13:27:25 +08:00
VSMDParser parser;
2025-06-27 10:03:05 +08:00
#define DEBUG_TX_PIN 17 // TX
#define DEBUG_RX_PIN 16 // RX
#define BUFFER_SIZE 1024
#define JSON_BUFFER_SIZE 512
2025-07-08 21:15:48 +08:00
HardwareSerial DebugSerial(1); // 使用UART1
// 全局变量
2025-06-27 10:03:05 +08:00
uint8_t receiveBuffer[BUFFER_SIZE]; // 接收缓冲区
uint8_t sendBuffer[BUFFER_SIZE]; // 发送缓冲区
uint8_t responseBuffer[BUFFER_SIZE]; // 响应缓冲区
uint8_t command; // 存储命令
int32_t bytesProcessed; // 处理的数据字节数
int32_t unpackResult; // 解包结果
int32_t packedLength; // 打包后的数据长度
// 设备状态变量
bool deviceEnabled = false;
2025-07-08 21:15:48 +08:00
int32_t pulsePerSecond;
2025-06-27 10:03:05 +08:00
void printDeviceStatus();
2025-07-08 21:15:48 +08:00
void handleSerialData();
void printDeviceStatus() {
DebugSerial.println("\n=== Device Status ===");
DebugSerial.print("Enabled: ");
DebugSerial.println(deviceEnabled ? "Yes" : "No");
DebugSerial.print("Bluetooth: ");
DebugSerial.println(isBluetoothConnected() ? "Connected" : "Disconnected");
DebugSerial.println("==================\n");
}
2025-06-27 10:03:05 +08:00
void setup() {
// 调试串口
2025-08-25 16:58:09 +08:00
DebugSerial.begin(9600, SERIAL_8N1, DEBUG_RX_PIN, DEBUG_TX_PIN);
2025-07-08 21:15:48 +08:00
DebugSerial.setTimeout(1);
2025-06-27 10:03:05 +08:00
2025-07-08 21:15:48 +08:00
// 电机数据通信串口
2025-08-25 16:58:09 +08:00
Serial.begin(9600);
2025-07-08 21:15:48 +08:00
Serial.setTimeout(100);
2025-06-27 10:03:05 +08:00
DebugSerial.println("VSMD INIT");
2025-07-08 21:15:48 +08:00
DebugSerial.println("CMD: dev, sts, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero, sav");
DebugSerial.println("cfg CMD: bdr, mcs, spd, acc, dec, cra, crn, crh, s1f, s1r, s2f, s2r, zmd, snr, osv, zsd, zsp, dmd, dar, msr, msv, psr, psv");
// 初始化蓝牙
initBluetooth();
2025-06-27 10:03:05 +08:00
printDeviceStatus();
}
void loop() {
2025-07-08 21:15:48 +08:00
// 处理串口数据
handleSerialData();
// 处理蓝牙数据
handleBluetoothData();
// 短暂延迟
delay(10);
}
void handleSerialData() {
2025-06-27 10:03:05 +08:00
if (DebugSerial.available() > 0) {
int bytesRead = DebugSerial.readBytes(receiveBuffer, sizeof(receiveBuffer));
bytesProcessed = IRIS_Cut_Befor_Header(receiveBuffer, bytesRead);
if (bytesProcessed > 0) {
unpackResult = IRIS_STM32_Protocol_Unpack(receiveBuffer, bytesProcessed, &command, responseBuffer);
if (unpackResult >= 0) {
// 根据命令类型处理数据
switch (command) {
case 0x00: { // JSON数据
char jsonString[JSON_BUFFER_SIZE];
memcpy(jsonString, responseBuffer, unpackResult);
2025-07-08 21:15:48 +08:00
jsonString[unpackResult] = '\0';
2025-08-25 16:58:09 +08:00
//printSerialRawData(responseBuffer, unpackResult);
2025-06-27 10:03:05 +08:00
processJsonCommand(jsonString);
2025-07-08 21:15:48 +08:00
//BLE Notify
sendBluetoothData(sendBuffer, packedLength);
2025-06-27 10:03:05 +08:00
break;
}
case 0x01: { // 字符串数据
char stringData[BUFFER_SIZE];
memcpy(stringData, responseBuffer, unpackResult);
stringData[unpackResult] = '\0';
DebugSerial.print("Received String: ");
DebugSerial.println(stringData);
processStringCommand(stringData);
break;
}
2025-07-08 21:15:48 +08:00
default: { // 自定义二进制数据
2025-06-27 10:03:05 +08:00
DebugSerial.print("Received Binary Data, Command: 0x");
DebugSerial.print(command, HEX);
DebugSerial.print(", Length: ");
DebugSerial.println(unpackResult);
// 回显
packedLength = IRIS_Protocol_Pack(command, unpackResult, responseBuffer, sendBuffer);
DebugSerial.write(sendBuffer, packedLength);
break;
}
}
} else {
DebugSerial.print("Unpack failed, error code: ");
DebugSerial.println(unpackResult);
}
}
}
}