From 8be32f4e57f25c15fa44dc2377ccfa68e870d7ec Mon Sep 17 00:00:00 2001 From: "DESKTOP-T3S0O41\\iris_lj" Date: Tue, 24 Mar 2026 16:17:32 +0800 Subject: [PATCH] V2.0.0.0 --- .gitignore | 5 + .vscode/extensions.json | 10 ++ .vscode/settings.json | 61 ++++++++++ data/hello.txt | 2 + data/system.bin | 0 extra_script.py | 3 + include/README | 39 ++++++ lib/README | 46 ++++++++ partitions.csv | 7 ++ platformio.ini | 30 +++++ src/IRIS_Method.cpp | 174 +++++++++++++++++++++++++++ src/IRIS_Method.h | 60 ++++++++++ src/community.cpp | 255 ++++++++++++++++++++++++++++++++++++++++ src/community.h | 16 +++ src/main.cpp | 42 +++++++ src/sensor.cpp | 253 +++++++++++++++++++++++++++++++++++++++ src/sensor.h | 74 ++++++++++++ 17 files changed, 1077 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 data/hello.txt create mode 100644 data/system.bin create mode 100644 extra_script.py create mode 100644 include/README create mode 100644 lib/README create mode 100644 partitions.csv create mode 100644 platformio.ini create mode 100644 src/IRIS_Method.cpp create mode 100644 src/IRIS_Method.h create mode 100644 src/community.cpp create mode 100644 src/community.h create mode 100644 src/main.cpp create mode 100644 src/sensor.cpp create mode 100644 src/sensor.h 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..d6f657a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,61 @@ +{ + "files.associations": { + "*.tpp": "cpp", + "sstream": "cpp", + "typeinfo": "cpp", + "array": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "*.tcc": "cpp", + "fstream": "cpp", + "utility": "cpp", + "ostream": "cpp", + "random": "cpp", + "atomic": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "ratio": "cpp", + "thread": "cpp", + "chrono": "cpp", + "condition_variable": "cpp", + "mutex": "cpp" + }, + "python-envs.defaultEnvManager": "ms-python.python:system", + "python-envs.pythonProjects": [], + "idf.portWin": "COM30" +} \ No newline at end of file diff --git a/data/hello.txt b/data/hello.txt new file mode 100644 index 0000000..7850d4f --- /dev/null +++ b/data/hello.txt @@ -0,0 +1,2 @@ +pio run --target uploadfs +hello \ No newline at end of file diff --git a/data/system.bin b/data/system.bin new file mode 100644 index 0000000..e69de29 diff --git a/extra_script.py b/extra_script.py new file mode 100644 index 0000000..1eafbb4 --- /dev/null +++ b/extra_script.py @@ -0,0 +1,3 @@ +Import("env") + +env.Replace(PROGNAME="firmware_%s" % env.GetProjectOption("custom_prog_version")) \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +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 usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +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..6debab1 --- /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 executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, 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 + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/partitions.csv b/partitions.csv new file mode 100644 index 0000000..5b57920 --- /dev/null +++ b/partitions.csv @@ -0,0 +1,7 @@ +# Name, Type, SubType, Offset, Size +nvs, data, nvs, 0x9000, 0x6000 +phy_init, data, phy, 0xf000, 0x1000 +factory, app, factory, 0x10000, 0x180000 +storage, data, spiffs, 0x190000, 0x100000 +coredump, data, coredump,0x3F0000 , 0x10000, + diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..44f44f2 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,30 @@ +; 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 @ 6.5.0 +board = esp32-s3-devkitc-1 +framework = arduino +platform_packages = + toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 + ; framework-arduinoespressif32 @ 2.0.6+sha.099b432 +board_upload.flash_size=4MB +board_upload.psram_size =2MB +lib_deps = + paulstoffregen/OneWire@^2.3.7 + +build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 ; Enable USB CDC + -DCORE_DEBUG_LEVEL=1 ; Set debug level + +build.arduino.memory_type = qspi_qspi + +board_build.partitions = partitions.csv +; board_build.filesystem = spiffs + diff --git a/src/IRIS_Method.cpp b/src/IRIS_Method.cpp new file mode 100644 index 0000000..20e853f --- /dev/null +++ b/src/IRIS_Method.cpp @@ -0,0 +1,174 @@ +/** + ****************************************************************************** + * @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" + +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); //减去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..99a3582 --- /dev/null +++ b/src/IRIS_Method.h @@ -0,0 +1,60 @@ +/** + ****************************************************************************** + * @file : IRIS_Method.h + * @author : xin + * @brief : None + * @attention : None + * @date : 2024/2/1 + ****************************************************************************** + */ + +// +// Created by xin on 2024/2/1. +// + +#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); + +// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令 BufferOut 是输出 +// 下位机使用的打包函数 Command 是输出 +// 成功返回解包后的数据长度 +// 0: 该命令返回无参数 +// 错误返回ERRor +// 成功返回解包后的数据长度 +int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut); + +// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令输入 BufferOut 是输出 上位机使用 +// 成功返回解包后的数据长度 +// 0: 该命令返回无参数 +// 错误返回ERRor +// 成功返回解包后的数据长度 +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); + +#endif //IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H diff --git a/src/community.cpp b/src/community.cpp new file mode 100644 index 0000000..35ee7ff --- /dev/null +++ b/src/community.cpp @@ -0,0 +1,255 @@ +#include "community.h" +#include "Sensor.h" +uint32_t usart_num = 0; +static EventGroupHandle_t community_event_group; +volatile bool stop_shutter_flag = true; +volatile uint8_t shutter_command = 0; +int Global_shutter_now=12; +void community_init() +{ + + Serial.setTimeout(1); + Serial.setRxBufferSize(1024); + Serial.setTxBufferSize(1039 * 5); + Serial.begin(921600); + + + Serial0.setTimeout(1); + Serial0.setRxBufferSize(1024); + Serial0.setTxBufferSize(1039 * 5); + Serial0.begin(921600); + + + community_event_group = xEventGroupCreate(); + + xTaskCreatePinnedToCore(read_task, "read_task", 1024*10, NULL, 21, NULL, 1); + xTaskCreatePinnedToCore(send_data_task, "send_data_task", 1024*10, NULL, 22, NULL, 1); +} + + +void Send_Data(uint8_t command,uint8_t *pData, uint16_t Size) +{ + uint8_t send_buff[1024*2]; + uint32_t send_lenth; + + send_lenth = IRIS_Protocol_Pack(command,Size,pData,send_buff); + + if (usart_num == 0) Serial0.write(send_buff,send_lenth); + else Serial.write(send_buff,send_lenth); +} + +void read_task(void *pvParameters) +{ + uint8_t command_data[1024 * 2] = {0}; + uint32_t stop_shutter = 0; + while (1) + { + uint32_t data_length = 0; + while (Serial0.available()) + { + data_length = Serial0.readBytes(command_data,1024*2); + usart_num = 0; + vTaskDelay(1); + } + + while (Serial.available()) + { + + data_length = Serial.readBytes(command_data,1024*2); + usart_num = 1; + vTaskDelay(1); + } + // Serial.write(command_data,data_length); + + if(data_length > 0) commander_run(command_data, data_length); + + if(stop_shutter_flag) stop_shutter ++; + if(stop_shutter == 50) + { + shutter_stop(); + stop_shutter_flag = false; + uint8_t d = 1; + Send_Data(shutter_command,&d,1); + stop_shutter = 0; + } + vTaskDelay(1); + } +} + + +//实现大小端交换 +void swap_buf(uint8_t *p , uint32_t size) { + uint8_t temp_buf[size]; + memcpy(temp_buf,p,size); + for (int i = 0; i < size; i++) { + p[i] = temp_buf[size-i-1]; + } +} +void commander_run(uint8_t *data,uint32_t lenth) +{ + uint8_t command_data[1024 * 2] = {0}; + uint16_t templenth = IRIS_Cut_Befor_Header(data,lenth); + uint8_t data_type = 0; + int ret = IRIS_STM32_Protocol_Unpack(data,templenth,&data_type,command_data); + + uint32_t shutter_time = 0; + if (ret > 0) + { + switch(data_type) + { + // 获取设备信息 + case 0x50: { + uint8_t str[8]; + str[0] = 'I'; + str[1] = 'S'; + str[2] = '3'; + str[3] = '-'; + str[4] = '0'+ sensor_info.sn/1000%10; + str[5] = '0'+ sensor_info.sn/100%10; + str[6] = '0'+ sensor_info.sn/10%10; + str[7] = '0'+ sensor_info.sn%10; + + // printf("shutter_time %d\n",shutter_time); + Send_Data(0x50,str,8); + + break; + } + //设置曝光时间 + case 0x51: { + shutter_time = command_data[0]<<24 | command_data[1]<<16 | command_data[2]<<8 | command_data[3]; + if (shutter_time < 12) shutter_time = 12; + Global_shutter_now = shutter_time; + Send_Shutter_Time(shutter_time); + Send_Data(0x51,command_data,4); + break; + } + //自动曝光 + case 0x52:{ + Send_Data(0x52,command_data,1); + break; + } + //获取曝光时间 + case 0x53:{ + uint8_t st[4]; + memcpy(st,&shutter_time,4); + swap_buf(st,4); + + Send_Data(0x53,st,4); + break; + } + //获取温度 + case 0x54: { + // 将Temperature的值转换为uint8_t数组,并将其作为参数传递给Send_Data函数。 + uint8_t temp[4]; + // Temperature = DS18B20_GetTemperature(); + float Temperature = 0; + memcpy(temp,&Temperature,4); + swap_buf(temp,4); + + Send_Data(0x54,temp,4); + break; + } + //开启快门 + case 0x55: { + Shutter_Close(command_data[0]); + // Send_Data(0x55,command_data,1); + shutter_command = 0x55; + stop_shutter_flag = true; + break; + } + //关闭快门 + case 0x56: { + Shutter_Open(command_data[0]); + // Send_Data(0x56,command_data,1); + shutter_command = 0x56; + stop_shutter_flag = true; + break; + } + //获取波长数 + case 0x57: + { + uint32_t d = Baudnum; + uint8_t st[4]; + memcpy(st,&d,4); + swap_buf(st,4); + Send_Data(0x57,st,4); + break; + } + //设置波长系数 + case 0x58: { + swap_buf(command_data,8); + swap_buf(command_data+8,8); + swap_buf(command_data+16,8); + swap_buf(command_data+24,8); + + memcpy(&sensor_info.a1,command_data,32); + uint8_t d = 0; + // stm32h7_write_flash(FLASH_SAVE_ADDR,(uint8_t *)bochangxishu,32); //写入flash + sensor_info.flag = 1; + sensor_data_save(&sensor_info); + Send_Data(0x58,&d,1); + break; + } + + //获取波长系数 + case 0x59: { + double temp[4]; + memcpy(temp,&sensor_info.a1,32); + swap_buf((uint8_t *)temp,8); + swap_buf((uint8_t *)temp+8,8); + swap_buf((uint8_t *)temp+16,8); + swap_buf((uint8_t *)temp+24,8); + + Send_Data(0x59,(uint8_t *)temp,32); + break; + } + + //设置数据处理方式 + case 0x60: { + // Send_Data_Type = command_data[0]; + // Target_Average_Times = command_data[1]<<24 | command_data[2]<<16 | command_data[3]<<8 | command_data[4]; + Send_Data(0x60,command_data,5); + break; + } + //获取数据 + case 0x61: { + // busy_sta = 1; + // Send_Data_Type = Send_Data_Type; + // Send_Data(0x61,command_data,5); + // xEventGroupClearBits(sensor_event_group,SENSOR_READ_DONE); + xEventGroupSetBits(community_event_group,Send_data_bit); + break; + } + + case 0x62: { + // uint8_t d = Send_Data_Type & 0x7F; + uint8_t d = 0; + Send_Data(0x62,&d,1); + break; + } + + case 0x6f: { + sensor_info.sn = command_data[0] << 24 | command_data[1] << 16 | command_data[2] << 8 | command_data[3]; + sensor_data_save(&sensor_info); + uint8_t d = 1; + Send_Data(0x6f,&d,1); + break; + } + } + } +} + +void send_data_task(void *pvParameters) +{ + uint16_t send_buff[517]; + while (1) + { + xEventGroupClearBits(community_event_group,Send_data_bit); + xEventGroupWaitBits(community_event_group,Send_data_bit,pdTRUE,pdFALSE,portMAX_DELAY); + // xEventGroupWaitBits(sensor_event_group,SENSOR_READ_DONE,pdTRUE,pdFALSE,portMAX_DELAY); + // get_sensor_data(send_buff,Global_shutter_now*0.9); + get_sensor_data(send_buff); + // send_buff[516] = send_buff[515]; + Send_Data(0x61,(uint8_t *)(&send_buff[1]), 515*2); + } +} \ No newline at end of file diff --git a/src/community.h b/src/community.h new file mode 100644 index 0000000..0803aa1 --- /dev/null +++ b/src/community.h @@ -0,0 +1,16 @@ +#ifndef COMMUNITY_H +#define COMMUNITY_H + +#include "Arduino.h" +#include "IRIS_Method.h" +#include "sensor.h" +// #define sn 0 +#define Send_data_bit (1<<0) + +void community_init(); +void read_task(void *pvParameters); +void send_data_task(void *pvParameters); +void commander_run(uint8_t *data,uint32_t lenth); +#endif + + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a2388f1 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,42 @@ +#include "Arduino.h" +#include "sensor.h" +#include "community.h" + +void setup() +{ + // esp_task_wdt_deinit(); + // setCpuFrequencyMhz(240); + community_init(); + sensor_init(); +} + + +void loop() +{ + + vTaskDelay(portMAX_DELAY); + // digitalWrite(38, 0); + // vTaskDelay(10); + // digitalWrite(38, 1); +} + + +// #include "Arduino.h" +// #include "SPIFFS.h" + +// void setup() { +// Serial.begin(115200); +// delay(1000); + +// if (!SPIFFS.begin(true)) { +// Serial.println("SPIFFS mount failed!"); +// return; +// } +// Serial.println("SPIFFS mount success!"); +// } + +// void loop() +// { + +// vTaskDelay(1000); +// } diff --git a/src/sensor.cpp b/src/sensor.cpp new file mode 100644 index 0000000..d3c58ab --- /dev/null +++ b/src/sensor.cpp @@ -0,0 +1,253 @@ +#include "Sensor.h" +#include "driver/uart.h" + +sensor_data sensor_info; + +EventGroupHandle_t sensor_event_group; +uint16_t receive_buff1[516]; +uint16_t receive_buff2[516]; +volatile uint32_t count = 0; +uint32_t pos = 0; +volatile uint64_t frame_count = 0; +volatile bool read_flag = false; +#define trigger_pin GPIO_NUM_8 +#define rst_pin GPIO_NUM_38 +void IRAM_ATTR handleFallingEdge() +{ + // read_flag = true; + // count = 0; + // if(count == 3) digitalWrite(rst_pin, 0); + if(digitalRead(trigger_pin) == 0) read_flag = true; + else read_flag = false; +} + +void sensor_init() +{ + sensor_info.sn = 0; + if (SPIFFS.begin(true)) + { + // Serial.println("SPIFFS mount ok!"); + sensor_data_read(&sensor_info); + } + else + { + // Serial.println("SPIFFS mount failed!"); + } + + if(sensor_info.flag != 1) + { + sensor_info.flag = 0; + sensor_info.a1 = 2.73e-8; + sensor_info.a2 = -0.000025; + sensor_info.a3 = -1.14675; + sensor_info.a4 = 1082.6422; + // sensor_info.sn = 1; + } + + pinMode(Shutter1_pin, OUTPUT); + pinMode(Shutter2_pin, OUTPUT); + pinMode(rst_pin, OUTPUT); + + + Shutter_Open(Shutter_1); + // digitalWrite(rst_pin, 1); + // vTaskDelay(36); + digitalWrite(rst_pin, 0); + vTaskDelay(36); + + Serial2.setTimeout(1); + Serial2.setRxBufferSize(1032); + Serial2.setTxBufferSize(256); + Serial2.begin(921600,SERIAL_8N1,rx,tx); + + Send_Shutter_Time(12); + +// Serial2.end(); + + sensor_event_group = xEventGroupCreate(); + + pinMode(trigger_pin, INPUT); + attachInterrupt(digitalPinToInterrupt(trigger_pin), handleFallingEdge, CHANGE); + + xTaskCreatePinnedToCore(sensor_read_task, "sensor_read_task", 1024*10, NULL, 30, NULL, 0); + + vTaskDelay(2000); + digitalWrite(rst_pin, 1); + shutter_stop(); +} + + + +void sensor_rest() +{ + digitalWrite(rst_pin, 0); + // for(int i = 0; i < 10000; i++); + vTaskDelay(1); + // delayMicroseconds(50); + digitalWrite(rst_pin, 1); +} + +void sensor_write(uint8_t *data,uint32_t len) +{ + Serial2.write(data,len); +} + +void Send_Shutter_Time(uint32_t shutter_time) { + uint32_t tt = shutter_time*1000; + + uint8_t t_buff[4]; + t_buff[0] = *((uint8_t *)&tt+3); + t_buff[1] = *((uint8_t *)&tt+2); + t_buff[2] = *((uint8_t *)&tt+1); + t_buff[3] = *((uint8_t *)&tt); + sensor_write(t_buff,4); +} + + +void sensor_read_task(void *pvParameters) +{ + uint8_t *p = new uint8_t[1040]; + int indexnow=0; + while (1) + { + if(read_flag == true) + { + int lent=0; + u_char c=-1; + while (1) + { + + int temp = uart_read_bytes(2,p,1032,10); + + if(temp == 1032) + { + // p[lent] = c; + // lent++; + // if(lent >= 1032) + // { + lent = 1032; + break; + // } + } + else + { + read_flag = false; + continue; + } + } + memcpy(receive_buff2,p,lent); + + frame_count++; + read_flag = false; + continue; + } + else + { + if(Serial2.available()) + { + Serial2.readBytes((uint8_t *)p, Serial2.available()); + } + vTaskDelay(1); + } + } +} + +void get_sensor_data(uint16_t *data_buff) +{ + // sensor_rest(); + // xEventGroupSetBits(sensor_event_group, START_READ_BIT); + uint32_t target = frame_count + 1; + while(frame_count < target) + { + vTaskDelay(1); + } + if(target == frame_count) + { + for (int i = 0; i < 516; i++) + { + data_buff[i] = receive_buff2[i]; + } + } +} + +// void get_sensor_data(uint16_t *data_buff,int timewaitinms) +// { +// sensor_rest(); +// // xEventGroupSetBits(sensor_event_group, START_READ_BIT); +// vTaskDelay(timewaitinms); +// uint32_t target = frame_count + 1; +// while(frame_count < target) +// { +// vTaskDelay(1); +// } +// if(target == frame_count) +// { +// for (int i = 0; i < 516; i++) +// { +// data_buff[i] = receive_buff2[i]; +// } +// } +// } +void Shutter_Open(uint8_t shutter) +{ + digitalWrite(Shutter2_pin, 1); + digitalWrite(Shutter1_pin, 0); + // if (shutter == Shutter_1) + // { + // digitalWrite( Shutter2_pin, 1); + // digitalWrite(Shutter1_pin, 0); + + // } + // else if(shutter == Shutter_2) + // { + // digitalWrite(Shutter1_pin, 1); + // } +} + +void shutter_stop() +{ + digitalWrite(Shutter1_pin, 0); + digitalWrite(Shutter2_pin, 0); +} + +void Shutter_Close(uint8_t shutter) +{ + digitalWrite(Shutter2_pin, 0); + digitalWrite(Shutter1_pin, 1); + // if (shutter == Shutter_1) + // { + // digitalWrite( Shutter1_pin, 1); + // digitalWrite( Shutter2_pin, 0); + // } + // else if (shutter == Shutter_2) + // { + // digitalWrite(Shutter1_pin, 0); + // } +} + + +void sensor_data_save(sensor_data *sensor_data_struct) +{ + File file; + file = SPIFFS.open("/system.bin", FILE_WRITE); + file.write((uint8_t *)sensor_data_struct,sizeof(sensor_data)); + file.flush(); + file.close(); +} + +void sensor_data_read(sensor_data *sensor_data_struct) +{ + File file; + file = SPIFFS.open("/system.bin", FILE_READ); + file.read((uint8_t *)sensor_data_struct,sizeof(sensor_data)); + file.flush(); + file.close(); +} + + + + + + + + diff --git a/src/sensor.h b/src/sensor.h new file mode 100644 index 0000000..58e764d --- /dev/null +++ b/src/sensor.h @@ -0,0 +1,74 @@ +#ifndef ___SENSOR_H_ +#define ___SENSOR_H_ +#include "Arduino.h" +#include "esp_task_wdt.h" +#include "SPIFFS.h" +// #define rx 7 +// #define tx 9 +#define rx 12 +#define tx 13 +#define Shutter_1 1 +#define Shutter_2 2 +#define Shutter1_pin 11 +#define Shutter2_pin 10 +#define Baudnum 515 + +#define SENSOR_READ_DONE (1<<0) +#define START_READ_BIT (1<<1) + +typedef struct +{ + uint32_t flag; + uint32_t sn; + double a1; + double a2; + double a3; + double a4; +}sensor_data; + +extern sensor_data sensor_info; +extern EventGroupHandle_t sensor_event_group; + +void sensor_init(); + +void sensor_rest(); +void sensor_read_task(void *pvParameters); +void Send_Shutter_Time(uint32_t shutter_time); +void Shutter_Open(uint8_t shutter); +void Shutter_Close(uint8_t shutter); +void shutter_stop(); +void get_sensor_data(uint16_t *data_buff2); +// void get_sensor_data(uint16_t *data_buff,int timewaitinms); +void sensor_data_save(sensor_data *sensor_data_struct); +void sensor_data_read(sensor_data *sensor_data_struct); +/////////////////////////////////////////////////////////// + +// #define Shutter_1 1 +// #define Shutter_2 2 +// #define Shutter1_pin 11 +// #define Shutter2_pin 10 +// #define Baudnum 515 +// #define SENSOR_READ_DONE (1<<0) + +// typedef struct +// { +// uint32_t sn; +// double a1; +// double a2; +// double a3; +// double a4; +// }sensor_data; + +// extern sensor_data sensor_info; +// extern EventGroupHandle_t sensor_event_group; + + +// void uart_init(); +// void sensor_init(); +// void get_sensor_data(uint16_t *data_buff); +// void sensor_write(uint8_t *data,uint32_t len); +// void Send_Shutter_Time(uint32_t shutter_time); +// void Shutter_Open(uint8_t shutter); +// void Shutter_Close(uint8_t shutter); + +#endif \ No newline at end of file