From 89a0366629c1d2afe2c75a49181aaca8ddcc916f Mon Sep 17 00:00:00 2001 From: chenxin Date: Fri, 27 Jun 2025 10:03:05 +0800 Subject: [PATCH] first --- .gitignore | 5 + .vscode/extensions.json | 10 + .vscode/settings.json | 60 + VinceMotorControllerUsb.cpp | 357 +++++ VinceMotorControllerUsb.h | 50 + device_config.json | 65 + include/README | 37 + lib/README | 46 + main_test.cpp | 2650 +++++++++++++++++++++++++++++++++++ platformio - 鍓湰.ini | 19 + platformio.ini | 15 + src/IRIS_Method.c | 208 +++ src/IRIS_Method.h | 66 + src/ble.cpp | 85 ++ src/ble.h | 14 + src/main.cpp | 493 +++++++ src/vsmd_parser.cpp | 272 ++++ src/vsmd_parser.h | 34 + test/README | 11 + test5.json | 23 + 20 files changed, 4520 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 VinceMotorControllerUsb.cpp create mode 100644 VinceMotorControllerUsb.h create mode 100644 device_config.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 main_test.cpp create mode 100644 platformio - 鍓湰.ini create mode 100644 platformio.ini create mode 100644 src/IRIS_Method.c create mode 100644 src/IRIS_Method.h create mode 100644 src/ble.cpp create mode 100644 src/ble.h create mode 100644 src/main.cpp create mode 100644 src/vsmd_parser.cpp create mode 100644 src/vsmd_parser.h create mode 100644 test/README create mode 100644 test5.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0422cc8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,60 @@ +{ + "window.zoomLevel": 0, + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/VinceMotorControllerUsb.cpp b/VinceMotorControllerUsb.cpp new file mode 100644 index 0000000..44f6892 --- /dev/null +++ b/VinceMotorControllerUsb.cpp @@ -0,0 +1,357 @@ +#include "VinceMotorControllerUsb.h" + +VinceMotorControllerUsb::VinceMotorControllerUsb(QString serialPortNumber, QString paramJson, QObject* parent) +:QObject(parent) +{ + m_pSerialPort = new QSerialPort(this); + + m_pSerialPort->setPortName(serialPortNumber); + m_pSerialPort->setReadBufferSize(512); + + bool bRes = m_pSerialPort->setBaudRate(9600); + if (!bRes) + { + //qDebug() << "Err:setBaudRate Failed.Exit Code:1"; + //std::cout << "Err.setBaudRate Failed" << std::endl; + printf("Err:setBaudRate Failed.Exit Code:1"); + } + + bRes = m_pSerialPort->open(QIODevice::ReadWrite); + if (!bRes) + { + //qDebug() << "Err:open Failed.Exit Code:2"; + //std::cout << "Err.open Failed" << std::endl; + printf("Err:open Failed.Exit Code:2"); + } + + m_iSpeed = 3000; + + init(paramJson); +} + +VinceMotorControllerUsb::~VinceMotorControllerUsb() +{ + delete m_pSerialPort; +} + +int VinceMotorControllerUsb::sendCommand2Motor(QString cmd) +{ + QByteArray commandData = cmd.toUtf8(); // 将 QString 转换为 QByteArray + qint64 bytesWritten = m_pSerialPort->write(commandData); + + /*if (bytesWritten == -1) { + qDebug() << "写入失败:" << m_pSerialPort->errorString(); + } + else if (bytesWritten != commandData.size()) { + qDebug() << "写入不完整"; + } + else { + qDebug() << "命令发送成功"; + }*/ + + return bytesWritten; +} + +int VinceMotorControllerUsb::recvData(QByteArray& dataRecv) +{ + dataRecv.clear(); + + QByteArray temp; + + temp = m_pSerialPort->readAll(); + dataRecv.append(temp); + + while (dataRecv.size() < 21) + { + m_pSerialPort->waitForReadyRead(100); + temp = m_pSerialPort->readAll(); + dataRecv.append(temp); + } + + return 0; +} + +IrisMotorErrorCode VinceMotorControllerUsb::extractOneValidFrame(QByteArray& buffer) +{ + //std::cout << "before------" << buffer.size() << std::endl; + //std::cout << buffer.toHex().toStdString() << std::endl; + + IrisMotorErrorCode errorCode = MOTOR_NO_ERROR; + + //方式1: + /*int startPos = buffer.lastIndexOf('\xFF'); + int endPos = buffer.indexOf('\xFE'); + + if (endPos <= startPos) + { + errorCode = MOTOR_INVALID_FRAME; + return errorCode; + } + + buffer = buffer.mid(startPos, endPos - startPos + 1);*/ + + //方式2: + //ff00ffffff000200000000000000391f6900000d42030001feff00ff000200000000000000391f6900000d42030001feff0002043458000000003c745e00000d402b001afeff00ff0002000000000000003d3b7200000d4207003efeff00020c34580000000039076e00000d4023005cfeff00ff00020000000000000038411900000d420f0022feff000204345800000000477e7700000d402b0042feff00ff00020000000000000048456a00000d420b0021feff00020434245400000049006d00000d400b0020feff00ff000200000000000000493c3100000d4203000afeff0002043458000000004d444b00000d402f004afe + //ff00ffffffff000200000000000000112d6f00000c42070018fe + QList validFrames; + int start = 0; + + while (start < buffer.size()) + { + // 查找帧头 'ff' + int frameStart = buffer.indexOf(MOTOR_SYNC, start); + if (buffer.at(frameStart + 1) == MOTOR_SYNC) + continue; + if (frameStart == -1) + { + break; // 没有找到帧头,退出循环 + } + + // 查找帧尾 'fe' + int frameEnd = buffer.indexOf(MOTOR_ETX, frameStart + 1); + if (frameEnd == -1) + { + break; // 没有找到帧尾,退出循环 + } + + // 提取有效帧(包括帧头和帧尾) + QByteArray frame = buffer.mid(frameStart, frameEnd - frameStart + 1); + + // 检查帧长度是否等于21 + if (frame.size() == 21) + { + validFrames.append(frame); + } + + // 更新起始位置,继续查找下一个帧 + start = frameEnd + 1; + } + if (validFrames.size() == 0) + { + std::cout << "error--------------: " << buffer.toHex().toStdString() << std::endl; + + + errorCode = MOTOR_INVALID_FRAME; + return errorCode; + } + + buffer = validFrames.at(validFrames.size() - 1); + + //方式3:从结尾遍历,找到有效帧(1)找到所有fe(2)从最后一个fe开始向前找到有效帧(长度为21) + + //std::cout << "after" << buffer.size() << std::endl; + //std::cout << buffer.toHex().toStdString() << std::endl; + + return errorCode; +} + +IrisMotorErrorCode VinceMotorControllerUsb::parseOneValidFrame(QByteArray& buf, State& backdatt) +{ + IrisMotorErrorCode errorCode = MOTOR_NO_ERROR; + + backdatt.Speed = MOTOR_disconnection; + + buf.remove(0, 3); + buf.remove(buf.length() - 3, 3); + int location; + unsigned char a[15]; + memcpy(a, buf.data(), 15); + location = ((a[0] & 0x0f) << 28) | ((a[1] & 0x7f) << 21) | ((a[2] & 0x7f) << 14) | ((a[3] & 0x7f) << 7) | ((a[4] & 0x7f)); + float speed; + + memcpy(&speed, &location, 4); + backdatt.Speed = speed; + int loc; + loc = ((a[5] & 0x0f) << 28) | ((a[6] & 0x7f) << 21) | ((a[7] & 0x7f) << 14) | ((a[8] & 0x7f) << 7) | ((a[9] & 0x7f)); + backdatt.Location = loc; + backdatt.Speed = speed; + location = ((a[10] & 0x0f) << 28) | ((a[11] & 0x7f) << 21) | ((a[12] & 0x7f) << 14) | ((a[13] & 0x7f) << 7) | ((a[14] & 0x7f)); + backdatt.Stata = unsigned int(location); + + /*QString str1 = "Speed " + QString::number(backdatt.Speed, 'f', 2) + " location " + QString::number(backdatt.Location); + qDebug() << str1;*/ + + return errorCode; +} + +IrisMotorErrorCode VinceMotorControllerUsb::parseBytedata(QByteArray buf, State& backdatt) +{ + IrisMotorErrorCode errorCode = extractOneValidFrame(buf); + + backdatt.Speed = MOTOR_disconnection; + backdatt.Location = 0; + if (errorCode == MOTOR_NO_ERROR) + { + parseOneValidFrame(buf, backdatt); + return errorCode; + } + + errorCode = MOTOR_INVALID_FRAME; + return errorCode; +} + +void VinceMotorControllerUsb::init(QString jsonPath) +{ + enable(); + + JsonOperate xxx(jsonPath); + //xxx.createJsonFile(); + QStringList re = xxx.parseJsonFile(); + + for (const QString& pair : re) + { + //qDebug().noquote() << pair; + sendCommand2Motor(pair); + + QByteArray buf; + recvData(buf); + } + + //QString cmd; + + ////cmd = "cfg s1f=2\n";//用不上 + ////sendCommand2Motor(cmd); + + ////归零设置 + //cmd = "cfg zmd=2\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg snr=0\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg osv=0\n";//归零用传感器常开常闭设置 + //sendCommand2Motor(cmd); + //cmd = "cfg zsd=3000\n";//正数向原点走 + //sendCommand2Motor(cmd); + //cmd = "cfg zsp=2400\n"; + //sendCommand2Motor(cmd); + + ////设置正负极限 + //cmd = "cfg msr=1\n";//负极限 + //sendCommand2Motor(cmd); + //cmd = "cfg msv=0\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg psr=2\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg psv=0\n"; + //sendCommand2Motor(cmd); + + ////加减速度和电流设置 + //cmd = "cfg mcs=7\n";//设置细分 + //sendCommand2Motor(cmd); + //cmd = "cfg acc=20000\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg dec=20000\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg crn=4\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg cra=4\n"; + //sendCommand2Motor(cmd); + //cmd = "cfg crh=1\n"; + //sendCommand2Motor(cmd); +} + +bool VinceMotorControllerUsb::isConnected() +{ + State re = getState(); + if (re.Speed == MOTOR_disconnection) + { + return false; + } + else + { + return true; + } +} + +void VinceMotorControllerUsb::enable() +{ + QString cmd = "ena\n"; + sendCommand2Motor(cmd); + QByteArray buf; + recvData(buf); +} + +void VinceMotorControllerUsb::move2Pos(double pos) +{ + QString cmd = "pos " + QString::number(pos, 'f', 10) + "\n"; + sendCommand2Motor(cmd); + QByteArray buf; + recvData(buf); +} + +void VinceMotorControllerUsb::move(bool towardOrigin) +{ + if (towardOrigin) + { + setSpeed(abs(m_iSpeed) * -1); + } + else + { + setSpeed(abs(m_iSpeed)); + } + + sendCommand2Motor("mov\n"); + QByteArray buf; + recvData(buf); +} + +void VinceMotorControllerUsb::stopMove() +{ + QString cmd = "stp\n"; + sendCommand2Motor(cmd); + QByteArray buf; + recvData(buf); +} + +void VinceMotorControllerUsb::setSpeed(double speed) +{ + m_iSpeed = speed; + QString cmd = "cfg spd=" + QString::number(speed) + "\n"; + sendCommand2Motor(cmd); + QByteArray buf; + recvData(buf); +} + +double VinceMotorControllerUsb::getSpeed() +{ + State re = getState(); + return re.Speed; +} + +double VinceMotorControllerUsb::getCurrentLoc() +{ + State re = getState(); + return re.Location; +} + +void VinceMotorControllerUsb::zeroStart() +{ + QString cmd = "zero start\n"; + sendCommand2Motor(cmd); + QByteArray buf; + recvData(buf); +} + +int VinceMotorControllerUsb::getSpeedLocation(double& speed, double& locatioin) +{ + State re = getState(); + speed = re.Speed; + locatioin = re.Location; + + return 0; +} + +State VinceMotorControllerUsb::getState() +{ + QString cmd = "sts\n"; + sendCommand2Motor(cmd); + + QByteArray buf; + recvData(buf); + //std::cout << buf.size() << std::endl; + //std::cout << buf.toHex().toStdString() << std::endl; + + State back; + IrisMotorErrorCode errorCode = parseBytedata(buf, back); + + return back; +} diff --git a/VinceMotorControllerUsb.h b/VinceMotorControllerUsb.h new file mode 100644 index 0000000..d89d4ff --- /dev/null +++ b/VinceMotorControllerUsb.h @@ -0,0 +1,50 @@ +#pragma once +#include +#include +#include +#include +#include + +#include "JsonOperate.h" +#include "MotorControllerBase.h" + +#define MOTOR_SYNC (0xFF)//帧头 +#define MOTOR_ETX (0xFE)//帧尾 + +#define MOTOR_disconnection -1000000//速度等于这个数就代表马达通讯异常 + +class VinceMotorControllerUsb : + public MotorControllerBase, QObject +{ + Q_OBJECT + +public: + VinceMotorControllerUsb(QString serialPort, QString paramJson, QObject* parent = nullptr); + ~VinceMotorControllerUsb(); + + void init(QString jsonPath); + int sendCommand2Motor(QString cmd); + bool isConnected(); + void enable(); + + void move2Pos(double pos); + void move(bool towardOrigin);//true:远离原点,false:靠近原点 + void stopMove(); + void setSpeed(double speed); + double getSpeed(); + double getCurrentLoc(); + void zeroStart(); + + int getSpeedLocation(double& speed, double& locatioin); + + State getState(); + int recvData(QByteArray& dataRecv); + IrisMotorErrorCode parseBytedata(QByteArray buf, State& backdatt); + IrisMotorErrorCode extractOneValidFrame(QByteArray& buffer); + IrisMotorErrorCode parseOneValidFrame(QByteArray& buffer, State& result); + +private: + QSerialPort* m_pSerialPort; + + double m_iSpeed; +}; diff --git a/device_config.json b/device_config.json new file mode 100644 index 0000000..aff581a --- /dev/null +++ b/device_config.json @@ -0,0 +1,65 @@ +{ + "cmd": "cfg", + "params": { + "cfg zmd": 1, + "cfg snr": 0, + "cfg osv": 0, + "cfg zsd": 1200, + "cfg zsp": 2400, + "cfg dmd": 2, + "cfg dar": 5, + "cfg msr": 1, + "cfg msv": 1, + "cfg psr": 1, + "cfg psv": 1, + "cfg bdr": 115200, + "cfg cid": 1, + "cfg mcs": 5, + "cfg spd": 1200, + "cfg acc": 12000, + "cfg dec": 12000, + "cfg cra": 0.8, + "cfg crn": 0.4, + "cfg crh": 0.0 + }, + "actions": [ + { + "cmd": "ena" + }, + { + "cmd": "off" + }, + { + "cmd": "mov" + }, + { + "cmd": "pos", + "value": 10000 + }, + { + "cmd": "rmv", + "value": -6400 + }, + { + "cmd": "pps" + }, + { + "cmd": "pps", + "value": 12800 + }, + { + "cmd": "org" + }, + { + "cmd": "stp" + }, + { + "cmd": "zero", + "value": "start" + }, + { + "cmd": "zero", + "value": "stop" + } + ] +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/main_test.cpp b/main_test.cpp new file mode 100644 index 0000000..773c3e2 --- /dev/null +++ b/main_test.cpp @@ -0,0 +1,2650 @@ +#include"Define.h" +#include +#include +#include +#include +#include +#include +#include +//#include "MyWebServer.h" +#include "SDmanger.h" +//#include "HttpsOTAUpdate.h" +#include "slave.h" +#include "log.h" + +#include +#include +#include + +#include "IRIS_Method.h" +#include "DS18B20.h" +#include "Servo.h" +#include "jiaresi.h" +#include +#include "servo.h" +#include "SensorIS11.h" +#include "tuigan.h" +#include "beep.h" +#include "My74HC595.h" +#include "SolarCalculator.h" +#define LOGGING +#define wb485PORT_RX 3 +#define wb485PORT_TX 2 + +#define MY74HC595_dataPin 35 +#define MY74HC595_clockPin 33 +#define MY74HC595_latchPin 34 + +#define IS1Serial_port 1 +#define IS1Serial_RX 18 +#define IS1Serial_TX 17 +// u_char ret[17]; + +// Ticker ticker; + +/* + 浠诲姟鍚嶏細Task0 + 浠诲姟鍔熻兘锛氬厜璋辨暟鎹噰闆 +*/ +TaskHandle_t Task0_Handler; +void Task0(void *pvParameters); +/* + 浠诲姟鍚嶏細Task1 + 浠诲姟鍔熻兘锛氬璁炬帶鍒,鍔犵儹 銆佹帹鏉嗐佽埖鏈恒佹俯搴﹂噰闆嗐佹椂闂磋幏鍙 +*/ +TaskHandle_t Task1_Handler; +void Task1(void *pvParameters); + +/* + 浠诲姟鍚嶏細Task2 + 浠诲姟鍔熻兘锛氭暟鎹笂浼 +*/ +TaskHandle_t Task2_Handler; +void Task2(void *pvParameters); + +//work_mode 0:鎵嬪姩 1锛氭椂闂存ā寮 time_mode 2锛氶珮绾фā寮 advanced_mode 3:low_power_mode +//caiji_mode 0锛涘仠姝 1锛氬崟娆 2锛氳繛缁 3锛氬畾鏍 4锛氶珮绾фā寮 5锛氬洖浼犳暟鎹 6锛氫綆鍔熻楁ā寮 +//atuo_return 0:涓嶈嚜鍔ㄨ繑鍥 1锛氳嚜鍔ㄨ繑鍥 +//return_mode 璁剧疆杩斿洖妯″紡 鏆傛椂娌$敤 +//return_data_type 璇︽儏瑙佷綘鎳傜殑O.o +//time_interval 鏃堕棿闂撮殧涓嶈兘绛変簬0锛 +//caiji_state 0 :鏈噰闆嗭紙no锛 1锛氶噰闆嗗畬鎴愶紙finish锛 2锛氶噰闆嗕腑锛坵orking锛 +//dingbiao_time 0鑷姩鏇濆厜 闈0浣跨敤杩欎釜鏃堕暱鏇濆厜 +volatile u_int32_t work_mode = 0,caiji_mode = 0,return_mode = 1,atuo_return = 0,return_data_type = 0x00,time_interval = 60 * 2,caiji_state = 0 ,db_shutter,db_avn; +volatile float start_time = 9.0,stop_time = 15.30,maximum_temperature = 40,Minimum_temperature = 0,bc_b1,bc_b2,bc_b3,bc_b4; + + + +/// @brief 楂樼骇妯″紡鍙傛暟 +// advanced_direction 0:up 1:down 2:dark +// advanced_shutter_time 鏇濆厜鏃堕棿 +// advanced_collect_times 閲囬泦娆℃暟 +// advanced_remove_dark 0:涓嶅幓鏆 1:鍘绘殫 +volatile u_int32_t advanced_direction=0,advanced_shutter_time=0,advanced_collect_times=0,advanced_remove_dark=0; + +u_int32_t timeOut = 10000; +// SoftwareSerial *wb485port; + +SemaphoreHandle_t xMutexInventory = NULL; //鍒涘缓淇″彿閲廐andler + +//json +DynamicJsonDocument doc(1024);//鎺ユ敹涓插彛json +DynamicJsonDocument sys_sd_doc(1024);//璇诲彇绯荤粺json +DynamicJsonDocument ds18b20_doc(1024);//璇诲彇ds18b20 json + + +void json_command(); +void sys_info_init(); + +//////ds1302鏃堕棿///////// +Ds1302 rtc(0, 8, 7); +Ds1302::DateTime now; +bool sys_set_time(uint32_t year,uint32_t month,uint32_t day); +// const static char* WeekDays[] = +// { +// "Monday", +// "Tuesday", +// "Wednesday", +// "Thursday", +// "Friday", +// "Saturday", +// "Sunday" +// }; + +struct IS11_datastruct *IS11_datastructure; + +//////is11///////// +SensorIS11 *is11Sensor; +STRSensorInfo STRSensorInfos_structure; + +// SoftwareSerial *IS1Serial; +HardwareSerial *IS1Serial; + +//鍘熸潵 +HardwareSerial wb485Serial(0); + +size_t serialwrite(u_char* data,size_t lenth) +{ + // return IS1Serial->write(data,lenth); + while (IS1Serial->available() > 0) + { + IS1Serial->read(); + + } + + int rec=IS1Serial->write(data,lenth); + + return rec; +} + +size_t serialread(u_char *data, size_t lenth) +{ + lenth=IS1Serial->available(); + + if (lenth<=0 ) + { + /* code */ + return 0; + } + int lenthofread = IS1Serial->readBytes(data,lenth); + + return lenthofread; +} +////////////////////////////////AIR780EG/////////////////////////////////////////// +GSMMannger *gsmmanger; +struct gps_struct{ + uint16_t year; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t minute; + uint8_t second; + float latitude; + float longitude; +}; +String get_GPS(gps_struct *gps); +String getnetData(); +HttpClient *http; +//////////////////////////////////////////////////////////////////////////////////// + +// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr); +// void sd_read_set_ds18b20_address(); + +bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff); +void send_dingbiao(String dingbiao_path); +void dingbiao_init(String dingbiao_path); + +void rm_dir_or_file(const char * dirname); +void low_power_mode(uint8_t state); +void setup() +{ + //鍏抽棴鍏夎氨浠 + pinMode(36,INPUT); + vTaskDelay(1000); + + log_init(); + // Serial.begin(115200); + + // wb485Serial.begin(115200); + wb485Serial.begin(115200, SERIAL_8N1, wb485PORT_RX, wb485PORT_TX, false); + + + vTaskDelay(1000); + + vTaskDelay(5000); + IS1Serial =new HardwareSerial(IS1Serial_port); + IS1Serial->begin(115200, SERIAL_8N1, IS1Serial_RX, IS1Serial_TX); + IS1Serial->setTimeout(1); + + + + //鍒濆鍖朣D鍗 + sdcard::init_sdcard(); + sdcard::testwriet(); + sdcard::Mkdir("/guangpu_data"); + sdcard::Mkdir("/up"); + sdcard::Mkdir("/down"); + sdcard::Mkdir("/other"); + sdcard::Mkdir("/gps"); + sdcard::Mkdir("/log"); + sdcard::Mkdir("/dingbiao"); + + /// + + // while(1) + // { + // write_log(log_path,"hello world",10); + // if(SD_MMC.exists("/guangpu_data/0")) + // { + // rm_dir_or_file("/guangpu_data/0"); + // } + // } + + dingbiao_init("/dingbiao/dingbiao_up_gain.bin"); + dingbiao_init("/dingbiao/dingbiao_up_offset.bin"); + dingbiao_init("/dingbiao/dingbiao_down_gain.bin"); + dingbiao_init("/dingbiao/dingbiao_down_offset.bin"); + //妫鏌D鍗℃槸鍚︽湁log.log鏂囦欢 娌℃湁鍒欏垱寤 + // unsigned char log_level = 10; + if(!SD_MMC.exists(log_path)) + { + File file; + file = SD_MMC.open(log_path,"ab+"); + file.flush(); + file.close(); + } + if(!SD_MMC.exists(gp_log)) + { + //妫鏌D鍗℃槸鍚︽湁log.log鏂囦欢 娌℃湁鍒欏垱寤 + File file; + file = SD_MMC.open(gp_log,"ab+"); + file.flush(); + file.close(); + } + // 鍒濆鍖朌S18b20 + uint8_t temp_number = DS18b20_init(); + // float temp[temp_number]; + // getall_temp(temp); + // while (1) + // { + // getall_temp(temp); + // for (size_t i = 0; i < temp_number; i++) + // { + // write_log(log_path,"temp " + String(i) + ": " + String(temp[i]),10); + // } + // vTaskDelay(1000 * 2); + // } + + // sd_read_set_ds18b20_address(); + + + //鍒濆鍖栬埖鏈 + servo_init(21,0); + + //鍒濆鍖栬渹楦e櫒 + beep_init(6,2000,12); + // while (1) + // { + // servo_set_angle(120); + // vTaskDelay(1000 * 1.5); + // servo_set_angle(0); + + // beep_run(2000,1000); + // vTaskDelay(1000 * 3); + // beep_stop(); + // } + + + // while(1) + // { + // beep_run(2000,1000); + // vTaskDelay(1000 * 3); + // beep_stop(); + // vTaskDelay(1000 * 3); + // } + + + + //鍒濆鍖栨椂闂 + rtc.init(); + // now.year = 24; + // now.month = 5; + // now.day = 21; + // now.hour = 13; + // now.minute = 45; + // now.second = 0; + // now.dow = Ds1302::DOW_WED; + // rtc.setDateTime(&now); + + + rtc.getDateTime(&now); + + + + //鍒濆鍖74hc595 + my74hc595_init(); +///////////////////////////鍔犵儹娴嬭瘯//////////////////////////////////// +// while(1) +// { +// write_log(log_path,"start jiaresi",10); +// jiaresi(0,start_jiare); +// jiaresi(1,start_jiare); +// jiaresi(2,start_jiare); +// jiaresi(3,start_jiare); +// jiaresi(4,start_jiare); +// jiaresi(5,start_jiare); + +// tuigan(tui); +// vTaskDelay(1000 * 15); + +// write_log(log_path,"stop jiaresi",10); +// jiaresi(0,start_jiare); +// jiaresi(1,stop_jiare); +// jiaresi(2,stop_jiare); +// jiaresi(3,stop_jiare); +// jiaresi(4,stop_jiare); +// jiaresi(5,stop_jiare); + +// tuigan(suo); +// vTaskDelay(1000 * 15); + +// } + + +///////////////////////////鍔犵儹娴嬭瘯//////////////////////////////////// + +///////////////////////////鎺ㄦ潌娴嬭瘯///////////////////////////////////// + // uint32_t x = 0; + // float n = 5; + // uint32_t data_length; + // uint8_t read_buf[100]; + // while (1) + // { + // while(wb485Serial.available()) + // { + // data_length += wb485Serial.readBytes(&read_buf[data_length],100); + // for (uint32_t i = 0; i < 5000; i++) + // { + // if(wb485Serial.available() > 0) + // { + // break; + // } + // } + // } + // //濡傛灉鎺ユ敹鍒版暟鎹 + // if (data_length > 0) + // { + // if(read_buf[0] == 'n') + // { + // //灏嗚浆鎹负鏁板瓧 + // n = atof((const char *) (read_buf+1)); + // if(n<5) n = 5; + // } + // if(read_buf[0] == 'H') + // { + // wb485Serial.println("n = " + String(n)); + // } + // memset(read_buf, 0, sizeof(read_buf)); + // data_length = 0; + // } + + + // if(x == 1) + // { + // wb485Serial.println("start tuigan tui"); + // tuigan(tui); + // } + // else if(x == 1000 *n) + // { + // wb485Serial.println("start tuigan suo"); + // tuigan(suo); + // } + // else if(x == 1000 * 2 * n) + // { + // x = 0; + // } + + // x++; + // vTaskDelay(1); + // } +///////////////////////////鎺ㄦ潌娴嬭瘯///////////////////////////////////// + + + + // //鍒濆鍖朅IR780EG + gsmmanger = new GSMMannger(2, 19, 20); + http = new HttpClient(*gsmmanger->client, "82.156.1.111"); + String Date = getnetData(); + write_log(log_path,"date is :"+Date,10); + + // while(1) + // { + // String Date = getnetData(); + // write_log(log_path,"date is :"+Date,10); + // vTaskDelay(1000); + // } + // //鍒濆鍖朅IR780EG + // gsmmanger = new GSMMannger(); + // //鑾峰彇GPS + gps_struct gps_structure; + + String gpsbac = get_GPS(&gps_structure); + + //璁$畻鏃ュ嚭鏃ヨ惤 + double transit, sunrise, sunset; + double utc_offset = 8; + + + // int i=0; + // while(1) + // { + // gpsbac = get_GPS(&gps_structure); + // write_log(log_path,String(i)+"Getting GPS " + gpsbac,10); + // vTaskDelay(1000); + // i++; + // if (gpsbac != "-1") + // { + // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); + // write_log(log_path,"sunrise: " + String(sunrise),10); + // write_log(log_path,"sunset: " + String(sunset),10); + // write_log(log_path,"transit: " + String(transit),10); + // write_log(log_path,"latitude: " + String(gps_structure.latitude),10); + // write_log(log_path,"longitude: " + String(gps_structure.longitude),10); + // write_log(log_path,"gps_date: " + gpsbac,10); + // } + // } + + + if (gpsbac != "-1") + { + calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); + } + else + { + write_log(log_path,"no gps",10); + } + + //寮鍚厜璋变华 + pinMode(36,OUTPUT); + digitalWrite(36,LOW); + vTaskDelay(1000 * 2); + InitFunction(serialwrite,serialread); + is11Sensor = new SensorIS11(); + STRSensorInfos_structure = is11Sensor->initSensor(); + + + // while(1) + // { + // is11Sensor->SetShutter(2); + // vTaskDelay(1000); + // is11Sensor->SetShutter(1); + // vTaskDelay(1000); + // } + // 鍒犻櫎鍘熸潵SD鍗$殑绯荤粺淇℃伅 + + + sys_info_init(); + + + + + servo_set_angle(120); + vTaskDelay(1000 * 1.5); + servo_set_angle(0); + vTaskDelay(1000 * 1.5); + + beep_run(2000,1000); + vTaskDelay(1000 * 1.5); + beep_stop(); + + // sys_sd_doc["is_spacial"] = "yes"; + + // deserializeJson(sys_sd_doc,system_info); + // while (1) + // { + // } + + xMutexInventory = xSemaphoreCreateMutex(); //鍒涘缓MUTEX + if (xMutexInventory == NULL) { + + } else{ + //鍒涘缓骞跺惎鍔–ore1鐨勪换鍔 xTaskCreatePinnedToCore + xTaskCreatePinnedToCore( + Task0, /* 浠诲姟鍑芥暟 */ + "Task0", /* 浠诲姟鍚嶇О */ + 1024 * 50, /* 鏍堝ぇ灏 */ + NULL, /* 浠诲姟鍙傛暟 */ + 2, /* 浠诲姟浼樺厛绾 */ + &Task0_Handler, /* 浠诲姟鍙ユ焺 */ + 0); /* Core 0 */ + + xTaskCreatePinnedToCore( + Task1, /* 浠诲姟鍑芥暟 */ + "Task1", /* 浠诲姟鍚嶇О */ + 1024 * 5, /* 鏍堝ぇ灏 */ + NULL, /* 浠诲姟鍙傛暟 */ + 1, /* 浠诲姟浼樺厛绾 */ + &Task1_Handler, /* 浠诲姟鍙ユ焺 */ + 0); + + //鍒涘缓骞跺惎鍔–ore0鐨勪换鍔 + xTaskCreatePinnedToCore( + Task2, /* 浠诲姟鍑芥暟 */ + "Task2", /* 浠诲姟鍚嶇О */ + 1024 * 30, /* 鏍堝ぇ灏 */ + NULL, /* 浠诲姟鍙傛暟 */ + 20, /* 浠诲姟浼樺厛绾 */ + &Task2_Handler, /* 浠诲姟鍙ユ焺 */ + 1); + } + // write_log(log_path,"setup ok",log_level); + // beep_run(2000,512); + // vTaskDelay(1000 * 3); + // beep_stop(); +} + +void loop() +{ + + +} +void Task0(void *pvParameters) +{ + vTaskDelay(1); + write_log(log_path,"task 0",10); + uint32_t uint_work_task0 = 100; + while (1) + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + if(uint_work_task0 == 100 && caiji_mode != 2) + { + caiji_mode = 0; + uint_work_task0 = 0; + } + if (caiji_state == 2) + { + caiji_state = 1; + } + uint_work_task0 = caiji_mode; + } + xSemaphoreGive(xMutexInventory); + //caiji_mode 0鍋滄 1鍗曟 2杩炵画 3瀹氭爣 4瀹氭爣 5鍥炰紶鏁版嵁 + switch (uint_work_task0) + { + case 0: + + break; + case 1: + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + caiji_state = 2; + } + xSemaphoreGive(xMutexInventory); + is11Sensor->TakeOneJob(false); + if (atuo_return == 1) + { + is11Sensor->senddata(&wb485Serial,return_data_type); + } + uint_work_task0 = 100; + break; + case 2: + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + caiji_state = 2; + } + xSemaphoreGive(xMutexInventory); + is11Sensor->TakeOneJob(false); + if (atuo_return == 1) + { + is11Sensor->senddata(&wb485Serial,return_data_type); + } + vTaskDelay(1000 * time_interval); + break; + + case 3: + is11Sensor->senddata(&wb485Serial,return_data_type); + + uint_work_task0 = 100; + break; + + case 4: + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + caiji_state = 2; + } + xSemaphoreGive(xMutexInventory); + is11Sensor->advanced_mode(advanced_direction,advanced_shutter_time,advanced_collect_times,advanced_remove_dark); + uint_work_task0 = 100; + break; + + case 5: + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + caiji_state = 2; + } + xSemaphoreGive(xMutexInventory); + is11Sensor->opt(advanced_direction); + uint_work_task0 = 100; + break; + + default: + break; + } + vTaskDelay(10); + } +} + + +//Task1鐨勫疄鐜 +void Task1(void *pvParameters) +{ + vTaskDelay(10); + write_log(log_path,"task 1",10); + + while(1) + { + // vTaskDelay(1000); + // //鑾峰彇鏃堕棿 + // rtc.getDateTime(&now); + + // //鑾峰彇娓╁害 + // // float temp[8]; + // // getall_temp(temp); + // // //骞冲潎娓╁害 + // // float temp_ave = 0; + // // for (size_t i = 0; i < 8; i++) + // // { + // // temp_ave += temp[i]; + // // } + // // temp_ave = temp_ave / 8; + // // write_log(log_path,"temp_ave: " + String(temp_ave),log_level); + + // //鑾峰彇GPS + // gps_struct gps_structure; + + // String gpsbac = get_GPS(&gps_structure); + // //璁$畻鏃ュ嚭鏃ヨ惤 + // double transit, sunrise, sunset; + // double utc_offset = 8; + // if (gpsbac != "-1") + // { + // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); + // } + // else + // { + // write_log(log_path,"no gps",10); + // } + + // //鏃ュ嚭鏃ヨ惤鎺у埗 澶槼鍗囪捣鍓嶆帹鏉嗙缉锛屽お闃宠惤涓嬪悗鎺ㄦ潌浼 + // write_log(log_path,"now.hour: " + String(now.hour),log_level); + // write_log(log_path,"sunrise: " + String((int)sunrise),log_level); + // write_log(log_path,"sunset: " + String((int)sunset),log_level); + // if (now.hour+1 == (int)sunrise) + // { + // tuigan(suo); + // } + // else if (now.hour+1 >= (int)sunset) + // { + // tuigan(tui); + // } + + // if(sys_sd_doc["work_mode"] == "time_mode") + // { + // float now_time = now.hour + now.minute/100; + // if(now_time > start_time && now_time < stop_time) + // { + + // } + // else + // { + // if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + // { + // caiji_mode = 0; + // } + // xSemaphoreGive(xMutexInventory); + // } + // } + + // write_log(log_path,"temp_ave: " + String(temp_ave),log_level); + // if(temp_ave < Minimum_temperature) + // { + // write_log(log_path,"start jiaresi",10); + // jiaresi(5,start_jiare); + // vTaskDelay(1000 * 10); + // } + // else if (temp_ave > Minimum_temperature + 5) + // { + // write_log(log_path,"stop jiaresi",10); + // jiaresi(5,stop_jiare); + // } + // for(int ab ; ab < 10; ab++) + // { + + rtc.getDateTime(&now); + // String path = "/guangpu_data/"+ String(now.year) + "/" + String(now.month) + "/" + String(now.day); + // if(!SD_MMC.exists(log_path)) + + ////////////////////////////////////////////////////////////////////////////////////////////////////////// + double cardSize = SD_MMC.cardSize() / (1024 * 1024); + + double used_size = SD_MMC.usedBytes() / (1024 * 1024); + + float used_percent = used_size / cardSize; + write_log(log_path,"used_percent: " + String(used_percent),10); + //鏁版嵁娓呴櫎瑙勫垯:1銆佹弧鍗佸勾娓呴櫎绗竴骞存暟鎹 2銆佸綋鏁版嵁>90%鏃跺垹闄ゆ渶鍓嶄竴涓湀鏁版嵁 + if(used_percent >= 0.8) + { + if (used_percent >= 0.9) + { + //鍒犻櫎鏂囦欢 + String path_b = "/gaugpu_data"; + for(int i=24;i<255 ;i++) + { + for (uint8_t j = 1; j < 13; i++) + { + String path_a = path_b + "/" +String(i)+ "/" +String(j); + if(SD_MMC.exists(path_a)) + { + rm_dir_or_file(path_a.c_str()); + } + } + } + } + //鎶ヨ + write_log(log_path,"sdcard will full," + String(used_percent * 100) + "%" +"used.",10); + } + + if(SD_MMC.exists("/guangpu_data/"+ String(now.year - 10) )) + { + String path = "/guangpu_data/"+ String(now.year - 10); + rm_dir_or_file(path.c_str()); + } + + + // write_log(log_path,"delete /guangpu_data/0 1111111111",10); + + // if(SD_MMC.exists("/guangpu_data/0")) + // { + // write_log(log_path,"delete /guangpu_data/0 2222222222222222",10); + // // sdcard::deleteFolderOrFile("/gaugpu_data/0"); + // rm_dir_or_file("/guangpu_data/0"); + // } + /////////////////////////////////////////////////////////////////////////////////////////// + + //////////////////////////////////////////////鏃堕棿//////////////////////////////////////////////////////////// + //鑾峰彇RTC鏃堕棿 + rtc.getDateTime(&now); + + // 鑾峰彇缃戠粶鏃堕棿 + gps_struct net_structure; + String net_date = getnetData(); + write_log(log_path,"net_date is :"+net_date,10); + // 2024-07-12 07:12:45 + + //鎴彇鏃堕棿 + if(net_date != "-1") + { + net_structure.year = net_date.substring(0,4).toInt() - 2000; + net_structure.month = net_date.substring(5,7).toInt(); + net_structure.day = net_date.substring(8,10).toInt(); + net_structure.hour = net_date.substring(11,13).toInt(); + net_structure.minute = net_date.substring(14,16).toInt(); + net_structure.second = net_date.substring(17,-1).toInt(); + + if(now.year != net_structure.year || now.month != net_structure.month || now.day != net_structure.day || now.hour - net_structure.hour || (fabs(now.minute - net_structure.minute)>5 )) + { + now.year = net_structure.year; + now.month = net_structure.month; + now.day = net_structure.day; + now.hour = net_structure.hour; + now.minute = net_structure.minute; + now.second = net_structure.second; + rtc.setDateTime(&now); + } + } + ////鑾峰彇GPS + gps_struct gps_structure; + String gpsbac = get_GPS(&gps_structure); + + if(net_date != "-1" && gpsbac != "-1") + { + gps_structure.year = gps_structure.year - 2000; + if(now.year != gps_structure.year || now.month != gps_structure.month || now.day != gps_structure.day || now.hour != gps_structure.hour || (fabs(now.minute - net_structure.minute)>5 )) + { + now.year = gps_structure.year; + now.month = gps_structure.month; + now.day = gps_structure.day; + now.hour = gps_structure.hour; + now.minute = gps_structure.minute; + now.second = gps_structure.second; + rtc.setDateTime(&now); + }else write_log(log_path,"no gps",10); + } + ////////////////////////////////////////////////////////////////////////////////////////////////////////// + + + //鑾峰彇鏃堕棿 + rtc.getDateTime(&now); + if(sys_sd_doc["work_mode"] == "time_mode") + { + float now_time = now.hour + now.minute/100; + if(now_time > start_time && now_time < stop_time) + { + + } + else + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + caiji_mode = 0; + } + xSemaphoreGive(xMutexInventory); + } + } + + + + // sdcard::Mkdir(path); + + float temp4 = getone_temp(4); + float temp6 = getone_temp(6); + write_log(log_path,"temp4 : " + String(temp4),10); + write_log(log_path,"temp6 : " + String(temp6),10); + + if((temp4+temp6)/2 < 0) + { + write_log(log_path,"start jiaresi",10); + jiaresi(5,start_jiare); + vTaskDelay(1000 * 10); + } + else if ((temp4+temp6)/2 > 5) + { + write_log(log_path,"stop jiaresi",10); + jiaresi(5,stop_jiare); + } + vTaskDelay(1000 * 10); + } +} + +// Task2鐨勫疄鐜 +void Task2(void *pvParameters) +{ + vTaskDelay(20); + write_log(log_path,"task 2",10); + unsigned char read_buf[1024*10]; + unsigned char command_data[1024 * 10]; + unsigned char data_type; + String send_data ; + int send_lenth; + u_char send_buff[300]; + + while(1) + { + unsigned short data_length = 0; + //鐩戞祴鎺ユ敹涓插彛鏁版嵁 + while(wb485Serial.available()) + { + data_length += wb485Serial.readBytes(&read_buf[data_length],1024*10); + for (uint32_t i = 0; i < 5000; i++) + { + if(wb485Serial.available() > 0) + { + break; + } + } + } + //濡傛灉鎺ユ敹鍒版暟鎹 + if (data_length > 0) + { + // 娴嬭瘯鏁版嵁 + write_log(log_path,"data_length: " + String(data_length),log_level); + + data_length = IRIS_Cut_Befor_Header(read_buf,data_length); + if (data_length < 0) + { + send_data = "send data error"; + write_log(log_path,send_data,log_level); + send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); + wb485Serial.write(send_buff,send_lenth); + // return; + } + int ret = IRIS_STM32_Protocol_Unpack(read_buf,data_length,&data_type,command_data); + write_log(log_path,"ret:" +String(ret),log_level); + if (ret > 0) + { + write_log(log_path,"data_type:" +String(data_type),log_level); + if(data_type == 0x00)//json + { + deserializeJson(doc,command_data); + write_log(log_path,"start json_command",log_level); + json_command(); + doc.clear(); + } + else if (data_type == 0x01)//String + { + // write_log(command_data,0); + } + else if (data_type == 0x03) //hex + { + write_log(log_path,"",log_level); + String dingbiao_path; + if (command_data[0] == 4 || command_data[0] == 5) + { + // is11Sensor->save_dingbiao(command_data); + //淇濆瓨鍒皊d鍗 + if(command_data[0] == 4) + { + if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_up_gain.bin"; + else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_up_offset.bin"; + } + else if(command_data[0] == 5) + { + if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_down_gain.bin"; + else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_down_offset.bin"; + } + is11Sensor->save_dingbiao(command_data); + File file = SD_MMC.open(dingbiao_path, "wb"); + file.write((uint8_t *)&command_data, ret); + file.flush(); + file.close(); + } + } + else if(data_type == 0x04) + { + uint8_t num = command_data[0]; + set_ds18b20_address(num,command_data+1); + } + + send_data = "wb485 success"; + write_log(log_path,send_data,log_level); + // send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); + // wb485Serial.write(send_buff,send_lenth); + } + else + { + send_data = "send data error"; + write_log(log_path,send_data,log_level); + send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); + wb485Serial.write(send_buff,send_lenth); + } + + memset(read_buf, 0, sizeof(read_buf)); + memset(command_data, 0, sizeof(command_data)); + } + // vTaskDelay(1); + } +} +/* +//澶勭悊json鍛戒护 +command : 1銆乬et_sensor_info + 2銆乻et_sensor_info + 3銆乻et_work_mode + 4銆乬et_work_mode + 5銆乻et_time_interval + 6銆乻tart_work + 7銆乻top_work + 8銆乻et_return_mode + 9銆乻et_atuo_return + 10銆乻et_return_data_type + 11銆乬et_9 + 12銆 + 13銆乬et_data + 14銆乻et_bochangxishu + 15銆乬et_bochangxishu + 16銆乬et_chongcaiyangbochang + + 18銆乬et_darkcurrent + 19銆乻et_servo_offset + 20銆乻et_start_time + 21銆乻et_stop_time + 22銆乻et_maximum_temperature + 23銆乻et_Minimum_temperature + 24銆 +*/ + +/*{"command":"","name":"gaoguangpu","version":"v2.0","SN":"123456","date":"","caiji_mode":"","time_interval":"","return_data_type":"","bochangxishu":""}*/ +void json_command() +{ + int ret; + bool save = false; + String send_str; + u_char send_buff[1000]; + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + //command 1 + if (doc["command"] == "get_sensor_info") + { + rtc.getDateTime(&now); + sys_sd_doc["date"] = String(now.year+2000) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second); + send_str = ""; + serializeJson(sys_sd_doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + serializeJson(sys_sd_doc, send_str); + wb485Serial.write(send_buff, ret); + } + //command 2 + else if (doc["command"] == "set_sensor_info") + { + write_log(log_path,"set_sensor_info",log_level); + if(doc["name"] == NULL) + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "Name cannot be null"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + save = true ; + sys_sd_doc["name"] = doc["name"].as(); + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + + if(doc["version"] == NULL) + { + + } + else + { + save = true ; + sys_sd_doc["version"] = doc["version"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 3 + else if (doc["command"] == "set_work_mode") + { + + if(doc["work_mode"] == "manual_mode") + { + if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); + save = true ; + sys_sd_doc["work_mode"] = "manual_mode"; + sys_sd_doc["caiji_mode"] = "single"; + sys_sd_doc["atuo_return"] ="no"; + sys_sd_doc["return_data_type"] = 0x00; + work_mode = 0; + caiji_mode = 0; + atuo_return = 0; + return_data_type = 0x00; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if (doc["work_mode"] == "time_mode") + { + if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); + save = true; + sys_sd_doc["work_mode"] = "time_mode"; + sys_sd_doc["caiji_mode"] = "continue"; + sys_sd_doc["atuo_return"] = "yes"; + sys_sd_doc["return_data_type"] = 0x00; + work_mode = 1; + caiji_mode = 0; + atuo_return = 1; + return_data_type = 0x00; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if(doc["work_mode"] == "advanced_mode") + { + if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); + + save = true; + sys_sd_doc["work_mode"] = "advanced_mode"; + sys_sd_doc["caiji_mode"] = "advanced"; + sys_sd_doc["atuo_return"] = "no"; + sys_sd_doc["return_data_type"] = 0x00; + + work_mode = 2; + caiji_mode = 0; + atuo_return = 0; + return_data_type = 0x00; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if(doc["work_mode"] == "low_power_mode") + { + save = true; + sys_sd_doc["work_mode"] = "low_power_mode"; + sys_sd_doc["caiji_mode"] = "low_power"; + sys_sd_doc["atuo_return"] = "no"; + sys_sd_doc["return_data_type"] = 0x00; + + work_mode = 3; + + low_power_mode(0); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "work mode error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 4.1 + else if (doc["command"] == "set_caiji_mode") + { + if(doc["caiji_mode"] == "start") + { + if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if(doc["caiji_mode"] == "stop") + { + if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if(doc["caiji_mode"] == "single") + { + save = true; + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + caiji_mode = 1; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if(doc["caiji_mode"] == "continue") + { + save = true; + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + caiji_mode = 2; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "work mode error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 4.2 + else if (doc["command"] == "take_one_job") + { + + + if(caiji_mode == 0) + { + caiji_mode = 1; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "Working"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 4.3 + else if (doc["command"] == "set_time_interval") + { + if (doc["time_interval"]>0) + { + save = true; + sys_sd_doc["time_interval"] = doc["time_interval"]; + time_interval = doc["time_interval"]; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "time_interval error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 5 + else if (doc["command"] == "get_work_mode") + { + doc.clear(); + send_str = ""; + doc["work_mode"] = sys_sd_doc["work_mode"].as(); + doc["caiji_mode"] = sys_sd_doc["caiji_mode"].as(); + doc["atuo_return"] = sys_sd_doc["atuo_return"].as(); + doc["return_data_type"] =sys_sd_doc["return_data_type"]; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + //command 6 + else if(doc["command"] == "set_atuo_return") + { + if(doc["atuo_return"] == "yes") + { + save = true; + sys_sd_doc["atuo_return"] = doc["atuo_return"].as(); + atuo_return = 1; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else if (doc["atuo_return"] == "no") + { + save = true; + sys_sd_doc["atuo_return"] = doc["atuo_return"].as(); + atuo_return = 0; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "atuo_return error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 7 + else if (doc["command"] == "set_return_data_type") + { + u_int8_t a = 0; + u_int32_t b = doc["return_data_type"]; + switch (b) + { + case 0x00: + a=1; + break; + case 0x01: + a=1; + break; + case 0x02: + a=1; + break; + case 0x03: + a=1; + break; + case 0x04: + a=1; + break; + case 0x05: + a=1; + break; + case 0x10: + a=1; + break; + case 0x11: + a=1; + break; + case 0x12: + a=1; + break; + case 0x13: + a=1; + break; + default: + break; + } + if(a) + { + save = true; + sys_sd_doc["return_data_type"] = doc["return_data_type"]; + return_data_type = doc["return_data_type"]; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "return_data_type error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 8 + //淇濈暀锛屾病鐢 + else if (doc["command"] == "set_return_mode") + { + save = true; + sys_sd_doc["return_mode"] = doc["return_mode"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + /*璁剧疆鍥炰紶妯″紡 */ + } + //command 9 + else if (doc["command"] == "get_data") + { + u_int8_t a = 0; + u_int32_t b = doc["return_data_type"]; + switch (b) + { + case 0x00: + a=1; + break; + case 0x01: + a=1; + break; + case 0x02: + a=1; + break; + case 0x03: + a=1; + break; + case 0x04: + a=1; + break; + case 0x05: + a=1; + break; + case 0x06: + a=1; + break; + case 0x10: + a=1; + break; + case 0x11: + a=1; + break; + case 0x12: + a=1; + break; + case 0x13: + a=1; + break; + default: + break; + } + if(a && caiji_mode !=3) + { + return_data_type = doc["return_data_type"]; + caiji_mode = 3; + } + else + { + send_str = ""; + doc["caiji_state"] = "working"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + /* code */ + } + //command 10 + else if (doc["command"] == "set_bochangxishu") + { + float b1=0,b2=0,b3=0,b4=0; + b1 = doc["bochangxishu"]["b0"].as(); + b2 = doc["bochangxishu"]["b1"].as(); + b3 = doc["bochangxishu"]["b2"].as(); + b4 = doc["bochangxishu"]["b3"].as(); + + if(b1!=0 && b2 !=0 && b3 != 0 && b4 != 0) + { + save = true; + bc_b1 = doc["bochangxishu"]["b0"].as(); + bc_b2 = doc["bochangxishu"]["b1"].as(); + bc_b3 = doc["bochangxishu"]["b2"].as(); + bc_b4 = doc["bochangxishu"]["b3"].as(); + + sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"].as(); + sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"].as(); + sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"].as(); + sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "bochangxishu error or null"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + + //command 11 + else if (doc["command"] == "get_bochangxishu") + { + doc.clear(); + doc["bochangxishu"]["a0"] = sys_sd_doc["bochangxishu"]["a0"].as(); + doc["bochangxishu"]["a1"] = sys_sd_doc["bochangxishu"]["a1"].as(); + doc["bochangxishu"]["a2"] = sys_sd_doc["bochangxishu"]["a2"].as(); + doc["bochangxishu"]["a3"] = sys_sd_doc["bochangxishu"]["a3"].as(); + + doc["bochangxishu"]["b0"] = sys_sd_doc["bochangxishu"]["b0"].as(); + doc["bochangxishu"]["b1"] = sys_sd_doc["bochangxishu"]["b1"].as(); + doc["bochangxishu"]["b2"] = sys_sd_doc["bochangxishu"]["b2"].as(); + doc["bochangxishu"]["b3"] = sys_sd_doc["bochangxishu"]["b3"].as(); + + send_str = ""; + doc["state"] = "ok"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + + //command 12 + else if (doc["command"] == "get_chongcaiyangbochang") + { + // String chongcaiyangbochang; + /* code */ + } + + //command 14 + else if(doc["command"] == "set_servo_offset") + { + float angle = doc["servo_offset_angle"]; + if(angle > 0 || angle < 180) + { + save = true; + sys_sd_doc["servo_offset_angle"] = doc["servo_offset_angle"].as(); + servo_offset(angle); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "servo_offset_angle error or out of range"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + + //command 15 + else if(doc["command"] == "set_start_time") + { + float t = doc["start_time"].as(); + if(t>0 && t < 24) + { + save = true; + sys_sd_doc["start_time"] = doc["start_time"].as(); + start_time = doc["start_time"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "start_time error or out of range"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 16 + else if(doc["command"] == "set_stop_time") + { + float tt = doc["stop_time"].as(); + if(tt>0 && tt < 24 && tt > start_time) + { + save = true; + sys_sd_doc["stop_time"] = doc["stop_time"].as(); + stop_time = doc["stop_time"]; + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "stop_time error or out of range"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 17 + else if(doc["command"] == "set_maximum_temperature") + { + float te = doc["maximum_temperature"].as(); + if(te < 100) + { + save = true; + maximum_temperature = doc["maximum_temperature"].as(); + sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "maximum_temperature error or out of range"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + + //command 18 + else if(doc["command"] == "set_Minimum_temperature") + { + float tte = doc["Minimum_temperature"].as(); + if(tte < maximum_temperature) + { + save = true; + maximum_temperature = doc["Minimum_temperature"].as(); + sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"].as(); + + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "Minimum_temperature error or out of range"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 19 + else if(doc["command"] == "get_date") + { + send_str = ""; + doc.clear(); + rtc.getDateTime(&now); + doc["state"] = "ok"; + doc["date"]["year"]= now.year; + doc["date"]["month"]= now.month; + doc["date"]["day"]= now.day; + doc["date"]["hour"]= now.hour; + doc["date"]["minute"]= now.minute; + doc["date"]["second"]= now.second; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + //command 20 + else if(doc["command"] == "get_caiji_state") + { + uint32_t percent; + send_str = ""; + doc.clear(); + if(caiji_state == 1) + { + doc["caiji_state"] = "finish"; + percent = is11Sensor->work_progress; + doc["percent"] = percent; + doc["info"] = "end"; + } + else if(caiji_state == 2 ) + { + doc["caiji_state"] = "working"; + percent = is11Sensor->work_progress; + doc["percent"] = percent; + if(percent < 100) + { + doc["info"] = "working"; + } + else + { + doc["info"] = "end"; + } + } + else doc["caiji_state"] = "no"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + //command 21 + else if(doc["command"] == "start_opt") + { + if(sys_sd_doc["work_mode"] == "advanced_mode") + { + caiji_mode = 5; + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "The working mode is not advanced_mode"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 22 + else if(doc["command"] == "get_opt") + { + if(sys_sd_doc["work_mode"] == "advanced_mode") + { + send_str = ""; + uint32_t aa = is11Sensor->dingbiao_shutter; + doc["opt"] = aa; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "The working mode is not advanced_mode"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + else if(doc["command"] == "start_collect") + { + bool collect = true; + if(sys_sd_doc["work_mode"] == "advanced_mode") + { + // caiji_mode = 3; + if (doc["direction"] == "up") + { + advanced_direction = 0; + } + else if (doc["direction"] == "down") + { + advanced_direction = 1; + } + else if (doc["direction"] == "dark") + { + advanced_direction = 2; + } + else + { + collect = false; + // send_str = ""; + // doc["state"] = "failed"; + // doc["info"] = "direction error"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + } + + if(doc["shutter_time"]>=1) + { + advanced_shutter_time = doc["shutter_time"]; + } + else + { + collect = false; + // send_str = ""; + // doc["state"] = "failed"; + // doc["info"] = "shutter_time error"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + } + + if(doc["collect_times"]>0) + { + advanced_collect_times = doc["collect_times"]; + } + else + { + collect = false; + // send_str = ""; + // doc["state"] = "failed"; + // doc["info"] = "collect_times error"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + } + if(doc["remove_dark"] == "yes") + { + advanced_remove_dark = 1; + } + else if(doc["remove_dark"] == "no") + { + advanced_remove_dark = 0; + } + else + { + collect = false; + // send_str = ""; + // doc["state"] = "failed"; + // doc["info"] = "remove_dark error"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + } + + if (collect) + { + caiji_mode = 4; + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "parameter error"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "The working mode is not advanced_mode"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + //command 25 + else if(doc["command"] == "set_sys_time") + { + uint32_t year,month,day,hour,minute,second; + + year = doc["year"]; + month = doc["month"]; + day = doc["day"]; + hour = doc["hour"]; + minute = doc["minute"]; + second = doc["second"]; + write_log(log_path,"year" + String(year) + "month" + String(month) + "day" + String(day) + "hour" + String(hour) + "minute" + String(minute) + "second" + String(second),10); + if(year>99 || month>12 || day>31 || hour > 23 || minute >59 || second > 59) + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "date error1"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + if(sys_set_time(year,month,day)) + { + now.year = year; + now.month = month; + now.day = day; + now.hour = hour; + now.minute =minute; + now.second =second; + // now.dow = 5; + rtc.setDateTime(&now); + send_str = ""; + doc["state"] = "success"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "date error2"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + } + } + //command 26 + // else if(doc["command"] == "set_ds18b20_address") + // { + // uint8_t num = doc["num"]; + // uint8_t addr[8]; + // if (num>=0 && num<8) + // { + // addr[0] = doc["addr"]["0"]; + // addr[1] = doc["addr"]["1"]; + // addr[2] = doc["addr"]["2"]; + // addr[3] = doc["addr"]["3"]; + // addr[4] = doc["addr"]["4"]; + // addr[5] = doc["addr"]["5"]; + // addr[6] = doc["addr"]["6"]; + // addr[7] = doc["addr"]["7"]; + + // set_ds18b20_address(num, addr); + // save_sd_ds18b20_address(num,addr); + // send_str = ""; + // doc["state"] = "success"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + // } + // else + // { + // send_str = ""; + // doc["state"] = "failed"; + // doc["info"] = "num error"; + // serializeJson(doc, send_str); + // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + // wb485Serial.write(send_buff, ret); + // } + // } + else if(doc["command"] == "get_dingbiao") + { + send_dingbiao("/dingbiao/dingbiao_up_gain.bin"); + send_dingbiao("/dingbiao/dingbiao_up_offset.bin"); + send_dingbiao("/dingbiao/dingbiao_down_gain.bin"); + send_dingbiao("/dingbiao/dingbiao_down_offset.bin"); + } + //command error + else + { + send_str = ""; + doc["state"] = "failed"; + doc["info"] = "no command"; + serializeJson(doc, send_str); + ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); + wb485Serial.write(send_buff, ret); + } + //灏嗕慨鏀瑰悗鐨勭郴缁熶俊鎭啓鍏ュ唴瀛樺崱 + if(save == true) + { + send_str = ""; + serializeJson(sys_sd_doc, send_str); + + File file; + file = SD_MMC.open("/system_info.json","w+"); + file.println(send_str); + file.flush(); + file.close(); + } + } + xSemaphoreGive(xMutexInventory); +} + + +String get_GPS(gps_struct *gps) +{ + write_log(log_path,"start get GPS ",10); + String gpsbac,Date,temp,Latitude,Longitude; + gsmmanger->modem->sendAT(GF("+CGNSPWR?")); + gsmmanger->modem->waitResponse("OK"); + + gsmmanger->modem->sendAT(GF("+CGNSPWR=1")); + gsmmanger->modem->waitResponse("OK"); + + gsmmanger->modem->sendAT(GF("+CGNSAID=31,1,1,1")); + gsmmanger->modem->waitResponse("OK"); + + gsmmanger->modem->sendAT(GF("+CGNSINF")); + + //鑾峰彇gps淇℃伅 + gsmmanger->modem->waitResponse(5000,gpsbac); + write_log(log_path,"gpsbac is " + gpsbac,10); + + //妫娴嬫槸鍚︽湁鏁 + temp = gpsbac.substring(16,17); + // write_log(log_path,"hahaha :" + temp,10); + + if(temp != "1") + { + // write_log(log_path,"gps error,hahahaha",10); + return "-1"; + } + //鎴彇鏃堕棿 + Date = gpsbac.substring(18,32); + temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1); + + gps->year = Date.substring(0,4).toInt(); + gps->month = Date.substring(4,6).toInt(); + gps->day = Date.substring(6,8).toInt(); + gps->hour = Date.substring(8,10).toInt(); + gps->minute = Date.substring(10,12).toInt(); + gps->second = Date.substring(12,-1).toInt(); + + Date = temp; +// +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,, + +// OK +////////鑾峰彇缁忕含搴////////// + temp = gpsbac.substring(33); + // write_log(log_path,"Longitude is :" + temp,10); + int pos = temp.indexOf(","); + Latitude = temp.substring(0,pos); + gps->latitude = Latitude.toFloat(); + // write_log(log_path,"Latitude ::::" + Latitude,10); + + temp = temp.substring(pos+1); + // write_log(log_path,"Longitude is :" + temp,10); + pos = temp.indexOf(","); + Longitude = temp.substring(0,pos); + gps->longitude = Longitude.toFloat(); + // write_log(log_path,"Longitude ::::" + Longitude,10); + + if(Date.indexOf(",") != -1) + { + return "-1"; + } + + return Date; +} + + + +void sys_info_init() +{ + gps_struct gps_structure; + String system_info; + String rtcdate_now ="20" + String(now.year) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second); + write_log(log_path,rtcdate_now,10); + if(SD_MMC.exists("/system_info.json")) + { + write_log(log_path,"has system_info.json file",log_level); + File file; + file = SD_MMC.open("/system_info.json","rb+"); + system_info = file.readString(); + file.flush(); + file.close(); + + deserializeJson(doc, system_info); + //鍒濆鍖栫郴缁熶俊鎭 + sys_sd_doc["name"] = doc["name"].as(); + sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber; + // sys_sd_doc["serialnumber"] = "is110008"; + sys_sd_doc["version"] = doc["version"].as(); + sys_sd_doc["date"] = rtcdate_now; + + sys_sd_doc["work_mode"] = doc["work_mode"].as(); //涓浼 + if(doc["work_mode"] == "manual_mode") + { + work_mode = 0; + } + else if (doc["work_mode"] == "time_mode") + { + work_mode = 1; + } + else + { + work_mode = 0; + } + + // if(caiji_mode == 7) + // { + // sys_sd_doc["work_mode"] = "specify"; + // } + + + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as();//涓浼 + if(doc["caiji_mode"] == "start") + { + if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2; + } + else if(doc["caiji_mode"] == "stop") + { + if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0; + } + else if(doc["caiji_mode"] == "single") + { + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + caiji_mode = 1; + } + else if(doc["caiji_mode"] == "continue") + { + sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); + caiji_mode = 2; + } + else + { + + } + + sys_sd_doc["time_interval"] = doc["time_interval"]; + time_interval = doc["time_interval"].as(); + sys_sd_doc["start_time"] = doc["start_time"].as(); + start_time = doc["start_time"].as(); + sys_sd_doc["stop_time"] = doc["stop_time"].as(); + stop_time = doc["stop_time"].as(); + sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"].as(); + maximum_temperature = doc["maximum_temperature"].as(); + sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"].as(); + Minimum_temperature = doc["Minimum_temperature"].as(); + sys_sd_doc["servo_offset_angle"] = doc["servo_offset_angle"].as(); + float a = doc["servo_offset_angle"].as(); + servo_offset(a); + sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1; + sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2; + sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3; + sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4; + + sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"].as(); + sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"].as(); + sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"].as(); + sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"].as(); + sys_sd_doc["return_data_type"] = doc["return_data_type"].as(); + sys_sd_doc["return_mode"] = doc["return_mode"]; //鏆傚畾 + sys_sd_doc["atuo_return"] = doc["atuo_return"].as();//涓浼 + if(doc["atuo_return"] == "yes") + { + atuo_return = 1; + } + else + { + atuo_return = 0; + } + sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude; + sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude; + + system_info = ""; + serializeJson(sys_sd_doc, system_info); + write_log(log_path, system_info,log_level); + + doc.clear(); + } + else{ + write_log(log_path,"do not has system_info.json file" ,log_level); + + sys_sd_doc["name"] = "HS"; + // sys_sd_doc["serialnumber"] = "is110008"; + sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber; + sys_sd_doc["version"] = "v2.0"; + sys_sd_doc["date"] = rtcdate_now; + sys_sd_doc["work_mode"] = "manual_mode"; + sys_sd_doc["caiji_mode"] = "stop"; + sys_sd_doc["time_interval"] = time_interval; + sys_sd_doc["start_time"] = start_time; + sys_sd_doc["stop_time"] = stop_time; + sys_sd_doc["maximum_temperature"] = maximum_temperature; + sys_sd_doc["Minimum_temperature"] = Minimum_temperature; + sys_sd_doc["servo_offset_angle"] = 0; + + sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1; + sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2; + sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3; + sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4; + + sys_sd_doc["bochangxishu"]["b0"] = bc_b1; + sys_sd_doc["bochangxishu"]["b1"] = bc_b2; + sys_sd_doc["bochangxishu"]["b2"] = bc_b3; + sys_sd_doc["bochangxishu"]["b3"] = bc_b4; + sys_sd_doc["return_data_type"] = return_data_type; + sys_sd_doc["return_mode"] = ""; //鏆傚畾 + sys_sd_doc["atuo_return"] = "no"; + sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude; + sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude; + + serializeJson(sys_sd_doc, system_info); + write_log(log_path, system_info,log_level); + + File file; + file = SD_MMC.open("/system_info.json","w+"); + file.println(system_info); + file.flush(); + file.close(); + } +} + + + +bool sys_set_time(uint32_t year,uint32_t month,uint32_t day) +{ + uint32_t m[4] ={4,6,9,11}; + year = year + 2000; + for(int i = 0; i < 4; i++) + { + if(month == m[i]) + { + if(day >= 31) return false; + } + } + if(month == 2) + { + if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + { + + // write_log(log_path,"is ruannian" + String(day),10); + if(day > 29) return false; + } + else + { + // write_log(log_path,"not is " + String(day),10); + if(day > 28) return false; + + } + } + return true; +} + +// void save_ds18b20_address(uint8_t *data) +// { +// uint8_t add[8][8]={0}; +// uint8_t num = data[0]; +// for(uint8_t i = 0; i < 8; i++) +// { +// add[num][i] = data[i+1]; +// } + +// File file = SD_MMC.open("/ds18b20_address.bin", "wb"); +// file.write((uint8_t *)add, 64); +// file.flush(); +// file.close(); +// } + +// void read_ds18b20_address() +// { +// uint8_t add[8][8]={0}; +// File file = SD_MMC.open("/ds18b20_address.bin", "rb"); +// file.read((uint8_t *)add, 64); +// file.flush(); +// file.close(); + +// for(uint8_t i = 0; i < 8; i++) +// { +// set_ds18b20_address(i,add[i]); +// } +// } + + + +// void sd_read_set_ds18b20_address() +// { +// uint8_t add[8]; +// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"}; +// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"}; + +// if(SD_MMC.exists("/temp_sensor.json")) +// { +// File file; +// file = SD_MMC.open("/temp_sensor.json","rb+"); +// String temp_sensor = file.readString(); +// file.flush(); +// file.close(); +// deserializeJson(doc, temp_sensor); +// for(uint8_t a = 0; a < 8; a++) +// { +// for(uint8_t b = 0; b < 8; b++) +// { +// add[b] = doc[str1[a]][str2[b]]; +// ds18b20_doc[a][b] = doc[str1[a]][str2[b]]; +// } +// set_ds18b20_address(a,add); +// } +// temp_sensor = ""; +// serializeJson(doc, temp_sensor); +// write_log(log_path, temp_sensor,log_level); +// } +// else +// { +// write_log(log_path,"do not has file",10); +// for(uint8_t a = 0; a < 8; a++) +// { +// for(uint8_t b = 0; b < 8; b++) +// { +// ds18b20_doc[a][b] = ""; +// } +// } +// File file; +// file = SD_MMC.open("/temp_sensor.json","w+"); +// file.flush(); +// file.close(); +// } +// doc.clear(); +// } + +// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr) +// { +// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"}; +// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"}; +// String temp_sensor; + +// for(uint8_t i = 0; i < 8; i++) +// { +// ds18b20_doc[str1[num]][str2[i]] = addr[i]; +// } + +// temp_sensor = ""; +// serializeJson(ds18b20_doc, temp_sensor); +// write_log(log_path, temp_sensor,log_level); +// File file; +// file = SD_MMC.open("/temp_sensor.json","w+"); +// file.println(temp_sensor); +// file.flush(); +// file.close(); +// } + +//dingbiao_path +//1銆乨ingbiao/dingbiao_up_gain.txt +//2銆乨ingbiao/dingbiao_up_offset.txt +//3銆乨ingbiao/dingbiao_down_gain.txt +//4銆乨ingbiao/dingbiao_down_offset.txt + + +bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff) +{ + if(SD_MMC.exists(dingbiao_path)) + { + File file; + file = SD_MMC.open(dingbiao_path,"rb"); + file.read(dianbiao_data_buff,sizeof(IS11_datastruct)); + file.flush(); + file.close(); + } + else + { + return false; + } + return true; +} + +void send_dingbiao(String dingbiao_path) +{ + uint32_t dingbiao_len = 0; + uint8_t *send_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)+7]; + uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)]; + + if(read_dingbiao(dingbiao_path,read_dingbiao_buff)) + { + dingbiao_len = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), read_dingbiao_buff,send_dingbiao_buff); + wb485Serial.write(send_dingbiao_buff,dingbiao_len); + } + + delete read_dingbiao_buff; + delete send_dingbiao_buff; +} + +void dingbiao_init(String dingbiao_path) +{ + uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)]; + + if(read_dingbiao(dingbiao_path,read_dingbiao_buff)) + { + is11Sensor->save_dingbiao(read_dingbiao_buff); + } + delete read_dingbiao_buff; +} + + + + +// /////////////////////////////////涓婁紶鏁版嵁///////////////////////////////////// +// bool UpdateData(String path, char *str, size_t lenth, String Contenttype = "") +// { +// http->beginRequest(); +// http->post(path); +// if (Contenttype != "") +// { +// http->sendHeader(HTTP_HEADER_CONTENT_TYPE, Contenttype); + +// /* code */ +// } + +// http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, lenth); +// http->endRequest(); +// int err = http->write((const byte *)str, lenth); +// delay(1000); +// Serial.print("send date size"); +// Serial.println(err); + +// /* +// for (size_t i = 0; i < lenth; i++) +// { +// Serial.print("0x"); +// Serial.print(str[i],HEX); +// Serial.print(" "); +// if (i%20==0) +// { +// Serial.println(" "); +// } +// } +// */ +// // http->write((const byte*)IS1Sensor.UpData, IS1Sensor.SensorInfo.BandNum*2); +// // http->stop(); +// Serial.println("hear1"); +// if (err == 0) +// { +// http->stop(); +// return false; +// } +// int status = http->responseStatusCode(); +// Serial.println("hear3"); +// if (!status) +// { +// http->stop(); +// return false; +// } + +// int length = http->contentLength(); +// Serial.println("hear4"); +// if (length == 0) +// { +// http->stop(); +// return false; +// } + +// String body = http->responseBody(); +// Serial.println("body:"+body); +// if (body != "ok") +// { +// http->stop(); +// return false; +// } + +// http->stop(); +// return true; +// } + + +// void ReuploadData(String path, String webpath, String content = "") +// { +// vTaskDelay(1); +// Vector files; +// String vec[20]; //姣忔澶勭悊20涓 +// files.setStorage(vec); +// while (!sdcard::ListDir(path.c_str(), files)) +// { +// if (files.size() != 0) +// { +// Serial.println("find " + String(files.size()) + "file"); +// for (size_t i = 0; i < files.size(); i++) +// { +// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); +// size_t size = nowfile.size(); +// char *arr = new char[size]; +// nowfile.readBytes(arr, size); +// bool flagsucc = UpdateData(webpath, arr, size, content); +// if (!flagsucc) +// { +// return; +// /* code */ +// } +// delete[] arr; +// sdcard::deleteFolderOrFile(files.at(i).c_str()); +// vTaskDelay(1); +// /* code */ +// } +// } +// vTaskDelay(1); +// } +// if (files.size() != 0) +// { +// Serial.println("find " + String(files.size()) + "file not enough 20"); +// for (size_t i = 0; i < files.size(); i++) +// { +// vTaskDelay(1); +// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); +// size_t size = nowfile.size(); +// char *arr = new char[size]; +// nowfile.readBytes(arr, size); +// Serial.println("run here now"); +// bool flagsucc = UpdateData(webpath, arr, size, content); +// if (!flagsucc) +// { +// return; +// /* code */ +// } + +// delete[] arr; +// sdcard::deleteFolderOrFile(files.at(i).c_str()); +// /* code */ +// } +// } +// vTaskDelay(1); +// } +// /////////////////////////////////////////////////////////////////////////////////////////////////// + + + + + +String getnetData() +{ + write_log(log_path,"start get net data",10); + int err = http->get("/weather/php/Date.php"); + if (err != 0) + { + return "-1"; + } + write_log(log_path,"responseStatusCode",10); + int status = http->responseStatusCode(); + if (!status) + { + return "-1"; + } + + int times_try = 0; + write_log(log_path,"headerAvailable",10); + while (http->headerAvailable()) + { + if(times_try > 100) + { + http->stop(); + return "-1"; + } + write_log(log_path,"readHeaderName",10); + String headerName = http->readHeaderName(); + write_log(log_path,"readHeaderValue",10); + String headerValue = http->readHeaderValue(); + + vTaskDelay(100); + times_try++; + } + write_log(log_path,"isResponseChunked",10); + if (http->isResponseChunked()) + { + } + write_log(log_path,"contentLength",10); + String body = http->responseBody(); + http->stop(); + return body; +} + + +void rm_dir_or_file( const char * dirname) +{ + File root = SD_MMC.open(dirname); + String dir = String(dirname); + if(root.isDirectory()) + { + File file = root.openNextFile(); + if(file) + { + if (file.isDirectory()) + { + dir = String(dirname) + "/" + String(file.name()); + write_log(log_path,dir,10); + rm_dir_or_file(dir.c_str()); + } + else + { + do + { + dir = String(dirname) + "/" + String(file.name()); + if(SD_MMC.remove(dir)) + { + write_log(log_path,"remove file success",10); + } + else + { + write_log(log_path,"remove file failed",10); + break; + } + file = root.openNextFile(); + if(!file) break; + } while (!file.isDirectory()); + + } + + } + else + { + SD_MMC.rmdir(dir); + } + } + else + { + SD_MMC.remove(dir); + } +} + + + + +// void rm_dir_or_file( const char * dirname) +// { +// File root = SD_MMC.open(dirname); +// String dir = String(dirname); + +// if(root.isDirectory()) +// { +// File file = root.openNextFile(); +// if(file) +// { +// while (file.isDirectory()) +// { +// dir = String(dirname) + "/" + String(file.name()); +// write_log(log_path,dir,10); +// rm_dir_or_file(dir.c_str()); +// file = root.openNextFile(); +// } +// if(!file.isDirectory()) +// { +// do +// { +// dir = String(dirname) + "/" + String(file.name()); +// if(SD_MMC.remove(dir)) +// { +// write_log(log_path,"remove file success",10); +// } +// else +// { +// write_log(log_path,"remove file failed",10); +// break; +// } +// file = root.openNextFile(); +// if(!file) break; +// } while (!file.isDirectory()); + +// } +// } +// else +// { +// SD_MMC.rmdir(dir); +// } +// } +// else +// { +// SD_MMC.remove(dir); +// } +// } + + + + + + + + + + + + +//0:鍏抽棴 1:寮鍚 +void low_power_mode(uint8_t state) +{ + if(state == 0) + { + //鍏抽棴鍏夎氨浠 + pinMode(36,INPUT); + //鍏抽棴AIR780E + pinMode(37,OUTPUT); + digitalWrite(37,LOW); + } + else + { + //寮鍚厜璋变华 + pinMode(36,OUTPUT); + digitalWrite(36,LOW); + //寮鍚疉IR780E + pinMode(37,OUTPUT); + digitalWrite(37,HIGH); + } +} \ No newline at end of file diff --git a/platformio - 鍓湰.ini b/platformio - 鍓湰.ini new file mode 100644 index 0000000..b83f4ab --- /dev/null +++ b/platformio - 鍓湰.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-s3-devkitc-1] +platform = espressif32 @ ^4.4.0 +board = esp32-s3-devkitc-1 +framework = arduino +platform_packages = + toolchain-riscv32-esp @ 8.4.0 +board_upload.flash_size = 4MB +board_upload.psram_size = 2MB + diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1031181 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32s3box] +platform = http://111.198.24.44:11080/v6.5.0.zip +board = esp32-s3-devkitc-1 +framework = arduino +lib_deps = bblanchon/ArduinoJson@^7.4.2 diff --git a/src/IRIS_Method.c b/src/IRIS_Method.c new file mode 100644 index 0000000..87566e7 --- /dev/null +++ b/src/IRIS_Method.c @@ -0,0 +1,208 @@ +/** + ****************************************************************************** + * @file : IRIS_Method.c + * @author : xin + * @brief : None + * @attention : None + * @date : 2024/2/1 + ****************************************************************************** + */ + +// +// Created by xin on 2024/2/1. +// +#include "IRIS_Method.h" + +// 鎴愬姛杩斿洖鎵撳寘鍚庣殑鏁版嵁闀垮害 +// -1: Error +int32_t IRIS_Protocol_Pack(uint8_t Command, uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData) { + if (PackData == NULL || (LenthofIn != 0 && BufferIn == NULL)) { + return -1; + } + + PackData[0] = 0x55; + PackData[1] = 0xAA; + PackData[2] = Command; + uint16_t datalenth = LenthofIn; + PackData[3] = (datalenth >> 8) & 0xFF; + PackData[4] = datalenth & 0xFF; + + if (LenthofIn != 0) { + memcpy(&PackData[5], BufferIn, LenthofIn); + } + uint16_t crcbytelenth = LenthofIn; + uint16_t CRC = IRIS_calcCRC(PackData + 5, crcbytelenth); + PackData[LenthofIn + 5] = (CRC >> 8) & 0xFF; + PackData[LenthofIn + 6] = CRC & 0xFF; + return LenthofIn + 7; + + +} +// int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut) { +// if (PackData == NULL || BufferOut == NULL) { +// return ERROR_INPUT; +// } +// if (PackData[0] != 0x55 || PackData[1] != 0xAA) { +// return ERROR_HEADER; +// } + +// uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); + +// // 淇敼闀垮害妫鏌ワ細纭繚鏈夎冻澶熺殑鏁版嵁鍖呭惈澶撮儴銆佹暟鎹拰鏍¢獙 +// if (LenthofIn < 5 + LenthofOut + 2) { +// return ERROR_NOT_ENOUGH_DATA; +// } + +// // 淇鏍¢獙鏍囪瘑浣嶇疆锛氬簲璇ユ槸 PackData[5 + LenthofOut] 鍜 PackData[5 + LenthofOut + 1] +// if (PackData[5 + LenthofOut] == 0xEE && PackData[5 + LenthofOut + 1] == 0xEE) { +// // 鐗规畩鏍¢獙鏍囪瘑锛岃烦杩嘋RC妫鏌 +// } else { +// uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); +// if (CRC != (PackData[5 + LenthofOut + 1] + (PackData[5 + LenthofOut] << 8))) { +// return ERROR_CRC; +// } +// } + +// if (LenthofOut == 0) { +// return 0; +// } +// *Command = PackData[2]; + +// memcpy(BufferOut, &PackData[5], LenthofOut); +// return LenthofOut; +// } +int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut) { + + if (PackData == NULL || BufferOut == NULL) { + return ERROR_INPUT; + } + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); //鍑忓幓CRC鐨勪袱涓瓧鑺 + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + + + if (LenthofOut == 0) { + return 0; + } + *Command = PackData[2]; + + memcpy(BufferOut, &PackData[5], LenthofOut); + return LenthofOut; +} + + +int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut) { + if (PackData == NULL || BufferOut == NULL) { + return ERROR_INPUT; + } + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + if (PackData[2] != Command) { + return ERROR_COMMAND; + } + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + if (LenthofOut == 0) { + return 0; + } + memcpy(BufferOut, &PackData[5], LenthofOut); + return LenthofOut; + +} + + +int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn) { + if (PackData == NULL) { + return ERROR_INPUT; + } + uint16_t i = 0; + for (i = 0; i < LenthofIn; i++) { + if (PackData[i] == 0x55 && PackData[i + 1] == 0xAA) { + break; + } + } + if (i == LenthofIn) { + //娓呯┖鏁版嵁 + memset(PackData, 0, LenthofIn); + return 0; + } + + uint16_t LenthofOut = LenthofIn - i; + memcpy(PackData, &PackData[i], LenthofOut); + return LenthofOut; +} + +int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn) { + if (PackData == NULL) { + return ERROR_INPUT; + } + if (LenthofIn < 7) { + return ERROR_NOT_ENOUGH_DATA; + /* code */ + } + + if (PackData[0] != 0x55 || PackData[1] != 0xAA) { + return ERROR_HEADER; + } + uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); + if (LenthofOut > LenthofIn - 7) { + return ERROR_NOT_ENOUGH_DATA; + } + if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) { + + } else { + uint16_t CRC = IRIS_calcCRC(PackData + 5, LenthofOut); + if (CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) { + return ERROR_CRC; + } + } + return 1; + +} + + +uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize) { + const uint8_t *pBytesArray = (const uint8_t *) pBuffer; + uint16_t poly = 0x8408; + uint16_t crc = 0; + uint8_t carry; + uint8_t i_bits; + uint16_t j; + for (j = 0; j < bufferSize; j++) { + crc = crc ^ pBytesArray[j]; + for (i_bits = 0; i_bits < 8; i_bits++) { + carry = crc & 1; + crc = crc / 2; + if (carry) { + crc = crc ^ poly; + } + } + } + return crc; +} + diff --git a/src/IRIS_Method.h b/src/IRIS_Method.h new file mode 100644 index 0000000..15512a9 --- /dev/null +++ b/src/IRIS_Method.h @@ -0,0 +1,66 @@ +/** + ****************************************************************************** + * @file : IRIS_Method.h + * @author : xin + * @brief : None + * @attention : None + * @date : 2024/2/1 + ****************************************************************************** + */ + +// +// Created by xin on 2024/2/1. +// + +#ifdef __cplusplus +extern "C" { +#endif + +// #ifndef IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H +// #define IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H + + +#define ERROR_NOT_ENOUGH_DATA -200 +#define ERROR_HEADER -300 +#define ERROR_COMMAND -400 +#define ERROR_INPUT -500 +#define ERROR_CRC -600 +#include +#include + +// 鎴愬姛杩斿洖鎵撳寘鍚庣殑鏁版嵁闀垮害 +// -1: Error +int32_t IRIS_Protocol_Pack(uint8_t Command, uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData); + +// 瑙e寘鍑芥暟 PackData 鏄帴鏀跺埌鐨勬暟鎹 LenthofIn 鏄暟鎹暱搴 Command 鏄懡浠 BufferOut 鏄緭鍑 +// 涓嬩綅鏈轰娇鐢ㄧ殑鎵撳寘鍑芥暟 Command 鏄緭鍑 +// 鎴愬姛杩斿洖瑙e寘鍚庣殑鏁版嵁闀垮害 +// 0: 璇ュ懡浠よ繑鍥炴棤鍙傛暟 +// 閿欒杩斿洖ERRor +// 鎴愬姛杩斿洖瑙e寘鍚庣殑鏁版嵁闀垮害 +int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut); + +// 瑙e寘鍑芥暟 PackData 鏄帴鏀跺埌鐨勬暟鎹 LenthofIn 鏄暟鎹暱搴 Command 鏄懡浠よ緭鍏 BufferOut 鏄緭鍑 涓婁綅鏈轰娇鐢 +// 鎴愬姛杩斿洖瑙e寘鍚庣殑鏁版嵁闀垮害 +// 0: 璇ュ懡浠よ繑鍥炴棤鍙傛暟 +// 閿欒杩斿洖ERRor +// 鎴愬姛杩斿洖瑙e寘鍚庣殑鏁版嵁闀垮害 +int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut); + +// 瀹氫箟瑁佸垏鍛戒护 +// 鎴愬姛杩斿洖瑁佸垏鍚庣殑鏁版嵁闀垮害 +// -1: Error +int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn); + +// 妫鏌ユ暟鎹槸鍚︽湁鏁 +// 鏈夋晥杩斿洖鍊1 +// 閿欒杩斿洖ERRor +int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn); + +// 杩斿洖CRC鏍¢獙鍊 +uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize); + +#ifdef __cplusplus +} +#endif + diff --git a/src/ble.cpp b/src/ble.cpp new file mode 100644 index 0000000..d4d7d93 --- /dev/null +++ b/src/ble.cpp @@ -0,0 +1,85 @@ +#include "ble.h" + +// 瀹氫箟 BLE 鏈嶅姟鍜岀壒寰佸肩殑 UUID +#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" +#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" + +BLEServer* pServer = nullptr; +BLECharacteristic* pCharacteristic = nullptr; +BLEAdvertising* pAdvertising; + +bool isClientConnected = false; + +class MyServerCallbacks : public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + Serial.println("CONNECT"); + isClientConnected = true; + } + + void onDisconnect(BLEServer* pServer) { + Serial.println("DISCONNECT"); + isClientConnected = false; + // 閲嶆柊鍚姩骞挎挱锛屼娇璁惧鍐嶆鍙鍙戠幇 + BLEDevice::startAdvertising(); + } +}; + +bool bleSentData = false; // 璁板綍 BLE 鏄惁宸插彂閫佹暟鎹 + +class MyCharacteristicCallbacks : public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic* pCharacteristic) { + std::string value = pCharacteristic->getValue(); + + if (value.length() > 0) { + bleSentData = true; + //Serial.print("Receive: "); + for (int i = 0; i < value.length(); i++) { + Serial.print(value[i]); + } + Serial.println(); + } + //bleSentData = false; + } +}; + + + +void bleInit() { + // 鍒濆鍖 BLE 璁惧 + BLEDevice::init("VSMD_BLE"); // 璁剧疆璁惧鍚嶇О + + // 鍒涘缓 BLE 鏈嶅姟鍣 + pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks()); + + // 鍒涘缓 BLE 鏈嶅姟 + BLEService* pService = pServer->createService(SERVICE_UUID); + + // 鍒涘缓 BLE 鐗瑰緛鍊 + pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE + ); + + // 娣诲姞鐗瑰緛鍊兼弿杩 + pCharacteristic->setCallbacks(new MyCharacteristicCallbacks()); + //pCharacteristic->setValue("Hello Client"); + + // 鍚姩鏈嶅姟 + pService->start(); + + // 鍒涘缓骞垮憡鏁版嵁 + BLEAdvertising* pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID)); + pAdvertising->setScanResponse(true); + pAdvertising->setMinPreferred(0x06); + BLEDevice::startAdvertising(); + + Serial.println("Waiting for client connection..."); + + // 鍦ㄥ垵濮嬪寲鏃惰缃洖璋 + pServer->setCallbacks(new MyServerCallbacks()); +} + + diff --git a/src/ble.h b/src/ble.h new file mode 100644 index 0000000..1f3dd27 --- /dev/null +++ b/src/ble.h @@ -0,0 +1,14 @@ +#ifndef BLE_H +#define BLE_H + +#include +#include +#include +#include +#include + + +// 钃濈墮鍒濆鍖栧嚱鏁 +void bleInit(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..78d6761 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,493 @@ +#include +#include "IRIS_Method.h" + + +#define DEBUG_TX_PIN 17 // TX +#define DEBUG_RX_PIN 16 // RX + + +HardwareSerial DebugSerial(1); // 浣跨敤UART1 + +#define BUFFER_SIZE 1024 +#define JSON_BUFFER_SIZE 512 + +// 鍏ㄥ眬鍙橀噺澹版槑 +uint8_t receiveBuffer[BUFFER_SIZE]; // 鎺ユ敹缂撳啿鍖 +uint8_t sendBuffer[BUFFER_SIZE]; // 鍙戦佺紦鍐插尯 +uint8_t responseBuffer[BUFFER_SIZE]; // 鍝嶅簲缂撳啿鍖 +uint8_t command; // 瀛樺偍鍛戒护 +int32_t bytesProcessed; // 澶勭悊鐨勬暟鎹瓧鑺傛暟 +int32_t unpackResult; // 瑙e寘缁撴灉 +int32_t packedLength; // 鎵撳寘鍚庣殑鏁版嵁闀垮害 + +// 璁惧鐘舵佸彉閲 +bool deviceEnabled = false; +int32_t currentPosition = 0; +int32_t targetPosition = 0; +int32_t pulsePerSecond = 1200; +bool isMoving = false; +bool isBackZero = false; + +// 璁惧閰嶇疆鍙傛暟 +struct DeviceConfig { + int zmd = 1; // cfg zmd + int snr = 0; // cfg snr + int osv = 0; // cfg osv + int zsd = 1200; // cfg zsd + int zsp = 2400; // cfg zsp + int dmd = 2; // cfg dmd + int dar = 5; // cfg dar + int msr = 1; // cfg msr + int msv = 1; // cfg msv + int psr = 1; // cfg psr + int psv = 1; // cfg psv + int bdr = 115200; // cfg bdr + int cid = 1; // cfg cid + int mcs = 5; // cfg mcs + int spd = 1200; // cfg spd + int acc = 12000; // cfg acc + int dec = 12000; // cfg dec + float cra = 0.8; // cfg cra + float crn = 0.4; // cfg crn + float crh = 0.0; // cfg crh +} deviceConfig; + + +void processJsonCommand(const char* jsonString); +void processStringCommand(const char* stringData); +void sendJsonResponse(const char* cmd, const char* status, int value = -1); +void sendStringResponse(const char* response); +void handleConfigCommand(JsonObject& doc); +void handleEnableCommand(); +void handleOffCommand(); +void handleMoveCommand(); +void handlePositionCommand(int value); +void handleRelativeMoveCommand(int value); +void handlePulsePerSecondCommand(int value = -1); +void handleOriginCommand(); +void handleStopCommand(); +void handleZeroCommand(const char* value); +void printDeviceStatus(); + +void setup() { + // 璋冭瘯涓插彛 + DebugSerial.begin(115200, SERIAL_8N1, DEBUG_RX_PIN, DEBUG_TX_PIN); + + // 涓讳覆鍙o紙鐢ㄤ簬鏁版嵁閫氫俊锛 + Serial.begin(115200); + + DebugSerial.println("VSMD INIT"); + DebugSerial.println("Commands: cfg, ena, off, mov, pos, rmv, pps, org, stp, zero"); + printDeviceStatus(); +} + +void loop() { + 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); + jsonString[unpackResult] = '\0'; // 纭繚瀛楃涓蹭互null缁撳熬 + + DebugSerial.print("Received JSON: "); + DebugSerial.println(jsonString); + + processJsonCommand(jsonString); + break; + } + + case 0x01: { // 瀛楃涓叉暟鎹 + char stringData[BUFFER_SIZE]; + memcpy(stringData, responseBuffer, unpackResult); + stringData[unpackResult] = '\0'; + + DebugSerial.print("Received String: "); + DebugSerial.println(stringData); + + processStringCommand(stringData); + break; + } + + default: { // 鑷畾涔変簩杩涘埗鏁版嵁 (0x02-0xff) + 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); + } + } + } +} + +void processJsonCommand(const char* jsonString) { + StaticJsonDocument doc; + DeserializationError error = deserializeJson(doc, jsonString); + + if (error) { + DebugSerial.print("JSON parsing failed: "); + DebugSerial.println(error.c_str()); + sendJsonResponse("error", "JSON_PARSE_ERROR"); + return; + } + + const char* cmd = doc["cmd"]; + if (cmd == nullptr) { + sendJsonResponse("error", "NO_CMD_FIELD"); + return; + } + + // 鏍规嵁cmd鎵ц鐩稿簲鎿嶄綔 + if (strcmp(cmd, "cfg") == 0) { + JsonObject configObj = doc.as(); + handleConfigCommand(configObj); + } else if (strcmp(cmd, "ena") == 0) { + handleEnableCommand(); + } else if (strcmp(cmd, "off") == 0) { + handleOffCommand(); + } else if (strcmp(cmd, "mov") == 0) { + handleMoveCommand(); + } else if (strcmp(cmd, "pos") == 0) { + int value = doc["value"] | 0; + handlePositionCommand(value); + } else if (strcmp(cmd, "rmv") == 0) { + int value = doc["value"] | 0; + handleRelativeMoveCommand(value); + } else if (strcmp(cmd, "pps") == 0) { + if (doc.containsKey("value")) { + int value = doc["value"]; + handlePulsePerSecondCommand(value); + } else { + handlePulsePerSecondCommand(); + } + } else if (strcmp(cmd, "org") == 0) { + handleOriginCommand(); + } else if (strcmp(cmd, "stp") == 0) { + handleStopCommand(); + } else if (strcmp(cmd, "zero") == 0) { + const char* value = doc["value"] | ""; + handleZeroCommand(value); + } else { + sendJsonResponse(cmd, "UNKNOWN_COMMAND"); + } +} + +void processStringCommand(const char* stringData) { + // 澶勭悊瀛楃涓插懡浠 + String response = "Echo: " + String(stringData); + sendStringResponse(response.c_str()); +} + +void sendJsonResponse(const char* cmd, const char* status, int value) { + StaticJsonDocument responseDoc; + responseDoc["cmd"] = cmd; + responseDoc["status"] = status; + + if (value != -1) { + responseDoc["value"] = value; + } + + // 璁惧鐘舵佷俊鎭 + responseDoc["enabled"] = deviceEnabled; + responseDoc["position"] = currentPosition; + responseDoc["moving"] = isMoving; + + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 鎵撳寘骞跺彂閫丣SON鍝嶅簲 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + DebugSerial.println(); + + DebugSerial.print("Sent JSON Response: "); + DebugSerial.println(jsonResponse); +} + +void sendStringResponse(const char* response) { + // 鎵撳寘骞跺彂閫佸瓧绗︿覆鍝嶅簲 + packedLength = IRIS_Protocol_Pack(0x01, strlen(response), (uint8_t*)response, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + DebugSerial.print("Sent String Response: "); + DebugSerial.println(response); +} + +void handleConfigCommand(JsonObject& doc) { + DebugSerial.println("Processing configuration command..."); + + // 鍒涘缓鍝嶅簲JSON瀵硅薄 + StaticJsonDocument responseDoc; + responseDoc["cmd"] = "cfg"; + responseDoc["status"] = "OK"; + + // 娣诲姞璁惧鐘舵佷俊鎭 + responseDoc["enabled"] = deviceEnabled; + responseDoc["position"] = currentPosition; + responseDoc["moving"] = isMoving; + + // 鍒涘缓閰嶇疆鍙傛暟瀵硅薄 + JsonObject configParams = responseDoc.createNestedObject("config"); + + // 澶勭悊閰嶇疆鍙傛暟 + if (doc.containsKey("params")) { + JsonObject params = doc["params"]; + + // 鏇存柊閰嶇疆鍙傛暟骞舵坊鍔犲埌鍝嶅簲涓 + if (params.containsKey("cfg zmd")) { + deviceConfig.zmd = params["cfg zmd"]; + configParams["cfg zmd"] = deviceConfig.zmd; + } + if (params.containsKey("cfg snr")) { + deviceConfig.snr = params["cfg snr"]; + configParams["cfg snr"] = deviceConfig.snr; + } + if (params.containsKey("cfg osv")) { + deviceConfig.osv = params["cfg osv"]; + configParams["cfg osv"] = deviceConfig.osv; + } + if (params.containsKey("cfg zsd")) { + deviceConfig.zsd = params["cfg zsd"]; + configParams["cfg zsd"] = deviceConfig.zsd; + } + if (params.containsKey("cfg zsp")) { + deviceConfig.zsp = params["cfg zsp"]; + configParams["cfg zsp"] = deviceConfig.zsp; + } + if (params.containsKey("cfg dmd")) { + deviceConfig.dmd = params["cfg dmd"]; + configParams["cfg dmd"] = deviceConfig.dmd; + } + if (params.containsKey("cfg dar")) { + deviceConfig.dar = params["cfg dar"]; + configParams["cfg dar"] = deviceConfig.dar; + } + if (params.containsKey("cfg msr")) { + deviceConfig.msr = params["cfg msr"]; + configParams["cfg msr"] = deviceConfig.msr; + } + if (params.containsKey("cfg msv")) { + deviceConfig.msv = params["cfg msv"]; + configParams["cfg msv"] = deviceConfig.msv; + } + if (params.containsKey("cfg psr")) { + deviceConfig.psr = params["cfg psr"]; + configParams["cfg psr"] = deviceConfig.psr; + } + if (params.containsKey("cfg psv")) { + deviceConfig.psv = params["cfg psv"]; + configParams["cfg psv"] = deviceConfig.psv; + } + if (params.containsKey("cfg bdr")) { + deviceConfig.bdr = params["cfg bdr"]; + configParams["cfg bdr"] = deviceConfig.bdr; + } + if (params.containsKey("cfg cid")) { + deviceConfig.cid = params["cfg cid"]; + configParams["cfg cid"] = deviceConfig.cid; + } + if (params.containsKey("cfg mcs")) { + deviceConfig.mcs = params["cfg mcs"]; + configParams["cfg mcs"] = deviceConfig.mcs; + } + if (params.containsKey("cfg spd")) { + deviceConfig.spd = params["cfg spd"]; + configParams["cfg spd"] = deviceConfig.spd; + } + if (params.containsKey("cfg acc")) { + deviceConfig.acc = params["cfg acc"]; + configParams["cfg acc"] = deviceConfig.acc; + } + if (params.containsKey("cfg dec")) { + deviceConfig.dec = params["cfg dec"]; + configParams["cfg dec"] = deviceConfig.dec; + } + if (params.containsKey("cfg cra")) { + deviceConfig.cra = params["cfg cra"]; + configParams["cfg cra"] = deviceConfig.cra; + } + if (params.containsKey("cfg crn")) { + deviceConfig.crn = params["cfg crn"]; + configParams["cfg crn"] = deviceConfig.crn; + } + if (params.containsKey("cfg crh")) { + deviceConfig.crh = params["cfg crh"]; + configParams["cfg crh"] = deviceConfig.crh; + } + + DebugSerial.println("Configuration updated successfully"); + } else { + // 濡傛灉娌℃湁params锛岃繑鍥炴墍鏈夊綋鍓嶉厤缃 + configParams["cfg zmd"] = deviceConfig.zmd; + configParams["cfg snr"] = deviceConfig.snr; + configParams["cfg osv"] = deviceConfig.osv; + configParams["cfg zsd"] = deviceConfig.zsd; + configParams["cfg zsp"] = deviceConfig.zsp; + configParams["cfg dmd"] = deviceConfig.dmd; + configParams["cfg dar"] = deviceConfig.dar; + configParams["cfg msr"] = deviceConfig.msr; + configParams["cfg msv"] = deviceConfig.msv; + configParams["cfg psr"] = deviceConfig.psr; + configParams["cfg psv"] = deviceConfig.psv; + configParams["cfg bdr"] = deviceConfig.bdr; + configParams["cfg cid"] = deviceConfig.cid; + configParams["cfg mcs"] = deviceConfig.mcs; + configParams["cfg spd"] = deviceConfig.spd; + configParams["cfg acc"] = deviceConfig.acc; + configParams["cfg dec"] = deviceConfig.dec; + configParams["cfg cra"] = deviceConfig.cra; + configParams["cfg crn"] = deviceConfig.crn; + configParams["cfg crh"] = deviceConfig.crh; + } + + // 搴忓垪鍖栧苟鍙戦佸搷搴 + char jsonResponse[JSON_BUFFER_SIZE]; + serializeJson(responseDoc, jsonResponse, sizeof(jsonResponse)); + + // 鎵撳寘骞跺彂閫丣SON鍝嶅簲 + packedLength = IRIS_Protocol_Pack(0x00, strlen(jsonResponse), (uint8_t*)jsonResponse, sendBuffer); + DebugSerial.write(sendBuffer, packedLength); + + DebugSerial.print("Sent Config Response: "); + DebugSerial.println(jsonResponse); +} + +void handleEnableCommand() { + deviceEnabled = true; + DebugSerial.println("Device enabled"); + Serial.print("ena\n"); + sendJsonResponse("ena", "OK"); +} + +void handleOffCommand() { + deviceEnabled = false; + isMoving = false; + DebugSerial.println("Device disabled"); + Serial.print("off\n"); + sendJsonResponse("off", "OK"); +} + +void handleMoveCommand() { + if (!deviceEnabled) { + sendJsonResponse("mov", "DEVICE_DISABLED"); + return; + } + + isMoving = true; + DebugSerial.println("Starting movement"); + Serial.print("mov\n"); + sendJsonResponse("mov", "OK"); +} + +void handlePositionCommand(int value) { + if (!deviceEnabled) { + sendJsonResponse("pos", "DEVICE_DISABLED"); + return; + } + + targetPosition = value; + currentPosition = value; // 妯℃嫙绔嬪嵆鍒拌揪 + DebugSerial.print("Position set to: "); + DebugSerial.println(value); + sendJsonResponse("pos", "OK", value); +} + +void handleRelativeMoveCommand(int value) { + if (!deviceEnabled) { + sendJsonResponse("rmv", "DEVICE_DISABLED"); + return; + } + + currentPosition += value; + DebugSerial.print("Relative move by: "); + DebugSerial.print(value); + DebugSerial.print(", new position: "); + DebugSerial.println(currentPosition); + sendJsonResponse("rmv", "OK", currentPosition); +} + +void handlePulsePerSecondCommand(int value) { + if (value == -1) { + // SET PPS + Serial.print("pps\n"); + sendJsonResponse("pps", "OK", pulsePerSecond); + } else { + // 璁剧疆PPS鍊 + pulsePerSecond = value; + DebugSerial.print("Pulse per second set to: "); + DebugSerial.println(value); + sendJsonResponse("pps", "OK", value); + } +} + +void handleOriginCommand() { + if (!deviceEnabled) { + sendJsonResponse("org", "DEVICE_DISABLED"); + return; + } + + currentPosition = 0; + targetPosition = 0; + DebugSerial.println("Returned to origin"); + Serial.print("org\n"); + sendJsonResponse("org", "OK", 0); +} + +void handleStopCommand() { + isMoving = false; + DebugSerial.println("Movement stopped"); + Serial.print("stp\n"); + sendJsonResponse("stp", "OK"); +} + +void handleZeroCommand(const char* value) { + if (strcmp(value, "start") == 0) { + Serial.print("zero start\n"); + isBackZero = true; + DebugSerial.println("Zero started"); + sendJsonResponse("zero", "ZERO_STARTED"); + } else if (strcmp(value, "stop") == 0) { + Serial.print("zero stop\n"); + isBackZero = false; + currentPosition = 0; + DebugSerial.println("Zero completed"); + sendJsonResponse("zero", "ZERO_COMPLETED"); + } else { + sendJsonResponse("zero", "INVALID_VALUE"); + } +} + +void printDeviceStatus() { + DebugSerial.println("\n=== Device Status ==="); + DebugSerial.print("Enabled: "); + DebugSerial.println(deviceEnabled ? "Yes" : "No"); + DebugSerial.print("Current Position: "); + DebugSerial.println(currentPosition); + DebugSerial.print("Target Position: "); + DebugSerial.println(targetPosition); + DebugSerial.print("Pulse Per Second: "); + DebugSerial.println(pulsePerSecond); + DebugSerial.print("Moving: "); + DebugSerial.println(isMoving ? "Yes" : "No"); + DebugSerial.print("Zero Backing: "); + DebugSerial.println(isBackZero ? "Yes" : "No"); + DebugSerial.println("==================\n"); +} \ No newline at end of file diff --git a/src/vsmd_parser.cpp b/src/vsmd_parser.cpp new file mode 100644 index 0000000..ba8c22f --- /dev/null +++ b/src/vsmd_parser.cpp @@ -0,0 +1,272 @@ +#include "vsmd_parser.h" +#include "ble.h" + +extern HardwareSerial debugSerial; + +// BCC 鏍¢獙鍑芥暟 +uint8_t VSMDParser::bcc_checksum(uint8_t* data, int size) { + uint8_t sum = 0; + for (int index = 0; index < size; index++) { + sum ^= data[index]; + } + return sum; +} + +// 鏋勯犲嚱鏁 +VSMDParser::VSMDParser() { + // 鍙夊垵濮嬪寲 +} + +// 鏁版嵁杞崲鍑芥暟 +void VSMDParser::convert(uint8_t* data, value_info& info) { + info.udata = data[0]; + info.udata <<= 7; + info.udata |= data[1]; + info.udata <<= 7; + info.udata |= data[2]; + info.udata <<= 7; + info.udata |= data[3]; + info.udata <<= 7; + info.udata |= data[4]; +} + +// 瑙f瀽鍑芥暟 +// void VSMDParser::parse(uint8_t* data, int size) { +// // if (size < 10) { +// // debugSerial.println("data too short"); +// // return; +// // } + +// if (data[0] == 0xFF && data[size - 1] == 0xFE) { +// uint8_t checksum = bcc_checksum(&data[1], size - 3); +// if (checksum == data[size - 2]) { +// switch (data[2]) { +// case 1: { // dev +// debugSerial.print("dev:"); +// int start = 0; +// while (start < size && data[start] != 'V') start++; +// if (start < size) { +// int end = start; +// while (end < size && data[end] >= 32 && data[end] < 127) end++; +// for (int i = start; i < end; i++) { +// debugSerial.print((char)data[i]); +// } +// debugSerial.println(); +// } else { +// debugSerial.println("dev err"); +// } +// break; +// } +// case 2: { // sts +// float spd = 0; +// int pos = 0; +// uint32_t sts = 0; +// value_info info; + +// convert(&data[3], info); +// spd = info.fdata; + +// convert(&data[8], info); +// pos = info.idata; + +// convert(&data[13], info); +// sts = info.udata; + +// debugSerial.print("Speed: "); +// debugSerial.print(spd); +// debugSerial.print(", Position: "); +// debugSerial.print(pos); +// debugSerial.print(", Status: "); +// debugSerial.println(sts); + +// break; +// } +// case 3: { // cfg +// debugSerial.print("cfg:"); +// int payloadStart = 3; +// int payloadEnd = size - 3; + +// if (payloadStart < payloadEnd) { +// for (int i = payloadStart; i < payloadEnd; i++) { +// debugSerial.print((char)data[i]); +// } +// debugSerial.println(); +// } else { +// debugSerial.println(" 鏁版嵁闀垮害涓嶈冻"); +// } +// break; +// } +// case 4: { // demo +// debugSerial.print("demo:"); +// int payloadStart = 3; +// int payloadEnd = size - 3; + +// if (payloadStart < payloadEnd) { +// for (int i = payloadStart; i < payloadEnd; i++) { +// debugSerial.print((char)data[i]); +// } +// debugSerial.println(); +// } else { +// debugSerial.println(" 鏁版嵁闀垮害涓嶈冻"); +// } +// break; +// } +// default: +// debugSerial.print("鏈煡鍛戒护: "); +// debugSerial.println(data[2]); +// break; +// } +// } else { +// return; +// debugSerial.println("BCC ERROR"); +// debugSerial.print("CHECK BCC: "); +// debugSerial.println(checksum); +// debugSerial.print("RES BCC: "); +// debugSerial.println(data[size - 2], HEX); +// } +// } else { +// debugSerial.println("Head or Tail ERROR"); +// debugSerial.print("Receive Data: "); +// for (int i = 0; i < size; i++) { +// debugSerial.print(data[i], HEX); +// debugSerial.print(" "); +// } +// debugSerial.println(); +// } +// } + +/**************************** ble read ************************************ */ +void VSMDParser::parse(uint8_t* data, int size) { + if (data[0] == 0xFF && data[size - 1] == 0xFE) { + uint8_t checksum = bcc_checksum(&data[1], size - 3); + if (checksum == data[size - 2]) { + switch (data[2]) { + case 1: { // dev + String response = "dev:"; + int start = 0; + while (start < size && data[start] != 'V') start++; + if (start < size) { + int end = start; + while (end < size && data[end] >= 32 && data[end] < 127) end++; + for (int i = start; i < end; i++) { + response += (char)data[i]; + } + response += "\n"; + } else { + response += "dev err\n"; + } + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + break; + } + case 2: { // sts + float spd = 0; + int pos = 0; + uint32_t sts = 0; + value_info info; + + convert(&data[3], info); + spd = info.fdata; + + convert(&data[8], info); + pos = info.idata; + + convert(&data[13], info); + sts = info.udata; + + String response = "Speed: " + String(spd) + ", Position: " + String(pos) + ", Status: " + String(sts) + "\n"; + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + break; + } + case 3: { // cfg + String response = "cfg:"; + int payloadStart = 3; + int payloadEnd = size - 3; + + if (payloadStart < payloadEnd) { + for (int i = payloadStart; i < payloadEnd; i++) { + response += (char)data[i]; + } + response += "\n"; + } else { + response += " 鏁版嵁闀垮害涓嶈冻\n"; + } + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + break; + } + case 4: { // demo + String response = "demo:"; + int payloadStart = 3; + int payloadEnd = size - 3; + + if (payloadStart < payloadEnd) { + for (int i = payloadStart; i < payloadEnd; i++) { + response += (char)data[i]; + } + response += "\n"; + } else { + response += " 鏁版嵁闀垮害涓嶈冻\n"; + } + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + break; + } + default: + String response = "鏈煡鍛戒护: "; + response += data[2]; + response += "\n"; + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + break; + } + } else { + // BCC 鏍¢獙澶辫触 + String response = "BCC ERROR\nCHECK BCC: "; + response += checksum; + response += "\nRES BCC: "; + response += data[size - 2], HEX; + response += "\n"; + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + } + } else { + String response = "Head or Tail ERROR\nReceive Data: "; + for (int i = 0; i < size; i++) { + response += data[i], HEX; + response += " "; + } + response += "\n"; + + // 鍙戦侀氳繃钃濈墮 + if (pCharacteristic) { + pCharacteristic->setValue(response.c_str()); + pCharacteristic->notify(); + } + } +} diff --git a/src/vsmd_parser.h b/src/vsmd_parser.h new file mode 100644 index 0000000..7ee5b96 --- /dev/null +++ b/src/vsmd_parser.h @@ -0,0 +1,34 @@ +#ifndef VSMD_PARSER_H +#define VSMD_PARSER_H + +#include +#include "ble.h" + + +// 鑱斿悎浣撳畾涔夛細鐢ㄤ簬鏁版嵁杞崲 +typedef union { + float fdata; + int32_t idata; + uint32_t udata; +} value_info; + +// 瑙f瀽绫诲0鏄 +class VSMDParser { +public: + VSMDParser(); + + void parse(uint8_t* data, int size); + +private: + // BCC鏍¢獙鍑芥暟 + static uint8_t bcc_checksum(uint8_t* data, int size); + + // 灏4瀛楄妭鏁版嵁杞崲涓鸿仈鍚堜綋涓殑鍊 + void convert(uint8_t* data, value_info& info); +}; + +extern VSMDParser vsmdParser; + +extern BLECharacteristic* pCharacteristic; // 澶栭儴 BLE 鐗瑰緛鍊兼寚閽堝0鏄 + +#endif \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html diff --git a/test5.json b/test5.json new file mode 100644 index 0000000..c18c2eb --- /dev/null +++ b/test5.json @@ -0,0 +1,23 @@ +{ + "limit": { + "cfg msr": 1, //璁剧疆璐熸瀬闄愪紶鎰熷櫒 + "cfg msv": 0, //璁剧疆璐熸瀬闄愯Е鍙戠數骞 + "cfg psr": 2, //璁剧疆姝f瀬闄愪紶鎰熷櫒 + "cfg psv": 0 //璁剧疆姝f瀬闄愯Е鍙戠數骞 + }, + "other": { + "cfg acc": 20000, //璁剧疆鍔犻熷害 + "cfg cra": 4, //璁剧疆鍔犻熸椂鐢垫祦 + "cfg crh": 1, //璁剧疆HOLD鐢垫祦 + "cfg crn": 4, //璁剧疆鍖閫熸椂鐢垫祦 + "cfg dec": 20000, //璁剧疆鍑忛熷害 + "cfg mcs": 7 //璁剧疆寰缁嗗垎 + }, + "zero start": { + "cfg osv": 0, //璁剧疆褰掗浂浼犳劅鍣∣PEN鏃剁數骞 + "cfg snr": 0, //璁剧疆褰掗浂鐢ㄤ紶鎰熷櫒 + "cfg zmd": 2, //璁剧疆褰掗浂鍔熻兘 + "cfg zsd": 3000, //璁剧疆褰掗浂閫熷害锛堟璐熶唬琛ㄦ柟鍚戯級 + "cfg zsp": 2400 //璁剧疆褰掗浂鍚庣殑瀹夊叏浣嶇疆 + } +}