From 895928440bfe68db80f543fa7ce2004d55f46dc4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-T3S0O41\\iris_lj" Date: Mon, 13 Jul 2026 17:18:22 +0800 Subject: [PATCH] first commit --- .gitignore | 5 + HH3.csv | 7 + platformio.ini | 81 + src/HH3_ESP32_VSCode.code-workspace | 23 + src/IRIS_Method.cpp | 174 ++ src/IRIS_Method.h | 60 + src/IS3.cpp | 547 +++++ src/IS3.h | 57 + src/adc.cpp | 88 + src/adc.h | 12 + src/button.cpp | 158 ++ src/button.h | 23 + src/ds1307.cpp | 77 + src/ds1307.h | 20 + src/header.h | 48 + src/hh3_slave.cpp | 81 + src/hh3_slave.h | 62 + src/hh3_system.cpp | 81 + src/hh3_system.h | 9 + src/lcd.cpp | 185 ++ src/lcd.h | 12 + src/log.cpp | 38 + src/log.h | 16 + src/main.cpp | 70 + src/my_aht10.cpp | 47 + src/my_aht10.h | 10 + src/qmi8658.cpp | 127 ++ src/qmi8658.h | 97 + src/red_ray.cpp | 14 + src/red_ray.h | 9 + src/save.h | 171 ++ src/sd_card.cpp | 198 ++ src/sd_card.h | 15 + src/speak.cpp | 26 + src/speak.h | 15 + src/ui_task.cpp | 3226 +++++++++++++++++++++++++++ src/ui_task.h | 102 + src/usb_camera.cpp | 364 +++ src/usb_camera.h | 20 + src/wifi_client.cpp | 115 + src/wifi_client.h | 12 + version.txt | 3 + 42 files changed, 6505 insertions(+) create mode 100644 .gitignore create mode 100644 HH3.csv create mode 100644 platformio.ini create mode 100644 src/HH3_ESP32_VSCode.code-workspace create mode 100644 src/IRIS_Method.cpp create mode 100644 src/IRIS_Method.h create mode 100644 src/IS3.cpp create mode 100644 src/IS3.h create mode 100644 src/adc.cpp create mode 100644 src/adc.h create mode 100644 src/button.cpp create mode 100644 src/button.h create mode 100644 src/ds1307.cpp create mode 100644 src/ds1307.h create mode 100644 src/header.h create mode 100644 src/hh3_slave.cpp create mode 100644 src/hh3_slave.h create mode 100644 src/hh3_system.cpp create mode 100644 src/hh3_system.h create mode 100644 src/lcd.cpp create mode 100644 src/lcd.h create mode 100644 src/log.cpp create mode 100644 src/log.h create mode 100644 src/main.cpp create mode 100644 src/my_aht10.cpp create mode 100644 src/my_aht10.h create mode 100644 src/qmi8658.cpp create mode 100644 src/qmi8658.h create mode 100644 src/red_ray.cpp create mode 100644 src/red_ray.h create mode 100644 src/save.h create mode 100644 src/sd_card.cpp create mode 100644 src/sd_card.h create mode 100644 src/speak.cpp create mode 100644 src/speak.h create mode 100644 src/ui_task.cpp create mode 100644 src/ui_task.h create mode 100644 src/usb_camera.cpp create mode 100644 src/usb_camera.h create mode 100644 src/wifi_client.cpp create mode 100644 src/wifi_client.h create mode 100644 version.txt 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/HH3.csv b/HH3.csv new file mode 100644 index 0000000..98f0cb9 --- /dev/null +++ b/HH3.csv @@ -0,0 +1,7 @@ +; # Name, Type, SubType, Offset, Size +; nvs, data, nvs, 0x9000, 0x8000 +; otadata, data, ota, 0x11000, 0x2000 +; phy_init, data, phy, 0x13000, 0x1000 +; app0, app, ota_0, 0x14000, 0x300000 +; app1, app, ota_1, 0x314000, 0x300000 +; spiffs, data, spiffs, 0x614000, 0x700000 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..a1ea5ea --- /dev/null +++ b/platformio.ini @@ -0,0 +1,81 @@ +; 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 +platform = espressif32 @ 6.5.0 +board = esp32-s3-devkitc-1 +framework = arduino +platform_packages = toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 +board_build.arduino.memory_type = qio_opi +board_build.psram = enabled +board_upload.psram_size = 8MB +board_upload.flash_size = 16MB +; usb 调试 +; board_build.embed_usb_device = true + +board_build.embed_usb_device = true +board_build.embed_usb_serial_jtag = true +build_flags = -D LV_LVGL_H_INCLUDE_SIMPLE + -DBOARD_HAS_PSRAM + + ; -DCONFIG_USB_HOST_ENABLED + ; -DCONFIG_USB_UVC_ENABLED + +; ; usb 调试 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DARDUINO_USB_MODE=1 + + +lib_deps = + + +; knolleary/PubSubClient@^2.8 +; arduino-libraries/ArduinoHttpClient@^0.4.0 +; bblanchon/ArduinoJson@^6.20.1 + +; ; lvgl/lvgl@8.4.0 + +; esphome/ESP32-audioI2S @ ^2.0.7 +; tamctec/TAMC_GT911 @ ^1.0.2 + +; esp-arduino-libs/ESP32_USB_STREAM @ ^0.0.1 + +; adafruit/RTClib @ ^2.1.4 +; enjoyneering/AHT10 @ ^1.1.0 + + +; bodmer/TJpg_Decoder @ 1.1.0 + +; ; pio run --target uploadfs +; ; debug_port=esp-builtin + +monitor_filters = esp32_exception_decoder +monitor_speed = 921600 + +; # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +; # Name, Type, SubType, Offset, Size, Flags +; nvs, data, nvs, 0x9000, 24k +; phy_init, data, phy, 0xf000, 4k +; factory, app, factory, , 8M +; storage, data, spiffs, , 3M + + +; # Name, Type, SubType, Offset, Size +; nvs, data, nvs, 0x9000, 0x8000 +; otadata, data, ota, 0x11000, 0x2000 +; phy_init, data, phy, 0x13000, 0x1000 +; app0, app, ota_0, 0x14000, 0x300000 +; app1, app, ota_1, 0x314000, 0x300000 +; spiffs, data, spiffs, 0x614000, 0x700000 + + + + diff --git a/src/HH3_ESP32_VSCode.code-workspace b/src/HH3_ESP32_VSCode.code-workspace new file mode 100644 index 0000000..784440c --- /dev/null +++ b/src/HH3_ESP32_VSCode.code-workspace @@ -0,0 +1,23 @@ +{ + "folders": [ + { + "path": ".." + } + ], + "settings": { + "files.associations": { + "widgets_init.h": "c", + "main.h": "c", + "mymain.h": "c", + "speak.h": "c", + "ds1307.h": "c", + "audio.h": "c", + "gui_guider.h": "c", + "lvgl.h": "c", + "header.h": "c", + "custom.h": "c", + "aht10.h": "c", + "arduino.h": "c" + } + } +} \ No newline at end of file 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/IS3.cpp b/src/IS3.cpp new file mode 100644 index 0000000..2b72126 --- /dev/null +++ b/src/IS3.cpp @@ -0,0 +1,547 @@ +#include "IS3.h" +#define IS3_usart_port 1 +#define IS3_TX 42 +#define IS3_RX 41 +#define IS3_RST 17 + +IS3::IS3() +{ + a1 = 0.0; + a2 = 0.0; + a3 = 0.0; + a4 = 0.0; +} + +void IS3::init() +{ + Serial1.setRxBufferSize(1024*2); + Serial1.setTimeout(10); + Serial1.setTxBufferSize(1024*2); + Serial1.begin(921600, SERIAL_8N1, IS3_RX, IS3_TX); + + // Serial1.onReceive(onUart1Data, true); // 建议用 true + pinMode(IS3_RST,OUTPUT); + on(); + IS3::reset(); + // IS3::reset(); + // vTaskDelay(1000); + + IS3::integration_time = 12; + bool success = false; + String is3_info; + double aa1, aa2, aa3, aa4; + uint32_t is3_bandnum; + + do + { + // Serial0.println("IS3 set_shutter_time"); + success = IS3::set_shutter_time(IS3::integration_time); + vTaskDelay(10); + } while (!success); + // Serial0.printf("set_shutter_time : %d\n",) + do + { + // Serial0.println("IS3 get_is3_info"); + success = IS3::get_is3_info(&is3_info); + vTaskDelay(10); + } while (!success); + // Serial0.printf("IS3_info %s\n", is3_info.c_str()); + do + { + // Serial0.println("IS3 get_bochangxishu"); + success = IS3::get_bochangxishu(&aa1, &aa2, &aa3, &aa4); + vTaskDelay(10); + } while (!success); + // Serial0.printf("aa1:%f aa2:%f aa3:%f aa4:%f\n",aa1,aa2,aa3,aa4); + do + { + // Serial0.println("IS3 get_bandnum"); + success = IS3::get_bandnum(&is3_bandnum); + vTaskDelay(10); + } while (!success); + // Serial0.printf("is3_bandnum:%d\n",is3_bandnum); +} + +void IS3::reset() +{ + digitalWrite(IS3_RST,LOW); + vTaskDelay(200); + digitalWrite(IS3_RST,HIGH); + vTaskDelay(1000); +} + + + +void IS3::on() +{ + digitalWrite(IS3_RST,HIGH); +} + +void IS3::off() +{ + digitalWrite(IS3_RST,LOW); +} + +void IS3::write(uint8_t command,uint8_t *pData, uint16_t Size) +{ + uart_flush_input(IS3_usart_port); + uint8_t send_buff[200]; + uint32_t send_lenth; + + send_lenth = IRIS_Protocol_Pack(command,Size,pData,send_buff); + Serial1.write(send_buff,send_lenth); +} + +// int IS3::read1(uint8_t *data_type,uint8_t *read_buf) +// { + +// unsigned char command_data[1024 * 2]; +// uint16_t data_length = 0; +// // int a = 200; +// int a = is3.integration_time * 3; +// while (a--) +// { +// while(Serial1.available()) +// { +// data_length += Serial1.readBytes(&command_data[data_length],1024*2); +// a = 2; +// } +// // if(a< 10) a--; +// vTaskDelay(1); +// } +// if (data_length <= 0) +// { +// return -1; +// } + +// data_length = IRIS_Cut_Befor_Header(command_data, data_length); +// int ret = IRIS_STM32_Protocol_Unpack(command_data,data_length,data_type,read_buf); +// if (ret < 0) +// { +// return -1; +// } + +// return ret; + +// } + + +uint32_t my_usart_available(uart_port_t uart_port) +{ + + size_t available = 0; + uart_get_buffered_data_len(uart_port, &available); + // Serial0.printf("available:%d\n",available); + return available; + // if (uart->has_peek) available++; +} + + +int IS3::read(uint8_t *data_type,uint8_t *read_buf) +{ + IS3::read_sta = 1; + unsigned char command_data[1024 * 2]; + uint16_t data_length = 0; + // int a = 200; + int a = is3.integration_time * 3 + 50; + while (a--) + { + // while(Serial1.available()) + // uint32_t len =0; + + // uart_get_buffered_data_len(IS3_usart_port,&len); + while(my_usart_available(IS3_usart_port)) + { + // data_length += Serial1.readBytes(&command_data[data_length],1024*2); + data_length += uart_read_bytes(IS3_usart_port,&command_data[data_length],1024*2,10); + a = 2; + } + + // if(a< 10) a--; + vTaskDelay(1); + } + if (data_length <= 0) + { + IS3::read_sta = 0; + return -1; + } + data_length = IRIS_Cut_Befor_Header(command_data, data_length); + int ret = IRIS_STM32_Protocol_Unpack(command_data,data_length,data_type,read_buf); + if (ret < 0) + { + IS3::read_sta = 0; + memset(read_buf,0,sizeof(read_buf)); + return -1; + } + IS3::read_sta = 0; + return ret; +} + +bool IS3::get_is3_info(String *is3_info) +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x50,&data,1); + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if (data_lenth <= 0 || data_type != 0x50) + { + return false; + } + *is3_info = String(command_data,data_lenth); + IS3::info = *is3_info; + return true; +} + +bool IS3::set_shutter_time(uint32_t shutter_time) +{ + if(IS3::read_sta == 1) return false; + uint8_t data[4]={0,0,0,0}; + data[0] |= shutter_time >> 24; + data[1] |= shutter_time >> 16; + data[2] |= shutter_time >> 8; + data[3] |= shutter_time ; + IS3::write(0x51,data,4); + + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if (data_type != 0x51) + { + return false; + } + IS3::integration_time = shutter_time; + return true; +} + +//有问题 +bool IS3::opt() +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x52,&data,1); + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x52) + { + return false; + } + return true; +} + + +bool IS3::get_shutter_time(uint32_t *shutter_time) +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x53,&data,1); + + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if (data_lenth <= 0 || data_type != 0x53) + { + return false; + } + *shutter_time = command_data[0] << 24 | command_data[1] << 16 | command_data[2] << 8 | command_data[3]; + IS3::integration_time = *shutter_time; + return true; +} + + +bool IS3::get_is3_temperature(float *temp) +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x54,&data,1); + + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if (data_lenth <= 0 || data_type != 0x54) + { + return false; + } + *temp = command_data[0] << 24 | command_data[1] << 16 | command_data[2] << 8 | command_data[3]; + IS3::temperature = *temp; + return true; +} + + +bool IS3::close_shutter() +{ + if(IS3::read_sta == 1) return false; + uint8_t shutter_num = 1; + IS3::write(0x55,&shutter_num,1); + + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x55) + { + return false; + } + return true; +} + +bool IS3::open_shutter() +{ + if(IS3::read_sta == 1) return false; + uint8_t shutter_num = 1; + IS3::write(0x56,&shutter_num,1); + + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x56) + { + return false; + } + return true; +} + +bool IS3::get_bandnum(uint32_t *is3_bandnum) +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x57,&data,1); + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x57) + { + return false; + } + *is3_bandnum = command_data[0] << 24 | command_data[1] << 16 | command_data[2] << 8 | command_data[3]; + IS3::bandnum = *is3_bandnum; + return true; +} + + +// bool set_bochangxishu(struct bochangxishu *bochangxishu); +// bool IS3::set_bochangxishu(double aa1,double aa2,double aa3,double aa4) +// { +// uint8_t bochangxishu[32] = {0}; + + +// IS3::write(0x58, (uint8_t *)bochangxishu, 32); + +// uint8_t data_type; +// uint8_t command_data[20]; +// int data_lenth = IS3::read(&data_type,command_data); +// if(data_lenth <= 0 || data_type != 0x58) +// { +// return false; +// } +// return true; +// } + +// 转换每个 double 为大端格式并写入缓冲区 +void double_to_big_endian(double value, uint8_t* out) { + uint8_t* p = (uint8_t*)&value; + for (int i = 0; i < 8; ++i) { + out[i] = p[7 - i]; // 反转字节序 + } +} + +bool IS3::set_bochangxishu(double aa1, double aa2, double aa3, double aa4) +{ + if(IS3::read_sta == 1) return false; + uint8_t bochangxishu[32] = {0}; // 4 * 8 = 32 bytes + + // 转换每个 double 为大端格式并写入缓冲区 + double_to_big_endian(aa1, &bochangxishu[0]); + double_to_big_endian(aa2, &bochangxishu[8]); + double_to_big_endian(aa3, &bochangxishu[16]); + double_to_big_endian(aa4, &bochangxishu[24]); + + // 写入地址 0x58 + IS3::write(0x58, bochangxishu, 32); + + // 读取设备返回数据 + uint8_t data_type; + uint8_t command_data[20]; + + int data_lenth = IS3::read(&data_type, command_data); + + // 检查响应类型是否正确 + if (data_lenth <= 0 || data_type != 0x58) { + return false; + } + IS3::a1 = aa1; + IS3::a2 = aa2; + IS3::a3 = aa3; + IS3::a4 = aa4; + + return true; +} + +// 从大端格式转换为 double(适配 ESP32 小端架构) +double big_endian_to_double(const uint8_t* in) { + uint8_t temp[8]; + for (int i = 0; i < 8; ++i) { + temp[i] = in[7 - i]; // 字节反转 + } + double result; + memcpy(&result, temp, sizeof(double)); + return result; +} + +bool IS3::get_bochangxishu(double *aa1, double *aa2, double *aa3, double *aa4) +{ + if(IS3::read_sta == 1) return false; + uint8_t data = 0; + IS3::write(0x59, &data, 1); + + uint8_t data_type; + uint8_t command_data[32]; + + int data_lenth = IS3::read(&data_type, command_data); + + if(data_lenth <= 0 || data_type != 0x59) { + return false; + } + + // 解码 4 个 double(每个 8 字节,大端) + *aa1 = big_endian_to_double(&command_data[0]); + *aa2 = big_endian_to_double(&command_data[8]); + *aa3 = big_endian_to_double(&command_data[16]); + *aa4 = big_endian_to_double(&command_data[24]); + + // 可选:存到 IS3 的静态成员变量中(如果需要) + IS3::a1 = *aa1; + IS3::a2 = *aa2; + IS3::a3 = *aa3; + IS3::a4 = *aa4; + + return true; +} + +bool IS3::set_data_processing(enum IS3_data_processing IS3_data_processing,uint32_t average_num) +{ + if(IS3::read_sta == 1) return false; + uint8_t data_buff[5]; + switch (IS3_data_processing) + { + case none: + data_buff[0] = 0; + break; + case average: + data_buff[0] = 1; + break; + case sgi: + data_buff[0] = 2; + break; + case average_sgi: + data_buff[0] = 3; + break; + case move_average: + data_buff[0] = 4; + break; + default: + break; + } + // memcpy(data_buff+1,(uint8_t *)&average_num, 5); + + data_buff[1] |= average_num >> 24; + data_buff[2] |= average_num >> 16; + data_buff[3] |= average_num >> 8; + data_buff[4] |= average_num; + + IS3::write(0x60,data_buff, 5); + + uint8_t data_type; + uint8_t command_data[32]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x60) + { + return false; + } + return true; +} + + +bool IS3::get_data(uint8_t *data) +{ + if(IS3::read_sta == 1) return false; + uint8_t b = 0 ; + IS3::write(0x61,&b, 1); + uint8_t data_type; + int data_lenth = IS3::read(&data_type,data); + + if(data_lenth <= 0 || data_type != 0x61) + { + memset(data,0,515*2); + return false; + } + + return true; +} + + +// bool IS3::get_data(uint8_t *data) +// { +// uint8_t b = 0 ; +// unsigned char command_data[1024 * 2]; +// uint16_t data_length = 0; +// int a = is3.integration_time * 3; +// uint8_t data_type; +// IS3::write(0x61,&b, 1); +// while(a--) +// { +// while(Serial1.available()) +// { +// data_length += Serial1.readBytes(&command_data[data_length],1024*2); +// a = 10; +// } +// if(data_length == 1037) break; +// // if(a <= 10 ) a--; +// vTaskDelay(1); +// } + +// data_length = IRIS_Cut_Befor_Header(command_data, data_length); +// int ret = IRIS_STM32_Protocol_Unpack(command_data,data_length,&data_type,data); + +// if (ret < 0) +// { +// return false; +// } + +// if(data_type != 0x61) +// { +// return false; +// } + +// return true; +// } + + + +bool IS3::get_data_processing(uint8_t *is3_processing) +{ + if(IS3::read_sta == 1) return false; + uint8_t b = 0 ; + IS3::write(0x62,&b, 1); + uint8_t data_type; + uint8_t command_data[32]; + + int data_lenth = IS3::read(&data_type,command_data); + if(data_lenth <= 0 || data_type != 0x62) + { + return false; + } + *is3_processing = command_data[0]; + IS3::is3_processing = *is3_processing; + return true; +} diff --git a/src/IS3.h b/src/IS3.h new file mode 100644 index 0000000..fb52d93 --- /dev/null +++ b/src/IS3.h @@ -0,0 +1,57 @@ +#ifndef __IS3_H__ +#define __IS3_H__ +#include "header.h" +#include "driver/uart.h" +class IS3 +{ +private: + /* data */ +public: + IS3(/* args */); + // ~IS3(); + enum IS3_data_processing{ + none = 0, + average, + sgi, + average_sgi, + move_average + }; + + + String info = "IS3"; + uint32_t integration_time,bandnum; + float temperature; + double a1, a2, a3, a4; + uint8_t is3_processing; + volatile uint8_t read_sta; + + void init(); + void on(); + void off(); + void reset(); + + void write(uint8_t command,uint8_t *pData, uint16_t Size); + int read(uint8_t *data_type,uint8_t *read_buf) ; + // int read1(uint8_t *data_type,uint8_t *read_buf); + bool get_is3_info(String *is3_info); + + bool set_shutter_time(uint32_t shutter_time); + bool opt(); + bool get_shutter_time(uint32_t *shutter_time); + bool get_is3_temperature(float *temp); + bool close_shutter(); + bool open_shutter(); + bool get_bandnum(uint32_t *is3_bandnum); + bool set_bochangxishu(double aa1, double aa2, double aa3, double aa4); + bool get_bochangxishu(double *aa1, double *aa2, double *aa3, double *aa4); + bool set_data_processing(enum IS3_data_processing IS3_data_processing,uint32_t average_num); + bool get_data(uint8_t *data); + bool get_data_processing(uint8_t *is3_processing); +}; +extern IS3 is3; + +uint32_t my_usart_available(uart_port_t uart_port); +#endif + + + diff --git a/src/adc.cpp b/src/adc.cpp new file mode 100644 index 0000000..c25b638 --- /dev/null +++ b/src/adc.cpp @@ -0,0 +1,88 @@ +#include "adc.h" +#define ADC_PIN 3 +float voltage_buff[200]; + +void adc_init() { + // pinMode(3,INPUT); + analogReadResolution(12); + voltage_buff[199] = -1; + // analogSetPinAttenuation(ADC_PIN, ADC_11db); +} +float voltage_last1 = 10; +float adc_read() { + uint32_t adcValue = 0;// = analogRead(ADC_PIN); + for(int i = 0; i < 10; i++) + { + adcValue += analogRead(ADC_PIN); + } + adcValue /= 10; + + // float voltage = (float)adcValue * (5.5 / 4095); + + float voltage = ((float)adcValue / 4095.0f * 3.3f) * 1.6667f; + // Serial0.printf("voltage : %f\n", voltage); + // if((voltage < 4.2) && (voltage > 3.8)) play_music("/baojing.mp3"); + // if((voltage < 4.1) && (voltage > 3.8)) xEventGroupSetBits(ui_event_group,POWER_OFF_BIT); + + // if((voltage < 2.0)) voltage = 5.5; + voltage = voltage * 0.1f + 0.9f * voltage_last1; + voltage_last1 = voltage; + return voltage; +} +uint32_t v_count = 0; +float temp_last = 1000; +float voltage_to_percent(float voltage) { + if (voltage >= 5.0f) return 100; + else if (voltage >= 4.7f) return 80 + (voltage - 4.7) / 0.3f * 20; + else if (voltage >= 4.4f) return 10 + (voltage - 4.4) / 0.3f * 70; + else if (voltage >= 4.2f) return (voltage - 4.2f) / 0.3f * 10; + else return 0; +} + + +float get_voltage() +{ + float voltage; + float temp; + float sum_voltage = 0; + voltage = adc_read(); + voltage_buff[v_count] = voltage; + v_count++; + if(v_count >= 200) + { + // Serial0.printf("voltage : %f\n", voltage); + v_count = 0; + } + + if(voltage_buff[199] == -1) + { + // for(int i = 0; i < v_count; i++) + // { + // sum_voltage += voltage_buff[i]; + // } + // sum_voltage /= v_count; + return 100; + } + else + { + for(int i = 0; i < 200; i++) + { + sum_voltage += voltage_buff[i]; + } + sum_voltage /= 200; + } + + // temp = (float)(sum_voltage - 4.2f) / 1.2f * 100; + temp = voltage_to_percent(sum_voltage); + + temp = temp > 100 ? 100 : temp; + + if(temp > temp_last) temp = temp_last; + else temp_last = temp; + + if(temp < 5) temp = 5; + + return temp; +} + + diff --git a/src/adc.h b/src/adc.h new file mode 100644 index 0000000..f6c9d20 --- /dev/null +++ b/src/adc.h @@ -0,0 +1,12 @@ +#ifndef __ADC_H__ +#define __ADC_H__ +#include +#include "header.h" + +void adc_init(); +float adc_read(); +float get_voltage(); + +#endif + + diff --git a/src/button.cpp b/src/button.cpp new file mode 100644 index 0000000..0e80600 --- /dev/null +++ b/src/button.cpp @@ -0,0 +1,158 @@ +#include "button.h" + + +#define TAG "BUTTON_ISR" + +#define WAKEUP_PIN GPIO_NUM_9 + +static EventGroupHandle_t gpio_event_group; + +static void IRAM_ATTR gpio_isr_handler(void* arg) { + uint32_t gpio_num = (uint32_t)arg; + if (gpio_num == BUTTON1_PIN) { + xEventGroupSetBitsFromISR(gpio_event_group, BUTTON1_BIT, NULL); + } else if (gpio_num == BUTTON2_PIN) { + xEventGroupSetBitsFromISR(gpio_event_group, BUTTON2_BIT, NULL); + } +} + + +void button1_task(void* arg) { + static TickType_t last_press_time = 0; + static bool waiting_second_click = false; + while (1) + { + xEventGroupWaitBits(gpio_event_group,BUTTON1_BIT,pdTRUE,pdFALSE,portMAX_DELAY); + vTaskDelay(20); + if(digitalRead(BUTTON1_PIN) != LOW) continue; + + system_time_count = 0; + uint32_t count = 0; + while(digitalRead(BUTTON1_PIN) == LOW) + { + EventBits_t uxBits = xEventGroupGetBits(ui_event_group); + count++; + if (count == 20) + { + if ((uxBits & MAIN_2_OPT_ING_BIT) || (uxBits & MAIN_2_DC_ING_BIT) || (uxBits & MAIN_2_WR_ING_BIT) || (uxBits & MAIN_2_SAVE_ING_BIT)) + { + + } + else + { + play_music("/miaozhuan.mp3"); + } + + } + else if(count > 20) + { + if ((uxBits & MAIN_2_OPT_ING_BIT) || (uxBits & MAIN_2_DC_ING_BIT) || (uxBits & MAIN_2_WR_ING_BIT) || (uxBits & MAIN_2_SAVE_ING_BIT)) + { + + } + else + { + xEventGroupSetBits(is3_event_group, RED_RAY_BIT); + } + + } + vTaskDelay(50); + } + vTaskDelay(150); + if(count < 10) + { + if(digitalRead(BUTTON1_PIN) == LOW) + { + if(digitalRead(BUTTON1_PIN) == LOW) + { + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.button1_count = 200; + xSemaphoreGive(xMutexInventory); + } + } + xEventGroupClearBits(gpio_event_group, BUTTON1_BIT); + } + else + { + waiting_second_click == false; + EventBits_t uxBits = xEventGroupGetBits(ui_event_group); + if ((uxBits & MAIN_2_OPT_ING_BIT) || (uxBits & MAIN_2_DC_ING_BIT) || (uxBits & MAIN_2_WR_ING_BIT) || (uxBits & MAIN_2_SAVE_ING_BIT)) continue; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.button1_count = 100; + xSemaphoreGive(xMutexInventory); + } + } + } + vTaskDelay(10); + xEventGroupClearBits(gpio_event_group, BUTTON1_BIT); + } +} + + +void button2_task(void* arg) { + while (1) + { + xEventGroupWaitBits(gpio_event_group,BUTTON2_BIT,pdTRUE, pdFALSE,portMAX_DELAY); + uint32_t count = 0; + vTaskDelay(20); + if(digitalRead(BUTTON2_PIN) != LOW) continue; + system_time_count = 0; + + // Serial0.println("button2"); + while(digitalRead(BUTTON2_PIN) == LOW) + { + count++; + if(count > 200) + { + // xEventGroupSetBits(ui_event_group, POWER_OFF_BIT); + // tft_clear(TFT_BLACK); + esp_restart(); + } + vTaskDelay(50); + } + if(count < 10) + { + EventBits_t uxBits = xEventGroupGetBits(ui_event_group); + if ((uxBits & MAIN_2_OPT_ING_BIT) || (uxBits & MAIN_2_DC_ING_BIT) || (uxBits & MAIN_2_WR_ING_BIT) || (uxBits & MAIN_2_SAVE_ING_BIT)) continue; + + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.button2_count++; + if(updata_struct_to_ui.mode == 0) + { + if (updata_struct_to_ui.button2_count == 1)updata_struct_to_ui.button2_count = 3; + } + updata_struct_to_ui.button2_count = updata_struct_to_ui.button2_count > 4 ? 0 : updata_struct_to_ui.button2_count; + xSemaphoreGive(xMutexInventory); + } + + } + vTaskDelay(20); + xEventGroupClearBits(gpio_event_group, BUTTON2_BIT); + } + +} + + +void button_init(void) { + gpio_event_group = xEventGroupCreate(); + + gpio_config_t io_conf = { + .pin_bit_mask = (1ULL << BUTTON1_PIN) | (1ULL << BUTTON2_PIN), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_NEGEDGE, // 按键下降沿触发 + }; + gpio_config(&io_conf); + + gpio_install_isr_service(0); + gpio_isr_handler_add(BUTTON1_PIN, gpio_isr_handler, (void*)BUTTON1_PIN); + gpio_isr_handler_add(BUTTON2_PIN, gpio_isr_handler, (void*)BUTTON2_PIN); + + xTaskCreate(button1_task, "button1_task", 1024*1, NULL, 100, NULL); + xTaskCreate(button2_task, "button2_task", 1024*1, NULL, 100, NULL); +} + diff --git a/src/button.h b/src/button.h new file mode 100644 index 0000000..e2c1eb3 --- /dev/null +++ b/src/button.h @@ -0,0 +1,23 @@ +#ifndef __BUTTON_H__ +#define __BUTTON_H__ + +#include "Arduino.h" +#include "header.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "driver/gpio.h" +#include "esp_log.h" + + + +#define BUTTON1_PIN GPIO_NUM_46 // 输出 "hello" +#define BUTTON2_PIN GPIO_NUM_9 // 输出 "nihao" + +#define BUTTON1_BIT (1 << 0) +#define BUTTON2_BIT (1 << 1) +#define clicked_bit (1 << 2) + +void button_init(void); + +#endif \ No newline at end of file diff --git a/src/ds1307.cpp b/src/ds1307.cpp new file mode 100644 index 0000000..df0cea5 --- /dev/null +++ b/src/ds1307.cpp @@ -0,0 +1,77 @@ +#include "ds1307.h" +// #define TOUCH_SDA 1 +// #define TOUCH_SCL 2 +RTC_DS1307 rtc; +char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; +void ds1307_init() +{ + if (!rtc.begin()) + { + // Serial0.println("Couldn't find RTC"); + } + if (!rtc.isrunning()) + { + // Serial0.println("RTC is NOT running, setting the time!"); + // 设置初始时间 + Serial0.println(DateTime(F(__DATE__), F(__TIME__)).timestamp()); + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + } + DateTime now = rtc.now(); + String date = String(now.year(), DEC) + "/" + String(now.month(), DEC) + "/" + String(now.day(), DEC) + " " + + String(now.hour(), DEC) + ":" + String(now.minute(), DEC) + ":" + String(now.second(), DEC); + Serial0.println(date); + + struct tm t; + t.tm_year = now.year() - 1900; + t.tm_mon = now.month() - 1; + t.tm_mday = now.day(); + t.tm_hour = now.hour(); + t.tm_min = now.minute(); + t.tm_sec = now.second(); + + time_t sys_now = mktime(&t); + struct timeval tv = { sys_now, 0 }; + settimeofday(&tv, nullptr); // 设置系统时间 +} + +void ds1307_get_time( ds1307_date *ds1307_date) +{ + // Serial0.println("get time"); + // getLocalTime(&timeinfo);获取系统时间: + uint32_t a; + DateTime now; + if(a % 5 == 0 ) now = rtc.now(); + ds1307_date->year = now.year(); + ds1307_date->month = now.month(); + ds1307_date->day = now.day(); + ds1307_date->hour = now.hour(); + ds1307_date->minute = now.minute(); + ds1307_date->second = now.second(); + a++; + + // Serial0.printf("\n%d/%02d/%02d %02d:%02d;%02d\n",ds1307_date->year,ds1307_date->month ,ds1307_date->day ,ds1307_date->hour ,ds1307_date->minute,ds1307_date->second ); +} + +void ds1307_set_time(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) +{ + rtc.adjust(DateTime(year, month, day, hour, minute, second)); // 设置为 2025年6月27日 14:30:00 + + DateTime now = rtc.now(); + String date = String(now.year(), DEC) + "/" + String(now.month(), DEC) + "/" + String(now.day(), DEC) + " " + + String(now.hour(), DEC) + ":" + String(now.minute(), DEC) + ":" + String(now.second(), DEC); + + // Serial0.println("set time:"); + // Serial0.println(date); + + struct tm t; + t.tm_year = now.year() - 1900; + t.tm_mon = now.month() - 1; // 7月,注意从0开始 + t.tm_mday = now.day(); + t.tm_hour = now.hour(); + t.tm_min = now.minute(); + t.tm_sec = now.second(); + + time_t sys_now = mktime(&t); + struct timeval tv = { sys_now, 0 }; + settimeofday(&tv, nullptr); // 设置系统时间 +} diff --git a/src/ds1307.h b/src/ds1307.h new file mode 100644 index 0000000..d18c7f4 --- /dev/null +++ b/src/ds1307.h @@ -0,0 +1,20 @@ +#ifndef __DS1307_H__ +#define __DS1307_H__ + +#include "RTClib.h" + +typedef struct ds1307_t +{ + uint16_t year; + uint16_t month; + uint16_t day; + uint16_t hour; + uint16_t minute; + uint16_t second; +} ds1307_date; + +void ds1307_init(); +void ds1307_get_time( ds1307_date *ds1307_date); +void ds1307_set_time(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second); + +#endif diff --git a/src/header.h b/src/header.h new file mode 100644 index 0000000..372ba92 --- /dev/null +++ b/src/header.h @@ -0,0 +1,48 @@ +#ifndef __HEADER_H__ +#define __HEADER_H__ +#include "Arduino.h" +#include "IRIS_Method.h" +#include "red_ray.h" +#include "hh3_system.h" +#include "hh3_slave.h" +#include "sd_card.h" +#include "save.h" +#include "speak.h" +#include "ui_task.h" +#include "qmi8658.h" +#include "ds1307.h" +#include "IS3.h" +#include "lcd.h" +#include "my_aht10.h" +#include "usb_camera.h" +#include "button.h" +#include "adc.h" +// #include "hh3_system.h" +// #include "speak.h" +// #include "lcd.h" +// #include "events_init.h" +// #include "gui_guider.h" +// #include "sd_card.h" +// #include "ds1307.h" +// #include "qmi8658.h" +// #include "my_aht10.h" +// #include "usb_camera.h" +// #include "adc.h" +// #include "red_ray.h" +// #include "button.h" + +#include +#include +#include +#include "lvgl.h" +#include "gui_guider.h" +#include "events_init.h" +#include "custom.h" +#include +#include +#include "wifi_client.h" +#include "log.h" + + + +#endif diff --git a/src/hh3_slave.cpp b/src/hh3_slave.cpp new file mode 100644 index 0000000..c666347 --- /dev/null +++ b/src/hh3_slave.cpp @@ -0,0 +1,81 @@ +#include "hh3_slave.h" + + // 发送命令给从机 +void sendCommand(uint8_t cmd) { + Wire.beginTransmission(SLAVE_ADDRESS); + Wire.write(cmd); + Wire.endTransmission(); + // Serial0.print("Sent command: 0x"); + // Serial0.println(cmd, HEX); +} + +bool requestData(hh3_slave_data *sensor_data) { + // 直接请求结构体大小的数据 + Wire.requestFrom(SLAVE_ADDRESS, sizeof(hh3_slave_data)); + + if (Wire.available() == sizeof(hh3_slave_data)) + { + Wire.readBytes((uint8_t*)sensor_data, sizeof(hh3_slave_data)); + return true; + } else { + // Serial0.println("Failed to receive data from slave."); + return false; + } +} +uint8_t height_sta = 1; +float height_last; +void get_hh3_slave_data(hh3_slave_data *data) +{ + requestData(data); + if(height_sta == 1) height_last = data->height; + else data->height = height_last; + // printSensorData(*data); +} + +void open_ceju() +{ + sendCommand(0x05); + height_sta = 1; +} + +void close_ceju() +{ + sendCommand(0x03); + height_sta = 0; +} +void printSensorData(hh3_slave_data sensor_data) { + + // 打印原始 hex 数据 + uint8_t* pData = (uint8_t*)&sensor_data; + Serial0.print("Received Hex Data: "); + for (int i = 0; i < sizeof(hh3_slave_data); i++) { + Serial0.printf("%02X ", pData[i]); + } + Serial0.println(); + + + Serial0.println("=== Sensor Data ==="); + + Serial0.print("GPS Status: "); + switch (sensor_data.gps_sta) { + case 0x00: Serial0.println("No Device"); break; + case 0x01: Serial0.println("Power Off"); break; + case 0x02: Serial0.println("No Signal"); break; + case 0x03: Serial0.println("Valid Signal"); break; + default: Serial0.println("Unknown"); break; + } + + Serial0.print("Height Status: "); + switch (sensor_data.height_sta) { + case 0x00: Serial0.println("No Device"); break; + case 0x01: Serial0.println("Power Off"); break; + case 0x03: Serial0.println("Valid"); break; + default: Serial0.println("Unknown"); break; + } + + Serial0.print("Latitude: "); Serial0.println(sensor_data.gps_lat, 6); + Serial0.print("Longitude: "); Serial0.println(sensor_data.gps_lon, 6); + Serial0.print("Height (m): "); Serial0.println(sensor_data.height, 2); + + Serial0.println("==================="); +} diff --git a/src/hh3_slave.h b/src/hh3_slave.h new file mode 100644 index 0000000..bcc484f --- /dev/null +++ b/src/hh3_slave.h @@ -0,0 +1,62 @@ +#ifndef __HH3_SLAVE_H__ +#define __HH3_SLAVE_H__ + +#include "header.h" +#define SLAVE_ADDRESS 0x55 + +// 命令定义(与从机一致) +#define CMD_MASTER_GET_SLAVE_DATA 0x01 +#define CMD_GPS_POWER_OFF 0x02 +#define CMD_RANGING_POWER_OFF 0x03 +#define CMD_GPS_POWER_ON 0x04 +#define CMD_RANGING_POWER_ON 0x05 + + + +typedef struct +{ + uint8_t gps_sta; + uint8_t height_sta; + float gps_lat; + float gps_lon; + float height; +}hh3_slave_data; + +void hh3_slave_init(void); +// void get_hh3_slave_data(hh3_slave_data * data); + +// void printSensorData(); +// bool requestData(); +// void sendCommand(uint8_t cmd); + + +void get_hh3_slave_data(hh3_slave_data *data); +void sendCommand(uint8_t cmd); +bool requestData(hh3_slave_data *sensor_data) ; +void printSensorData(hh3_slave_data sensor_data); +void close_ceju(); +void open_ceju(); + +// 从机地址 0X55; +// 命令1, 0x01获取数据 + +// 发送: +// 0x55 0x01 +// 回复数据格式: +// typedef struct +// { +// uint8_t gps_sta; //0x00没有设备 , 0x01关机 , 0x02没有搜到gps ,0x03搜到gps +// uint8_t height_sta; //0x00没有设备 , 0x01关机 +// float gps_lat; //没有返回0 +// float gps_lon; //没有返回0 +// float height; //没有返回0 +// }hh3_slave_data; + +// 命令2,0x02关闭GPS电源。 +// 命令3,0x03关闭测距电源。 + +// 命令4,0x04开启GPS电源。 +// 命令5,0x05开启测距电源。 + + +#endif \ No newline at end of file diff --git a/src/hh3_system.cpp b/src/hh3_system.cpp new file mode 100644 index 0000000..0b868ab --- /dev/null +++ b/src/hh3_system.cpp @@ -0,0 +1,81 @@ +#include "hh3_system.h" +#define sys_power_pin 48 +#define LCD_BL_PIN 14 // 背光引脚连接到 GPIO14 +// #define SPK_CTRL 38 +// #define BUTTON1_PIN GPIO_NUM_46 // 输出 "hello" +// #define BUTTON2_PIN GPIO_NUM_9 // 输出 "nihao" + +void hh3_system_init(void) +{ + + + pinMode(sys_power_pin, OUTPUT); + pinMode(sys_power_pin, OUTPUT); + pinMode(SPK_CTRL, OUTPUT); + pinMode(BUTTON1_PIN, INPUT_PULLUP); + pinMode(BUTTON2_PIN, INPUT_PULLUP); + pinMode(17, OUTPUT); + digitalWrite(17 ,LOW);//IS3_RST + + digitalWrite(SPK_CTRL, LOW); + digitalWrite(LCD_BL_PIN, HIGH); + + hh3_system_off(); +} + +void hh3_system_off(void) +{ + + + digitalWrite(sys_power_pin, LOW); + // digitalWrite(LCD_BL_PIN, LOW); +} + +void hh3_system_on(void) +{ + digitalWrite(sys_power_pin, HIGH); + // digitalWrite(LCD_BL_PIN, LOW); +} + +void hh3_system_start(void) +{ + // while(1) + // { + // uint32_t count = 0; + // hh3_system_init(); + // // hh3_system_off(); + // esp_sleep_enable_ext0_wakeup(BUTTON2_PIN, 0); // 低电平唤醒 + // // esp_deep_sleep_start(); + // esp_light_sleep_start(); + // // hh3_system_on(); + // while (digitalRead(BUTTON2_PIN) == 0) + // { + // if(digitalRead(BUTTON1_PIN) == 0) count ++; + // if(count > 300) break; + // vTaskDelay(10); + // } + // if(count > 300) break; + // } + hh3_system_on(); + // delay(20); + lcd_init(); + speak_init(); + // is3.init(); + + audio.connecttoFS(SPIFFS,"/kaiji.mp3" ); + while (audio.isRunning()) + { + audio.loop(); + vTaskDelay(5); + } + + vTaskDelay(500); + audio.connecttoFS(SPIFFS,"/huanyinshiyong.mp3" ); + while (audio.isRunning()) + { + audio.loop(); + vTaskDelay(5); + } +} + + diff --git a/src/hh3_system.h b/src/hh3_system.h new file mode 100644 index 0000000..8109605 --- /dev/null +++ b/src/hh3_system.h @@ -0,0 +1,9 @@ +#ifndef _HH3HH3_SYSTEM_H_ +#define _HHHH3_SYSTEM_H_ +#include "header.h" + +void hh3_system_init(void); +void hh3_system_off(void); +void hh3_system_on(void); +void hh3_system_start(void); +#endif \ No newline at end of file diff --git a/src/lcd.cpp b/src/lcd.cpp new file mode 100644 index 0000000..7a63476 --- /dev/null +++ b/src/lcd.cpp @@ -0,0 +1,185 @@ +#include "lcd.h" + + +#define BL_PIN 14 // 背光引脚连接到 GPIO14 +#define PWM_CHANNEL 0 +#define PWM_FREQ 5000 +#define PWM_RESOLUTION 8 // 分辨率:0~255 + + +#define TOUCH_SDA 1 +#define TOUCH_SCL 2 +#define TOUCH_INT 18 +#define TOUCH_RST 47 +// #define LCD_BL 14 +#define TOUCH_WIDTH 480 +#define TOUCH_HEIGHT 320 + +static const uint16_t screenWidth = 320; +static const uint16_t screenHeight = 480; + +TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */ +TAMC_GT911 touch = TAMC_GT911(TOUCH_SDA, TOUCH_SCL, TOUCH_INT, TOUCH_RST, TOUCH_WIDTH, TOUCH_HEIGHT); + +SemaphoreHandle_t lvgl_mutex; + +static lv_disp_draw_buf_t draw_buf; +lv_color_t *lvgl_buf_1 = NULL; +lv_color_t *lvgl_buf_2 = NULL; + +lv_ui guider_ui; +void my_disp_flush( lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p ) +{ + uint32_t w = ( area->x2 - area->x1 + 1 ); + uint32_t h = ( area->y2 - area->y1 + 1 ); + + tft.startWrite(); + tft.setAddrWindow( area->x1, area->y1, w, h ); + tft.pushColors( ( uint16_t * )&color_p->full, w * h, true ); + tft.endWrite(); + + lv_disp_flush_ready( disp_drv ); +} + +/*Read the touchpad*/ +bool last_touch = false; +void my_touchpad_read( lv_indev_drv_t * indev_drv, lv_indev_data_t * data ) +{ + uint16_t touchX, touchY; + static uint16_t touchX_last, touchY_last; + touch.read(); + bool touched = touch.isTouched; + + touchX = touch.points[0].x; + touchY = touch.points[0].y; + int id = touch.points[0].id; + + + // Serial0 + + if( !touched ) + { + data->state = LV_INDEV_STATE_REL; + } + else + { + if (last_touch == false) + { + data->state = LV_INDEV_STATE_REL; + last_touch=touched; + return; + + /* code */ + } + + + + + // Serial0.printf("X: %d,Y: %d\n",touchX,touchY); + // if(touchX != touchX_last || touchY != touchY_last) + // { + // touchX_last = touchX; + // touchY_last = touchY; + // data->state = LV_INDEV_STATE_REL; + // return; + // } + // Serial0.printf("X: %d,Y: %d\n , id: %d \n",touchX,touchY,id); + data->state = LV_INDEV_STATE_PR; + system_time_count = 0; + data->point.x = touchX; + data->point.y = touchY; + + // digitalWrite(TOUCH_RST, LOW); + // digitalWrite(TOUCH_RST, HIGH); + } + last_touch=touched; +} + +void setBrightness(uint8_t brightness) { + ledcWrite(PWM_CHANNEL, brightness); // 写入 PWM 占空比 +} +void lcd_BLK_int() +{ + // pinMode(TFT_BL, OUTPUT); + // digitalWrite(TFT_BL, HIGH); + + ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION); // 设置PWM通道 + ledcAttachPin(BL_PIN, PWM_CHANNEL); // 绑定GPIO到PWM通道 + + setBrightness(0); // 设置初始亮度(0~255) +} + +void lvgl_loop_task(void *pvParameters) +{ + while(1) + { + // lv_timer_handler(); + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + lv_timer_handler(); + xSemaphoreGive(lvgl_mutex); + } + vTaskDelay(5); + } +} +void lcd_init() { + lvgl_mutex = xSemaphoreCreateMutex(); + + pinMode(TOUCH_RST, OUTPUT); + + // setBrightness(0); + lvgl_buf_1 = (lv_color_t*)heap_caps_malloc(screenWidth * screenHeight*5, MALLOC_CAP_SPIRAM); + lvgl_buf_2 = (lv_color_t*)heap_caps_malloc(screenWidth * screenHeight*5, MALLOC_CAP_SPIRAM); + lv_init(); + lv_disp_draw_buf_init( &draw_buf, lvgl_buf_1, lvgl_buf_2, screenWidth * screenHeight *5 ); + + lcd_BLK_int(); + tft.begin(); + tft.setRotation(1); /* Landscape orientation, flipped */ + tft.fillScreen(TFT_WHITE); + // setBrightness(180); // 设置初始亮度(0~255) + setBrightness(255); + + // vTaskDelay(30); + touch.begin(); + touch.setRotation(2); + + // lvgl_buf_1 = (lv_color_t*) malloc(screenWidth * screenHeight*5); + // lvgl_buf_2 = (lv_color_t*) malloc(screenWidth * screenHeight*5); + + // lvgl_buf_1 = (lv_color_t*)heap_caps_malloc(screenWidth * screenHeight*5, MALLOC_CAP_SPIRAM); + // lvgl_buf_2 = (lv_color_t*)heap_caps_malloc(screenWidth * screenHeight*5, MALLOC_CAP_SPIRAM); + // lv_disp_draw_buf_init( &draw_buf, lvgl_buf_1, lvgl_buf_2, screenWidth * screenHeight *5 ); + + static lv_disp_drv_t disp_drv; + lv_disp_drv_init( &disp_drv ); + /*Change the following line to your display resolution*/ + // disp_drv.hor_res = screenWidth; + // disp_drv.ver_res = screenHeight; + + disp_drv.hor_res = 480; + disp_drv.ver_res = 320; + disp_drv.flush_cb = my_disp_flush; + disp_drv.draw_buf = &draw_buf; + lv_disp_drv_register( &disp_drv ); + + /*Initialize the (dummy) input device driver*/ + static lv_indev_drv_t indev_drv; + lv_indev_drv_init( &indev_drv ); + // indev_drv.long_press_time = 50; // 默认是 500 ms + indev_drv.type = LV_INDEV_TYPE_POINTER; + indev_drv.read_cb = my_touchpad_read; + lv_indev_drv_register( &indev_drv ); + + + setup_ui(&guider_ui); + events_init(&guider_ui); + custom_init(&guider_ui); + + xTaskCreatePinnedToCore(lvgl_loop_task,"lvgl_loop_task", 1024*5 , NULL, 20, NULL, 1); +} + +void tft_clear(uint16_t color) +{ + tft.fillScreen(color); +} \ No newline at end of file diff --git a/src/lcd.h b/src/lcd.h new file mode 100644 index 0000000..b984e29 --- /dev/null +++ b/src/lcd.h @@ -0,0 +1,12 @@ +#ifndef __LCD_H__ +#define __LCD_H__ +#include "header.h" +#include +#include +#include "lvgl.h" +extern SemaphoreHandle_t lvgl_mutex; + +void lcd_init(); +void setBrightness(uint8_t brightness); +void tft_clear(uint16_t color); +#endif \ No newline at end of file diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..e49b13a --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,38 @@ +#include "log.h" + +void write_log(String log_info,unsigned char level) +{ + // if(level == LOG_LEVEL_DEBUG) + // { + // Serial0.println(log_info); + // } + + // ds1307_date date; + // ds1307_get_time(&date); + // String date_info = String(date.year); + // if(date.month < 10) date_info = date_info + "0" + String(date.month); + // else date_info = date_info + String(date.month); + + // if(date.day < 10) date_info = date_info + "0" + String(date.day); + // else date_info = date_info + String(date.day); + + // if(date.hour < 10) date_info = date_info + "0" + String(date.hour); + // else date_info = date_info + String(date.hour); + + // if(date.minute < 10) date_info = date_info + "0" + String(date.minute); + // else date_info = date_info + String(date.minute); + + // if(date.second < 10) date_info = date_info + "0" + String(date.second); + // else date_info = date_info + String(date.second); + + // String log_path = LOG_PATH + "" + ".txt"; + // date_info = "---------------------------" + date_info + "---------------------------"; + // File file; + // file = SD_MMC.open(LOG_PATH,FILE_WRITE); + // file.println(date_info); + // file.println(log_info); + // file.flush(); + // file.close(); +} + + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..b9403b4 --- /dev/null +++ b/src/log.h @@ -0,0 +1,16 @@ +#ifndef LOG_H +#define LOG_H + +#include "header.h" +#define LOG_PATH "/log/" +#define LOG_LEVEL_DEBUG 10 +#define LOG_LEVEL_INFO 20 + + +void write_log(String log_info,unsigned char level); + + + +#endif + + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a86fe11 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,70 @@ +#include +#include "header.h" + +#define ADC_PIN 3 +#define i2c_sda 1 +#define i2c_scl 2 +#define i2c_FREQ_HZ 100000 // 100kHz + + +void setup() +{ + hh3_system_init(); + hh3_system_start(); + + // // // Serial0.begin(115200); + // Serial0.setTimeout(1); + // // Serial0.setRxBufferSize(1024*2); + // // Serial0.setTxBufferSize(1024*2); + + Serial0.begin(921600); + + // Serial0.setTimeout(1); + Serial0.println("============start============="); + + is3.init(); + + + ds1307_init(); + sd_card_init(); + + update_firmware(); + + + system_data_init(); + Wire.begin(i2c_sda, i2c_scl,i2c_FREQ_HZ); + + + open_ceju(); + + // wifi_init(); + + + qmi8658_init(); + aht10_init(); + adc_init(); + red_ray_init(); + + ui_task_init(); + button_init(); + USB_CAM_Init(); + + + // write_log("system start", LOG_LEVEL_INFO); +} +void loop() +{ + // lv_timer_handler(); /* let the GUI do its work */ + vTaskDelay(portMAX_DELAY); +} + + +// while (1) { +// sendCommand(0x03); + +// delay(100); +// sendCommand(0x05); + +// delay(100); +// Serial0.println("start"); +// } \ No newline at end of file diff --git a/src/my_aht10.cpp b/src/my_aht10.cpp new file mode 100644 index 0000000..b94dfd3 --- /dev/null +++ b/src/my_aht10.cpp @@ -0,0 +1,47 @@ +#include "my_aht10.h" + + +uint8_t readStatus = 0; + +AHT10 myAHT10(AHT10_ADDRESS_0X38); + + +bool aht10_init() +{ + bool sta = myAHT10.begin(); + uint8_t count = 0; + while (!sta) + { + sta = myAHT10.begin(); + if (count > 10) + { + + return false; + } + // Serial0.println("AHT10 init fail"); + count++; + vTaskDelay(10); + } + // Serial0.println("AHT10 init success"); + return true; +} + +void get_aht10_data() +{ + + Serial0.print("Temperature: "); + Serial0.print(myAHT10.readTemperature()); + Serial0.print(" C"); + Serial0.print("Humidity: "); + Serial0.print(myAHT10.readHumidity()); + Serial0.println(" %"); + +} + + +void get_aht10_data(float *temperature, float *humidity) +{ + *temperature = myAHT10.readTemperature(); + *humidity = myAHT10.readHumidity(); +} + diff --git a/src/my_aht10.h b/src/my_aht10.h new file mode 100644 index 0000000..0171f70 --- /dev/null +++ b/src/my_aht10.h @@ -0,0 +1,10 @@ +#ifndef __MY_AHT10_H__ +#define __MY_AHT10_H__ +#include +#include +#include +bool aht10_init(); +void get_aht10_data(); +void get_aht10_data(float *temperature, float *humidity); +#endif + diff --git a/src/qmi8658.cpp b/src/qmi8658.cpp new file mode 100644 index 0000000..3685e4c --- /dev/null +++ b/src/qmi8658.cpp @@ -0,0 +1,127 @@ +#include "qmi8658.h" + +// 读取QMI8658寄存器的值 +esp_err_t qmi8658_register_read(uint8_t reg_addr, uint8_t *data, size_t len) +{ + return i2c_master_write_read_device(0, QMI8658_SENSOR_ADDR, ®_addr, 1, data, len, 1000 / portTICK_PERIOD_MS); +} + +// 给QMI8658的寄存器写值 +esp_err_t qmi8658_register_write_byte(uint8_t reg_addr, uint8_t data) +{ + uint8_t write_buf[2] = {reg_addr, data}; + + return i2c_master_write_to_device(0, QMI8658_SENSOR_ADDR, write_buf, sizeof(write_buf), 1000 / portTICK_PERIOD_MS); +} + +// 初始化qmi8658 +esp_err_t qmi8658_init(void) +{ + // Serial0.println("11111111 OK!"); // 打印信息 + esp_err_t ret = ESP_OK; + uint8_t id = 0; // 芯片的ID号 + uint8_t count = 0; + + qmi8658_register_read(QMI8658_WHO_AM_I, &id ,1); // 读芯片的ID号 + while (id != 0x05) // 判断读到的ID号是否是0x05 + { + vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100豪秒 + qmi8658_register_read(QMI8658_WHO_AM_I, &id ,1); // 读取ID号 + count++; + if (count >= 5){ + ret = ESP_FAIL; + return ret; + } + // Serial0.print("."); // 打印信息 + } + // Serial0.println("QMI8658 OK!"); // 打印信息 + + qmi8658_register_write_byte(QMI8658_RESET, 0xb0); // 复位 + vTaskDelay(10 / portTICK_PERIOD_MS); // 延时10ms + + // 配置运动状态检测 + qmi8658_register_write_byte(QMI8658_CATL1_L, 1); // AnyMotionXThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL1_H, 1); // AnyMotionYThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL2_L, 1); // AnyMotionZThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL2_H, 1); // NoMotionXThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL3_L, 1); // NoMotionYThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL3_H, 1); // NoMotionZThr 必须是0~32之间的数 + qmi8658_register_write_byte(QMI8658_CATL4_L, 0x77); // MOTION_MODE_CTRL 0111 0111 + qmi8658_register_write_byte(QMI8658_CATL4_H, 0x01); // 0x01(means 1st command) + qmi8658_register_write_byte(QMI8658_CTRL9, 0x0E); // CTRL_CMD_CONFIGURE_MOTION + + qmi8658_register_write_byte(QMI8658_CATL1_L, 1); // AnyMotionWindow + qmi8658_register_write_byte(QMI8658_CATL1_H, 1); // NoMotionWindow + qmi8658_register_write_byte(QMI8658_CATL2_L, 0xE8); // SigMotionWaitWindow[7:0] + qmi8658_register_write_byte(QMI8658_CATL2_H, 0x03); // SigMotionWaitWindow [15:8] + qmi8658_register_write_byte(QMI8658_CATL3_L, 0xE8); // SigMotionConfirmWindow[7:0] + qmi8658_register_write_byte(QMI8658_CATL3_H, 0x03); // SigMotionConfirmWindow[15:8] + // qmi8658_register_write_byte(QMI8658_CATL4_L, 0x40); // NA + qmi8658_register_write_byte(QMI8658_CATL4_H, 0x02); // 0x02(means 2nd command) + qmi8658_register_write_byte(QMI8658_CTRL9, 0x0E); // CTRL_CMD_CONFIGURE_MOTION + + + qmi8658_register_write_byte(QMI8658_CTRL1, 0x40); // CTRL1 设置地址自动增加 + qmi8658_register_write_byte(QMI8658_CTRL7, 0x03); // CTRL7 允许加速度和陀螺仪 + qmi8658_register_write_byte(QMI8658_CTRL2, 0x95); // CTRL2 设置ACC 4g 250Hz + qmi8658_register_write_byte(QMI8658_CTRL3, 0xd5); // CTRL3 设置GRY 512dps 250Hz + + qmi8658_register_write_byte(QMI8658_CTRL8, 0x0E); // CTRL7 允许Any-Motion No-Motion and Significant-Motion + + return ret; +} + +// 关闭芯片运行 +void qmi8658_close(void) +{ + qmi8658_register_write_byte(QMI8658_CTRL1, 0x01); // 关闭芯片运行 +} + +// 读取加速度和陀螺仪寄存器值 +void qmi8658_Read_AccAndGry(t_sQMI8658 *p) +{ + uint8_t status, data_ready=0; + int16_t buf[6]; + + qmi8658_register_read(QMI8658_STATUS0, &status, 1); // 读状态寄存器 + if (status & 0x03) // 判断加速度和陀螺仪数据是否可读 + data_ready = 1; + if (data_ready == 1){ // 如果数据可读 + data_ready = 0; + qmi8658_register_read(QMI8658_AX_L, (uint8_t *)buf, 12); // 读加速度和陀螺仪值 + p->acc_x = buf[0]; + p->acc_y = buf[1]; + p->acc_z = buf[2]; + p->gyr_x = buf[3]; + p->gyr_y = buf[4]; + p->gyr_z = buf[5]; + } +} + +// 获取XYZ轴的倾角值 +void qmi8658_fetch_angleFromAcc(t_sQMI8658 *p) +{ + float temp; + + qmi8658_Read_AccAndGry(p); // 读取加速度和陀螺仪的寄存器值 + // 根据寄存器值 计算倾角值 并把弧度转换成角度 + temp = (float)p->acc_x / sqrt( ((float)p->acc_y * (float)p->acc_y + (float)p->acc_z * (float)p->acc_z) ); + p->AngleX = atan(temp)*57.29578f; // 180/π=57.29578 + temp = (float)p->acc_y / sqrt( ((float)p->acc_x * (float)p->acc_x + (float)p->acc_z * (float)p->acc_z) ); + p->AngleY = atan(temp)*57.29578f; // 180/π=57.29578 + temp = sqrt( ((float)p->acc_x * (float)p->acc_x + (float)p->acc_y * (float)p->acc_y) ) / (float)p->acc_z; + p->AngleZ = atan(temp)*57.29578f; // 180/π=57.29578 + + // Serial0.printf("X:%.1f , Y:%.2f , Z:%.2f\n",p->AngleX,p->AngleY,p->AngleZ); +} + +// 获取Motion状态 +uint8_t qmi8658_fetch_motion(void) +{ + uint8_t status = 0; + qmi8658_register_read(QMI8658_STATUS1, &status, 1); // 读状态寄存器 + return status; +} + +/*************************** 姿态传感器 QMI8658 ↑ ****************************/ +/*******************************************************************************/ \ No newline at end of file diff --git a/src/qmi8658.h b/src/qmi8658.h new file mode 100644 index 0000000..468eb16 --- /dev/null +++ b/src/qmi8658.h @@ -0,0 +1,97 @@ +#ifndef __QMI8658_H__ +#define __QMI8658_H__ + +#include "Arduino.h" +#include "Wire.h" +#include "driver/i2c.h" +#define QMI8658_SENSOR_ADDR 0x6A // QMI8658 I2C地址 + +// QMI8658寄存器地址 +enum qmi8658_reg +{ + QMI8658_WHO_AM_I, + QMI8658_REVISION_ID, + QMI8658_CTRL1, + QMI8658_CTRL2, + QMI8658_CTRL3, + QMI8658_CTRL4, + QMI8658_CTRL5, + QMI8658_CTRL6, + QMI8658_CTRL7, + QMI8658_CTRL8, + QMI8658_CTRL9, + QMI8658_CATL1_L, + QMI8658_CATL1_H, + QMI8658_CATL2_L, + QMI8658_CATL2_H, + QMI8658_CATL3_L, + QMI8658_CATL3_H, + QMI8658_CATL4_L, + QMI8658_CATL4_H, + QMI8658_FIFO_WTM_TH, + QMI8658_FIFO_CTRL, + QMI8658_FIFO_SMPL_CNT, + QMI8658_FIFO_STATUS, + QMI8658_FIFO_DATA, + QMI8658_STATUSINT = 45, + QMI8658_STATUS0, + QMI8658_STATUS1, + QMI8658_TIMESTAMP_LOW, + QMI8658_TIMESTAMP_MID, + QMI8658_TIMESTAMP_HIGH, + QMI8658_TEMP_L, + QMI8658_TEMP_H, + QMI8658_AX_L, + QMI8658_AX_H, + QMI8658_AY_L, + QMI8658_AY_H, + QMI8658_AZ_L, + QMI8658_AZ_H, + QMI8658_GX_L, + QMI8658_GX_H, + QMI8658_GY_L, + QMI8658_GY_H, + QMI8658_GZ_L, + QMI8658_GZ_H, + QMI8658_COD_STATUS = 70, + QMI8658_dQW_L = 73, + QMI8658_dQW_H, + QMI8658_dQX_L, + QMI8658_dQX_H, + QMI8658_dQY_L, + QMI8658_dQY_H, + QMI8658_dQZ_L, + QMI8658_dQZ_H, + QMI8658_dVX_L, + QMI8658_dVX_H, + QMI8658_dVY_L, + QMI8658_dVY_H, + QMI8658_dVZ_L, + QMI8658_dVZ_H, + QMI8658_TAP_STATUS = 89, + QMI8658_STEP_CNT_LOW, + QMI8658_STEP_CNT_MIDL, + QMI8658_STEP_CNT_HIGH, + QMI8658_RESET = 96 +}; + +// 倾角结构体 +typedef struct{ + int16_t acc_x; + int16_t acc_y; + int16_t acc_z; + int16_t gyr_x; + int16_t gyr_y; + int16_t gyr_z; + float AngleX; + float AngleY; + float AngleZ; +}t_sQMI8658; + +esp_err_t qmi8658_init(void); // QMI8658初始化 +void qmi8658_close(void); // 关闭芯片运行 +void qmi8658_fetch_angleFromAcc(t_sQMI8658 *p); // 获取倾角 +uint8_t qmi8658_fetch_motion(void); // 获取运动状态 + + +#endif \ No newline at end of file diff --git a/src/red_ray.cpp b/src/red_ray.cpp new file mode 100644 index 0000000..0d00254 --- /dev/null +++ b/src/red_ray.cpp @@ -0,0 +1,14 @@ +#include "red_ray.h" +#define RED_RAY_PIN 8 +void red_ray_init() { + pinMode(RED_RAY_PIN, OUTPUT); + red_ray_off(); +} + +void red_ray_on() { + digitalWrite(RED_RAY_PIN, HIGH); +} + +void red_ray_off() { + digitalWrite(RED_RAY_PIN, LOW); +} diff --git a/src/red_ray.h b/src/red_ray.h new file mode 100644 index 0000000..89ae6c7 --- /dev/null +++ b/src/red_ray.h @@ -0,0 +1,9 @@ +#ifndef __RED_RAY_H__ +#define __RED_RAY_H__ +#include "Arduino.h" + +void red_ray_init(); +void red_ray_on(); +void red_ray_off(); + +#endif \ No newline at end of file diff --git a/src/save.h b/src/save.h new file mode 100644 index 0000000..dbf886b --- /dev/null +++ b/src/save.h @@ -0,0 +1,171 @@ +#ifndef __SAVE_H__ +#define __SAVE_H__ + +#include "Arduino.h" + +#pragma pack(1) +#define SpectralData 0x00ff00ff + +#define SpectralInfo 0xff00ff00 + +#define Other 0xf0f0f0f0 + +#define Image 0x0f0f0f0f + + +// #define SpectralData_Squantity 0x0003 +#define SpectralInfo_Squantity 0x0001 +// #define Other_Squantity 0x0000 +#define Image_Squantity 0x0001 + +#define hh3_DCFiberID 0 +#define hh3_FiberID 1 +#define hh3_FlatFiberID 2 + +#define s_dn 0 +#define rad 1 +#define ref 2 +#define irad 3 +#define califile 4 +#define flat_ref 5 +#define dark_dn 6 +#define flat_dn 7 + +#define DATA_TYPE_UINT8 0x10 +#define DATA_TYPE_INT16 0x11 +#define DATA_TYPE_UINT16 0x12 +#define DATA_TYPE_INT32 0x13 +#define DATA_TYPE_UINT32 0x14 +#define DATA_TYPE_FLOAT32 0x20 +#define DATA_TYPE_FLOAT64 0x21 + +typedef struct +{ + int8_t timezone; // 时区 + uint16_t year; // 年 + uint8_t month; // 月 + uint8_t day; // 日 + uint8_t hour; // 时 + uint8_t minute; // 分 + uint8_t second; // 秒 + uint16_t millisecond; // 毫秒 +}IRIS_Time_Struct; + + +typedef struct { + uint64_t DataLength; // 图像数据长度 + char Name[100]; // 图像名称 + IRIS_Time_Struct CollectionTime; // 采集时间 + uint8_t Type; // 图像类型 + uint8_t *ImageDataAddress; // 图像数据地址 (pointer to data) +} One_Image_Info_Struct; + +typedef struct { + char name[100]; // basename_number_type字符串 \0结尾 + char sensor_id[50]; // 传感器ID \0结束符 + uint8_t FiberID; // 光纤ID + IRIS_Time_Struct date; // 时间结构体 + double integration_time; //积分时间 + float gain; //填0 + uint8_t data_type; //数据类型 0x10 uint8_t ,0x11 int16_t, 0x12 uint16_t, 0x13 int32_t, 0x14 uint32_t, 0x20 float, 0x21 double + uint8_t PixelSize; //数据类型长度 1,2,4,8 + uint8_t GroundType; //地物类别 0 dn 1 rad 2 ref 3 irad 4 califile 5 flat_ref 6 dark_dn 6 flat_dn + uint16_t band_num; //波段数 + uint8_t ValidFlag; //数据是否有效 + uint16_t data[515]; //数据 +} HH3_data_struct; + +typedef struct { + char Name[100]; // 光谱数据名称 + char SensorId[50]; // 传感器ID + uint8_t FiberID; // 光纤ID + IRIS_Time_Struct CollectionTime; // 采集时间 + double Exposure; // 曝光时间 + float Gain; // 增益 + uint8_t DataType; // 数据类型 + uint8_t PixelSize; // 像素大小 + uint8_t GroundType; // 地面类型 + uint16_t Bands; // 波段数 + uint8_t ValidFlag; // 有效标志 + uint8_t *SpectralDataAddress; // 光谱数据地址 (pointer to data) +} One_Spectral_Data_Struct; + + + +typedef struct{ + uint32_t SectionFlag; + uint64_t SectionLength; + uint16_t Squantity; +}SpectralData_Section_Header; + +typedef struct{ + uint32_t SectionFlag; + uint64_t SectionLength; + + uint16_t Squantity; + uint16_t info1_length; + uint8_t info1_type; +}SpectralInfo_Section_Header; + +typedef struct{ + uint32_t SectionFlag; + uint64_t SectionLength; + // uint16_t Squantity; +}Other__Section_Header; + +typedef struct{ + uint16_t Squantity; + uint64_t length; + uint8_t name[100]; + IRIS_Time_Struct date; // 时间结构体 + uint8_t type; + uint8_t *data; +}picture; + +typedef struct{ + uint32_t SectionFlag; + uint64_t SectionLength; + + + // uint32_t length; + // uint64_t name; + // IRIS_Time_Struct date; // 时间结构体 + // uint32_t type; +}Image_Section_Header; + +typedef struct{ + char name[100]; // basename_number_type字符串 \0结尾 + char sensor_id[50]; // 传感器ID \0结束符 + uint8_t FiberID; // 光纤ID + IRIS_Time_Struct date; // 时间结构体 + double integration_time; //积分时间 + float gain; //填0 + uint8_t data_type; //数据类型 0x10 uint8_t ,0x11 int16_t, 0x12 uint16_t, 0x13 int32_t, 0x14 uint32_t, 0x20 float, 0x21 double + uint8_t PixelSize; //数据类型长度 1,2,4,8 + uint8_t GroundType; //地物类别 0 dn 1 rad 2 ref 3 irad 4 califile 5 flat_ref 6 dark_dn 6 flat_dn + uint16_t band_num; //波段数 + uint8_t ValidFlag; //数据是否有效 + double data[515]; //数据 //LJ 20250513 512--->515 +}califlie_data_struct; + +typedef struct{ + SpectralData_Section_Header SpectralData_Header; + califlie_data_struct gian; + califlie_data_struct califlie_dn; + califlie_data_struct califlie_lampvalue; +}HH3califlie; + + +typedef struct{ + One_Spectral_Data_Struct *s_dn_data; + One_Spectral_Data_Struct *wr_dn_data; + One_Spectral_Data_Struct *dark_dn_data; + One_Spectral_Data_Struct *flat_ref_data; + One_Spectral_Data_Struct *califile_data; + uint8_t *data_info; + picture *image_info; +} read_file_point; + + +#pragma pack() +#endif diff --git a/src/sd_card.cpp b/src/sd_card.cpp new file mode 100644 index 0000000..c579769 --- /dev/null +++ b/src/sd_card.cpp @@ -0,0 +1,198 @@ + +#include "sd_card.h" + +#define sd_cmd 7 +#define sd_clk 6 +#define sd_dat0 5 +#define sd_dat1 4 +#define sd_dat2 16 +#define sd_dat3 15 + +uint32_t used_percent; + +bool sd_card_status = true; +// bool sd_card_init() +// { + +// SD_MMC.end(); +// SD_MMC.setPins(sd_clk,sd_cmd,sd_dat0,sd_dat1,sd_dat2,sd_dat3); +// int succ = SD_MMC.begin("/sdcard", false, false, 10); +// if (succ) +// { +// // Serial0.println("SD_MMC Mount successful"); +// uint8_t cardType = SD_MMC.cardType(); + +// if (cardType == CARD_MMC) { + +// } else if (cardType == CARD_SD) { +// } else if (cardType == CARD_SDHC) { +// } else { +// } + +// uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024); +// // uint32_t c = cardSize; +// // Serial0.printf("SD_MMC Card Size: %lluMB\n", cardSize); + +// // float a = SD_MMC.totalBytes() / (1024 * 1024); +// // Serial0.printf("Total space: %lluMB\n", a); +// // float b = SD_MMC.usedBytes() / (1024 * 1024); +// // Serial0.printf("Used space: %lluMB\n", b); +// // used_percent = b/a*100; +// } else { + +// Serial0.println("SD_MMC Mount failed"); +// sd_card_status = false; +// return false; +// } +// sd_card_status = true; +// SD_MMC.mkdir("/data"); +// SD_MMC.mkdir("/log"); +// SD_MMC.mkdir("/system"); +// // sd_card_status = true; +// // sd_card_status = sd_card_status & SD_MMC.mkdir("/data"); +// // sd_card_status = sd_card_status & SD_MMC.mkdir("/log"); +// // sd_card_status = sd_card_status & SD_MMC.mkdir("/system"); + +// // if(sd_card_status == false) +// // { +// // Serial0.println(" sdcard_mkdir_fail "); +// // } + + +// return sd_card_status; +// } + + +bool sd_card_init() +{ + + SD_MMC.end(); + SD_MMC.setPins(sd_clk,sd_cmd,sd_dat0,sd_dat1,sd_dat2,sd_dat3); + + uint16_t i = 1; + while(i) + { + if(SD_MMC.begin("/sdcard", false, false,SDMMC_FREQ_HIGHSPEED, 10)) + { + Serial0.println("SD_MMC begin success"); + uint8_t cardType = SD_MMC.cardType(); + if (cardType == CARD_MMC) { + } else if (cardType == CARD_SD) { + } else if (cardType == CARD_SDHC) { + } else { + } + + uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024); + break; + } + + if(i >= 10) + { + Serial0.println("SD_MMC begin fail"); + sd_card_status = false; + return false; + } + i++; + vTaskDelay(20); + } + + + sd_card_status = true; + SD_MMC.mkdir("/data"); + SD_MMC.mkdir("/log"); + SD_MMC.mkdir("/system"); + + return sd_card_status; +} + +// bool get_sd_card_status() +// { +// // if(SD_MMC.totalBytes()/(1024 * 1024) < 100) sd_card_status = false; + +// return sd_card_status; +// } + +bool get_sd_card_status() { + // SD.cardType() 返回 0 表示没有检测到卡 + if (SD_MMC.cardType() == CARD_NONE) { + sd_card_status = false; + } else { + sd_card_status = true; + } + return sd_card_status; +} + + uint32_t i = 0; +float get_sd_used_percent() +{ + + if(sd_card_status == false ) return 0; + float used; + if((i % (60 * 5 * 5))== 0) + { + used = ceil((float)SD_MMC.usedBytes() / (float)SD_MMC.totalBytes() * 100.f); + i = 0; + } + + i++; + used = used > 1 ? used : 1; + return used; +} + + + +#define OTA_BUF_SIZE 1024 +#include "esp_ota_ops.h" + +void sdcard_ota_update(void) +{ + FILE *f = fopen("/sdcard/config/firmware/firmware.bin", FILE_READ); + if (!f) { + ESP_LOGE("OTA", "Failed to open firmware"); + return; + } + + const esp_partition_t *update_partition = + esp_ota_get_next_update_partition(NULL); + + esp_ota_handle_t ota_handle; + esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &ota_handle); + + uint8_t buf[OTA_BUF_SIZE]; + int len; + while ((len = fread(buf, 1, OTA_BUF_SIZE, f)) > 0) { + esp_ota_write(ota_handle, buf, len); + // vTaskDelay(1); + } + fclose(f); + + + esp_ota_end(ota_handle); + + esp_ota_set_boot_partition(update_partition); + + ESP_LOGI("OTA", "Update success, rebooting..."); + esp_restart(); +} +void update_firmware() +{ + if(get_sd_card_status() == false) return; + + String version_now = String(Version_3); + + String version_new; + File file = SD_MMC.open("/config/firmware/Version.txt",FILE_READ); + if(file) + { + version_new = file.readString(); + Serial0.println(version_new); + file.close(); + if(version_new != version_now) + { + + Serial0.println("start update"); + + sdcard_ota_update(); + } + } +} \ No newline at end of file diff --git a/src/sd_card.h b/src/sd_card.h new file mode 100644 index 0000000..37d68be --- /dev/null +++ b/src/sd_card.h @@ -0,0 +1,15 @@ +#ifndef __SD_CARD_H__ +#define __SD_CARD_H__ + +#include +// #include +#include "FS.h" +#include +#include "header.h" + +bool sd_card_init(); +bool get_sd_card_status(); +float get_sd_used_percent(); +void update_firmware(); +#endif + diff --git a/src/speak.cpp b/src/speak.cpp new file mode 100644 index 0000000..a667c91 --- /dev/null +++ b/src/speak.cpp @@ -0,0 +1,26 @@ +#include "speak.h" + +Audio audio; +void speak_init() +{ + audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); + audio.setVolume(21); // 0...21 + + pinMode(SPK_CTRL, OUTPUT); + digitalWrite(SPK_CTRL, HIGH); + if (!SPIFFS.begin(true)) { + Serial0.println("SPIFFS mount failed!"); + return; + } +} + +void speak_on() +{ + digitalWrite(SPK_CTRL, HIGH); +} + + +void speak_off() +{ + digitalWrite(SPK_CTRL, LOW); +} \ No newline at end of file diff --git a/src/speak.h b/src/speak.h new file mode 100644 index 0000000..bce5604 --- /dev/null +++ b/src/speak.h @@ -0,0 +1,15 @@ +#ifndef __SPEAK_H__ +#define __SPEAK_H__ + +#define I2S_DOUT 45 +#define I2S_BCLK 39 +#define I2S_LRC 40 +#define SPK_CTRL 38 + +#include "Audio.h" +extern Audio audio; +void speak_init(); +void speak_on(); +void speak_off(); +#endif + diff --git a/src/ui_task.cpp b/src/ui_task.cpp new file mode 100644 index 0000000..7e89954 --- /dev/null +++ b/src/ui_task.cpp @@ -0,0 +1,3226 @@ +#include "ui_task.h" + +updata updata_struct_to_ui; + +// u_int32_t timeOut = 10000; + +IS3 is3; + +// uint16_t is3_data[515]; +// uint16_t is3_dark_data[515]; + +typedef struct { + uint16_t s_data[515]; + uint16_t dark_data[515]; + uint16_t wr_data[515]; + uint16_t s_average_data[515]; + // uint16_t opt_data[515]; + // uint16_t plot_data[515]; +} is3_data_t; + +is3_data_t is3_data; + +EventGroupHandle_t is3_event_group; + + + +DynamicJsonDocument califile_doc(1024); +DynamicJsonDocument SpectralInfo_doc(1024); +DynamicJsonDocument sys_doc(1024); +DynamicJsonDocument file_doc(1024); +String SpectralInfo_data_str; +IRIS_Time_Struct hh3_time_structure; + + +SpectralData_Section_Header SpectralData_header; +SpectralInfo_Section_Header SpectralInfo_header; +Other__Section_Header Other_header; +Image_Section_Header Image_header; +picture image_picture; +HH3_data_struct hh3_data_s_dn; +HH3_data_struct hh3_data_dark_dn; +HH3_data_struct hh3_data_wr_dn; +califlie_data_struct hh3_data_flat_ref; + +HH3califlie hh3_califlie_struct; +HH3califlie hh3_flatfile_struct; +SemaphoreHandle_t xMutexInventory = NULL; //创建信号量Handler +EventGroupHandle_t ui_event_group; +EventGroupHandle_t ui_event_group2; +uint32_t dc_wr_time = 0; +uint32_t system_time_count = 0; +uint32_t califile_Count = 0; + +uint8_t *file_data = NULL; +const uint32_t file_heap_size = 60 * 1024; +uint32_t stop_polt = 0; +// 任务句柄声明 +TaskHandle_t updata_to_ui_TaskHandle; +TaskHandle_t get_is3_TaskHandle; +TaskHandle_t main_2_plot_TaskHandle; +TaskHandle_t main_2_opt_TaskHandle; +TaskHandle_t main_2_dc_TaskHandle; +TaskHandle_t main_2_save_TaskHandle; +TaskHandle_t main_2_wr_TaskHandle; +TaskHandle_t set_it_TaskHandle; +TaskHandle_t main_2_rad_TaskHandle; +TaskHandle_t red_ray_TaskHandle; +TaskHandle_t power_off_TaskHandle; +TaskHandle_t play_music_TaskHandle; +TaskHandle_t fiber_sel_TaskHandle; +TaskHandle_t calculate_average_TaskHandle; +TaskHandle_t ui_time_TaskHandle; +TaskHandle_t show_data_TaskHandle; +TaskHandle_t set_system_time_TaskHandle; +TaskHandle_t save_path_TaskHandle; + +volatile uint32_t pc_sta = 0; + +void ui_task_init(void) +{ + xTaskCreatePinnedToCore(updata_to_ui, "updata", 1024*(3 ), NULL, 21, &updata_to_ui_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_opt, "opt_task", 1024*(4 ), NULL, 25, &main_2_opt_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_plot, "plot_task", 1024*(2 ), NULL, 25, &main_2_plot_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_dc, "dc_task", 1024*(4 ), NULL, 25, &main_2_dc_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_save, "save_task", 1024*(6 +2), NULL, 25, &main_2_save_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_wr, "wr_task", 1024*(4 ), NULL, 25, &main_2_wr_TaskHandle, 1); + xTaskCreatePinnedToCore(get_is3_data, "get_is3_task", 1024*(6 ), NULL, 30, &get_is3_TaskHandle, 1); + xTaskCreatePinnedToCore(set_it_task, "set_it_task", 1024*(4 ), NULL, 25, &set_it_TaskHandle, 1); + xTaskCreatePinnedToCore(main_2_rad, "set_polt_type", 1024*(2 ), NULL, 21, &main_2_rad_TaskHandle, 1); + xTaskCreatePinnedToCore(red_ray_task, "red_ray_task", 1024*(1), NULL, 21, &red_ray_TaskHandle, 1); + xTaskCreatePinnedToCore(power_off_task, "power_off_task", 1024*(1 ), NULL, 21, &power_off_TaskHandle, 1); + xTaskCreatePinnedToCore(play_music_task, "play_music_task", 1024*(4 ), NULL, 29, &play_music_TaskHandle, 1); + xTaskCreatePinnedToCore(fiber_sel_task, "fiber_sel_task", 1024*(8), NULL, 21, &fiber_sel_TaskHandle, 1); + xTaskCreatePinnedToCore(calculate_average_task, "average_task", 1024*(4 ), NULL, 28, &calculate_average_TaskHandle, 1); + xTaskCreatePinnedToCore(ui_time_task, "ui_time_task", 1024*(1 ), NULL, 21, &ui_time_TaskHandle, 1); + xTaskCreatePinnedToCore(show_data_task, "show_data_task", 1024*(8 ), NULL, 21, &show_data_TaskHandle, 1); + xTaskCreatePinnedToCore(set_system_time_task, "set_time_task", 1024*(2 ), NULL, 21, &set_system_time_TaskHandle, 1); + xTaskCreatePinnedToCore(save_path_task, "save_path_task", 1024*(4 ), NULL, 21, &save_path_TaskHandle, 1); + + xTaskCreatePinnedToCore(pc_mode_task, "pc_mode_task", 1024*8, NULL, 21, NULL, 1); + xTaskCreatePinnedToCore(flat_task, "flat_task", 1024*8, NULL, 21, NULL, 1); +} + +// 挂起所有任务 +void suspend_all_tasks(void) +{ + // xEventGroupWaitBits(is3_event_group, IS3_UPDATA_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupWaitBits(is3_event_group, IS3_UPDATA_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // vTaskSuspend(updata_to_ui_TaskHandle); + + + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // vTaskSuspend(get_is3_TaskHandle); + + pc_sta = 1; +} +// 恢复所有任务 +void resume_all_tasks(void) +{ + // vTaskResume(updata_to_ui_TaskHandle); + // vTaskResume(get_is3_TaskHandle); + pc_sta = 0; +} + + +void get_is3_data(void *pvParameters) +{ + uint16_t read_buff[515]; + uint32_t it = 15; + uint32_t a; + double a1, a2, a3, a4; + a1 = is3.a1; + a2 = is3.a2; + a3 = is3.a3; + a4 = is3.a4; + // Serial0.printf("a1:%f,a2:%f,a3:%f,a4:%f\n", a1, a2, a3, a4); + while (1) + { + while (pc_sta == 1) + { + vTaskDelay(100); + } + bool suc = is3.get_data((uint8_t *) read_buff); + if(suc == false) + { + // Serial0.println("get_is3_data fail"); + // vTaskDelay(10); + vTaskDelay(it*2); + a++; + // if(a > 20) + // { + // is3.reset(); + // // vTaskDelay(2000); + // } + continue; + } + + // Serial0.println("get_is3_data ok"); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + + //反 + // int n = 0 ; + // for (int i = 512; i >= 0; i--) + // { + // // double x= a1*i*i*i + a2*i*i + a3*i + a4; + // is3_data.s_data[n] = read_buff[i]; + // n++; + // } + + // 正 + for (int i = 0; i < 515; i++) + { + is3_data.s_data[i] = read_buff[i]; + } + // is3_data.s_data[513] = read_buff[513]; + // is3_data.s_data[514] = read_buff[514]; + it = is3.integration_time; + xSemaphoreGive(xMutexInventory); + } + + // a++; + // Serial0.println("get_is3_data ook"); + + // // float is3_temperature; + // // is3.get_is3_temperature(&is3_temperature); + + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE0_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE1_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE2_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE3_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE4_BIT); + xEventGroupSetBits(is3_event_group, IS3_GET_DATA_DONE5_BIT); + // Serial0.println("get_is3_data done"); + vTaskDelay(it/2 + 4); + } +} + +void calculate_average_task(void *pvParameters) +{ + + uint16_t average_buffer[515]; + uint32_t S_count= 0; + while (1) + { + uint32_t a=0; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + S_count = updata_struct_to_ui.S_count; + xSemaphoreGive(xMutexInventory); + } + + while(S_count--) + { + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(uint16_t i = 0; i < 515; i++) + { + average_buffer[i] = (average_buffer[i] * a + is3_data.s_data[i])/(a+1); + } + xSemaphoreGive(xMutexInventory); + } + a++; + } + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(uint16_t i = 0; i < 512; i++) + { + is3_data.s_average_data[i] = average_buffer[i]; + } + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(is3_event_group, IS3_AVERAGE_DONE1_BIT); + xEventGroupSetBits(is3_event_group, IS3_AVERAGE_DONE2_BIT); + } +} + +void updata_to_ui(void *pvParameters) +{ + uint32_t aa = 0; + while (1) + { + while (pc_sta == 1) + { + vTaskDelay(100); + } + + + uint32_t Brightness,volume,last_Brightness,last_volume; + uint8_t system_save = 0; + uint8_t sd_sta = 0; + // bool wifi_sw; + // bool bluetooth_sw; + bool screen_sw; + uint16_t screen_keep_time; + float temperature, humidity ,voltage; + ds1307_date ds1307_date; + t_sQMI8658 qmi8658_data; + hh3_slave_data slave_data = {0,0,0,0,0}; + if(get_sd_card_status() == false) sd_sta = 0; + else sd_sta = 1; + get_aht10_data(&temperature, &humidity); + get_hh3_slave_data(&slave_data); + + qmi8658_fetch_angleFromAcc(&qmi8658_data); // 获取倾角 + ds1307_get_time(&ds1307_date); + float sd_used = get_sd_used_percent(); + voltage = get_voltage(); + if(slave_data.gps_sta != 3) + { + slave_data.gps_lat = 39.56; + slave_data.gps_lon = 116.28; + } + if(slave_data.height_sta != 3) + { + slave_data.height = 0; + } + // Serial0.printf("gps_sta:%d,gps_lat:%f,gps_lon:%f\n", slave_data.gps_sta, slave_data.gps_lat,slave_data.gps_lon); + // Serial0.printf("height_sta:%d,height:%f\n", slave_data.height_sta, slave_data.height); + // height_sta + String system_info; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.sd_used_precent = sd_used; + + // updata_struct_to_ui.W_count = 10; + // updata_struct_to_ui.D_count = 10; + // updata_struct_to_ui.S_count = 10; + + + if(updata_struct_to_ui.mode == 0) dc_wr_time = 0; + updata_struct_to_ui.dc_wr_time = dc_wr_time; + + updata_struct_to_ui.height_sta = slave_data.height_sta; + updata_struct_to_ui.gps_sta = slave_data.gps_sta; + updata_struct_to_ui.gps_lat = slave_data.gps_lat; + updata_struct_to_ui.gps_lon = slave_data.gps_lon; + + updata_struct_to_ui.voltage = voltage; + + updata_struct_to_ui.temperature = temperature; + updata_struct_to_ui.humidity = humidity; + + // strcpy(updata_struct_to_ui.save_path, "test"); + // updata_struct_to_ui.save_num = 10; + + updata_struct_to_ui.height = slave_data.height; + + updata_struct_to_ui.integration_time = is3.integration_time; + + updata_struct_to_ui.AngleX = qmi8658_data.AngleX; + updata_struct_to_ui.AngleY = qmi8658_data.AngleY; + updata_struct_to_ui.AngleZ = qmi8658_data.AngleZ; + + updata_struct_to_ui.year = ds1307_date.year; + updata_struct_to_ui.month = ds1307_date.month; + updata_struct_to_ui.day = ds1307_date.day; + updata_struct_to_ui.hour = ds1307_date.hour; + updata_struct_to_ui.minute = ds1307_date.minute; + updata_struct_to_ui.second = ds1307_date.second; + + hh3_time_structure.timezone = 0; + hh3_time_structure.year = ds1307_date.year; + hh3_time_structure.month = ds1307_date.month; + hh3_time_structure.day = ds1307_date.day; + hh3_time_structure.hour = ds1307_date.hour; + hh3_time_structure.minute = ds1307_date.minute; + hh3_time_structure.second = ds1307_date.second; + hh3_time_structure.millisecond = 0; + + updata_struct_to_ui.sd_alert = sd_sta; + + String rtcdate_now = String(ds1307_date.year) + "-" + String(ds1307_date.month) + "-" + String(ds1307_date.day) + " " + String(ds1307_date.hour) + ":" + String(ds1307_date.minute) + ":" + String(ds1307_date.second); + SpectralInfo_doc["info_type"] = "infolist"; + SpectralInfo_doc["info_number"] = 2; + JsonArray info_list = SpectralInfo_doc.createNestedArray("info_list"); + // 第一个info对象 - devinfo + JsonObject info0 = info_list.createNestedObject(); + info0["info_type"] = "devinfo"; + info0["sensor_id"] = is3.info; + info0["bandnum"] = is3.bandnum; + info0["IT"] = is3.integration_time; + + JsonObject wave_coeff = info0.createNestedObject("wave_coeff"); + wave_coeff["a1"] = is3.a1; + wave_coeff["a2"] = is3.a2; + wave_coeff["a3"] = is3.a3; + wave_coeff["a4"] = is3.a4; + info0["termperature"] = is3.temperature; + + + // 第二个info对象 - environment + JsonObject info1 = info_list.createNestedObject(); + info1["info_type"] = "environment"; + info1["MN"] = "IS3-HH"; + info1["SN"] = SN_2; + info1["version"] = Version_3; + info1["date"] = rtcdate_now; + info1["humidity"] = humidity; + info1["temperature"] = temperature; + info1["height"] = slave_data.height; + info1["voltage"] = voltage; + + info1["ForeOptics"] = updata_struct_to_ui.fiber_sel_type; + info1["FiberLength"] = updata_struct_to_ui.fiber_sel_length; + info1["FiberWidth"] = updata_struct_to_ui.fiber_sel_width; + info1["cailifilePath"] = updata_struct_to_ui.fiber_path; + info1["flatfile_name"] = sys_doc["flatfile_name"]; + + info1["save_count"] = updata_struct_to_ui.save_count; + info1["save_interval"] = updata_struct_to_ui.save_time; + info1["W_count"] = updata_struct_to_ui.W_count; + info1["D_count"] = updata_struct_to_ui.D_count; + info1["S_count"] = updata_struct_to_ui.S_count; + + // info1["mode"] = updata_struct_to_ui.mode; + JsonObject gps = info1.createNestedObject("gps"); + gps["status"] = slave_data.gps_sta; + gps["longitude"] = slave_data.gps_lon; + gps["latitude"] = slave_data.gps_lat; + + + SpectralInfo_data_str.clear(); + serializeJson(SpectralInfo_doc, SpectralInfo_data_str); + SpectralInfo_doc.clear(); + //////////////////////////////////////////////// + Brightness = updata_struct_to_ui.brightness_value; + volume = updata_struct_to_ui.volume_value; + + + /////////////////////////sys_doc///////////////////////////// + sys_doc["save_data_path"] = updata_struct_to_ui.save_path; + + sys_doc["W_count"] = updata_struct_to_ui.W_count; + sys_doc["D_count"] = updata_struct_to_ui.D_count; + sys_doc["S_count"] = updata_struct_to_ui.S_count; + + sys_doc["save_count"] = updata_struct_to_ui.save_count; + sys_doc["save_time"] = updata_struct_to_ui.save_time ; + sys_doc["save_data_num"] = updata_struct_to_ui.save_num; + + sys_doc["califile_num"] = updata_struct_to_ui.califile_num; + + sys_doc["brightness_value"] = updata_struct_to_ui.brightness_value; + sys_doc["volume_value"] = updata_struct_to_ui.volume_value; + + sys_doc["ForeOptics_sel1"] = updata_struct_to_ui.ForeOptics_sel1 ; + sys_doc["ForeOptics_sel2"] = updata_struct_to_ui.ForeOptics_sel2 ; + sys_doc["ForeOptics_sel3"] = updata_struct_to_ui.ForeOptics_sel3 ; + + sys_doc["wifi_ssid"] = updata_struct_to_ui.wifi_ssid; + sys_doc["wifi_pwd"] = updata_struct_to_ui.wifi_pwd; + sys_doc["wifi_sw"] = updata_struct_to_ui.wifi_sw; + sys_doc["bluetooth_sw"] = updata_struct_to_ui.bluetooth_sw; + + sys_doc["screen_keep_time"] = updata_struct_to_ui.screen_keep_time ; + sys_doc["screen_sw"] = updata_struct_to_ui.screen_sw; + sys_doc["set_save_num"] = updata_struct_to_ui.set_save_num; + + + + system_save = updata_struct_to_ui.system_save; + //////////////////// + screen_keep_time = updata_struct_to_ui.screen_keep_time; + screen_sw = updata_struct_to_ui.screen_sw; + + if(screen_sw == false) system_time_count = 0; + + if(system_time_count < 60*screen_keep_time) Brightness = Brightness; + else if(system_time_count > 60*(screen_keep_time + 1)) Brightness = 0; + else Brightness = 50; + + + xSemaphoreGive(xMutexInventory); + } + serializeJson(sys_doc, system_info); + if(system_save) save_system_info(system_info); + + if(last_Brightness != Brightness) + { + setBrightness(Brightness); + last_Brightness = Brightness; + } + + if(last_volume != volume) + { + last_volume = volume; + audio.setVolume(volume); + play_music("/OK2.mp3"); + } + + + if(aa%200 == 0) + { + // write_log(system_info, LOG_LEVEL_INFO); + // write_log(SpectralInfo_data_str, LOG_LEVEL_INFO); + } + if(voltage < 4.2) + { + if(aa%20 == 0) + { + // play_music("meidian.mp3"); + } + } + + aa++; + xEventGroupSetBits(is3_event_group, IS3_UPDATA_BIT); + vTaskDelay(100 / portTICK_PERIOD_MS); + } +} + +void main_2_plot(void *pvParameters) +{ + uint32_t a; + while (1) + { + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + if( stop_polt ) continue; + + uint32_t plot_type,it_time; + uint16_t max = 0; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.saturation_alert = 0; + for (uint16_t i = 0; i < 512; i++) + { + // updata_struct_to_ui.plot_data[i] = is3_data.s_data[i]; + updata_struct_to_ui.plot_data[i] = is3_data.s_average_data[i]; + max = max < updata_struct_to_ui.plot_data[i] ? updata_struct_to_ui.plot_data[i] : max; + } + + if(max > 65535 * 0.99) + { + updata_struct_to_ui.saturation_alert = 1; + } + + plot_type = updata_struct_to_ui.plot_type; + it_time = is3.integration_time; + xSemaphoreGive(xMutexInventory); + } + if(max > 65535 * 0.99) play_music("/baojing.mp3"); + //0 dn 1 rad 2 ref + switch (plot_type) + { + case 0: + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(uint16_t i = 0; i < 512; i++) + { + updata_struct_to_ui.plot_data[i] = updata_struct_to_ui.plot_data[i] - hh3_data_dark_dn.data[i]; + } + xSemaphoreGive(xMutexInventory); + } + } + break; + case 1: + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(uint16_t i = 0; i < 512; i++) + { + updata_struct_to_ui.plot_data[i] = (double)(updata_struct_to_ui.plot_data[i] - hh3_data_dark_dn.data[i])/ (double)it_time *hh3_califlie_struct.gian.integration_time *(hh3_califlie_struct.gian.data[i] * 1000000); + } + xSemaphoreGive(xMutexInventory); + } + } + break; + case 2: + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(uint16_t i = 0; i < 512; i++) + { + + updata_struct_to_ui.plot_data[i] = (float)(updata_struct_to_ui.plot_data[i] - hh3_data_dark_dn.data[i]) / (float)(hh3_data_wr_dn.data[i] - hh3_data_dark_dn.data[i]) *hh3_data_flat_ref.data[i]* 10000; + } + xSemaphoreGive(xMutexInventory); + } + } + break; + default: + break; + } + xEventGroupSetBits(ui_event_group, MAIN_2_PLOT_BIT); + } +} + +void main_2_opt(void *pvParameters) +{ + while (1) + { + + xEventGroupWaitBits(ui_event_group, MAIN_2_OPT_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupSetBits(ui_event_group, MAIN_2_OPT_ING_BIT); + + play_music("/baoguang.mp3"); + // write_log("start opt", LOG_LEVEL_INFO); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.bar_value = 0; + updata_struct_to_ui.button2_count = 0; + xSemaphoreGive(xMutexInventory); + } + uint32_t n = 100; + bool suc; + + do + { + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + suc = is3.set_shutter_time(12); + } while (!suc); + + xEventGroupClearBits(is3_event_group, IS3_GET_DATA_DONE1_BIT); + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // is3.reset(); + // xEventGroupClearBits(is3_event_group, IS3_GET_DATA_DONE1_BIT); + // do + // { + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // suc = is3.set_shutter_time(12); + // } while (!suc); + + + + uint16_t set_shutter_time = is3.integration_time; + while(n -- ) + { + uint16_t max = 0; + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // vTaskSuspend(get_is3_TaskHandle); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 515; i++) + { + max = max > is3_data.s_data[i] ? max : is3_data.s_data[i]; + } + xSemaphoreGive(xMutexInventory); + } + + if ((max > 65535 * 0.7) && (max < 65535 * 0.8)) + { + // Serial0.println("max > 65535 * 0.7"); + break; + } + + if(max < ( 65535 * 0.8)) + { + float bbb = (65535.f*0.8) / (float)max; + + + if(bbb > 6) set_shutter_time = set_shutter_time * 4; + else if(bbb > 3) set_shutter_time = set_shutter_time * 2; + else if(bbb > 2) set_shutter_time = set_shutter_time * 1.5; + else if(bbb > 1.4) set_shutter_time = set_shutter_time * 1.3; + else set_shutter_time = set_shutter_time * 1.2 + 1; + } + else + { + // set_shutter_time = set_shutter_time / 2; + float bbb = (65535.f*0.8) / (float)max; + set_shutter_time = set_shutter_time * bbb - 1; + + } + + + do + { + if(set_shutter_time > 1100) + { + set_shutter_time = 1100; + } + if(set_shutter_time < 12) + { + set_shutter_time = 12; + } + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE1_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + suc = is3.set_shutter_time(set_shutter_time); + } while (!suc); + + if(set_shutter_time == 1100 || set_shutter_time == 12) break; + } + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + system_time_count = 0; + updata_struct_to_ui.bar_value = 40; + updata_struct_to_ui.mode = 1; + updata_struct_to_ui.plot_type = 0; + xSemaphoreGive(xMutexInventory); + } + + stop_polt = 1; + dark_dn_collect(); + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + stop_polt = 0; + play_music("/OK2.mp3"); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.bar_value = 100; + xSemaphoreGive(xMutexInventory); + } + // write_log("end opt", LOG_LEVEL_INFO); + xEventGroupClearBits(ui_event_group, MAIN_2_OPT_ING_BIT); + // xEventGroupSetBits(ui_event_group, MAIN_2_DC_BIT); + } +} + + +void main_2_wr(void *pvParameters) +{ + while (1) + { + //wr + xEventGroupWaitBits(ui_event_group, MAIN_2_WR_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupSetBits(ui_event_group, MAIN_2_WR_ING_BIT); + + play_music("/cankao.mp3"); + // write_log("start wr", LOG_LEVEL_INFO); + vTaskDelay(1000); + // UBaseType_t watermark = uxTaskGetStackHighWaterMark(NULL); + // Serial0.printf("updata_to_ui watermark: %u bytes\n", watermark); + // Serial0.printf( "Free DMA heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_DMA)); + uint16_t n; + float a,b; + a = 0; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.button2_count = 1; + updata_struct_to_ui.bar_value = 0; + n = updata_struct_to_ui.W_count; + xSemaphoreGive(xMutexInventory); + } + b = n; + while (n--) + { + //1更新polt + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + //2暗电流采集 + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 515; i++) + { + /* code */ + is3_data.wr_data[i] =((float)is3_data.wr_data[i]*(float)a + (float)is3_data.s_data[i])/(float)(a+1); + } + updata_struct_to_ui.bar_value = a/(b)*45; + xSemaphoreGive(xMutexInventory); + } + a++; + } + wr_dn_pack(false); + + stop_polt = 1; + dark_dn_collect(); + + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + stop_polt = 0; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + system_time_count = 0; + if(updata_struct_to_ui.mode == 1) updata_struct_to_ui.mode = 2; + updata_struct_to_ui.plot_type = 2; + + updata_struct_to_ui.bar_value = 100; + xSemaphoreGive(xMutexInventory); + } + play_music("/OK2.mp3"); + // write_log("end wr", LOG_LEVEL_INFO); + xEventGroupClearBits(ui_event_group, MAIN_2_WR_BIT); + xEventGroupClearBits(ui_event_group, MAIN_2_WR_ING_BIT); + // xEventGroupSetBits(ui_event_group, MAIN_2_DC_BIT); + } +} + +void save_path_task(void *pvParameters) +{ + String data_path = "/data"; + while(1) + { + xEventGroupWaitBits(ui_event_group, MAIN_2_SAVE_PATH_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + uint32_t save_time; + uint32_t save_num; + String save_str,SpectralInfo_str; + uint16_t year,month,day; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + save_time = updata_struct_to_ui.save_time; + save_str = updata_struct_to_ui.save_path; + save_num = updata_struct_to_ui.save_num; + + year = updata_struct_to_ui.year; + month = updata_struct_to_ui.month; + day = updata_struct_to_ui.day; + + xSemaphoreGive(xMutexInventory); + } + // String date_now = String(year) + "_" + String(month) + "_" + String(day); + String date_now = String(year) + "_"; + if(month < 10) date_now = date_now + "0" + String(month) + "_"; + else date_now = date_now + String(month) + "_"; + + if(day < 10) date_now = date_now + "0" + String(day); + else date_now = date_now + String(day); + + String save_path = data_path+ "/" + date_now ; + // SD_MMC.mkdir(save_path);////// + save_path = save_path + "/"; + + // String file_name = save_str + String(save_num) + ".iris"; + char file_name_buf[128]; + String file_name; + + sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + file_name = String(file_name_buf); + + while (SD_MMC.exists(save_path + file_name)) + { + save_num++; + sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + file_name = String(file_name_buf); + } + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.save_num = save_num; + xSemaphoreGive(xMutexInventory); + } + char file_num_str[32]; + snprintf(file_num_str, sizeof(file_num_str), "%04u", save_num); + lv_textarea_set_text(guider_ui.Setting_filename_ta_2, file_num_str); + } +} + +void main_2_save(void *pvParameters) +{ + uint32_t save_num = 0; + String data_path = "/data"; + while (1) + { + //save + xEventGroupWaitBits(ui_event_group, MAIN_2_SAVE_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + USB_CAM_SetResolution(800,480); + if(get_sd_card_status() == false) + { + continue; + } + xEventGroupSetBits(ui_event_group,MAIN_2_SAVE_ING_BIT); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.button2_count = 2; + updata_struct_to_ui.bar_value = 0; + image_picture.length = 0; + xSemaphoreGive(xMutexInventory); + } + close_ceju(); + + play_music("/baocun.mp3"); + // write_log("start save", LOG_LEVEL_INFO); + //打开相机 + + + vTaskDelay(100); + + // if(image_picture.length != 0) + // { + // // play_music("/baojing.mp3"); + // vTaskDelay(5 * 1000); + // } + + uint16_t save_count; + uint32_t save_time; + uint32_t save_num; + uint8_t mode; + String save_str,SpectralInfo_str; + uint16_t year,month,day; + // hour,minute,second; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + + updata_struct_to_ui.button2_count = 2; + + save_count = updata_struct_to_ui.save_count; + save_time = updata_struct_to_ui.save_time; + save_str = updata_struct_to_ui.save_path; + save_num = updata_struct_to_ui.save_num; + mode = updata_struct_to_ui.mode; + year = updata_struct_to_ui.year; + month = updata_struct_to_ui.month; + day = updata_struct_to_ui.day; + // hour = updata_struct_to_ui.hour; + // minute = updata_struct_to_ui.minute; + // second = updata_struct_to_ui.second; + SpectralInfo_str = SpectralInfo_data_str; + xSemaphoreGive(xMutexInventory); + } + + String date_now = String(year) + "_"; + if(month < 10) date_now += "0" + String(month) + "_"; + else date_now += String(month) + "_"; + + if(day < 10) date_now += "0" + String(day); + else date_now += String(day); + + + String save_path = data_path + "/" + date_now ; + // SD_MMC.mkdir(save_path); + if(SD_MMC.exists(save_path) == false) + { + SD_MMC.mkdir(save_path); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + save_num = updata_struct_to_ui.set_save_num; + updata_struct_to_ui.save_num = save_num; + xSemaphoreGive(xMutexInventory); + } + } + + save_path = save_path + "/"; + + char file_name_buf[128]; + String file_name; + + sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + file_name = String(file_name_buf); + + while (SD_MMC.exists(save_path + file_name)) + { + save_num++; + sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + file_name = String(file_name_buf); + } + + float a=0,b; + b=save_count; + xEventGroupClearBits(is3_event_group, IS3_AVERAGE_DONE2_BIT); + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + while(save_count --) + { + xEventGroupWaitBits(is3_event_group, IS3_AVERAGE_DONE2_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupClearBits(is3_event_group, IS3_CAM_DONE_BIT); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + // system_time_count = 0; + updata_struct_to_ui.save_num = save_num; + SpectralInfo_str = SpectralInfo_data_str; + updata_struct_to_ui.system_save = 1; + image_picture.length = 0; + image_picture.Squantity = 0; + + xSemaphoreGive(xMutexInventory); + } + + // char file_name_buf[64]; + sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + + file_name = String(file_name_buf); + // file_name = save_str + String(save_num) + ".iris"; + // Serial0.println(String(save_count)); + // Serial0.println("save_path : " + save_path + file_name); + header_pack(SpectralInfo_str); + // Serial0.println("header_pack"); + Ground_DN_pack(); + // Serial0.println("Ground_DN_pack"); + wr_dn_pack(true); + // Serial0.println("wr_dn_pack"); + dark_dn_pack(true); + // Serial0.println("dark_dn_pack"); + flat_ref_pack(); + // Serial0.println("flat_ref_pack"); + picture_pack(save_str,String(save_num)); + // Serial0.println("picture_pack"); + save_data(save_path,file_name,SpectralInfo_str); + // Serial0.println("save_data"); + a++; + save_num++; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.save_num = save_num; + updata_struct_to_ui.bar_value = a/(b) * 90; + + xSemaphoreGive(xMutexInventory); + } + uint32_t t = save_time * 1000; + if(save_count != 0) vTaskDelay(t); + + // write_log("end save", LOG_LEVEL_INFO); + xEventGroupClearBits(is3_event_group, IS3_AVERAGE_DONE2_BIT); + } + read_data_from_file(save_path + file_name); + + USB_CAM_SetResolution(480,320); + play_music("/OK2.mp3"); + open_ceju(); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.system_save = 1; + updata_struct_to_ui.bar_value = 100; + xSemaphoreGive(xMutexInventory); + } + xEventGroupClearBits(ui_event_group, MAIN_2_SAVE_BIT); + xEventGroupClearBits(ui_event_group, MAIN_2_SAVE_ING_BIT); + } +} + +void dark_dn_collect() +{ + bool shutter_flag = false; + xEventGroupClearBits(is3_event_group, IS3_GET_DATA_DONE4_BIT); + do + { + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // Serial0.println("dark_dn_collect"); + shutter_flag = is3.close_shutter(); + } while (!shutter_flag); + vTaskDelay(500); + // xEventGroupClearBits(is3_event_group, IS3_GET_DATA_DONE4_BIT); + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + + uint16_t n; + float a = 0; + float bar ; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + n = updata_struct_to_ui.D_count; + bar = updata_struct_to_ui.bar_value; + xSemaphoreGive(xMutexInventory); + } + float b = n; + while (n--) + { + //1更新polt + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + //2暗电流采集 + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 515; i++) + { + /* code */ + is3_data.dark_data[i] = (is3_data.dark_data[i]*a + is3_data.s_data[i])/(a+1); + } + a++; + + if(bar > 30) updata_struct_to_ui.bar_value = a/b *45 + 45; + else + { + updata_struct_to_ui.bar_value = a/b *90; + } + xSemaphoreGive(xMutexInventory); + } + // Serial0.printf("dark n : %d\n",n); + } + + dark_dn_pack(false); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + system_time_count = 0; + if(updata_struct_to_ui.mode == 0) updata_struct_to_ui.mode = 1; + dc_wr_time = 0; + xSemaphoreGive(xMutexInventory); + } + + // vTaskSuspend(get_is3_TaskHandle); + xEventGroupClearBits(is3_event_group, IS3_GET_DATA_DONE4_BIT); + do + { + xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE4_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + shutter_flag = is3.open_shutter(); + } while (!shutter_flag); + + // vTaskResume(main_2_plot_TaskHandle); +} + +void main_2_dc(void *pvParameters) +{ + + while(1) + { + //dc + + xEventGroupWaitBits(ui_event_group, MAIN_2_DC_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + xEventGroupSetBits(ui_event_group, MAIN_2_DC_ING_BIT); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.bar_value = 0; + xSemaphoreGive(xMutexInventory); + } + play_music("/andianliu.mp3"); + // write_log("start dc ", LOG_LEVEL_INFO); + // vTaskSuspend(get_is3_TaskHandle); + + dark_dn_collect(); + + + // vTaskDelay(2000 / portTICK_PERIOD_MS); + vTaskDelay(1000); + play_music("/OK2.mp3"); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.button2_count = 4; + updata_struct_to_ui.bar_value = 100; + xSemaphoreGive(xMutexInventory); + } + // write_log("end dc ", LOG_LEVEL_INFO); + + xEventGroupClearBits(ui_event_group, MAIN_2_DC_BIT); + xEventGroupClearBits(ui_event_group, MAIN_2_DC_ING_BIT); + + } +} + + +void power_off_task(void *pvParameters) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group,POWER_OFF_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // write_log("power", LOG_LEVEL_INFO); + vTaskDelay(3000); + // xEventGroupWaitBits(is3_event_group,IS3_GET_DATA_DONE5_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + // xEventGroupWaitBits(ui_event_group,TOP_UPDATA_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + vTaskSuspend(updata_to_ui_TaskHandle); + // vTaskSuspend(get_is3_TaskHandle); + SD_MMC.end(); + esp_restart(); + } +} + + +void set_it_task(void *pvParameters) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group, SET_INTEGRATION_TIME_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + + uint32_t shutter_time ; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + shutter_time = updata_struct_to_ui.set_integration_time; + xSemaphoreGive(xMutexInventory); + } + bool suc; + + // vTaskSuspend(get_is3_TaskHandle); + do + { + // xEventGroupWaitBits(is3_event_group, IS3_GET_DATA_DONE5_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + suc = is3.set_shutter_time(shutter_time); + vTaskDelay(1); + } while (!suc); + // vTaskResume(get_is3_TaskHandle); + + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.mode = 0; + updata_struct_to_ui.button2_count = 0; + updata_struct_to_ui.plot_type = 0; + xSemaphoreGive(xMutexInventory); + } + play_music("/OK2.mp3"); + // write_log("set_it : " + String(shutter_time), LOG_LEVEL_INFO); + // xEventGroupSetBits(ui_event_group, MAIN_2_DC_BIT); + } +} + +void main_2_rad(void *pvParameters) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group,MAIN_2_RAD_BIT,pdTRUE,pdFALSE,portMAX_DELAY); + + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.button2_count = 3; + updata_struct_to_ui.plot_type ++; + updata_struct_to_ui.plot_type = updata_struct_to_ui.plot_type > 2 ? 0 : updata_struct_to_ui.plot_type; + + switch (updata_struct_to_ui.mode) + { + case 0: + updata_struct_to_ui.plot_type = 0; + break; + case 1: + updata_struct_to_ui.plot_type = updata_struct_to_ui.plot_type > 1 ? 0 : updata_struct_to_ui.plot_type; + if(updata_struct_to_ui.califile_num == 0 && updata_struct_to_ui.plot_type == 1) updata_struct_to_ui.plot_type = 0; + break; + case 2: + updata_struct_to_ui.plot_type = updata_struct_to_ui.plot_type > 2 ? 0 : updata_struct_to_ui.plot_type; + if(updata_struct_to_ui.califile_num == 0 && updata_struct_to_ui.plot_type == 1) updata_struct_to_ui.plot_type = 2; + break; + + default: + break; + } + xSemaphoreGive(xMutexInventory); + } + } +} + + +void red_ray_task(void* arg) { + while (1) + { + xEventGroupWaitBits(is3_event_group,RED_RAY_BIT,pdTRUE, pdFALSE,portMAX_DELAY); + EventBits_t uxBits = xEventGroupGetBits(ui_event_group); + if ((uxBits & MAIN_2_OPT_ING_BIT) || (uxBits & MAIN_2_DC_ING_BIT) || (uxBits & MAIN_2_WR_ING_BIT) || (uxBits & MAIN_2_SAVE_ING_BIT)) continue; + red_ray_on(); + vTaskDelay(1000); + red_ray_off(); + } +} + + +void play_music_task(void* arg) +{ + while (1) + { + xEventGroupWaitBits(ui_event_group,MUSIC_BIT,pdTRUE,pdFALSE,portMAX_DELAY); + + String music_path; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + music_path = updata_struct_to_ui.music_path; + memset(updata_struct_to_ui.music_path,'\0',sizeof(updata_struct_to_ui.music_path)); + xSemaphoreGive(xMutexInventory); + } + + const char* path = music_path.c_str(); + // Serial0.println(path); + if(language == chinese) audio.connecttoFS(SPIFFS, path); + else if(language == english) + { + music_path = "/config/music/english/" + music_path; + const char* path_2 = music_path.c_str(); + audio.connecttoFS(SD_MMC, path_2); + } + + + while (audio.isRunning()) + { + audio.loop(); + vTaskDelay(1); + } + } + +} + +bool is_leap_year(uint32_t year) { + return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)); +} +void set_system_time_task(void *pvParameters) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group,SET_SYSTEM_TIME_BIT,pdTRUE,pdFALSE,portMAX_DELAY); + bool flag = true; + + uint16_t year,month,day,hour,minute,second; + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + const char * year_str = lv_textarea_get_text(guider_ui.Setting_system_ta_3); + year = (uint32_t)atoi(year_str); + const char * month_str = lv_textarea_get_text(guider_ui.Setting_system_ta_5); + month = (uint32_t)atoi(month_str); + const char * day_str = lv_textarea_get_text(guider_ui.Setting_system_ta_4); + day = (uint32_t)atoi(day_str); + const char * hour_str = lv_textarea_get_text(guider_ui.Setting_system_ta_7); + hour = (uint32_t)atoi(hour_str); + const char * minute_str = lv_textarea_get_text(guider_ui.Setting_system_ta_6); + minute = (uint32_t)atoi(minute_str); + const char * second_str = lv_textarea_get_text(guider_ui.Setting_system_ta_8); + second = (uint32_t)atoi(second_str); + + xSemaphoreGive(lvgl_mutex); + } + + if(year < 2000 || year > 2099) flag = false;; + if(month < 1 || month > 12) flag = false; + if(day < 1 || day > 31) flag = false; + if(hour < 0 || hour > 23) flag = false; + if(minute < 0 || minute > 59) flag = false; + if(second < 0 || second > 59) flag = false; + + const uint8_t days_in_month[12] = { + 31, 28, 31, 30, 31, 30, + 31, 31, 30, 31, 30, 31 + }; + + + uint8_t max_day = days_in_month[month - 1]; + + if(month == 2 && is_leap_year(year)) { + max_day = 29; + } + + if(day < 1 || day > max_day) flag = false; + + + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.set_time_flag = flag; + xSemaphoreGive(xMutexInventory); + } + if(!flag) continue; + + + ds1307_set_time(year, month, day, hour, minute, second); + } +} + + +void fiber_sel_task(void* arg) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group,FiBER_SEL_BIT , pdTRUE, pdFALSE, portMAX_DELAY); + String fiber_type , fiber_length, fiber_width; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + fiber_type = updata_struct_to_ui.fiber_sel_type; + fiber_length = updata_struct_to_ui.fiber_sel_length; + fiber_width = updata_struct_to_ui.fiber_sel_width; + xSemaphoreGive(xMutexInventory); + } + uint32_t a = find_califile(fiber_type, fiber_length, fiber_width); + califile_read(a); + } +} + + + +void ui_time_task(void *pvParameters) +{ + while (1) + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + dc_wr_time ++; + if(updata_struct_to_ui.mode == 0) dc_wr_time = 0; + system_time_count ++; + xSemaphoreGive(xMutexInventory); + } + vTaskDelay(1000); + } + +} + +void save_system_info(String system_info) +{ + // if(!get_sd_card_status()) return; + + File file; + file = SPIFFS.open("/system_tem.json", FILE_WRITE); + // file = SD_MMC.open(system_tem_path, FILE_WRITE); + file.println(system_info); + file.flush(); + file.close(); + + // Serial0.println("system_info="+ system_info); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + updata_struct_to_ui.system_save = 0; + xSemaphoreGive(xMutexInventory); + } + +} + +String read_system_info() +{ + String system_info; + + // if(SD_MMC.exists(system_tem_path)) + if(SPIFFS.exists("/system_tem.json")) + { + File file; + // file = SD_MMC.open(system_tem_path, FILE_READ); + file = SPIFFS.open("/system_tem.json", FILE_READ); + system_info = file.readString(); + + file.flush(); + file.close(); + } + else + { + return "-1"; + } + // Serial0.println("system_temp : "+system_info); + return system_info; +} + +bool califile_init() +{ + File root = SD_MMC.open(califile_path); + if (!root || !root.isDirectory()) { + // Serial0.println("Failed to open directory"); + root.close(); + return false; + } + File file = root.openNextFile(); + // Serial0.println("Listing files in directory:"); + while (file) + { + if (!file.isDirectory()) + { + califile_Count++; + califile_doc[String(califile_path)][String(califile_Count)]["name"] = String(file.name()); + // Serial0.printf("FILE: %s, SIZE: %d bytes\n", file.name(), file.size()); + } + else + { + // Serial0.printf("DIR : %s\n", file.name()); + } + file.close(); + file = root.openNextFile(); + } + String cali_str; + serializeJson(califile_doc, cali_str); + // Serial0.println(cali_str); + // Serial0.println("califile_Count: " + String(califile_Count)); + + root.close(); + + return true; +} + + +bool califile_read(uint32_t califile_num) +{ + if(califile_num == 0) + { + String str = "NULL"; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.califile_num = califile_num; + memset(&hh3_califlie_struct, 1, sizeof(hh3_califlie_struct)); + + memset(updata_struct_to_ui.fiber_path, '\0', 100); + strcpy(updata_struct_to_ui.fiber_path, str.c_str()); + xSemaphoreGive(xMutexInventory); + } + return false; + } + + String califile_name = califile_doc[String(califile_path)][String(califile_num)]["name"]; + String read_file_path = String(califile_path) + "/" + califile_name; + if(califile_name.indexOf("_180d") != -1) + { + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.IRAD = 1; + xSemaphoreGive(xMutexInventory); + } + } + else + { + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.IRAD = 0; + xSemaphoreGive(xMutexInventory); + } + } + // Serial0.println("read_file_path: " + read_file_path); + // if(!SD_MMC.exists(read_file_path)) + // { + // Serial0.println("File not exist : "+ read_file_path); + + // String str = "NULL"; + // if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + // { + // updata_struct_to_ui.califile_num = califile_num; + // memset(&hh3_califlie_struct, 1, sizeof(hh3_califlie_struct)); + + // memset(updata_struct_to_ui.fiber_path, '\0', 100); + // strcpy(updata_struct_to_ui.fiber_path, str.c_str()); + // xSemaphoreGive(xMutexInventory); + // } + // return false; + // } + + File file = SD_MMC.open(read_file_path,FILE_READ); + uint32_t file_size = file.size(); + file.read((uint8_t *)&hh3_califlie_struct, sizeof(hh3_califlie_struct)); + file.flush(); + file.close(); + + if(hh3_califlie_struct.gian.GroundType != califile || file_size < sizeof(hh3_califlie_struct)/2) + { + String str = "NULL"; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.califile_num = califile_num; + memset(&hh3_califlie_struct, 1, sizeof(hh3_califlie_struct)); + + memset(updata_struct_to_ui.fiber_path, '\0', 100); + strcpy(updata_struct_to_ui.fiber_path, str.c_str()); + xSemaphoreGive(xMutexInventory); + } + // Serial0.println(read_file_path + " is not a califlie file"); + return false; + } + + + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + updata_struct_to_ui.califile_num = califile_num; + memset(updata_struct_to_ui.fiber_path, '\0', 100); + strcpy(updata_struct_to_ui.fiber_path, read_file_path.c_str()); + + updata_struct_to_ui.system_save = 1; + xSemaphoreGive(xMutexInventory); + } + + return true; +} + + +void fiber_fields() { + uint32_t a = califile_Count; + + // String fiber_type = "无\n光纤\n"; + // String fiber_width = "无\n"; + // String fiber_length = "无\n"; + + String fiber_type = "光纤\n"; + String fiber_width = ""; + String fiber_length = ""; + + uint32_t fiber_count = 0; + while(a) + { + String name = califile_doc[String(califile_path)][String(a)]["name"]; + int f_pos = name.indexOf("_F_"); + if (f_pos != -1) + { + int m_pos = name.indexOf("m_", f_pos); + if (m_pos != -1) { + String m_Str = name.substring(f_pos + 3, m_pos); + if(fiber_length.indexOf(m_Str) == -1) fiber_length = fiber_length + m_Str + '\n'; + } + int d_pos = name.indexOf("d", f_pos); + + if (d_pos != -1 && d_pos > name.indexOf("m", f_pos)) + { + String d_Str = name.substring(m_pos+2, d_pos); + if(fiber_width.indexOf(d_Str) == -1) fiber_width = fiber_width + d_Str + '\n'; + } + fiber_count++; + } + else + { + int d_pos = name.indexOf("d"); + if (d_pos != -1) + { + int start = name.lastIndexOf("_", d_pos - 1); + if (start != -1) + { + String d_Str = name.substring(start + 1, d_pos); + if(fiber_type.indexOf(d_Str) == -1) fiber_type = fiber_type + d_Str + '\n'; + } + } + } + a--; + } + if(fiber_count == 0) fiber_type = fiber_type.substring(7, fiber_type.length()); + strcpy(updata_struct_to_ui.fiber_type, fiber_type.c_str()); + strcpy(updata_struct_to_ui.fiber_length, fiber_length.c_str()); + strcpy(updata_struct_to_ui.fiber_width, fiber_width.c_str()); + + updata_struct_to_ui.fiber_type[fiber_type.length()-1] = '\0'; + // updata_struct_to_ui.fiber_length[fiber_length.length()-1] = '\0'; + // updata_struct_to_ui.fiber_width[fiber_width.length()-1] = '\0'; +} + +uint32_t find_califile(String fiber_type, String fiber_length, String fiber_width) +{ + uint32_t a; + if(xSemaphoreTake(xMutexInventory, 1000) == pdPASS) + { + a = califile_Count; + xSemaphoreGive(xMutexInventory); + } + + // Serial0.println("fiber_type: " + fiber_type); + // Serial0.println("fiber_length: " + fiber_length); + // Serial0.println("fiber_width: " + fiber_width); + + // 处理 fiber_type + if (fiber_type == "g") { + fiber_type = "_F_"; + } else if ((fiber_type != "w") && (fiber_type != "_F_")) { + fiber_type = "_" + fiber_type + "d_"; + } else { + fiber_type = "-1"; + } + // 处理 fiber_length + if (fiber_length != "w") { + fiber_length = "_" + fiber_length + "m_"; + } else { + fiber_length = "-1"; + } + // 处理 fiber_width + if (fiber_width != "w") { + fiber_width = "_" + fiber_width + "d_"; + } else { + fiber_width = "-1"; + } + + if(fiber_type == "-1") + { + return 0; + } + String name2; + // String find_str; + // if(fiber_type == "_F_") find_str = fiber_type + fiber_length + fiber_width; + // else find_str = fiber_type; + while(a) + { + String name = califile_doc[String(califile_path)][String(a)]["name"]; + + // Serial0.println("name:" + name); + + if(fiber_type == "_F_") + { + if((name.indexOf(fiber_type) != -1) && (name.indexOf(fiber_length) != -1 )&& (name.indexOf(fiber_width) != -1)) + { + name2 = name; + break; + } + // if(name.indexOf(find_str) != -1) + // { + // name2 = name; + // break; + // } + } + else + { + if((name.indexOf(fiber_type) != -1) && (name.indexOf("_F_") == -1)) + { + name2 = name; + break; + } + } + a--; + } + + return a; +} + +void system_data_init() +{ + + ui_event_group = xEventGroupCreate(); + if (ui_event_group == NULL) { + + while (1) + { + // Serial0.println("ui_event_group create fail!"); + vTaskDelay(1000 / portTICK_PERIOD_MS); // 停住系统防止崩溃 + } + } + + ui_event_group2 = xEventGroupCreate(); + if (ui_event_group == NULL) { + + while (1) + { + // Serial0.println("ui_event_group create fail!"); + vTaskDelay(1000 / portTICK_PERIOD_MS); // 停住系统防止崩溃 + } + } + is3_event_group = xEventGroupCreate(); + if (is3_event_group == NULL) { + while (1) + { + // Serial0.println("is3_event_group create fail!"); + vTaskDelay(1000 / portTICK_PERIOD_MS); // 停住系统防止崩溃 + } + } + xMutexInventory = xSemaphoreCreateMutex(); //创建MUTEX + // if(xMutexInventory == NULL) Serial0.println("xMutexInventory is NULL"); + + image_picture.data = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); + assert(image_picture.data != NULL); + + file_data = (uint8_t *)heap_caps_malloc(file_heap_size, MALLOC_CAP_SPIRAM); + memset(file_data, 0, file_heap_size); + califile_init(); + if(califile_Count != 0) fiber_fields(); + + uint32_t califile_num = 0; + String system_info = read_system_info(); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + + if(system_info !="-1") + { + deserializeJson(sys_doc, system_info); + String save_data_path = sys_doc["save_data_path"]; + + updata_struct_to_ui.W_count = sys_doc["W_count"]; + updata_struct_to_ui.W_count = updata_struct_to_ui.W_count > 1 ? updata_struct_to_ui.W_count : 1; + + updata_struct_to_ui.D_count = sys_doc["D_count"]; + updata_struct_to_ui.D_count = updata_struct_to_ui.D_count > 1 ? updata_struct_to_ui.D_count : 1; + + updata_struct_to_ui.S_count = sys_doc["S_count"]; + updata_struct_to_ui.S_count = updata_struct_to_ui.S_count > 1 ? updata_struct_to_ui.S_count : 1; + + updata_struct_to_ui.save_count = sys_doc["save_count"]; + updata_struct_to_ui.save_count = updata_struct_to_ui.save_count > 1 ? updata_struct_to_ui.save_count : 1; + updata_struct_to_ui.save_time = sys_doc["save_time"]; + updata_struct_to_ui.save_time = updata_struct_to_ui.save_time > 0 ? updata_struct_to_ui.save_time : 0; + + updata_struct_to_ui.califile_num = sys_doc["califile_num"]; + califile_num = updata_struct_to_ui.califile_num; + updata_struct_to_ui.brightness_value = sys_doc["brightness_value"]; + updata_struct_to_ui.brightness_value = updata_struct_to_ui.brightness_value > 50 ? updata_struct_to_ui.brightness_value : 50; + updata_struct_to_ui.volume_value = sys_doc["volume_value"]; + + updata_struct_to_ui.ForeOptics_sel1 = sys_doc["ForeOptics_sel1"]; + updata_struct_to_ui.ForeOptics_sel2 = sys_doc["ForeOptics_sel2"]; + updata_struct_to_ui.ForeOptics_sel3 = sys_doc["ForeOptics_sel3"]; + + updata_struct_to_ui.wifi_sw = sys_doc["wifi_sw"]; + updata_struct_to_ui.bluetooth_sw = sys_doc["bluetooth_sw"]; + + String wifi_ssid = sys_doc["wifi_ssid"]; + String wifi_pwd = sys_doc["wifi_pwd"] ; + + strcpy(updata_struct_to_ui.wifi_ssid,wifi_ssid.c_str()); + strcpy(updata_struct_to_ui.wifi_pwd,wifi_pwd.c_str()); + + updata_struct_to_ui.screen_keep_time = sys_doc["screen_keep_time"]; + updata_struct_to_ui.screen_keep_time = updata_struct_to_ui.screen_keep_time > 1 ? updata_struct_to_ui.screen_keep_time : 1; + updata_struct_to_ui.screen_sw = sys_doc["screen_sw"]; + + updata_struct_to_ui.set_save_num = sys_doc["set_save_num"]; + + strcpy(updata_struct_to_ui.save_path, save_data_path.c_str()); + updata_struct_to_ui.save_num = sys_doc["save_data_num"]; + } + + if ( system_info =="-1" || updata_struct_to_ui.W_count == 0) + { + updata_struct_to_ui.W_count = 10; + updata_struct_to_ui.D_count = 25; + updata_struct_to_ui.S_count = 10; + + updata_struct_to_ui.save_count = 1; + updata_struct_to_ui.save_time = 0; + + updata_struct_to_ui.brightness_value = 255; + updata_struct_to_ui.volume_value = 21; + + updata_struct_to_ui.set_save_num = 0; + + strcpy(updata_struct_to_ui.save_path, "data"); + updata_struct_to_ui.save_num = 00; + } + + updata_struct_to_ui.bar_value = 0; + updata_struct_to_ui.mode = 0; + updata_struct_to_ui.plot_type = 0; + + updata_struct_to_ui.button1_count = 0; + updata_struct_to_ui.button2_count = 0; + + updata_struct_to_ui.set_time_flag = true; + xSemaphoreGive(xMutexInventory); + } + if(califile_num != 0) califile_read(califile_num); + else + { + // if(califile_Count != 0) califile_read(1); + // else califile_read(0); + } + + ds1307_date now; + ds1307_get_time(&now); + // String date_now = String(now.year) + "_" + String(now.month) + "_" + String(now.day); + String date_now = String(now.year) + "_"; + + if(now.month < 10) date_now = date_now+"0" + String(now.month) + "_"; + else date_now = date_now + String(now.month) + "_"; + + if(now.day < 10) date_now = date_now + "0" + String(now.day); + else date_now = date_now + String(now.day); + + String save_path = "/data/" + date_now ; + + if(SD_MMC.exists(save_path) == false) + { + // SD_MMC.mkdir(save_path); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + uint32_t save_num = updata_struct_to_ui.set_save_num; + updata_struct_to_ui.save_num = save_num; + xSemaphoreGive(xMutexInventory); + } + } +} + +void header_pack(String SpectralInfo_str) +{ + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + + uint32_t SpectralData_Squantity = 2; + uint32_t SpectralData_SectionLength = 2 + sizeof(hh3_data_s_dn) + sizeof(hh3_data_dark_dn); + if(updata_struct_to_ui.mode == 2) + { + SpectralData_Squantity += 2; + SpectralData_SectionLength = SpectralData_SectionLength+sizeof(hh3_data_wr_dn) + sizeof(hh3_data_flat_ref); + } + if(hh3_califlie_struct.gian.GroundType == califile) + { + SpectralData_Squantity ++; + SpectralData_SectionLength += sizeof(hh3_califlie_struct.gian); + } + + SpectralData_header.SectionFlag = SpectralData; + SpectralData_header.SectionLength = SpectralData_SectionLength; + SpectralData_header.Squantity = SpectralData_Squantity; + + SpectralInfo_header.SectionFlag = SpectralInfo; + SpectralInfo_header.SectionLength = SpectralInfo_str.length() + 2 + 2 + 1; + SpectralInfo_header.Squantity = SpectralInfo_Squantity; + SpectralInfo_header.info1_length = SpectralInfo_str.length(); + SpectralInfo_header.info1_type = 0x00; + + Other_header.SectionFlag = Other; + Other_header.SectionLength = 0; + + Image_header.SectionFlag = Image; + Image_header.SectionLength = 0; + + xSemaphoreGive(xMutexInventory); + } +} + +void Ground_DN_pack() +{ + String save_str; + uint32_t save_num; + uint16_t max = 0; + String name; + char file_name_buf[64]; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + save_str = updata_struct_to_ui.save_path; + save_num = updata_struct_to_ui.save_num; + + //name + sprintf(file_name_buf, "%s%04d_Ground_DN", save_str.c_str(), save_num); + // name = save_str + String(save_num) +"_" + "Ground_DN"; + name = String(file_name_buf); + memset(hh3_data_s_dn.name,'\0',100); + memcpy(hh3_data_s_dn.name,name.c_str(),name.length()); + hh3_data_s_dn.name[name.length()] = '\0'; + //sensor_id + memcpy(hh3_data_s_dn.sensor_id,(uint8_t *)&is3.info,is3.info.length()); + hh3_data_s_dn.sensor_id[is3.info.length()] = '\0'; + hh3_data_s_dn.FiberID = hh3_FiberID; + + //date + hh3_data_s_dn.date = hh3_time_structure; + hh3_data_s_dn.integration_time = is3.integration_time; + hh3_data_s_dn.gain = 0.0; + + hh3_data_s_dn.data_type = DATA_TYPE_UINT16; + hh3_data_s_dn.PixelSize = 2; + hh3_data_s_dn.GroundType = s_dn; + hh3_data_s_dn.band_num = 515; + hh3_data_s_dn.ValidFlag = 1 ; + + for (uint16_t i = 0; i < 515; i++) + { + hh3_data_s_dn.data[i] = is3_data.s_average_data[i]; + max = max < is3_data.s_average_data[i] ? is3_data.s_average_data[i] : max; + } + if(max > 65535 * 0.99) hh3_data_s_dn.ValidFlag = 0; + + xSemaphoreGive(xMutexInventory); + } +} +void dark_dn_pack(bool flag) +{ + uint32_t save_num; + String save_str; + uint16_t max = 0; + String name; + char name_buf[64]; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + save_str = updata_struct_to_ui.save_path; + save_num = updata_struct_to_ui.save_num; + + + // name = save_str+String(save_num) + "_dark_dn"; + sprintf(name_buf, "%s%04d_dark_dn", save_str.c_str(), save_num); + name = String(name_buf); + + memset(hh3_data_dark_dn.name,'\0',100); + memcpy(hh3_data_dark_dn.name,name.c_str(),name.length()); + hh3_data_dark_dn.name[name.length()] = '\0'; + if(flag) + { + xSemaphoreGive(xMutexInventory); + return; + } + // hh3_data_dark_dn.sensor_id + memcpy(hh3_data_dark_dn.sensor_id,(uint8_t *)&is3.info,is3.info.length()); + hh3_data_dark_dn.sensor_id[is3.info.length()] = '\0'; + hh3_data_dark_dn.FiberID = hh3_FiberID; + + hh3_data_dark_dn.date = hh3_time_structure; + hh3_data_dark_dn.integration_time = is3.integration_time; + hh3_data_dark_dn.gain = 0.0; + + hh3_data_dark_dn.data_type = DATA_TYPE_UINT16; + hh3_data_dark_dn.PixelSize = 2; + hh3_data_dark_dn.GroundType = dark_dn; + hh3_data_dark_dn.band_num = 515; + hh3_data_dark_dn.ValidFlag = 1 ; + + for (uint16_t i = 0; i < 515; i++) + { + hh3_data_dark_dn.data[i] = is3_data.dark_data[i]; + max = max < is3_data.dark_data[i] ? is3_data.dark_data[i] : max; + } + if(max > 65535 * 0.99) hh3_data_dark_dn.ValidFlag = 0; + + xSemaphoreGive(xMutexInventory); + } +} + +void wr_dn_pack(bool flag) +{ + uint32_t save_num; + String save_str; + uint16_t max = 0; + String name; + char file_name_buf[64]; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + save_str = updata_struct_to_ui.save_path; + save_num = updata_struct_to_ui.save_num; + + // name = save_str+String(save_num) + "_flat_dn"; + sprintf(file_name_buf, "%s%04d_flat_dn", save_str.c_str(), save_num); + name = String(file_name_buf); + + memset(hh3_data_wr_dn.name,'\0',100); + memcpy(hh3_data_wr_dn.name,name.c_str(),name.length()); + hh3_data_wr_dn.name[name.length()] = '\0'; + if(flag) + { + xSemaphoreGive(xMutexInventory); + return; + } + + // hh3_data_wr_dn.sensor_id + memcpy(hh3_data_wr_dn.sensor_id,(uint8_t *)&is3.info,is3.info.length()); + hh3_data_wr_dn.sensor_id[is3.info.length()] = '\0'; + hh3_data_wr_dn.FiberID = hh3_FiberID; + + hh3_data_wr_dn.date = hh3_time_structure; + hh3_data_wr_dn.integration_time = is3.integration_time; + hh3_data_wr_dn.gain = 0.0; + + hh3_data_wr_dn.data_type = DATA_TYPE_UINT16; + hh3_data_wr_dn.PixelSize = 2; + hh3_data_wr_dn.GroundType = flat_dn; + hh3_data_wr_dn.band_num = 515; + hh3_data_wr_dn.ValidFlag = 1 ; + + for (uint16_t i = 0; i < 515; i++) + { + hh3_data_wr_dn.data[i] = is3_data.wr_data[i]; + max = max < is3_data.wr_data[i] ? is3_data.wr_data[i] : max; + } + if(max > 65535 * 0.99) hh3_data_wr_dn.ValidFlag = 0; + + xSemaphoreGive(xMutexInventory); + } +} + +void flat_ref_pack() +{ + // uint8_t mode; + + // String save_str; + // uint32_t save_num; + // uint16_t max = 0; + // String name; + // char file_name_buf[64]; + // if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + // { + // save_str = updata_struct_to_ui.save_path; + // save_num = updata_struct_to_ui.save_num; + // mode = updata_struct_to_ui.mode; + // xSemaphoreGive(xMutexInventory); + // } + // if(mode != 2) return; + + //name + // name = save_str + String(save_num) +"_" + "flat_ref"; + // sprintf(file_name_buf, "%s%04d.iris", save_str.c_str(), save_num); + // name = String(file_name_buf); + // memset(hh3_data_flat_ref.name,'\0',100); + // memcpy(hh3_data_flat_ref.name,name.c_str(),name.length()); + // hh3_data_flat_ref.name[name.length()] = '\0'; + // //sensor_id + // memcpy(hh3_data_flat_ref.sensor_id,(uint8_t *)&is3.info,is3.info.length()); + // hh3_data_flat_ref.sensor_id[is3.info.length()] = '\0'; + // hh3_data_flat_ref.FiberID = hh3_FiberID; + + // hh3_data_flat_ref.date = hh3_time_structure; + // hh3_data_flat_ref.integration_time = is3.integration_time; + // hh3_data_flat_ref.gain = 0.0; + + // hh3_data_flat_ref.data_type = DATA_TYPE_FLOAT64; + // hh3_data_flat_ref.PixelSize = 8; + // hh3_data_flat_ref.GroundType = flat_ref; + // hh3_data_flat_ref.band_num = 512; + // hh3_data_flat_ref.ValidFlag = 1 ; + + // for (uint16_t i = 0; i < 512; i++) + // { + // hh3_data_flat_ref.data[i] = (double)(is3_data.s_average_data[i] - hh3_data_dark_dn.data[i]) / (double)(hh3_data_wr_dn.data[i] - hh3_data_dark_dn.data[i]); + // } + // if(max > 65535 * 0.99) hh3_data_flat_ref.ValidFlag = 0; +} + + +void picture_pack(String name,String num) +{ + if(get_cam_status() == 0) + { + // Serial0.println("cam stop"); + return; + } + // image_picture.length = 0; + + if(image_picture.length == 0) + { + xEventGroupClearBits(is3_event_group, IS3_CAM_DONE_BIT); + xEventGroupWaitBits(is3_event_group, IS3_CAM_DONE_BIT, pdTRUE, pdFALSE, 100 / portTICK_PERIOD_MS); + } + + if(image_picture.length == 0) return; + vTaskDelay(1); + // String name_str = name + num + ".jpg"; + char file_name_buf[64]; + sprintf(file_name_buf, "%s%04d.jpg", name.c_str(), num); + String name_str = String(file_name_buf); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + Image_header.SectionFlag = Image; + image_picture.Squantity = Image_Squantity; + Image_header.SectionLength = image_picture.length + sizeof(image_picture) - sizeof(uint8_t *); + memcpy(image_picture.name,name_str.c_str(),name_str.length()); + image_picture.length = image_picture.length + 100 + 10 + 1; + image_picture.date = hh3_time_structure; + image_picture.type = 0; + xSemaphoreGive(xMutexInventory); + } + vTaskDelay(10); +} + +void save_data(String path ,String name,String SpectralInfo_str) +{ + uint8_t mode; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + mode = updata_struct_to_ui.mode; + xSemaphoreGive(xMutexInventory); + } + File file; + file = SD_MMC.open(path + name,FILE_WRITE); + + //SpectralData_Section + file.write((uint8_t *)&SpectralData_header,sizeof(SpectralData_header)); + //地物DN + file.write((uint8_t *)&hh3_data_s_dn,sizeof(hh3_data_s_dn)); + // file.flush(); + //白板DN + if(mode == 2) + { + file.write((uint8_t *)&hh3_data_wr_dn,sizeof(hh3_data_wr_dn)); + } + //DARK_DN + file.write((uint8_t *)&hh3_data_dark_dn,sizeof(hh3_data_dark_dn)); + //flat_ref + // file.flush(); + if(mode == 2) + { + // file.write((uint8_t *)&hh3_data_wr_dn,sizeof(hh3_data_wr_dn)); + file.write((uint8_t *)&hh3_data_flat_ref,sizeof(hh3_data_flat_ref)); + } + //califlie + if(hh3_califlie_struct.gian.GroundType == califile) file.write((uint8_t *)&hh3_califlie_struct.gian,sizeof(hh3_califlie_struct.gian)); + // file.flush(); + //SpectralInfo_Section + file.write((uint8_t *)&SpectralInfo_header,sizeof(SpectralInfo_header)); + file.write((uint8_t *)SpectralInfo_str.c_str(),SpectralInfo_str.length()); + // file.flush(); + //other_Section + file.write((uint8_t *)&Other_header,sizeof(Other_header)); + + //Image_Section + file.write((uint8_t *)&Image_header,sizeof(Image_header)); + // file.flush(); + if(image_picture.Squantity > 0) + { + file.flush(); + file.write((uint8_t *)&image_picture,sizeof(image_picture) - sizeof(uint8_t *)); + file.write(image_picture.data,image_picture.length - sizeof(image_picture.name) - sizeof(image_picture.date) - sizeof(image_picture.type)); + } + file.flush(); + file.close(); +} + +void print_heap_detail() { + Serial0.printf("Heap : %u bytes\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); + Serial0.printf("Heap : %u bytes\n", heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT)); + Serial0.printf(": %u bytes\n", heap_caps_get_total_size(MALLOC_CAP_DEFAULT)); + Serial0.printf(": %u bytes\n", heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT)); + + Serial0.printf("PSRAM : %u bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); + Serial0.printf("PSRAM : %u bytes\n", heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)); + Serial0.printf("PSRAM : %u bytes\n", heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM)); +} + + +void show_data_task(void *pvParameters) +{ + String path = "/data"; + uint32_t path_deep = 0; + uint32_t page_log[100]= {0}; + uint16_t file_s_dn[512]; + uint8_t plot_type = 0; + show_data_structure.mode = 0xff; + read_file_point file_point = {NULL,NULL,NULL,NULL,NULL,NULL,NULL}; + bool first_flag = true; + memset(page_log,0,sizeof(page_log)); + + while(1) + { + xEventGroupWaitBits(ui_event_group, SHOW_DATA_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + if(get_sd_card_status() == false) + { + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + lv_obj_set_size(guider_ui.screen_data_spinner_1, 1, 1); + lv_label_set_text(guider_ui.screen_data_label_17, ""); + lv_obj_clear_state(guider_ui.screen_data_btnm_1, LV_STATE_DISABLED); + xSemaphoreGive(lvgl_mutex); + } + continue; + } + + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + lv_obj_set_size(guider_ui.screen_data_spinner_1, 41, 52); + lv_label_set_text(guider_ui.screen_data_label_17, "加载中"); + lv_obj_add_state(guider_ui.screen_data_btnm_1, LV_STATE_DISABLED); // 禁用按钮 + xSemaphoreGive(lvgl_mutex); + } + + // lock_screen(); + + uint32_t mode; + String target_path = "/"; + uint32_t target_page =0; + uint32_t total_page = 0; + String read_path; + bool flag = false; + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + mode = show_data_structure.mode; + target_page = show_data_structure.target_page; + target_page = target_page - 1; + target_path += show_data_structure.target_path; + plot_type = show_data_structure.plot_type; + if(mode == 7) plot_type++; + plot_type = plot_type > 3 ? 0 : plot_type; + show_data_structure.plot_type = plot_type; + + total_page = show_data_structure.total_page; + xSemaphoreGive(xMutexInventory); + } + //更新列表 + //0下一页 1上一页 2跳转 3返回 4进入 5加载 6查看数据 7改变曲线 + uint32_t page = page_log[path_deep]; + switch (mode) + { + case 0: + if(total_page == 0 ) break; + if((page_log[path_deep] + 1 )< total_page) + { + page_log[path_deep]++; + page = page_log[path_deep]; + show_page(path, page); + } + break; + case 1: + if(total_page == 0 ) break; + if(page_log[path_deep] > 0) + { + page_log[path_deep]--; + page = page_log[path_deep]; + show_page(path, page); + } + break; + case 2: + if(page_log[path_deep] != target_page) + { + if(target_page > total_page) target_page = total_page - 1; + // if(target_page < 0) target_page = 0; + + page_log[path_deep] = target_page; + page = page_log[path_deep]; + show_page(path, page); + } + break; + case 3: + { + int slashIndex = path.lastIndexOf('/'); + if (slashIndex > 0) + { + path = path.substring(0, slashIndex); + if (path_deep > 0) path_deep--; + + total_page = get_file_num(path); + page = page_log[path_deep]; + show_page(path, page); + } + break; + } + case 4: + { + path = path + target_path; + path_deep ++; + page_log[path_deep] = 0; + total_page = get_file_num(path); + // page = page_log[path_deep]; + page = total_page - 1; + page_log[path_deep] = page; + show_page(path, page); + break; + } + case 5: + { + flag = file_data_analysis(&file_point); + if(flag) change_plot_type(file_point,plot_type); + if(flag) show_picture(&file_point); + + total_page = get_file_num(path); + page = total_page - 1; + show_page(path, page); + break; + } + case 6: + { + read_path = path + target_path; + + // plot_type = 0; + // if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + // { + // show_data_structure.plot_type = plot_type; + // xSemaphoreGive(xMutexInventory); + // } + flag = read_data_from_file(read_path); + if(flag) flag = file_data_analysis(&file_point); + if(flag) change_plot_type(file_point,plot_type); + if(flag) show_picture(&file_point); + break; + } + case 7: + { + change_plot_type(file_point,plot_type); + break; + } + default: + break; + } + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.total_page = total_page; + show_data_structure.now_page = page + 1; + if(total_page == 0) show_data_structure.now_page = 0; + show_data_structure.mode = 0xff; + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(ui_event_group,SHOW_DATA_DONE_BIT); + + + + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + lv_obj_set_size(guider_ui.screen_data_spinner_1, 1, 1); + lv_label_set_text(guider_ui.screen_data_label_17, ""); + lv_obj_clear_state(guider_ui.screen_data_btnm_1, LV_STATE_DISABLED); + xSemaphoreGive(lvgl_mutex); + } + xEventGroupClearBits(ui_event_group,SHOW_DATA_BIT); + } + +} + +//0 dn 1 rad 2ref 3dark_dn +void change_plot_type(read_file_point file_point,uint8_t plot_type) +{ + if((file_point.s_dn_data == NULL )|| (file_point.dark_dn_data == NULL) || (file_point.data_info == NULL)) + { + return; + } + uint16_t str_len; + memcpy(&str_len,file_point.data_info,2); + + char temp_str[str_len + 1]; + memcpy(temp_str, file_point.data_info + 2 + 1, str_len); + temp_str[str_len] = '\0'; + + String SpectralInfo_str(temp_str); + + // file_doc + deserializeJson(file_doc, SpectralInfo_str); + uint32_t dn_it; + One_Spectral_Data_Struct *p = file_point.s_dn_data; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + memcpy(show_data_structure.file_name,p->Name,sizeof(show_data_structure.file_name)); + + show_data_structure.year = p->CollectionTime.year; + show_data_structure.month = p->CollectionTime.month; + show_data_structure.day = p->CollectionTime.day; + show_data_structure.hour = p->CollectionTime.hour; + show_data_structure.minute = p->CollectionTime.minute; + show_data_structure.second = p->CollectionTime.second; + + show_data_structure.IT = p->Exposure; + dn_it = p->Exposure; + + JsonObject info1 = file_doc["info_list"][1]; // 如果确定是第二个对象 + show_data_structure.W_count = info1["W_count"] | 0; + show_data_structure.D_count = info1["D_count"] | 0; + show_data_structure.S_count = info1["S_count"] | 0; + show_data_structure.height = info1["height"].as(); + xSemaphoreGive(xMutexInventory); + } + + if(plot_type == 1) + { + if(file_point.califile_data == NULL) plot_type++; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.plot_type = plot_type; + xSemaphoreGive(xMutexInventory); + } + } + + if(plot_type == 2) + { + if(file_point.wr_dn_data == NULL) plot_type++; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.plot_type = plot_type; + xSemaphoreGive(xMutexInventory); + } + } + + uint16_t *dn = (uint16_t *)(file_point.s_dn_data->SpectralDataAddress); + uint16_t *dark = (uint16_t *)(file_point.dark_dn_data->SpectralDataAddress); + + + switch (plot_type) + { + case 0: + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + + for (uint16_t i = 0; i < 512; i++) + { + show_data_structure.plot_data[i] = (int)dn[i] - (int)dark[i]; + } + xSemaphoreGive(xMutexInventory); + } + } + break; + case 1://rad + { + if(file_point.califile_data == NULL) return; + double *gian = (double *)(file_point.califile_data->SpectralDataAddress); + double gain_it = file_point.califile_data->Exposure; + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 512; i++) + { + show_data_structure.plot_data[i] = (double)(dn[i] - dark[i])/ (double)dn_it * gain_it * (gian[i] * 1000000); + } + xSemaphoreGive(xMutexInventory); + } + } + break; + case 2: //ref + { + if(file_point.wr_dn_data == NULL) return; + uint16_t *wr = (uint16_t *)(file_point.wr_dn_data->SpectralDataAddress); + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 512; i++) + { + show_data_structure.plot_data[i] = (float)(dn[i] - dark[i]) / (float)(wr[i] - dark[i]) * 10000; + } + xSemaphoreGive(xMutexInventory); + } + } + break; + case 3: //dark_dn + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for (uint16_t i = 0; i < 512; i++) + { + show_data_structure.plot_data[i] = dark[i]; + } + xSemaphoreGive(xMutexInventory); + } + } + break; + + default: + break; + } +} + +static void updata_dir(lv_event_t *e) +{ + + uint8_t mode; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + mode = show_data_structure.mode; + xSemaphoreGive(xMutexInventory); + } + + if(mode == 0xff) + { + lv_obj_t * obj = lv_event_get_target(e); + const char *dir = lv_list_get_btn_text(guider_ui.screen_data_list_1, obj); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.mode = 4; + strcpy(show_data_structure.target_path, dir); + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(ui_event_group, SHOW_DATA_BIT); + } + +} + +//更新数据 +static void updata_data(lv_event_t *e) +{ + uint8_t mode; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + mode = show_data_structure.mode; + xSemaphoreGive(xMutexInventory); + } + + if(mode == 0xff) + { + lv_obj_t * obj = lv_event_get_target(e); + const char *dir = lv_list_get_btn_text(guider_ui.screen_data_list_1, obj); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.mode = 6; + strcpy(show_data_structure.target_path, dir); + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(ui_event_group, SHOW_DATA_BIT); + } +} + +// 0_0 /data +// static uint32_t show_page(String path, uint32_t page) +// { +// File root = SD_MMC.open(path); +// if (!root || !root.isDirectory()) { + +// return 0; +// } + +// if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) +// { +// lv_obj_clean(guider_ui.screen_data_list_1); // 清空列表 +// xSemaphoreGive(lvgl_mutex); +// } + +// // lv_obj_invalidate(guider_ui.screen_data_list_1); // 强制刷新父对象 +// // vTaskDelay(50); + + +// uint32_t start_index = page * ITEMS_PER_PAGE; +// uint32_t end_index = start_index + ITEMS_PER_PAGE; +// uint32_t current_index = 0; + +// File file = root.openNextFile(); +// uint32_t count = 0; +// while (file) +// { +// // Serial0.println("current_index" + String(current_index)); +// if (current_index >= start_index) +// { +// String filename = file.name(); + +// bool isDir = file.isDirectory(); +// const char* icon = isDir ? LV_SYMBOL_DIRECTORY : LV_SYMBOL_FILE; + +// if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) +// { + +// guider_ui.screen_data_list_1_btn = lv_list_add_btn(guider_ui.screen_data_list_1, icon, filename.c_str()); +// lv_obj_t* label = lv_obj_get_child(guider_ui.screen_data_list_1_btn, 1); + +// if (label != NULL) +// { +// lv_label_set_long_mode(label, LV_LABEL_LONG_CLIP); +// } + +// if(isDir) lv_obj_add_event_cb(guider_ui.screen_data_list_1_btn,updata_dir, LV_EVENT_CLICKED, NULL); +// else lv_obj_add_event_cb(guider_ui.screen_data_list_1_btn,updata_data, LV_EVENT_CLICKED, NULL); + +// xSemaphoreGive(lvgl_mutex); +// } + +// } + +// file = root.openNextFile(); + +// current_index++; +// if(current_index >= end_index) break; + +// count++; +// if(count%20 == 0) vTaskDelay(1); +// } +// root.close(); +// return end_index; +// } +// static uint32_t get_file_num(String path) +// { +// uint32_t file_count = 0; + +// File root = SD_MMC.open(path); + +// if (!root) +// { +// // Serial0.println("Failed to open directory"); +// return 0; +// } +// if (!root.isDirectory()) +// { +// // Serial0.println("Not a directory"); +// root.close(); +// return 0; +// } + +// if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) +// { +// lv_obj_clean(guider_ui.screen_data_list_1); // 清空列表 +// xSemaphoreGive(lvgl_mutex); +// } +// // lv_obj_clean(guider_ui.screen_data_list_1); // 清空列表 +// // vTaskDelay(50); + + +// uint32_t count = 0; +// File file = root.openNextFile(); +// while (file) +// { +// file_count++; +// // file.close(); // 关闭当前文件 +// file = root.openNextFile(); // 获取下一个文件 + +// if(count%20 == 0) vTaskDelay(1); +// } +// root.close(); + +// uint32_t total_page = ceil((float)file_count / ITEMS_PER_PAGE); + +// if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) +// { +// show_data_structure.total_page = total_page; +// xSemaphoreGive(xMutexInventory); +// } +// // Serial0.println("file_count:" + String(file_count)); +// // Serial0.println("total_page:" + String(total_page)); +// return total_page; +// } +/////////////////////////////////////////////////////////////////////////////// +#include "driver/sdmmc_host.h" +#include + +static uint32_t get_file_num(String path_str) +{ + uint32_t file_count = 0; + esp_err_t ret; + DIR *dir; + struct dirent *ent; + lv_obj_t * btn; + + path_str = "/sdcard" + path_str; + const char *path = path_str.c_str(); + // Serial0.println("path:" + String(path)); + if ((dir = opendir(path))!= NULL) + { + // 打开目录 + while ((ent = readdir(dir))!= NULL) + { + file_count++; + } + closedir(dir); + } + else + { + // Serial0.println("Failed to open directory"); + return 0; + } + uint32_t total_page = ceil((float)file_count / ITEMS_PER_PAGE); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + show_data_structure.total_page = total_page; + xSemaphoreGive(xMutexInventory); + } + // Serial0.println("file_count:" + String(file_count)); + // Serial0.println("total_page:" + String(total_page)); + return total_page; +} + +static uint32_t show_page(String path, uint32_t page) +{ + File root = SD_MMC.open(path); + if (!root || !root.isDirectory()) { + + return 0; + } + root.close(); + + + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + lv_obj_clean(guider_ui.screen_data_list_1); // 清空列表 + xSemaphoreGive(lvgl_mutex); + } + + uint32_t start_index = page * ITEMS_PER_PAGE; + uint32_t end_index = start_index + ITEMS_PER_PAGE; + uint32_t current_index = 0; + uint32_t count = 0; + + + esp_err_t ret; + DIR *dir; + struct dirent *ent; + lv_obj_t * btn; + path = "/sdcard" + path; + const char *path_str = path.c_str(); + if ((dir = opendir(path_str))!= NULL) + { + // 打开目录 + while ((ent = readdir(dir))!= NULL) + { + if (current_index >= start_index) + { + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + if (ent->d_type == DT_REG)// 如果是常规文件 + { + guider_ui.screen_data_list_1_btn = lv_list_add_btn(guider_ui.screen_data_list_1, LV_SYMBOL_FILE, (const char *)ent->d_name); + lv_obj_t* label_1 = lv_obj_get_child(guider_ui.screen_data_list_1_btn, 1); + + if (label_1 != NULL) + { + lv_label_set_long_mode(label_1, LV_LABEL_LONG_CLIP); + } + + lv_obj_add_event_cb(guider_ui.screen_data_list_1_btn,updata_data, LV_EVENT_CLICKED, NULL); + } + else if (ent->d_type == DT_DIR) + { + guider_ui.screen_data_list_1_btn = lv_list_add_btn(guider_ui.screen_data_list_1, LV_SYMBOL_DIRECTORY, (const char *)ent->d_name); + lv_obj_t* label_2 = lv_obj_get_child(guider_ui.screen_data_list_1_btn, 1); + + if (label_2 != NULL) + { + lv_label_set_long_mode(label_2, LV_LABEL_LONG_CLIP); + } + lv_obj_add_event_cb(guider_ui.screen_data_list_1_btn,updata_dir, LV_EVENT_CLICKED, NULL); + + } + + xSemaphoreGive(lvgl_mutex); + } + } + current_index++; + if(current_index >= end_index) break; + + // count++; + // if(count%20 == 0) vTaskDelay(1); + + } + closedir(dir); + } + else + { + return 0; + } + return end_index; +} +/////////////////////////////////////////////////// + +bool read_data_from_file(String path) +{ + if(path.indexOf(".iris") < 0) + { + // Serial0.println("file_type_error"); + return false; + } + memset(file_data,0,file_heap_size); + bool flag = false; + uint32_t size; + uint32_t type; + File file; + file = SD_MMC.open(path,FILE_READ); + size = file.size(); + + if(size && (size < file_heap_size)) + { + + file.read(file_data,size); + flag = true; + } + file.close(); + + // Serial0.println("file size:" + String(size)); + // Serial0.println("if read:" + String(flag)); + // Serial0.printf("file_data:%x %x %x %x\n",file_data[0],file_data[1],file_data[2],file_data[3]); + // Serial0.write(file_data,4); + return flag; +} + + +bool file_data_analysis(read_file_point *p) +{ + // p = {NULL,NULL,NULL,NULL,NULL,NULL,NULL}; + p->califile_data = NULL; + p->dark_dn_data = NULL; + p->flat_ref_data = NULL; + p->s_dn_data = NULL; + p->wr_dn_data= NULL; + + p->data_info = NULL; + p->image_info = NULL; + + uint32_t SpectralData_pos,Spectralinfo_pos,Other_pos,Image_pos; + uint32_t pos=0; + + uint32_t header; + uint32_t length = 0; + memcpy(&header,file_data,4); + if(header != SpectralData) return false; + + for (uint16_t i = 0; i < 4; i++) + { + memcpy(&header,file_data+pos,4); + memcpy(&length,file_data+pos+4,8); + + // Serial0.printf("i:%d\n",i); + // Serial0.printf("header:%x,length:%d\n",header,length); + + if((header == SpectralData)) SpectralData_pos = pos; + else if((header == SpectralInfo)) Spectralinfo_pos = pos; + else if((header == Other) ) Other_pos = pos; + else if((header == Image)) Image_pos = pos; + + + pos = pos + 4 + 8 + length; + } + + // Serial0.printf("SpectralData_pos:%d\n",SpectralData_pos); + // Serial0.printf("SpectralInfo_pos:%d\n",Spectralinfo_pos); + // Serial0.printf("Other_pos:%d\n",Other_pos); + // Serial0.printf("Image_pos:%d\n",Image_pos); + + // SpectralData_len + uint64_t SpectralData_len = 0; + memcpy(&SpectralData_len,file_data+SpectralData_pos+4,8); + // Serial0.printf("SpectralData_len:%d\n",SpectralData_len); + uint16_t SpectralData_quantity = 0; + memcpy(&SpectralData_quantity,file_data+SpectralData_pos+12,2); + // Serial0.printf("SpectralData_quantity:%d\n",SpectralData_quantity); + + uint32_t offset = 4 + 8 + 2; + for(int i = 0;i < SpectralData_quantity;i++) + { + One_Spectral_Data_Struct *Spectral_Data_Structure; + + // memcpy(&Spectral_Data_Structure,file_data+offset,sizeof(Spectral_Data_Structure)-sizeof(Spectral_Data_Structure.SpectralDataAddress)); + Spectral_Data_Structure = (One_Spectral_Data_Struct *)(file_data+offset); + + + switch (Spectral_Data_Structure->GroundType) + { + case s_dn: + p->s_dn_data = Spectral_Data_Structure; + p->s_dn_data->SpectralDataAddress = file_data + offset + sizeof(One_Spectral_Data_Struct) - sizeof(uint8_t *); + break; + case flat_dn: + p->wr_dn_data = Spectral_Data_Structure; + p->wr_dn_data->SpectralDataAddress = file_data + offset + sizeof(One_Spectral_Data_Struct) - sizeof(uint8_t *); + break; + case dark_dn: + p->dark_dn_data = Spectral_Data_Structure; + p->dark_dn_data->SpectralDataAddress = file_data + offset + sizeof(One_Spectral_Data_Struct) - sizeof(uint8_t *); + break; + case flat_ref: + p->flat_ref_data = Spectral_Data_Structure; + p->flat_ref_data->SpectralDataAddress = file_data + offset + sizeof(One_Spectral_Data_Struct) - sizeof(uint8_t *); + break; + case califile: + p->califile_data = Spectral_Data_Structure; + p->califile_data->SpectralDataAddress = file_data + offset + sizeof(One_Spectral_Data_Struct) - sizeof(uint8_t *); + break; + default: + break; + } + offset = offset + sizeof(One_Spectral_Data_Struct)-sizeof(uint8_t *) + Spectral_Data_Structure->Bands*Spectral_Data_Structure->PixelSize; + } + + uint64_t Spectralinfo_len; + memcpy(&Spectralinfo_len,file_data+Spectralinfo_pos+4,sizeof(uint64_t)); + // Serial0.printf("Spectralinfo_len:%d\n",Spectralinfo_len); + + offset = 4 + 8 + 2; + if(Spectralinfo_len) p->data_info = file_data + Spectralinfo_pos + offset; + else p->data_info = NULL; + + uint64_t image_len; + memcpy(&image_len,file_data+Image_pos+4,sizeof(uint64_t)); + // Serial0.printf("image_len:%d\n",image_len); + if(image_len != 0) + { + p->image_info = (picture *)(file_data + Image_pos + 4 + 8); + // p->image_info->data = (uint8_t *)(file_data + Image_pos + 4 + 8 + 2 + 8 + 100 + 10 + 1); + } + else p->image_info = NULL; + + return true; +} + + +bool picture_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap) +{ + + if (y >= 204) return 0; // 忽略底部多余区域 + + for (int j = 0; j < h; j++) { + int yy = y + j; + if (yy >= 204) continue; // 裁剪下边界 + + for (int i = 0; i < w; i++) { + int xx = x + i; + if (xx >= 324) continue; // 裁剪右边界 + + int dst_idx = yy * 324 + xx; + + buf_screen_data_canvas_1[dst_idx].full = bitmap[j * w + i]; + } + } + + return 1; +} + +void clear_screen(uint16_t color = 0xffff) // 白色,RGB565 +{ + for (int y = 0; y < 204; ++y) + { + for (int x = 0; x < 324; ++x) + { + int idx = y * 324 + x; + buf_screen_data_canvas_1[idx].full = color; + } + } +} + +void show_picture(read_file_point *p) +{ + clear_screen(0xffff); + if(p->image_info == NULL) + { + // buf_screen_data_canvas_1[dst_idx].full = bitmap[j * w + i]; + clear_screen(0xffff); + // Serial0.println("p->image_info == NULL"); + return; + } + + if( p->image_info->Squantity != 1 ) + { + clear_screen(0xffff); + // Serial0.println("no picture"); + return; + } + + uint64_t picture_len = p->image_info->length - 100 - 10 -1; + // Serial0.printf("picture_len : %d",picture_len); + char p_name[100] ; + memcpy(p_name,p->image_info->name,100); + + + xEventGroupSetBits(ui_event_group,CAM_STOP_BIT); + TJpgDec.setJpgScale(1); // 缩小 + TJpgDec.setSwapBytes(false); // RGB转换 + TJpgDec.setCallback(picture_output); + TJpgDec.drawJpg(0, 0, (uint8_t *)p->image_info + 2 + 8 + 100 + 10 + 1,picture_len); + vTaskDelay(200); + initJPEGDecoder(); +} + + + + +void pc_mode_task(void *pvParameters) +{ + while (1) + { + xEventGroupWaitBits(ui_event_group, PC_MODE_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + play_music("diannaomoshi.mp3"); + suspend_all_tasks(); + is3.reset(); + uint32_t i = 0; + uint8_t s0rx_buf[1024 * 2]; + uint8_t s1rx_buf[1024 * 2]; + while (1) + { + EventBits_t uxBits = xEventGroupGetBits(ui_event_group); + if(!(uxBits & PC_MODE_BIT)) break; + + // 1. Serial0 -> Serial1 + // if (Serial0.available()) + if(my_usart_available(0)) + { + // int byte_from_pc = Serial0.readBytes(s0rx_buf,1024); + int byte_from_pc = uart_read_bytes(0,s0rx_buf,1024*2,10); + Serial1.write(s0rx_buf,byte_from_pc); + } + + // 2. Serial1 -> Serial0 + // if (Serial1.available()) + if(my_usart_available(1)) + { + // int byte_from_uart = Serial1.readBytes(s1rx_buf,1024); + int byte_from_uart = uart_read_bytes(1,s1rx_buf,1024*2,10); + Serial0.write(s1rx_buf,byte_from_uart); + } + + if(i == 1000) + { + vTaskDelay(1); + i = 0; + } + i++; + } + // Serial1.flush(); + is3.reset(); + is3.set_shutter_time(is3.integration_time); + resume_all_tasks(); + vTaskDelay(200); + } +} + +bool read_flat_file(String file_name) +{ + String path = "/config/flatfile/" + file_name; + File file = SD_MMC.open(path); + if(!file) + { + return false; + } + file.read((uint8_t *)(&hh3_flatfile_struct),sizeof(hh3_flatfile_struct)); + file.close(); + // Serial0.printf("file_name:%s\n",file_name.c_str()); + // Serial0.printf("GroundType:%d\n",hh3_flatfile_struct.gian.GroundType); + if(hh3_flatfile_struct.gian.GroundType != 5) + { + return false; + } + // uint32_t bandnum = hh3_flatfile_struct.gian.band_num; + uint32_t bandnum = 512; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + // memcpy(&hh3_data_flat_ref,&hh3_flatfile_struct.gian,sizeof(hh3_data_flat_ref)); + for(int i = 0; i < bandnum; i++) + { + flat_structure.plot_data[i] = hh3_flatfile_struct.gian.data[i] * 10000; + } + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(ui_event_group2,flat_plot_BIT); + // if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + // { + // int32_t nn = 511; + // for (uint16_t i = 0; i < 512; i++) + // { + // guider_ui.Setting_ckb_chart_1_0->y_points[i] = flat_structure.plot_data[i]; + // nn--; + // } + // lv_chart_set_range(guider_ui.Setting_ckb_chart_1, LV_CHART_AXIS_PRIMARY_Y, 0, 1.25 * 10000); //设置坐标y轴范围 + // lv_chart_refresh(guider_ui.Setting_ckb_chart_1); + // xSemaphoreGive(lvgl_mutex); + // } + + // if(hh3_flatfile_struct.gian.GroundType == 5) + // { + // memcpy(&hh3_data_flat_ref,&hh3_flatfile_struct.gian,sizeof(hh3_data_flat_ref)); + // return true; + // } + return true; +} +//更新数据 +static lv_obj_t * plat_last_btn; // 记录上一次点击的按钮 +static void flatfile_read(lv_event_t *e) +{ + // static lv_obj_t *last_btn; // 记录上一次点击的按钮 + lv_obj_t * obj = lv_event_get_target(e); + const char *dir = lv_list_get_btn_text(guider_ui.screen_data_list_1,obj); + if(plat_last_btn && plat_last_btn != obj) + { + lv_obj_set_style_bg_color(plat_last_btn, + lv_color_hex(0xFFFFFF), // 默认背景色 + LV_PART_MAIN | LV_STATE_DEFAULT); + } + lv_obj_set_style_bg_color(obj, lv_color_hex(0x0E49F8), LV_PART_MAIN|LV_STATE_DEFAULT); + plat_last_btn = obj; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + flat_structure.flat_type = 1; + strcpy(flat_structure.flat_file_name,dir); + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(ui_event_group2, flat_BIT); + +} + +void add_flatfile_item() +{ + String path = "/config/flatfile"; + File root = SD_MMC.open(path); + if (!root || !root.isDirectory()) + { + return; + } + root.close(); + String flat_name; + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + String str = sys_doc["flatfile_name"]; + flat_name = str; + xSemaphoreGive(xMutexInventory); + } + + esp_err_t ret; + DIR *dir; + struct dirent *ent; + lv_obj_t * btn; + path = "/sdcard" + path; + const char *path_str = path.c_str(); + + if ((dir = opendir(path_str))!= NULL) + { + // 打开目录 + uint8_t num = 0; + while ((ent = readdir(dir))!= NULL) + { + if (xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + if (ent->d_type == DT_REG)// 如果是常规文件 + { + guider_ui.Setting_ckb_list_1_btn = lv_list_add_btn(guider_ui.Setting_ckb_list_1, LV_SYMBOL_FILE, (const char *)ent->d_name); + lv_obj_t* label_1 = lv_obj_get_child(guider_ui.Setting_ckb_list_1_btn, 1); + if(String(ent->d_name) == flat_name) + { + lv_obj_set_style_bg_color(guider_ui.Setting_ckb_list_1_btn, lv_color_hex(0x0E49F8), LV_PART_MAIN|LV_STATE_DEFAULT); + plat_last_btn = guider_ui.Setting_ckb_list_1_btn; + } + if (label_1 != NULL) + { + lv_label_set_long_mode(label_1, LV_LABEL_LONG_CLIP); + } + lv_obj_add_event_cb(guider_ui.Setting_ckb_list_1_btn,flatfile_read, LV_EVENT_CLICKED, NULL); + } + else if(ent->d_type == DT_DIR) + { + + } + xSemaphoreGive(lvgl_mutex); + } + num++; + if (num > 20) break; + } + closedir(dir); + read_flat_file(flat_name); + } +} +void reset_flat_sturct() +{ + hh3_data_flat_ref.name[0] = '\0'; + hh3_data_flat_ref.sensor_id[0] = '\0'; + hh3_data_flat_ref.FiberID = 0; + hh3_data_flat_ref.date = {0,0,0,0,0,0,0,0}; + hh3_data_flat_ref.integration_time = 0; + hh3_data_flat_ref.gain = 0; + hh3_data_flat_ref.data_type = 0x21; + hh3_data_flat_ref.PixelSize = 8; + hh3_data_flat_ref.GroundType = 5; + hh3_data_flat_ref.band_num = 515; + hh3_data_flat_ref.ValidFlag = 0; + // memset(hh3_data_flat_ref.data,1,sizeof(hh3_data_flat_ref.data)); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(int i = 0; i < hh3_data_flat_ref.band_num; i++) + { + hh3_data_flat_ref.data[i] = 1; + } + xSemaphoreGive(xMutexInventory); + } + + +} + + +void flat_task(void *pvParameters) +{ + reset_flat_sturct(); + uint8_t flag = 0; + bool reset = false; + while(1) + { + uint8_t flat_type; + String flat_file_name; + if(flag == 0) + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + String str = sys_doc["flatfile_name"]; + strcpy(flat_structure.flat_file_name,str.c_str()); + flat_file_name = str; + xSemaphoreGive(xMutexInventory); + } + read_flat_file(flat_file_name); + flag = 1; + } + xEventGroupWaitBits(ui_event_group2,flat_BIT,pdTRUE,pdFALSE,portMAX_DELAY); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + flat_type = flat_structure.flat_type; + flat_file_name = flat_structure.flat_file_name; + xSemaphoreGive(xMutexInventory); + } + + // Serial0.println("flat_task"); + switch (flat_type) + { + case 0: //加载 + add_flatfile_item(); + break; + case 1: //读取 + read_flat_file(flat_file_name); + reset = false; + break; + case 2: //保存 + { + if(reset) + { + reset_flat_sturct(); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + for(int i = 0; i < 512; i++) + { + flat_structure.plot_data[i] = hh3_data_flat_ref.data[i] * 10000; + // flat_structure.plot_data[i] = 1 * 10000; + } + sys_doc["flatfile_name"] = "flat_default.iris"; + updata_struct_to_ui.system_save = 1; + xSemaphoreGive(xMutexInventory); + } + } + else + { + if(hh3_flatfile_struct.gian.GroundType == 5) + { + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + sys_doc["flatfile_name"] = flat_file_name; + updata_struct_to_ui.system_save = 1; + xSemaphoreGive(xMutexInventory); + } + memcpy(&hh3_data_flat_ref,&hh3_flatfile_struct.gian,sizeof(hh3_data_flat_ref)); + } + } + + play_music("/OK2.mp3"); + break; + } + case 3: //还原默认 + { + reset = true; + + if(xSemaphoreTake(lvgl_mutex, portMAX_DELAY)) + { + for (uint16_t i = 0; i < 512; i++) + { + guider_ui.Setting_ckb_chart_1_0->y_points[i] = 1 * 10000; + + } + + lv_chart_set_range(guider_ui.Setting_ckb_chart_1, LV_CHART_AXIS_PRIMARY_Y, 0, 1.25 * 10000); //设置坐标y轴范围 + lv_chart_refresh(guider_ui.Setting_ckb_chart_1); + xSemaphoreGive(lvgl_mutex); + } + // xEventGroupSetBits(ui_event_group2,flat_plot_BIT); + play_music("/OK2.mp3"); + break; + } + + default: + break; + } + + + } +} + + + diff --git a/src/ui_task.h b/src/ui_task.h new file mode 100644 index 0000000..8e9b2cc --- /dev/null +++ b/src/ui_task.h @@ -0,0 +1,102 @@ +#ifndef UI_TASK_H +#define UI_TASK_H + +#include "header.h" +#include "driver/i2c.h" +#define system_tem_path "/system/system_tem.json" +#define califile_path "/config/califile" +#define ITEMS_PER_PAGE 10 + +#define english 0 +#define chinese 1 + +#define language chinese + +#define IS3_GET_DATA_DONE_BIT (1<<0) +#define IS3_GET_DATA_DONE0_BIT (1<<1) //plot +#define IS3_GET_DATA_DONE1_BIT (1<<2) //opt +#define IS3_GET_DATA_DONE2_BIT (1<<3) //wr +#define IS3_GET_DATA_DONE3_BIT (1<<4) //save +#define IS3_GET_DATA_DONE4_BIT (1<<5) //dc +#define IS3_GET_DATA_DONE5_BIT (1<<6) //set it +#define RED_RAY_BIT (1<<7) +#define is3_kaijimusic_bit (1<<8) +#define IS3_AVERAGE_DONE1_BIT (1<<9) +#define IS3_AVERAGE_DONE2_BIT (1<<10) +#define IS3_CAM_DONE_BIT (1<<11) +#define IS3_UPDATA_BIT (1<<12) + +void ui_task_init(void); +void updata_to_ui(void *pvParameters); +void main_2_opt(void *pvParameters); +void main_2_plot(void *pvParameters); +void main_2_dc(void *pvParameters); + +void main_2_save(void *pvParameters); +void main_2_wr(void *pvParameters); +void get_is3_data(void *pvParameters); +void set_it_task(void *pvParameters); +void main_2_rad(void *pvParameters); +void red_ray_task(void* arg); + +void power_off_task(void *pvParameters); +void play_music_task(void* arg); +void set_system_time_task(void *pvParameters); +// void kaiji_music_task(void* arg); +void calculate_average_task(void *pvParameters); +void fiber_sel_task(void* arg); +void ui_time_task(void *pvParameters); + +void show_data_task(void *pvParameters); +void save_path_task(void *pvParameters); + +void save_system_info(String system_info); +String read_system_info(); + +bool califile_init(); +bool califile_read(uint32_t califile_num); +void system_data_init(); +// void play_music(String music_path); +// void play_music(const char *music_path); +// void header_pack(); +void header_pack(String SpectralInfo_str); +void Ground_DN_pack(); +void wr_dn_pack(bool flag); +void dark_dn_pack(bool flag); +void flat_ref_pack(); +void picture_pack(String name,String num); +void dark_dn_collect(); +uint32_t find_califile(String fiber_type, String fiber_length, String fiber_width); + +// void save_data(String path ,String name); +void save_data(String path ,String name,String SpectralInfo_str); + +void print_heap_detail(); + +static uint32_t show_page(String path, uint32_t page); +// static uint32_t get_file_num(String path); +static uint32_t get_file_num(String path_str); +bool read_data_from_file(String path); + +bool file_data_analysis(read_file_point *p); +void change_plot_type(read_file_point file_point,uint8_t plot_type); +void show_picture(read_file_point *p); + +void pc_mode_task(void *pvParameters); +void flat_task(void *pvParameters); +void reset_flat_sturct(); + +extern EventGroupHandle_t is3_event_group; +extern uint32_t system_time_count; +extern picture image_picture; +#endif + + +////////////// +/* +数据查看 + + + +*/ +////////////// \ No newline at end of file diff --git a/src/usb_camera.cpp b/src/usb_camera.cpp new file mode 100644 index 0000000..eeaf762 --- /dev/null +++ b/src/usb_camera.cpp @@ -0,0 +1,364 @@ +// #include "usb_camera.h" +// USB_STREAM *usb = NULL; +// static uint8_t *_xferBufferA = NULL; +// static uint8_t *_xferBufferB = NULL; +// static uint8_t *_frameBuffer = NULL; + + +// const uint16_t img_width = 480; +// const uint16_t img_height = 320; + +// const uint16_t CROP_WIDTH = 480; +// const uint16_t CROP_HEIGHT = 230; + + +// static uint8_t camera_status = 0; //0停止 1运行 +// volatile uint16_t camera_W = 480; +// lv_color_t * picture_buff=NULL; +// bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap) +// { +// // if (y >= CROP_HEIGHT) return 0; // 忽略底部多余区域 + +// for (int j = 0; j < h; j++) { +// int yy = y + j; +// if (yy >= CROP_HEIGHT) continue; // 裁剪下边界 + +// for (int i = 0; i < w; i++) { +// int xx = x + i; +// // if (xx >= CROP_WIDTH) continue; // 裁剪右边界 + +// int dst_idx = yy * CROP_WIDTH + xx; + +// picture_buff[dst_idx].full = bitmap[j * w + i]; +// } +// } + +// return 1; +// } + +// void initJPEGDecoder() { +// TJpgDec.setJpgScale(1); // 原尺寸 +// TJpgDec.setSwapBytes(false); // RGB 转换 +// TJpgDec.setCallback(tft_output); +// } + + +// uint8_t get_cam_status() +// { +// return camera_status; +// } + + + + +// uint16_t last_length = 0; +// static void onCameraFrameCallback(uvc_frame *frame, void *user_ptr) +// { +// // Serial0.printf("uvc callback! frame_format = %d, seq = %" PRIu32 ", width = %" PRIu32", height = %" PRIu32 ", length = %u, ptr = %d\n", +// // frame->frame_format, frame->sequence, frame->width, frame->height, frame->data_bytes, (int)user_ptr); + +// // uint8_t *ptr = (uint8_t *)frame->data; +// // if (!(ptr[frame->data_bytes - 2] == 0xFF && ptr[frame->data_bytes - 1] == 0xD9)) return; +// if(camera_status == 0 ) +// { +// camera_status = 1 ; +// } + + + +// // if((camera_W == 800) && (image_picture.length == 0)) +// // { + +// // uint8_t *ptr = (uint8_t *)frame->data; +// // if(frame->frame_format == UVC_FRAME_FORMAT_MJPEG) +// // { +// // if(ptr[0] != 0xFF || ptr[1] != 0xD8 || // SOI +// // ptr[frame->data_bytes - 2] != 0xFF || ptr[frame->data_bytes - 1] != 0xD9) // EOI +// // { +// // // Serial0.println("data not eghough"); +// // if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) +// // { +// // image_picture.length = last_length; +// // xSemaphoreGive(xMutexInventory); +// // } +// // return; // 数据不完整 +// // } +// // } + + +// if((camera_W == 800)) +// { + +// uint8_t *ptr = (uint8_t *)frame->data; +// if(frame->frame_format == UVC_FRAME_FORMAT_MJPEG) +// { +// if(ptr[0] != 0xFF || ptr[1] != 0xD8 || // SOI +// ptr[frame->data_bytes - 2] != 0xFF || ptr[frame->data_bytes - 1] != 0xD9) // EOI +// { +// // Serial0.println("data not eghough"); +// if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) +// { +// image_picture.length = last_length; +// xSemaphoreGive(xMutexInventory); +// } +// return; // 数据不完整 +// } +// } + +// if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) +// { +// memcpy(image_picture.data, frame->data, frame->data_bytes); + +// image_picture.length = frame->data_bytes; +// last_length = frame->data_bytes; +// xSemaphoreGive(xMutexInventory); +// } +// xEventGroupSetBits(is3_event_group,IS3_CAM_DONE_BIT); +// } + +// TJpgDec.drawJpg(0, 0, (uint8_t *)frame->data,frame->data_bytes); +// memcpy(buf_main_2_canvas_2,picture_buff,CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t)); +// vTaskDelay(1); + + +// xEventGroupSetBits(ui_event_group,CAM_DONE_BIT); + + +// } +// void CAM_start_task(void *pvParameters) +// { +// while(1) +// { +// xEventGroupWaitBits(ui_event_group,CAM_START_BIT,pdTRUE,pdFALSE,portMAX_DELAY); +// // Serial0.println("CAM_start_task"); +// if(camera_status == 1) continue; +// if(camera_W == 800) continue; + +// usb->uvcCamResume(NULL); +// // camera_status = 1; +// } +// } + +// void CAM_stop_task(void *pvParameters) +// { +// while(1) +// { +// xEventGroupWaitBits(ui_event_group,CAM_STOP_BIT,pdTRUE,pdFALSE,portMAX_DELAY); +// // Serial0.println("CAM_stop_task"); +// if(camera_status == 0) continue; +// if(camera_W == 800) continue; + +// usb->uvcCamSuspend(NULL); +// camera_status = 0; +// } +// } +// esp_err_t USB_CAM_Init() +// { +// initJPEGDecoder(); +// usb = new USB_STREAM(); + +// buf_main_2_canvas_2 = (lv_color_t *)heap_caps_malloc(CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + +// picture_buff = (lv_color_t *)heap_caps_malloc(CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + +// buf_screen_data_canvas_1 = (lv_color_t *)heap_caps_malloc(324 * 204 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + +// _xferBufferA = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); +// assert(_xferBufferA != NULL); +// _xferBufferB = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); +// assert(_xferBufferB != NULL); +// _frameBuffer = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); +// assert(_frameBuffer != NULL); + +// // cam_mode(1); +// usb->uvcConfiguration(480, 320, FRAME_INTERVAL_FPS_15, 55 * 1024, _xferBufferA, _xferBufferB, 55 * 1024, _frameBuffer); +// // uvc_set_ctrl(); +// camera_W = 480; +// usb->uvcCamRegisterCb(&onCameraFrameCallback, NULL); +// usb->start(); +// usb->connectWait(1000); + +// xTaskCreatePinnedToCore(CAM_start_task, "CAM_start_task", 1024*3, NULL, 1, NULL, 1); +// xTaskCreatePinnedToCore(CAM_stop_task, "CAM_stop_task", 1024*3, NULL, 1, NULL, 1); + +// // usb_streaming_ctrl_camera(); +// return ESP_OK; +// } + +// esp_err_t USB_CAM_SetResolution(uint16_t cam_width, uint16_t cam_height) +// { +// // 停止当前摄像头 +// // usb->stop(); + +// // 重新配置摄像头分辨率和其他参数 +// // usb->uvcConfiguration(cam_width, cam_height, FRAME_INTERVAL_FPS_15, 55 * 1024, _xferBufferA, _xferBufferB, 55 * 1024, _frameBuffer); + +// // 重新启动摄像头 +// // usb->start(); + +// // usb->uvcCamSuspend(NULL); +// // camera_status = 0; +// // // Serial0.println("width = " + String(cam_width) + " height = " + String(cam_height)); +// // usb->uvcCamFrameReset(cam_width, cam_height, FRAME_INTERVAL_FPS_15); + +// // if(get_cam_status() == 0) xEventGroupSetBits(ui_event_group,CAM_START_BIT); + +// // usb->uvcCamResume(NULL); + + +// // int scroll_x = lv_obj_get_scroll_x(guider_ui.main_2_tileview_1); + +// // if(cam_width == 800) +// // { +// // usb->uvcCamResume(NULL); +// // } + +// // if(cam_width == 400) +// // { +// // if(scroll_x == 0) usb->uvcCamSuspend(NULL); +// // } +// camera_status = 0; +// camera_W = cam_width; +// return ESP_OK; +// } + +///////////////////////////////////////////////////////////////////////////// + +#include "usb_camera.h" +#include +static uint8_t *_xferBufferA = NULL; + + + +const uint16_t img_width = 480; +const uint16_t img_height = 320; + +const uint16_t CROP_WIDTH = 480; +const uint16_t CROP_HEIGHT = 230; + + +static uint8_t camera_status = 0; //0停止 1运行 +volatile uint16_t camera_W = 480; +lv_color_t * picture_buff=NULL; +bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap) +{ + // if (y >= CROP_HEIGHT) return 0; // 忽略底部多余区域 + + for (int j = 0; j < h; j++) { + int yy = y + j; + if (yy >= CROP_HEIGHT) continue; // 裁剪下边界 + + for (int i = 0; i < w; i++) { + int xx = x + i; + // if (xx >= CROP_WIDTH) continue; // 裁剪右边界 + + int dst_idx = yy * CROP_WIDTH + xx; + + picture_buff[dst_idx].full = bitmap[j * w + i]; + } + } + + return 1; +} + +void initJPEGDecoder() { + TJpgDec.setJpgScale(1); // 原尺寸 + TJpgDec.setSwapBytes(false); // RGB 转换 + TJpgDec.setCallback(tft_output); +} + + +uint8_t get_cam_status() +{ + return camera_status; +} + + + + +uint16_t last_length = 0; +static void onCameraFrameCallback(void* event_handler_arg,esp_event_base_t event_base,int32_t event_id,void* event_data) +{ + + if(camera_status == 0 ) + { + camera_status = 1 ; + } + + + size_t len = 0; + while(Serial.available()) + { + len += Serial.readBytes(_xferBufferA + len, 55 * 1024); + } + + + + if(camera_W == 800 && len > 0) + { + + uint8_t *ptr = _xferBufferA; + if(ptr[0] != 0xFF || ptr[1] != 0xD8 || // SOI + ptr[len - 2] != 0xFF || ptr[len - 1] != 0xD9) // EOI + { + // Serial0.println("data not eghough"); + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + image_picture.length = last_length; + xSemaphoreGive(xMutexInventory); + } + return; // 数据不完整 + } + + if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) + { + memcpy(image_picture.data, _xferBufferA, len); + + image_picture.length = len; + last_length = len; + xSemaphoreGive(xMutexInventory); + } + xEventGroupSetBits(is3_event_group,IS3_CAM_DONE_BIT); + } + + if(len > 0) + { + TJpgDec.drawJpg(0, 0, _xferBufferA,len); + memcpy(buf_main_2_canvas_2,picture_buff,CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t)); + xEventGroupSetBits(ui_event_group,CAM_DONE_BIT); + } + vTaskDelay(1); +} + +esp_err_t USB_CAM_Init() +{ + initJPEGDecoder(); + // usb = new USB_STREAM(); + + Serial.onEvent(onCameraFrameCallback); + Serial.setRxBufferSize(1024 * 20); + Serial.setTimeout(10); + Serial.begin(12000000); + //lvgl + buf_main_2_canvas_2 = (lv_color_t *)heap_caps_malloc(CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + buf_screen_data_canvas_1 = (lv_color_t *)heap_caps_malloc(324 * 204 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + // + picture_buff = (lv_color_t *)heap_caps_malloc(CROP_WIDTH * CROP_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); + + _xferBufferA = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); + assert(_xferBufferA != NULL); + // _xferBufferB = (uint8_t *)heap_caps_malloc(55 * 1024, MALLOC_CAP_SPIRAM); + // assert(_xferBufferB != NULL); + + + + return ESP_OK; +} + +esp_err_t USB_CAM_SetResolution(uint16_t cam_width, uint16_t cam_height) +{ + + camera_status = 0; + camera_W = cam_width; + return ESP_OK; +} \ No newline at end of file diff --git a/src/usb_camera.h b/src/usb_camera.h new file mode 100644 index 0000000..477b6cb --- /dev/null +++ b/src/usb_camera.h @@ -0,0 +1,20 @@ +#ifndef __USB_CAMERA_H__ +#define __USB_CAMERA_H__ +#include +#include "header.h" +#include "USB_STREAM.h" +// #include + +esp_err_t USB_CAM_Init(); +// esp_err_t USB_CAM_SetResolution(uint16_t width, uint16_t height); +esp_err_t USB_CAM_SetResolution(uint16_t cam_width, uint16_t cam_height); +uint8_t get_cam_status(); +void initJPEGDecoder(); +typedef struct { + + uint16_t width; + uint16_t height; + uint8_t *data; +} cam_jpg_data; + +#endif \ No newline at end of file diff --git a/src/wifi_client.cpp b/src/wifi_client.cpp new file mode 100644 index 0000000..65aab9a --- /dev/null +++ b/src/wifi_client.cpp @@ -0,0 +1,115 @@ +#include "wifi_client.h" + + +const char* ssid = "123"; +const char* password = "12345678"; + +const char* host = "192.168.144.57"; // 服务器 IP 地址 +const uint16_t port = 1133; // TCP 端口 + +WiFiClient client; +bool wifi_init() +{ + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + Serial0.println("Connecting to WiFi..."); + uint8_t i = 0; + while (WiFi.status() != WL_CONNECTED) + { + i++; + if (i > 20) + { + Serial0.println("Failed to connect to WiFi."); + // continue; + // ESP.restart(); + return false; + } + Serial0.print("."); + vTaskDelay(100); + } + + Serial0.println("\nWiFi connected."); + Serial0.print("IP address: "); + Serial0.println(WiFi.localIP()); + + configTime(8 * 3600, 0, "pool.ntp.org", "ntp.aliyun.com"); + + struct tm timeinfo; + while (!getLocalTime(&timeinfo)) { + Serial0.println("Waiting for NTP time..."); + delay(500); + } + + Serial0.printf("NTP Time: %04d-%02d-%02d %02d:%02d:%02d\n", + timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, + timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + + + ds1307_set_time(timeinfo.tm_year + 1900,timeinfo.tm_mon + 1, timeinfo.tm_mday,timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec); + +// // 连接 TCP 服务器 +// Serial0.printf("Connecting to %s:%d ...\n", host, port); +// if (client.connect(host, port)) { +// Serial0.println("Connected to server."); +// client.println("Hello from ESP32-S3!"); +// // client.stop(); // 关闭连接 +// } else { +// Serial0.println("Connection to server failed."); +// } +// client.println("Hello World" + String(count)); +// xTaskCreatePinnedToCore(wifi_connect_to, "wifi_connect_to", 1024*5, NULL, 1, NULL, 0); +} + + +void wifi_connect_to(void *pvParameters) +{ + while(1) + { + xEventGroupWaitBits(ui_event_group, WIFI_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + Serial0.println("Connecting to WiFi..."); + uint8_t i = 0; + while (WiFi.status() != WL_CONNECTED) + { + i++; + if (i > 20) + { + Serial0.println("Failed to connect to WiFi."); + continue; + // ESP.restart(); + // return false; + } + Serial0.print("."); + vTaskDelay(100); + } + + Serial0.println("\nWiFi connected."); + Serial0.print("IP address: "); + Serial0.println(WiFi.localIP()); + + configTime(8 * 3600, 0, "pool.ntp.org", "ntp.aliyun.com"); + + struct tm timeinfo; + while (!getLocalTime(&timeinfo)) { + Serial0.println("Waiting for NTP time..."); + delay(500); + } + + Serial0.printf("NTP Time: %04d-%02d-%02d %02d:%02d:%02d\n", + timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, + timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + + + ds1307_set_time(timeinfo.tm_year + 1900,timeinfo.tm_mon + 1, timeinfo.tm_mday,timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec); + } +} + + +// uint32_t count = 0; +// void loop() +// { + +// count++; +// vTaskDelay(1000 / portTICK_PERIOD_MS); +// } diff --git a/src/wifi_client.h b/src/wifi_client.h new file mode 100644 index 0000000..fe4402e --- /dev/null +++ b/src/wifi_client.h @@ -0,0 +1,12 @@ +#ifndef WIFI_CLIENT_H +#define WIFI_CLIENT_H + + +#include "header.h" +#include "WiFi.h" +void wifi_connect_to(void *pvParameters); +bool wifi_init(); + +#endif + + diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..3a38bab --- /dev/null +++ b/version.txt @@ -0,0 +1,3 @@ +//2026/03/23 +//发行版 +V2.0.2.5 \ No newline at end of file