From a2605479a526d13775a72c7bb638f6a4ea157db6 Mon Sep 17 00:00:00 2001 From: ipason Date: Mon, 21 Apr 2025 17:46:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=87=E4=BB=B6=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=89=BE=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E5=B0=B1=E6=96=B0=E5=BB=BA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/FluxPower/Flux_Power.h | 2 +- components/FluxSD/FluxSD.c | 48 ++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/components/FluxPower/Flux_Power.h b/components/FluxPower/Flux_Power.h index 64902a7..f9f6010 100644 --- a/components/FluxPower/Flux_Power.h +++ b/components/FluxPower/Flux_Power.h @@ -40,7 +40,7 @@ extern "C" { #define LEDC_DUTY (4096) // Set duty to 50%. (2 ** 13) * 50% = 4096 #define LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz -#define DEFAULT_LIGHT (9000) +#define DEFAULT_LIGHT (900) /* 定义进行电池AD采样的通道 */ #define BATTERY_ADC_CHANNEL ADC_CHANNEL_8 diff --git a/components/FluxSD/FluxSD.c b/components/FluxSD/FluxSD.c index 091f459..cb353c4 100644 --- a/components/FluxSD/FluxSD.c +++ b/components/FluxSD/FluxSD.c @@ -251,37 +251,53 @@ void flux_sd_init(void) /** * @brief 获取文件索引 * - * 每次开机后重置当前文件索引值 + * 1.每次开机后重置当前文件索引值 * 以该索引值新建日志文件、测试结果输出文件 * + * 2.当用于存储索引的文件不存在时进行自动新建索引文件 */ esp_err_t sd_current_fileIndex_get(void) { /* 读取当前文件索引值 */ ESP_LOGI(SD_TAG, "Opening file %s", INDEX_FILE_NAME); - FILE *f = fopen(INDEX_FILE_NAME, "r"); - if (f == NULL) { - ESP_LOGE(SD_TAG, "Failed to open file for reading"); - return ESP_FAIL; + /* 判断文件是否存在 */ + if (access(INDEX_FILE_NAME, F_OK) == -1) + { + /*当文件不存在时创建文件*/ + FILE *new_f = fopen(INDEX_FILE_NAME, "w"); + fprintf(new_f, "0"); + fclose(new_f); + sdData.file_index = 0; + + ESP_LOGI(SD_TAG, "File not found, creating new file"); + + }else{ + + /* 当文件存在时直接获取当前索引值 */ + FILE *f = fopen(INDEX_FILE_NAME, "r"); + if (f == NULL) { + ESP_LOGE(SD_TAG, "Failed to open file for reading"); + return ESP_FAIL; + } + char line[EXAMPLE_MAX_CHAR_SIZE]; + fgets(line, sizeof(line), f); + + fclose(f); + sdData.file_index = atoi(line); + ESP_LOGI(SD_TAG, "Read from file: '%s' , current Index = %d", line,sdData.file_index); + } - char line[EXAMPLE_MAX_CHAR_SIZE]; - fgets(line, sizeof(line), f); - - fclose(f); - sdData.file_index = atoi(line); - ESP_LOGI(SD_TAG, "Read from file: '%s' , current Index = %d", line,sdData.file_index); - /* 重置索引值 */ - f = fopen(INDEX_FILE_NAME, "w"); - if (f == NULL) + FILE *new_index_f = fopen(INDEX_FILE_NAME, "w"); + if (new_index_f == NULL) { return ESP_FAIL; } - fprintf(f, "%d", sdData.file_index+1); - fclose(f); + fprintf(new_index_f, "%d", sdData.file_index+1); + fclose(new_index_f); return ESP_OK; }