From 07eb06f67bdd6f2c541bdd68b468bb42958ab766 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Wed, 8 Apr 2026 10:27:18 +0200 Subject: [PATCH] Save History Sync on flash, still some issues in deleting previous file --- .../partitions/no_ota_10mb_spiffs.csv | 4 +- RotaxMonitor/platformio.ini | 2 +- RotaxMonitor/src/datasave.cpp | 49 ++++++++++--------- RotaxMonitor/src/datasave.h | 36 +++++++++----- RotaxMonitor/src/main.cpp | 10 ++-- 5 files changed, 59 insertions(+), 42 deletions(-) diff --git a/RotaxMonitor/partitions/no_ota_10mb_spiffs.csv b/RotaxMonitor/partitions/no_ota_10mb_spiffs.csv index 0ee5735..c585eb5 100644 --- a/RotaxMonitor/partitions/no_ota_10mb_spiffs.csv +++ b/RotaxMonitor/partitions/no_ota_10mb_spiffs.csv @@ -2,5 +2,5 @@ # Name, Type, SubType, Offset, Size nvs, data, nvs, 0x9000, 0x4000 phy_init, data, phy, 0xd000, 0x1000 -factory, app, factory, 0x10000, 0x5F0000 -spiffs, data, spiffs, 0x600000, 0xA00000 \ No newline at end of file +factory, app, factory, 0x10000, 0x300000 +spiffs, data, spiffs, 0x310000, 0xCF0000 \ No newline at end of file diff --git a/RotaxMonitor/platformio.ini b/RotaxMonitor/platformio.ini index 808c594..9001479 100644 --- a/RotaxMonitor/platformio.ini +++ b/RotaxMonitor/platformio.ini @@ -47,7 +47,7 @@ lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps} ;Upload protocol configuration upload_protocol = esptool -upload_port = COM4 +upload_port = COM8 upload_speed = 921600 ;Monitor configuration diff --git a/RotaxMonitor/src/datasave.cpp b/RotaxMonitor/src/datasave.cpp index ccf52bf..7387dc4 100644 --- a/RotaxMonitor/src/datasave.cpp +++ b/RotaxMonitor/src/datasave.cpp @@ -1,45 +1,51 @@ #include "datasave.h" static const size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB) -static bool first_save = true; // flag to indicate if this is the first save (to write header) -void save_history(const PSRAMVector &history, const std::filesystem::path &file_path) +void saveHistoryTask(void *pvParameters) +{ + const auto *params = static_cast(pvParameters); + const auto &history = *params->history; + const auto &file_path = params->file_path; + if (!params) { + LOG_ERROR("Invalid parameters for saveHistoryTask"); + return; + } + LOG_DEBUG("Starting saving: ", file_path.c_str()); + save_history(history, file_path); + vTaskDelete(NULL); +} + +void save_history(const PSRAMVector &history, const std::filesystem::path &file_name) { // Initialize SPIFFS - if (!SPIFFS.begin(true)) - { - LOG_ERROR("Failed to mount SPIFFS"); - LOG_ERROR("5 seconds to restart..."); - vTaskDelay(pdMS_TO_TICKS(5000)); - esp_restart(); - } + auto spiffs_guard = SPIFFSGuard(); // use RAII guard to ensure SPIFFS is properly mounted and unmounted - LOG_INFO("SPIFFS mounted successfully"); if (SPIFFS.totalBytes() - SPIFFS.usedBytes() < min_free ) // check if at least 1MB is free for saving history { LOG_ERROR("Not enough space in SPIFFS to save history"); return; } - std::filesystem::path to_save = file_path; - if (file_path.root_path() != "/spiffs") - to_save = std::filesystem::path("/spiffs") / file_path; + std::filesystem::path file_path = file_name; + if (file_name.root_path() != "/spiffs") + file_path = std::filesystem::path("/spiffs") / file_name; auto save_flags = std::ios::out; - if (first_save && SPIFFS.exists(to_save.c_str())) + if (first_save && SPIFFS.exists(file_path.c_str())) { first_save = false; save_flags |= std::ios::trunc; // overwrite existing file - SPIFFS.remove(to_save.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS - LOG_INFO("Saving history to SPIFFS, new file: ", to_save.c_str()); + SPIFFS.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS + LOG_INFO("Saving history to SPIFFS, new file:", file_path.c_str()); } else { save_flags |= std::ios::app; // append to new file - LOG_INFO("Saving history to SPIFFS, appending to existing file: ", to_save.c_str()); + LOG_INFO("Saving history to SPIFFS, appending to existing file:", file_path.c_str()); } - std::ofstream ofs(to_save, save_flags); + std::ofstream ofs(file_path, save_flags); if (ofs.fail()) { LOG_ERROR("Failed to open file for writing"); @@ -50,9 +56,9 @@ void save_history(const PSRAMVector &history, const std::file if (first_save) { ofs << "TS,\ - EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\ - EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\ - ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS" << std::endl; + EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\ + EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\ + ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS" << std::endl; ofs.flush(); } @@ -84,5 +90,4 @@ void save_history(const PSRAMVector &history, const std::file ofs.close(); LOG_INFO("Ignition A history saved to SPIFFS, records written: ", history.size()); - SPIFFS.end(); // unmount SPIFFS to ensure data is written and avoid corruption on next mount } diff --git a/RotaxMonitor/src/datasave.h b/RotaxMonitor/src/datasave.h index 109e3e0..60e7599 100644 --- a/RotaxMonitor/src/datasave.h +++ b/RotaxMonitor/src/datasave.h @@ -1,4 +1,5 @@ #pragma once +#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO // System Includes #include @@ -14,6 +15,7 @@ const uint32_t max_history = 256; const bool SAVE_HISTORY_TO_SPIFFS = true; // Set to true to enable saving history to SPIFFS, false to disable +static bool first_save = true; // flag to indicate if this is the first save (to write header) struct dataSaveParams { @@ -21,18 +23,26 @@ struct dataSaveParams const std::filesystem::path file_path; }; +class SPIFFSGuard +{ +public: + SPIFFSGuard() { + if (!SPIFFS.begin(true)) { + LOG_ERROR("Failed to mount SPIFFS"); + LOG_ERROR("5 seconds to restart..."); + vTaskDelay(pdMS_TO_TICKS(5000)); + esp_restart(); + } + LOG_DEBUG("SPIFFS mounted successfully"); + } + + ~SPIFFSGuard() { + SPIFFS.end(); + LOG_DEBUG("SPIFFS unmounted successfully"); + } +}; + +// Task and function declarations +void saveHistoryTask(void *pvParameters); void save_history(const PSRAMVector &history, const std::filesystem::path& file_path); -static void saveHistoryTask(void *pvParameters) -{ - const auto *params = static_cast(pvParameters); - const auto &history = *params->history; - const auto &file_path = params->file_path; - if (!params) { - LOG_ERROR("Invalid parameters for saveHistoryTask"); - return; - } - LOG_INFO("Starting saving: ", file_path.c_str()); - save_history(history, file_path); - //vTaskDelete(NULL); -} diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index 050146a..65a2ec8 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -189,10 +189,10 @@ void loop() auto *temp = active_history; active_history = writable_history; // switch active and writable buffers writable_history = temp; // ensure writable_history points to the buffer we just filled - dataSaveParams save_params{ + dataSaveParams save_params { .history = writable_history, .file_path = "ignition_history.csv"}; - saveHistoryTask(&save_params); // directly call the save task function to save without delay, since we already switched buffers and writable_history is now empty and ready for new data + save_history(*writable_history, "ignition_history.csv"); // directly call the save task function to save without delay, since we already switched buffers and writable_history is now empty and ready for new data // if (SAVE_HISTORY_TO_SPIFFS) // if (pdFAIL == // xTaskCreatePinnedToCore( @@ -220,10 +220,12 @@ void loop() Serial.println("Waiting for data... "); if (!partial_save && counter > 0) // if timeout occurs but we have unsaved data, save it before next timeout { + active_history->resize(counter); // resize active history to actual number of records received to avoid saving empty records + save_history(*active_history, "ignition_history.csv"); + active_history->resize(max_history); // resize back to max history size for next data cycle counter = 0; // reset counter after saving partial_save = true; - Serial.println("Saving history to SPIFFS..."); - save_history(*active_history, "ignition_history.csv"); + first_save = true; } delay(500); }