Example using the MCIFileSystem class in EALib to access an uSD card.

Dependencies:   EALib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002  * Includes
00003  *****************************************************************************/
00004  
00005 #include "mbed.h"
00006 #include "MCIFileSystem.h"
00007 
00008 /******************************************************************************
00009  * Typedefs and defines
00010  *****************************************************************************/
00011 
00012 typedef bool (*syncFunc)(const char* name, bool isDir);
00013 
00014 /******************************************************************************
00015  * Local variables
00016  *****************************************************************************/
00017 
00018 MCIFileSystem mcifs("mci");
00019 
00020 DigitalOut myled1(LED1);
00021 DigitalOut myled2(LED2);
00022 
00023 /******************************************************************************
00024  * Local functions
00025  *****************************************************************************/
00026 
00027 static void ledShowProgress()
00028 {
00029   static int state = 0;
00030   state = (state + 1) % 2;
00031   switch (state)
00032   {
00033     case 0:
00034       myled1 = 1;
00035       myled2 = 0;
00036       break;
00037 
00038     case 1:
00039     default:
00040       myled1 = 0;
00041       myled2 = 1;
00042   }
00043 }
00044 
00045 static void handleError(const char* msg)
00046 {
00047   printf(msg);
00048   while(true) {
00049     myled1 = 1;
00050     myled2 = 1;
00051     wait(0.3);
00052     myled1 = 0;
00053     myled2 = 0;
00054     wait(0.3);
00055   }
00056 }
00057 
00058 static bool recursiveProcessFS(char* buff, const char* name, syncFunc func, bool adding)
00059 {
00060   uint32_t len = strlen(buff);
00061   if (len > 0) {
00062     if (buff[len - 1] != '/') {
00063       buff[len++] = '/';
00064       buff[len] = '\0';
00065     }
00066   }
00067   strcat(buff, name);
00068   len += strlen(name);
00069   
00070   if (len > 60) {
00071     // ugly fix to avoid crashes that occurs when file name is larger than buffer 
00072     // in FATFileSystem.
00073     printf("skipped:   %s\n", buff);
00074     return true;
00075   }
00076 
00077   DIR *d = opendir(buff);
00078   bool result = true; // success
00079   if (d != NULL) {
00080     if (adding) {
00081       // when processing in adding mode folders must be created before it's content
00082       result = func(buff, true);
00083     }
00084     struct dirent *p;
00085     while (result && ((p = readdir(d)) != NULL)) {
00086       result = recursiveProcessFS(buff, p->d_name, func, adding);
00087       buff[len] = '\0';
00088     }
00089     closedir(d);
00090     if (result && !adding) {
00091       // when processing in removing mode folders must be deleted after it's content
00092       result = func(buff, true);
00093     }
00094   } else {
00095     // a file
00096     result = func(buff, false);
00097   }
00098   return result;
00099 }
00100 
00101 static uint32_t fileLen(FILE* f)
00102 {
00103   uint32_t pos = ftell(f);
00104   fseek(f, 0, SEEK_END);
00105   uint32_t size = ftell(f);
00106   fseek(f, pos, SEEK_SET);
00107   return size;
00108 }
00109 
00110 static bool copyFH(FILE* fSrc, FILE* fDst)
00111 {
00112   char buff[512];
00113   uint32_t left = fileLen(fSrc);
00114   uint32_t num;
00115   uint32_t chunk;
00116 
00117   fseek(fSrc, 0, SEEK_SET);
00118   do {
00119     chunk = (left < 512) ? left : 512;
00120     num = fread(buff, 1, chunk, fSrc);
00121     if (num > 0) {
00122       uint32_t tmp = fwrite(buff, 1, num, fDst);
00123       if (tmp != num) {
00124         // failed to write
00125         return false;
00126       }
00127       left -= num;
00128       ledShowProgress();
00129     }
00130   } while(num > 0 && left > 0);
00131 
00132   // copied entire file
00133   return true;
00134 }
00135 
00136 static bool copy(const char* fnameSrc, const char* fnameDest)
00137 {
00138   FILE* fhSrc = NULL;
00139   FILE* fhDest = NULL;
00140   bool success = false;
00141   
00142   do {
00143     fhSrc = fopen(fnameSrc, "r");
00144     if (fhSrc == NULL) {
00145       break;
00146     }
00147     
00148     fhDest = fopen(fnameDest, "w");
00149     if (fhDest == NULL) {
00150       break;
00151     }
00152     
00153     if (!copyFH(fhSrc, fhDest)) {
00154       break;
00155     }
00156     
00157     success = true;
00158   } while (false);
00159   
00160   if (fhSrc != NULL) {
00161     fclose(fhSrc);
00162   }
00163   if (fhDest != NULL) {
00164     fclose(fhDest);
00165   }
00166   
00167   return success;
00168 }
00169 
00170 static bool list(const char* name, bool isDir)
00171 {
00172   if (isDir) {
00173     printf("d:         %s\n", name);
00174   } else {
00175     FILE* f = fopen(name, "r");
00176     if (f != NULL) {
00177       uint32_t len = fileLen(f);
00178       printf("f: %7u %s\n", len, name);
00179       fclose(f);
00180     } else {
00181       printf("f:     ??? %s\n", name);
00182     }
00183   }
00184   return true;
00185 }
00186 
00187 static void recursiveList(const char* dirname)
00188 {
00189   printf("\nRecursive list of file and folders in %s\n", dirname);
00190   char* buff = (char*)malloc(512);
00191   if (buff != NULL)
00192   {
00193     buff[0] = '\0';
00194     recursiveProcessFS(buff, dirname, list, true);
00195     free(buff);
00196   }
00197 }
00198 
00199 static void testAppend(const char* filename)
00200 {
00201     FILE *fp = fopen(filename, "a");
00202     if (fp != NULL) {
00203         fprintf(fp, "Hello World!");
00204         fclose(fp);
00205     }
00206 }
00207 
00208 /******************************************************************************
00209  * Main function
00210  *****************************************************************************/
00211 
00212 int main()
00213 {
00214   printf("\n-----------------\n\nWelcome to the MCI file system example...\n");
00215   
00216   if (!mcifs.cardInserted()) {
00217     printf("Please insert a SD/MMC card...\n");
00218     while (!mcifs.cardInserted()) {
00219       wait(0.5);
00220     }
00221     printf("Card detected!\n");
00222   }
00223 
00224   recursiveList("/mci/");
00225 
00226   copy("/mci/expanding.txt", "/mci/expanding.old");
00227   testAppend("/mci/expanding.txt");
00228 
00229   recursiveList("/mci/");
00230   
00231   handleError("Program completed!\n");
00232 }
00233