Pendrive to sd card data transfer coding

Dependencies:   FatFileSystem TextLCD mbed

Fork of MSCUsbHost by gavin beardall

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MSCFileSystem.h"
00003 //#include <stat.h>
00004 DigitalInOut button1(p11);
00005 
00006 DigitalInOut button2(p12);
00007 
00008 #include "TextLCD.h"
00009  
00010 #define FSNAME "msc"
00011 MSCFileSystem msc(FSNAME);
00012 #include "SDFileSystem.h"
00013 SDFileSystem sd(p5, p6, p7, p8, "sd"); 
00014 
00015 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7
00016 int k;
00017 int main()
00018 {
00019     while(1)
00020     {
00021         
00022         
00023         
00024         
00025      button1 = 1;
00026      button2 = 1; 
00027      lcd.locate(0,0);
00028       lcd.printf("1.Pendrv 2 SD");
00029           lcd.locate(0,1);
00030       lcd.printf("2.SD 2 Pendrv");
00031       wait(0.5);
00032 if(button1==0)
00033     {
00034         lcd.cls();
00035         lcd.locate(0,0);
00036       lcd.printf("1.Pendrv 2 SD");
00037           lcd.locate(0,1);
00038       lcd.printf(" Reading Files..");
00039       wait(5);
00040       lcd.cls();                
00041       lcd.locate(0,0);
00042       lcd.printf("1.test.docx");
00043           lcd.locate(0,1);
00044       lcd.printf("2.demo.txt");
00045             wait(2);
00046       k=1;
00047 while(k)
00048 {
00049       
00050       
00051       
00052 //////////////////////////////Pendrive to SD Card/////////////////////////////////////////////////////////
00053 
00054     if(button1==0)
00055     {
00056         
00057         lcd.cls();
00058            lcd.locate(0,0);
00059       lcd.printf("1.demo.txt");
00060           lcd.locate(0,1);
00061       lcd.printf("Transfer2SDCard");
00062             wait(7);
00063     ///////////////////
00064     DIR *d;
00065     struct dirent *p;
00066     //struct stat st;
00067     //char path[PATH_MAX];
00068 
00069     
00070     d = opendir("/" FSNAME);
00071     
00072     printf("\nList of files on the flash drive:\n");
00073     if ( d != NULL )
00074     {
00075         while ( (p = readdir(d)) != NULL )
00076         {
00077             printf(" - %s\n", p->d_name);
00078         
00079         }
00080     }
00081     else
00082     {
00083         error("Could not open directory!");
00084     }
00085     /*printf("\nTesting file write:\n");
00086     FILE *fp1 = fopen( "/" FSNAME "/demo.txt", "w");
00087     if ( fp1 == NULL )
00088     {
00089         error("Could not open file for write\n");
00090     }
00091     fprintf(fp1, "Hello mass storage sathguru!");
00092     fclose(fp1); 
00093     printf("\n - OK\n");
00094 */
00095     printf("\nTesting file read:\n");
00096     FILE *fp1 =fopen( "/" FSNAME "/demo.txt", "r");
00097     if ( fp1 == NULL )
00098     {
00099          lcd.cls();
00100           lcd.locate(0,0);
00101       lcd.printf("Error in  ");
00102         lcd.locate(0,1);
00103       lcd.printf("Opening File  ");
00104       wait(2);
00105       lcd.cls();
00106     }
00107    char buf[256];
00108     if ( NULL == fgets(buf, sizeof(buf), fp1) )
00109     {
00110             lcd.cls();
00111           lcd.locate(0,0);
00112       lcd.printf("Error in  ");
00113         lcd.locate(0,1);
00114       lcd.printf("Reading File  ");
00115       wait(2);
00116       lcd.cls();
00117     }
00118     fclose(fp1); 
00119     printf("\n - OK, read string: '%s'\n\n", buf);
00120     
00121     /////////////////////////////////////////////////////////////
00122       wait(0.5);
00123   mkdir("/sd/mydir", 0777);
00124  
00125     
00126     FILE *fp = fopen("/sd/mydir/demo.txt", "w");
00127     if(fp == NULL) {
00128         lcd.cls();
00129           lcd.locate(0,0);
00130       lcd.printf("Error in  ");
00131         lcd.locate(0,1);
00132       lcd.printf("Opening File  ");
00133       wait(2);
00134       lcd.cls();
00135     }
00136     fprintf(fp, buf);
00137     fclose(fp); 
00138     
00139     printf(buf);
00140     k=0;
00141         lcd.cls();
00142            lcd.locate(0,0);
00143       lcd.printf("File   ");
00144          lcd.locate(0,1);
00145       lcd.printf(" Transferred ");
00146       wait(5);
00147     }
00148     
00149     else if(button2==0)
00150     {
00151         
00152     
00153         
00154         lcd.cls();
00155            lcd.locate(0,0);
00156       lcd.printf("2.test.doc");
00157           lcd.locate(0,1);
00158       lcd.printf("Transfer2SDCard");
00159             wait(7);
00160     ///////////////////
00161     DIR *d;
00162     struct dirent *p;
00163     //struct stat st;
00164     //char path[PATH_MAX];
00165 
00166     
00167     d = opendir("/" FSNAME);
00168     
00169     printf("\nList of files on the flash drive:\n");
00170     if ( d != NULL )
00171     {
00172         while ( (p = readdir(d)) != NULL )
00173         {
00174             printf(" - %s\n", p->d_name);
00175         
00176         }
00177     }
00178     else
00179     {
00180         error("Could not open directory!");
00181     }
00182     /*printf("\nTesting file write:\n");
00183     FILE *fp1 = fopen( "/" FSNAME "/demo.txt", "w");
00184     if ( fp1 == NULL )
00185     {
00186         error("Could not open file for write\n");
00187     }
00188     fprintf(fp1, "Hello mass storage sathguru!");
00189     fclose(fp1); 
00190     printf("\n - OK\n");
00191 */
00192     printf("\nTesting file read:\n");
00193     FILE *fp1 =fopen( "/" FSNAME "/test.doc", "r");
00194     if ( fp1 == NULL )
00195     {
00196          lcd.cls();
00197           lcd.locate(0,0);
00198       lcd.printf("Error in  ");
00199         lcd.locate(0,1);
00200       lcd.printf("Opening File  ");
00201       wait(2);
00202       lcd.cls();
00203     }
00204  char buf[256];
00205     if ( NULL == fgets(buf, sizeof(buf), fp1) )
00206     {
00207            lcd.cls();
00208           lcd.locate(0,0);
00209       lcd.printf("Error in  ");
00210         lcd.locate(0,1);
00211       lcd.printf("Reading File  ");
00212       wait(2);
00213       lcd.cls();
00214     }
00215     fclose(fp1); 
00216     printf("\n - OK, read string: '%s'\n\n", buf);
00217     
00218     /////////////////////////////////////////////////////////////
00219       
00220  
00221       wait(0.5);
00222   mkdir("/sd/mydir", 0777);
00223     
00224     FILE *fp = fopen("/sd/mydir/test.doc", "w");
00225     if(fp == NULL) {
00226             lcd.cls();
00227           lcd.locate(0,0);
00228       lcd.printf("Error in  ");
00229         lcd.locate(0,1);
00230       lcd.printf("Opening File  ");
00231       wait(2);
00232       lcd.cls();
00233     }
00234     fprintf(fp, buf);
00235     fclose(fp); 
00236     
00237     printf(buf);
00238     k=0;
00239         lcd.cls();
00240            lcd.locate(0,0);
00241       lcd.printf("File   ");
00242          lcd.locate(0,1);
00243       lcd.printf("  Transferred ");
00244       wait(5);
00245     }
00246         }
00247     
00248     }
00249     
00250     
00251     //////////////////////////////////////// SD card to Pendrive /////////////////////////////////
00252     
00253     
00254     
00255 else if(button2==0)
00256     {
00257         lcd.cls();
00258         lcd.locate(0,0);
00259       lcd.printf("1.Pendrv2Pendrv");
00260           lcd.locate(0,1);
00261       lcd.printf(" Reading Files..");
00262       wait(5);
00263       lcd.cls();                
00264       lcd.locate(0,0);
00265       lcd.printf("1.blue.txt");
00266           lcd.locate(0,1);
00267       lcd.printf("2.check.txt");
00268             wait(2);
00269       k=1;
00270 while(k)
00271 {
00272       
00273       
00274       
00275 //////////////////////////////Pendrive to SD Card/////////////////////////////////////////////////////////
00276 
00277     if(button1==0)
00278     {
00279         
00280         lcd.cls();
00281            lcd.locate(0,0);
00282       lcd.printf("1.blue.txt");
00283           lcd.locate(0,1);
00284       lcd.printf("Transfer2Pendrv");
00285             wait(7);
00286     ///////////////////
00287     DIR *d;
00288     struct dirent *p;
00289     //struct stat st;
00290     //char path[PATH_MAX];
00291 
00292     
00293     d = opendir("/" FSNAME);
00294     
00295     printf("\nList of files on the flash drive:\n");
00296     if ( d != NULL )
00297     {
00298         while ( (p = readdir(d)) != NULL )
00299         {
00300             printf(" - %s\n", p->d_name);
00301         
00302         }
00303     }
00304     else
00305     {
00306         error("Could not open directory!");
00307     }
00308     
00309     mkdir("/sd/mydir", 0777);
00310  
00311     
00312     FILE *fp = fopen("/sd/mydir/blue.txt", "r");
00313     if(fp == NULL) {
00314             lcd.cls();
00315           lcd.locate(0,0);
00316       lcd.printf("Error in  ");
00317         lcd.locate(0,1);
00318       lcd.printf("Opening File  ");
00319       wait(2);
00320       lcd.cls();
00321     }
00322       char buf[256];
00323     if ( NULL == fgets(buf, sizeof(buf), fp) )
00324     {
00325                  lcd.cls();
00326           lcd.locate(0,0);
00327       lcd.printf("Error in  ");
00328         lcd.locate(0,1);
00329       lcd.printf("Reading File  ");
00330       wait(2);
00331       lcd.cls();
00332         }
00333     fprintf(fp, buf);
00334     fclose(fp); 
00335     
00336     printf(buf);
00337     
00338     
00339     
00340    printf("\nTesting file write:\n");
00341     FILE *fp1 = fopen( "/" FSNAME "/blue.txt", "w");
00342     if ( fp1 == NULL )
00343     {
00344         lcd.cls();
00345           lcd.locate(0,0);
00346       lcd.printf("Error in  ");
00347         lcd.locate(0,1);
00348       lcd.printf("Opening File  ");
00349       wait(2);
00350       lcd.cls();
00351     }
00352     fprintf(fp1,buf);
00353     fclose(fp1); 
00354       
00355 
00356     
00357     /////////////////////////////////////////////////////////////
00358       wait(0.5);
00359   
00360     k=0;
00361         lcd.cls();
00362            lcd.locate(0,0);
00363       lcd.printf("File   ");
00364          lcd.locate(0,1);
00365       lcd.printf(" Transferred ");
00366       wait(5);
00367     }
00368     
00369     else if(button2==0)
00370     {
00371         
00372     
00373         lcd.cls();
00374            lcd.locate(0,0);
00375       lcd.printf("1.check.txt");
00376           lcd.locate(0,1);
00377       lcd.printf("Transfer2Pendrv");
00378             wait(7);
00379     ///////////////////
00380     DIR *d;
00381     struct dirent *p;
00382     //struct stat st;
00383     //char path[PATH_MAX];
00384 
00385     
00386     d = opendir("/" FSNAME);
00387     
00388     printf("\nList of files on the flash drive:\n");
00389     if ( d != NULL )
00390     {
00391         while ( (p = readdir(d)) != NULL )
00392         {
00393             printf(" - %s\n", p->d_name);
00394         
00395         }
00396     }
00397     else
00398     {
00399         error("Could not open directory!");
00400     }
00401     
00402     mkdir("/sd/mydir", 0777);
00403  
00404     
00405     FILE *fp = fopen("/sd/mydir/check.txt", "r");
00406     if(fp == NULL) {
00407             lcd.cls();
00408           lcd.locate(0,0);
00409       lcd.printf("Error in  ");
00410         lcd.locate(0,1);
00411       lcd.printf("Opening File  ");
00412       wait(2);
00413       lcd.cls();
00414     }
00415          char buf[256];
00416     if ( NULL == fgets(buf, sizeof(buf), fp) )
00417     {
00418                  lcd.cls();
00419           lcd.locate(0,0);
00420       lcd.printf("Error in  ");
00421         lcd.locate(0,1);
00422       lcd.printf("Reading File  ");
00423       wait(2);
00424       lcd.cls();
00425         }
00426     fprintf(fp,buf);
00427     fclose(fp); 
00428     
00429     printf(buf);
00430     
00431     
00432     
00433    printf("\nTesting file write:\n");
00434     FILE *fp1 = fopen( "/" FSNAME "/check.txt", "w");
00435     if ( fp1 == NULL )
00436     {
00437         lcd.cls();
00438           lcd.locate(0,0);
00439       lcd.printf("Error in  ");
00440         lcd.locate(0,1);
00441       lcd.printf("Opening File  ");
00442       wait(2);
00443       lcd.cls();
00444     }
00445     fprintf(fp1,buf);
00446     fclose(fp1); 
00447       
00448 
00449     
00450     /////////////////////////////////////////////////////////////
00451       wait(0.5);
00452   
00453     k=0;
00454         lcd.cls();
00455            lcd.locate(0,0);
00456       lcd.printf("File   ");
00457          lcd.locate(0,1);
00458       lcd.printf(" Transferred ");
00459       wait(5);
00460         }
00461     
00462     }
00463     
00464     
00465     }   
00466     }
00467     }
00468