From a7596c3499582a51c6d68b1df64938937ed48500 Mon Sep 17 00:00:00 2001 From: bsongis Date: Sat, 21 Apr 2012 16:19:27 +0000 Subject: [PATCH] SD card Models Restore now working! --- src/eeprom_avr.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ src/eeprom_avr.h | 3 ++- src/gruvin9x/ff.h | 2 +- src/model_menus.cpp | 48 +++++++++++++++++------------------ 4 files changed, 87 insertions(+), 27 deletions(-) diff --git a/src/eeprom_avr.cpp b/src/eeprom_avr.cpp index 0f7758558..8919b1cac 100644 --- a/src/eeprom_avr.cpp +++ b/src/eeprom_avr.cpp @@ -577,6 +577,67 @@ const pm_char * eeArchiveModel(uint8_t i_fileSrc) f_close(&archiveFile); return NULL; } + +const pm_char * eeRestoreModel(uint8_t i_fileDst, char *model_name) +{ + char *buf = reusableBuffer.model_name; + FIL restoreFile; + UINT read; + + FRESULT result = f_mount(0, &g_FATFS_Obj); + if (result != FR_OK) { + return SDCARD_ERROR(result); + } + + strcpy_P(buf, PSTR(MODELS_PATH "/")); // TODO Not PSTR everywhere ... + strcpy(&buf[sizeof(MODELS_PATH)], model_name); + strcpy_P(&buf[strlen(buf)], PSTR(".bin")); // TODO Not PSTR everywhere ... + + result = f_open(&restoreFile, buf, FA_OPEN_EXISTING | FA_READ); + if (result != FR_OK) { + return SDCARD_ERROR(result); + } + + result = f_read(&restoreFile, (uint8_t *)buf, 1, &read); + if (result != FR_OK || read != 1) { + return SDCARD_ERROR(result); + } + + if (buf[0] != g_eeGeneral.myVers) { + // TODO + } + + theFile.create(FILE_MODEL(i_fileDst), FILE_TYP_MODEL, true); + + do { + result = f_read(&restoreFile, (uint8_t *)buf, 15, &read); + if (result != FR_OK) { + s_sync_write = false; + return SDCARD_ERROR(result); + } + if (read > 0) { + theFile.write((uint8_t *)buf, read); + if (write_errno() != 0) { + s_sync_write = false; + return STR_EEPROMOVERFLOW; + } + } + } while (read == 15); + + uint8_t fri=0; + if (theFile.m_currBlk && (fri=EeFsGetLink(theFile.m_currBlk))) + EeFsSetLink(theFile.m_currBlk, 0); + + if (fri) EeFsFree(fri); //chain in + + eeFs.files[FILE_TMP].size = theFile.m_pos; + EFile::swap(theFile.m_fileId, FILE_TMP); // s_sync_write is set to false in swap(); + + assert(!theFile.m_write_step); + + f_close(&restoreFile); + return NULL; +} #endif void RlcFile::writeRlc(uint8_t i_fileId, uint8_t typ, uint8_t*buf, uint16_t i_len, uint8_t sync_write) diff --git a/src/eeprom_avr.h b/src/eeprom_avr.h index 5379a4eba..012b600d3 100644 --- a/src/eeprom_avr.h +++ b/src/eeprom_avr.h @@ -120,7 +120,7 @@ class EFile uint8_t read(uint8_t*buf, uint16_t i_len); - protected: +// protected: uint8_t m_fileId; //index of file in directory = filename uint16_t m_pos; //over all filepos @@ -216,6 +216,7 @@ uint16_t evalChkSum(); #ifdef SDCARD const pm_char * eeArchiveModel(uint8_t i_fileSrc); +const pm_char * eeRestoreModel(uint8_t i_fileDst, char *model_name); #endif #endif diff --git a/src/gruvin9x/ff.h b/src/gruvin9x/ff.h index 5fe8e23da..6178492bb 100644 --- a/src/gruvin9x/ff.h +++ b/src/gruvin9x/ff.h @@ -431,7 +431,7 @@ typedef enum { FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ FRESULT f_open (FIL*, const TCHAR*, BYTE); /* Open or create a file */ -FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */ +FRESULT f_read (FIL*, BYTE*, UINT, UINT*); /* Read data from a file */ FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */ FRESULT f_close (FIL*); /* Close an open file object */ FRESULT f_opendir (DIR*, const TCHAR*); /* Open an existing directory */ diff --git a/src/model_menus.cpp b/src/model_menus.cpp index 0a246ba76..9a9a4f5cf 100644 --- a/src/model_menus.cpp +++ b/src/model_menus.cpp @@ -425,33 +425,31 @@ void menuProcModelSelect(uint8_t event) if (s_menu_count) { const char * result = displayMenu(event); if (result) { - if (s_menu_flags) { - // The user choosed a file on SD + if (result == STR_LOAD_MODEL || result == STR_CREATE_MODEL) { + displayPopup(STR_LOADINGMODEL); + eeCheck(true); // force writing of current model data before this is changed + if (g_eeGeneral.currModel != sub) { + g_eeGeneral.currModel = sub; + STORE_GENERALVARS; + eeLoadModel(sub); + } + } + else if (result == STR_ARCHIVE_MODEL) { + s_sdcard_error = eeArchiveModel(sub); + if (!s_sdcard_error) + eeDeleteModel(sub); // delete the model, it's archived! + } + else if (result == STR_RESTORE_MODEL) { + if (!listSDcardModels()) { + s_sdcard_error = PSTR("No Models on SD"); + } + } + else if (result == STR_DELETE_MODEL) { + s_warning = STR_DELETEMODEL; } else { - if (result == STR_LOAD_MODEL || result == STR_CREATE_MODEL) { - displayPopup(STR_LOADINGMODEL); - eeCheck(true); // force writing of current model data before this is changed - if (g_eeGeneral.currModel != sub) { - g_eeGeneral.currModel = sub; - STORE_GENERALVARS; - eeLoadModel(sub); - } - } - else if (result == STR_ARCHIVE_MODEL) { - s_sdcard_error = eeArchiveModel(sub); - /* TODO as soon as archive and restore do work! if (!s_sdcard_error) - eeDeleteModel(sub); // delete the model, it's archived! - */ - } - else if (result == STR_RESTORE_MODEL) { - if (!listSDcardModels()) { - s_sdcard_error = PSTR("No Models on SD"); - } - } - else if (result == STR_DELETE_MODEL) { - s_warning = STR_DELETEMODEL; - } + // The user choosed a file on SD to restore + s_sdcard_error = eeRestoreModel(sub, (char *)result); } } }