新增文件检测功能,找不到索引就新建文件

This commit is contained in:
ipason 2025-04-21 17:46:58 +08:00
parent 0095bbf59d
commit a2605479a5
2 changed files with 33 additions and 17 deletions

View File

@ -40,7 +40,7 @@ extern "C" {
#define LEDC_DUTY (4096) // Set duty to 50%. (2 ** 13) * 50% = 4096 #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 LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz
#define DEFAULT_LIGHT (9000) #define DEFAULT_LIGHT (900)
/* 定义进行电池AD采样的通道 */ /* 定义进行电池AD采样的通道 */
#define BATTERY_ADC_CHANNEL ADC_CHANNEL_8 #define BATTERY_ADC_CHANNEL ADC_CHANNEL_8

View File

@ -251,21 +251,35 @@ void flux_sd_init(void)
/** /**
* @brief * @brief
* *
* * 1.
* *
* *
* 2.
*/ */
esp_err_t sd_current_fileIndex_get(void) esp_err_t sd_current_fileIndex_get(void)
{ {
/* 读取当前文件索引值 */ /* 读取当前文件索引值 */
ESP_LOGI(SD_TAG, "Opening file %s", INDEX_FILE_NAME); ESP_LOGI(SD_TAG, "Opening file %s", INDEX_FILE_NAME);
/* 判断文件是否存在 */
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"); FILE *f = fopen(INDEX_FILE_NAME, "r");
if (f == NULL) { if (f == NULL) {
ESP_LOGE(SD_TAG, "Failed to open file for reading"); ESP_LOGE(SD_TAG, "Failed to open file for reading");
return ESP_FAIL; return ESP_FAIL;
} }
char line[EXAMPLE_MAX_CHAR_SIZE]; char line[EXAMPLE_MAX_CHAR_SIZE];
fgets(line, sizeof(line), f); fgets(line, sizeof(line), f);
@ -273,15 +287,17 @@ esp_err_t sd_current_fileIndex_get(void)
sdData.file_index = atoi(line); sdData.file_index = atoi(line);
ESP_LOGI(SD_TAG, "Read from file: '%s' , current Index = %d", line,sdData.file_index); ESP_LOGI(SD_TAG, "Read from file: '%s' , current Index = %d", line,sdData.file_index);
}
/* 重置索引值 */ /* 重置索引值 */
f = fopen(INDEX_FILE_NAME, "w"); FILE *new_index_f = fopen(INDEX_FILE_NAME, "w");
if (f == NULL) if (new_index_f == NULL)
{ {
return ESP_FAIL; return ESP_FAIL;
} }
fprintf(f, "%d", sdData.file_index+1); fprintf(new_index_f, "%d", sdData.file_index+1);
fclose(f); fclose(new_index_f);
return ESP_OK; return ESP_OK;
} }