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

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Wed Apr 09 10:35:12 2014 +0000
Revision:
2:a5a0291d5a5c
Parent:
0:9b273b2aef38
Updated to latest version of EALib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:9b273b2aef38 1 /******************************************************************************
embeddedartists 0:9b273b2aef38 2 * Includes
embeddedartists 0:9b273b2aef38 3 *****************************************************************************/
embeddedartists 0:9b273b2aef38 4
embeddedartists 0:9b273b2aef38 5 #include "mbed.h"
embeddedartists 0:9b273b2aef38 6 #include "MCIFileSystem.h"
embeddedartists 0:9b273b2aef38 7
embeddedartists 0:9b273b2aef38 8 /******************************************************************************
embeddedartists 0:9b273b2aef38 9 * Typedefs and defines
embeddedartists 0:9b273b2aef38 10 *****************************************************************************/
embeddedartists 0:9b273b2aef38 11
embeddedartists 0:9b273b2aef38 12 typedef bool (*syncFunc)(const char* name, bool isDir);
embeddedartists 0:9b273b2aef38 13
embeddedartists 0:9b273b2aef38 14 /******************************************************************************
embeddedartists 0:9b273b2aef38 15 * Local variables
embeddedartists 0:9b273b2aef38 16 *****************************************************************************/
embeddedartists 0:9b273b2aef38 17
embeddedartists 0:9b273b2aef38 18 MCIFileSystem mcifs("mci");
embeddedartists 0:9b273b2aef38 19
embeddedartists 0:9b273b2aef38 20 DigitalOut myled1(LED1);
embeddedartists 0:9b273b2aef38 21 DigitalOut myled2(LED2);
embeddedartists 0:9b273b2aef38 22
embeddedartists 0:9b273b2aef38 23 /******************************************************************************
embeddedartists 0:9b273b2aef38 24 * Local functions
embeddedartists 0:9b273b2aef38 25 *****************************************************************************/
embeddedartists 0:9b273b2aef38 26
embeddedartists 0:9b273b2aef38 27 static void ledShowProgress()
embeddedartists 0:9b273b2aef38 28 {
embeddedartists 0:9b273b2aef38 29 static int state = 0;
embeddedartists 0:9b273b2aef38 30 state = (state + 1) % 2;
embeddedartists 0:9b273b2aef38 31 switch (state)
embeddedartists 0:9b273b2aef38 32 {
embeddedartists 0:9b273b2aef38 33 case 0:
embeddedartists 0:9b273b2aef38 34 myled1 = 1;
embeddedartists 0:9b273b2aef38 35 myled2 = 0;
embeddedartists 0:9b273b2aef38 36 break;
embeddedartists 0:9b273b2aef38 37
embeddedartists 0:9b273b2aef38 38 case 1:
embeddedartists 0:9b273b2aef38 39 default:
embeddedartists 0:9b273b2aef38 40 myled1 = 0;
embeddedartists 0:9b273b2aef38 41 myled2 = 1;
embeddedartists 0:9b273b2aef38 42 }
embeddedartists 0:9b273b2aef38 43 }
embeddedartists 0:9b273b2aef38 44
embeddedartists 0:9b273b2aef38 45 static void handleError(const char* msg)
embeddedartists 0:9b273b2aef38 46 {
embeddedartists 0:9b273b2aef38 47 printf(msg);
embeddedartists 0:9b273b2aef38 48 while(true) {
embeddedartists 0:9b273b2aef38 49 myled1 = 1;
embeddedartists 0:9b273b2aef38 50 myled2 = 1;
embeddedartists 0:9b273b2aef38 51 wait(0.3);
embeddedartists 0:9b273b2aef38 52 myled1 = 0;
embeddedartists 0:9b273b2aef38 53 myled2 = 0;
embeddedartists 0:9b273b2aef38 54 wait(0.3);
embeddedartists 0:9b273b2aef38 55 }
embeddedartists 0:9b273b2aef38 56 }
embeddedartists 0:9b273b2aef38 57
embeddedartists 0:9b273b2aef38 58 static bool recursiveProcessFS(char* buff, const char* name, syncFunc func, bool adding)
embeddedartists 0:9b273b2aef38 59 {
embeddedartists 0:9b273b2aef38 60 uint32_t len = strlen(buff);
embeddedartists 0:9b273b2aef38 61 if (len > 0) {
embeddedartists 0:9b273b2aef38 62 if (buff[len - 1] != '/') {
embeddedartists 0:9b273b2aef38 63 buff[len++] = '/';
embeddedartists 0:9b273b2aef38 64 buff[len] = '\0';
embeddedartists 0:9b273b2aef38 65 }
embeddedartists 0:9b273b2aef38 66 }
embeddedartists 0:9b273b2aef38 67 strcat(buff, name);
embeddedartists 0:9b273b2aef38 68 len += strlen(name);
embeddedartists 0:9b273b2aef38 69
embeddedartists 0:9b273b2aef38 70 if (len > 60) {
embeddedartists 0:9b273b2aef38 71 // ugly fix to avoid crashes that occurs when file name is larger than buffer
embeddedartists 0:9b273b2aef38 72 // in FATFileSystem.
embeddedartists 0:9b273b2aef38 73 printf("skipped: %s\n", buff);
embeddedartists 0:9b273b2aef38 74 return true;
embeddedartists 0:9b273b2aef38 75 }
embeddedartists 0:9b273b2aef38 76
embeddedartists 0:9b273b2aef38 77 DIR *d = opendir(buff);
embeddedartists 0:9b273b2aef38 78 bool result = true; // success
embeddedartists 0:9b273b2aef38 79 if (d != NULL) {
embeddedartists 0:9b273b2aef38 80 if (adding) {
embeddedartists 0:9b273b2aef38 81 // when processing in adding mode folders must be created before it's content
embeddedartists 0:9b273b2aef38 82 result = func(buff, true);
embeddedartists 0:9b273b2aef38 83 }
embeddedartists 0:9b273b2aef38 84 struct dirent *p;
embeddedartists 0:9b273b2aef38 85 while (result && ((p = readdir(d)) != NULL)) {
embeddedartists 0:9b273b2aef38 86 result = recursiveProcessFS(buff, p->d_name, func, adding);
embeddedartists 0:9b273b2aef38 87 buff[len] = '\0';
embeddedartists 0:9b273b2aef38 88 }
embeddedartists 0:9b273b2aef38 89 closedir(d);
embeddedartists 0:9b273b2aef38 90 if (result && !adding) {
embeddedartists 0:9b273b2aef38 91 // when processing in removing mode folders must be deleted after it's content
embeddedartists 0:9b273b2aef38 92 result = func(buff, true);
embeddedartists 0:9b273b2aef38 93 }
embeddedartists 0:9b273b2aef38 94 } else {
embeddedartists 0:9b273b2aef38 95 // a file
embeddedartists 0:9b273b2aef38 96 result = func(buff, false);
embeddedartists 0:9b273b2aef38 97 }
embeddedartists 0:9b273b2aef38 98 return result;
embeddedartists 0:9b273b2aef38 99 }
embeddedartists 0:9b273b2aef38 100
embeddedartists 0:9b273b2aef38 101 static uint32_t fileLen(FILE* f)
embeddedartists 0:9b273b2aef38 102 {
embeddedartists 0:9b273b2aef38 103 uint32_t pos = ftell(f);
embeddedartists 0:9b273b2aef38 104 fseek(f, 0, SEEK_END);
embeddedartists 0:9b273b2aef38 105 uint32_t size = ftell(f);
embeddedartists 0:9b273b2aef38 106 fseek(f, pos, SEEK_SET);
embeddedartists 0:9b273b2aef38 107 return size;
embeddedartists 0:9b273b2aef38 108 }
embeddedartists 0:9b273b2aef38 109
embeddedartists 0:9b273b2aef38 110 static bool copyFH(FILE* fSrc, FILE* fDst)
embeddedartists 0:9b273b2aef38 111 {
embeddedartists 0:9b273b2aef38 112 char buff[512];
embeddedartists 0:9b273b2aef38 113 uint32_t left = fileLen(fSrc);
embeddedartists 0:9b273b2aef38 114 uint32_t num;
embeddedartists 0:9b273b2aef38 115 uint32_t chunk;
embeddedartists 0:9b273b2aef38 116
embeddedartists 0:9b273b2aef38 117 fseek(fSrc, 0, SEEK_SET);
embeddedartists 0:9b273b2aef38 118 do {
embeddedartists 0:9b273b2aef38 119 chunk = (left < 512) ? left : 512;
embeddedartists 0:9b273b2aef38 120 num = fread(buff, 1, chunk, fSrc);
embeddedartists 0:9b273b2aef38 121 if (num > 0) {
embeddedartists 0:9b273b2aef38 122 uint32_t tmp = fwrite(buff, 1, num, fDst);
embeddedartists 0:9b273b2aef38 123 if (tmp != num) {
embeddedartists 0:9b273b2aef38 124 // failed to write
embeddedartists 0:9b273b2aef38 125 return false;
embeddedartists 0:9b273b2aef38 126 }
embeddedartists 0:9b273b2aef38 127 left -= num;
embeddedartists 0:9b273b2aef38 128 ledShowProgress();
embeddedartists 0:9b273b2aef38 129 }
embeddedartists 0:9b273b2aef38 130 } while(num > 0 && left > 0);
embeddedartists 0:9b273b2aef38 131
embeddedartists 0:9b273b2aef38 132 // copied entire file
embeddedartists 0:9b273b2aef38 133 return true;
embeddedartists 0:9b273b2aef38 134 }
embeddedartists 0:9b273b2aef38 135
embeddedartists 0:9b273b2aef38 136 static bool copy(const char* fnameSrc, const char* fnameDest)
embeddedartists 0:9b273b2aef38 137 {
embeddedartists 0:9b273b2aef38 138 FILE* fhSrc = NULL;
embeddedartists 0:9b273b2aef38 139 FILE* fhDest = NULL;
embeddedartists 0:9b273b2aef38 140 bool success = false;
embeddedartists 0:9b273b2aef38 141
embeddedartists 0:9b273b2aef38 142 do {
embeddedartists 0:9b273b2aef38 143 fhSrc = fopen(fnameSrc, "r");
embeddedartists 0:9b273b2aef38 144 if (fhSrc == NULL) {
embeddedartists 0:9b273b2aef38 145 break;
embeddedartists 0:9b273b2aef38 146 }
embeddedartists 0:9b273b2aef38 147
embeddedartists 0:9b273b2aef38 148 fhDest = fopen(fnameDest, "w");
embeddedartists 0:9b273b2aef38 149 if (fhDest == NULL) {
embeddedartists 0:9b273b2aef38 150 break;
embeddedartists 0:9b273b2aef38 151 }
embeddedartists 0:9b273b2aef38 152
embeddedartists 0:9b273b2aef38 153 if (!copyFH(fhSrc, fhDest)) {
embeddedartists 0:9b273b2aef38 154 break;
embeddedartists 0:9b273b2aef38 155 }
embeddedartists 0:9b273b2aef38 156
embeddedartists 0:9b273b2aef38 157 success = true;
embeddedartists 0:9b273b2aef38 158 } while (false);
embeddedartists 0:9b273b2aef38 159
embeddedartists 0:9b273b2aef38 160 if (fhSrc != NULL) {
embeddedartists 0:9b273b2aef38 161 fclose(fhSrc);
embeddedartists 0:9b273b2aef38 162 }
embeddedartists 0:9b273b2aef38 163 if (fhDest != NULL) {
embeddedartists 0:9b273b2aef38 164 fclose(fhDest);
embeddedartists 0:9b273b2aef38 165 }
embeddedartists 0:9b273b2aef38 166
embeddedartists 0:9b273b2aef38 167 return success;
embeddedartists 0:9b273b2aef38 168 }
embeddedartists 0:9b273b2aef38 169
embeddedartists 0:9b273b2aef38 170 static bool list(const char* name, bool isDir)
embeddedartists 0:9b273b2aef38 171 {
embeddedartists 0:9b273b2aef38 172 if (isDir) {
embeddedartists 0:9b273b2aef38 173 printf("d: %s\n", name);
embeddedartists 0:9b273b2aef38 174 } else {
embeddedartists 0:9b273b2aef38 175 FILE* f = fopen(name, "r");
embeddedartists 0:9b273b2aef38 176 if (f != NULL) {
embeddedartists 0:9b273b2aef38 177 uint32_t len = fileLen(f);
embeddedartists 0:9b273b2aef38 178 printf("f: %7u %s\n", len, name);
embeddedartists 0:9b273b2aef38 179 fclose(f);
embeddedartists 0:9b273b2aef38 180 } else {
embeddedartists 0:9b273b2aef38 181 printf("f: ??? %s\n", name);
embeddedartists 0:9b273b2aef38 182 }
embeddedartists 0:9b273b2aef38 183 }
embeddedartists 0:9b273b2aef38 184 return true;
embeddedartists 0:9b273b2aef38 185 }
embeddedartists 0:9b273b2aef38 186
embeddedartists 0:9b273b2aef38 187 static void recursiveList(const char* dirname)
embeddedartists 0:9b273b2aef38 188 {
embeddedartists 0:9b273b2aef38 189 printf("\nRecursive list of file and folders in %s\n", dirname);
embeddedartists 0:9b273b2aef38 190 char* buff = (char*)malloc(512);
embeddedartists 0:9b273b2aef38 191 if (buff != NULL)
embeddedartists 0:9b273b2aef38 192 {
embeddedartists 0:9b273b2aef38 193 buff[0] = '\0';
embeddedartists 0:9b273b2aef38 194 recursiveProcessFS(buff, dirname, list, true);
embeddedartists 0:9b273b2aef38 195 free(buff);
embeddedartists 0:9b273b2aef38 196 }
embeddedartists 0:9b273b2aef38 197 }
embeddedartists 0:9b273b2aef38 198
embeddedartists 0:9b273b2aef38 199 static void testAppend(const char* filename)
embeddedartists 0:9b273b2aef38 200 {
embeddedartists 0:9b273b2aef38 201 FILE *fp = fopen(filename, "a");
embeddedartists 0:9b273b2aef38 202 if (fp != NULL) {
embeddedartists 0:9b273b2aef38 203 fprintf(fp, "Hello World!");
embeddedartists 0:9b273b2aef38 204 fclose(fp);
embeddedartists 0:9b273b2aef38 205 }
embeddedartists 0:9b273b2aef38 206 }
embeddedartists 0:9b273b2aef38 207
embeddedartists 0:9b273b2aef38 208 /******************************************************************************
embeddedartists 0:9b273b2aef38 209 * Main function
embeddedartists 0:9b273b2aef38 210 *****************************************************************************/
embeddedartists 0:9b273b2aef38 211
embeddedartists 0:9b273b2aef38 212 int main()
embeddedartists 0:9b273b2aef38 213 {
embeddedartists 0:9b273b2aef38 214 printf("\n-----------------\n\nWelcome to the MCI file system example...\n");
embeddedartists 0:9b273b2aef38 215
embeddedartists 0:9b273b2aef38 216 if (!mcifs.cardInserted()) {
embeddedartists 0:9b273b2aef38 217 printf("Please insert a SD/MMC card...\n");
embeddedartists 0:9b273b2aef38 218 while (!mcifs.cardInserted()) {
embeddedartists 0:9b273b2aef38 219 wait(0.5);
embeddedartists 0:9b273b2aef38 220 }
embeddedartists 0:9b273b2aef38 221 printf("Card detected!\n");
embeddedartists 0:9b273b2aef38 222 }
embeddedartists 0:9b273b2aef38 223
embeddedartists 0:9b273b2aef38 224 recursiveList("/mci/");
embeddedartists 0:9b273b2aef38 225
embeddedartists 0:9b273b2aef38 226 copy("/mci/expanding.txt", "/mci/expanding.old");
embeddedartists 0:9b273b2aef38 227 testAppend("/mci/expanding.txt");
embeddedartists 0:9b273b2aef38 228
embeddedartists 0:9b273b2aef38 229 recursiveList("/mci/");
embeddedartists 0:9b273b2aef38 230
embeddedartists 0:9b273b2aef38 231 handleError("Program completed!\n");
embeddedartists 0:9b273b2aef38 232 }
embeddedartists 0:9b273b2aef38 233