#include "datasave.h" void save_history(const PSRAMVector &history) { // 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(); } else { LOG_INFO("SPIFFS mounted successfully"); return; } std::ofstream ofs("/spiffs/ignA_history.csv", std::ios::out | std::ios::trunc); if (ofs.fail()) { LOG_ERROR("Failed to open file for writing"); return; } //write csv header 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"; for (auto &entry : history) { ofs << std::to_string(entry.timestamp) << "," << std::to_string(entry.coils12.n_events) << "," << std::to_string(entry.coils12.spark_delay) << "," << std::string(sparkStatusNames.at(entry.coils12.spark_status)) << "," << std::to_string(entry.coils12.peak_p_in) << "," << std::to_string(entry.coils12.peak_n_in) << "," << std::to_string(entry.coils12.peak_p_out) << "," << std::to_string(entry.coils12.peak_n_out) << "," << std::string(softStartStatusNames.at(entry.coils12.sstart_status)) << "," << std::to_string(entry.coils34.n_events) << "," << std::to_string(entry.coils34.spark_delay) << "," << std::string(sparkStatusNames.at(entry.coils34.spark_status)) << "," << std::to_string(entry.coils34.peak_p_in) << "," << std::to_string(entry.coils34.peak_n_in) << "," << std::to_string(entry.coils34.peak_p_out) << "," << std::to_string(entry.coils34.peak_n_out) << "," << std::string(softStartStatusNames.at(entry.coils34.sstart_status)) << "," << std::to_string(entry.eng_rpm) << "," << std::to_string(entry.adc_read_time) << "," << std::to_string(entry.n_queue_errors); ofs << std::endl; } auto written_bytes = ofs.tellp(); ofs.flush(); ofs.close(); LOG_INFO("Ignition A history saved to SPIFFS, bytes written: ", (uint32_t)written_bytes); SPIFFS.end(); // unmount SPIFFS to ensure data is written and avoid corruption on next mount }