Pendrive to sd card data transfer coding

Dependencies:   FatFileSystem TextLCD mbed

Fork of MSCUsbHost by gavin beardall

Revision:
1:77e50c06ea01
Parent:
0:e6a539e59b52
--- a/main.cpp	Mon Oct 17 10:41:52 2011 +0000
+++ b/main.cpp	Thu Apr 16 05:12:20 2015 +0000
@@ -1,20 +1,168 @@
 #include "mbed.h"
 #include "MSCFileSystem.h"
 //#include <stat.h>
+DigitalInOut button1(p11);
 
+DigitalInOut button2(p12);
+
+#include "TextLCD.h"
+ 
 #define FSNAME "msc"
 MSCFileSystem msc(FSNAME);
+#include "SDFileSystem.h"
+SDFileSystem sd(p5, p6, p7, p8, "sd"); 
 
+TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4-d7
+int k;
 int main()
 {
+	while(1)
+	{
+		
+		
+		
+		
+	 button1 = 1;
+     button2 = 1; 
+     lcd.locate(0,0);
+      lcd.printf("1.Pendrv 2 SD");
+          lcd.locate(0,1);
+      lcd.printf("2.SD 2 Pendrv");
+      wait(0.5);
+if(button1==0)
+	{
+		lcd.cls();
+		lcd.locate(0,0);
+      lcd.printf("1.Pendrv 2 SD");
+          lcd.locate(0,1);
+      lcd.printf(" Reading Files..");
+      wait(5);
+      lcd.cls();                
+      lcd.locate(0,0);
+      lcd.printf("1.test.docx");
+          lcd.locate(0,1);
+      lcd.printf("2.demo.txt");
+            wait(2);
+      k=1;
+while(k)
+{
+      
+      
+      
+//////////////////////////////Pendrive to SD Card/////////////////////////////////////////////////////////
+
+	if(button1==0)
+	{
+		
+		lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("1.demo.txt");
+          lcd.locate(0,1);
+      lcd.printf("Transfer2SDCard");
+            wait(7);
+	///////////////////
 	DIR *d;
 	struct dirent *p;
 	//struct stat st;
 	//char path[PATH_MAX];
+
     
-    printf("\n\n================================\n");
-    printf("USB Mass storage demo program for mbed LPC1768\n");
-    printf("================================\n\n");
+	d = opendir("/" FSNAME);
+    
+    printf("\nList of files on the flash drive:\n");
+    if ( d != NULL )
+    {
+        while ( (p = readdir(d)) != NULL )
+        {
+        	printf(" - %s\n", p->d_name);
+        
+        }
+    }
+    else
+    {
+    	error("Could not open directory!");
+    }
+    /*printf("\nTesting file write:\n");
+    FILE *fp1 = fopen( "/" FSNAME "/demo.txt", "w");
+    if ( fp1 == NULL )
+    {
+        error("Could not open file for write\n");
+    }
+    fprintf(fp1, "Hello mass storage sathguru!");
+    fclose(fp1); 
+    printf("\n - OK\n");
+*/
+    printf("\nTesting file read:\n");
+    FILE *fp1 =fopen( "/" FSNAME "/demo.txt", "r");
+    if ( fp1 == NULL )
+    {
+         lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+   char buf[256];
+    if ( NULL == fgets(buf, sizeof(buf), fp1) )
+    {
+            lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Reading File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fclose(fp1); 
+    printf("\n - OK, read string: '%s'\n\n", buf);
+    
+    /////////////////////////////////////////////////////////////
+	  wait(0.5);
+  mkdir("/sd/mydir", 0777);
+ 
+    
+    FILE *fp = fopen("/sd/mydir/demo.txt", "w");
+    if(fp == NULL) {
+        lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fprintf(fp, buf);
+    fclose(fp); 
+    
+    printf(buf);
+    k=0;
+    	lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("File   ");
+         lcd.locate(0,1);
+      lcd.printf(" Transferred ");
+      wait(5);
+	}
+	
+	else if(button2==0)
+	{
+		
+	
+		
+		lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("2.test.doc");
+          lcd.locate(0,1);
+      lcd.printf("Transfer2SDCard");
+            wait(7);
+	///////////////////
+	DIR *d;
+	struct dirent *p;
+	//struct stat st;
+	//char path[PATH_MAX];
+
     
 	d = opendir("/" FSNAME);
     
@@ -24,46 +172,297 @@
         while ( (p = readdir(d)) != NULL )
         {
         	printf(" - %s\n", p->d_name);
-        	/* no <stat.h> on mbed, it seems :/
-        	sprintf(path, "/"FSNAME"/%s", p->d_name);
-        	if ( stat(path, &st) == 0 )
-        	{
-        	  if ( S_ISDIR(st.st_mode) )
-        	    printf(" <directory>\n");
-        	  else
-        	    printf(" %d\n", st.st_size);
-        	}
-        	else
-        	{
-        	  printf(" ???\n");
-        	}*/
+        
+        }
+    }
+    else
+    {
+    	error("Could not open directory!");
+    }
+    /*printf("\nTesting file write:\n");
+    FILE *fp1 = fopen( "/" FSNAME "/demo.txt", "w");
+    if ( fp1 == NULL )
+    {
+        error("Could not open file for write\n");
+    }
+    fprintf(fp1, "Hello mass storage sathguru!");
+    fclose(fp1); 
+    printf("\n - OK\n");
+*/
+    printf("\nTesting file read:\n");
+    FILE *fp1 =fopen( "/" FSNAME "/test.doc", "r");
+    if ( fp1 == NULL )
+    {
+         lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+ char buf[256];
+    if ( NULL == fgets(buf, sizeof(buf), fp1) )
+    {
+           lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Reading File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fclose(fp1); 
+    printf("\n - OK, read string: '%s'\n\n", buf);
+    
+    /////////////////////////////////////////////////////////////
+	  
+ 
+      wait(0.5);
+  mkdir("/sd/mydir", 0777);
+    
+    FILE *fp = fopen("/sd/mydir/test.doc", "w");
+    if(fp == NULL) {
+            lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fprintf(fp, buf);
+    fclose(fp); 
+    
+    printf(buf);
+    k=0;
+    	lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("File   ");
+         lcd.locate(0,1);
+      lcd.printf("  Transferred ");
+      wait(5);
+	}
+		}
+	
+	}
+	
+	
+	//////////////////////////////////////// SD card to Pendrive /////////////////////////////////
+	
+	
+	
+else if(button2==0)
+	{
+		lcd.cls();
+		lcd.locate(0,0);
+      lcd.printf("1.Pendrv2Pendrv");
+          lcd.locate(0,1);
+      lcd.printf(" Reading Files..");
+      wait(5);
+      lcd.cls();                
+      lcd.locate(0,0);
+      lcd.printf("1.blue.txt");
+          lcd.locate(0,1);
+      lcd.printf("2.check.txt");
+            wait(2);
+      k=1;
+while(k)
+{
+      
+      
+      
+//////////////////////////////Pendrive to SD Card/////////////////////////////////////////////////////////
+
+	if(button1==0)
+	{
+		
+		lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("1.blue.txt");
+          lcd.locate(0,1);
+      lcd.printf("Transfer2Pendrv");
+            wait(7);
+	///////////////////
+	DIR *d;
+	struct dirent *p;
+	//struct stat st;
+	//char path[PATH_MAX];
+
+    
+	d = opendir("/" FSNAME);
+    
+    printf("\nList of files on the flash drive:\n");
+    if ( d != NULL )
+    {
+        while ( (p = readdir(d)) != NULL )
+        {
+        	printf(" - %s\n", p->d_name);
+        
         }
     }
     else
     {
     	error("Could not open directory!");
     }
-    printf("\nTesting file write:\n");
-    FILE *fp = fopen( "/" FSNAME "/msctest.txt", "w");
-    if ( fp == NULL )
-    {
-        error("Could not open file for write\n");
+    
+    mkdir("/sd/mydir", 0777);
+ 
+    
+    FILE *fp = fopen("/sd/mydir/blue.txt", "r");
+    if(fp == NULL) {
+            lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
     }
-    fprintf(fp, "Hello mass storage!");
-    fclose(fp); 
-    printf("\n - OK\n");
-
-    printf("\nTesting file read:\n");
-    fp = fopen( "/" FSNAME "/msctest.txt", "r");
-    if ( fp == NULL )
-    {
-        error("Could not open file for read\n");
-    }
-    char buf[256];
+      char buf[256];
     if ( NULL == fgets(buf, sizeof(buf), fp) )
     {
-        error("Error reading from file\n");
-    }
+    	         lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Reading File  ");
+      wait(2);
+      lcd.cls();
+    	}
+    fprintf(fp, buf);
     fclose(fp); 
-    printf("\n - OK, read string: '%s'\n\n", buf);
-}
+    
+    printf(buf);
+    
+    
+    
+   printf("\nTesting file write:\n");
+    FILE *fp1 = fopen( "/" FSNAME "/blue.txt", "w");
+    if ( fp1 == NULL )
+    {
+        lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fprintf(fp1,buf);
+    fclose(fp1); 
+      
+
+    
+    /////////////////////////////////////////////////////////////
+	  wait(0.5);
+  
+    k=0;
+    	lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("File   ");
+         lcd.locate(0,1);
+      lcd.printf(" Transferred ");
+      wait(5);
+	}
+	
+	else if(button2==0)
+	{
+		
+	
+		lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("1.check.txt");
+          lcd.locate(0,1);
+      lcd.printf("Transfer2Pendrv");
+            wait(7);
+	///////////////////
+	DIR *d;
+	struct dirent *p;
+	//struct stat st;
+	//char path[PATH_MAX];
+
+    
+	d = opendir("/" FSNAME);
+    
+    printf("\nList of files on the flash drive:\n");
+    if ( d != NULL )
+    {
+        while ( (p = readdir(d)) != NULL )
+        {
+        	printf(" - %s\n", p->d_name);
+        
+        }
+    }
+    else
+    {
+    	error("Could not open directory!");
+    }
+    
+    mkdir("/sd/mydir", 0777);
+ 
+    
+    FILE *fp = fopen("/sd/mydir/check.txt", "r");
+    if(fp == NULL) {
+            lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+         char buf[256];
+    if ( NULL == fgets(buf, sizeof(buf), fp) )
+    {
+    	         lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Reading File  ");
+      wait(2);
+      lcd.cls();
+    	}
+    fprintf(fp,buf);
+    fclose(fp); 
+    
+    printf(buf);
+    
+    
+    
+   printf("\nTesting file write:\n");
+    FILE *fp1 = fopen( "/" FSNAME "/check.txt", "w");
+    if ( fp1 == NULL )
+    {
+        lcd.cls();
+          lcd.locate(0,0);
+      lcd.printf("Error in  ");
+        lcd.locate(0,1);
+      lcd.printf("Opening File  ");
+      wait(2);
+      lcd.cls();
+    }
+    fprintf(fp1,buf);
+    fclose(fp1); 
+      
+
+    
+    /////////////////////////////////////////////////////////////
+	  wait(0.5);
+  
+    k=0;
+    	lcd.cls();
+		   lcd.locate(0,0);
+      lcd.printf("File   ");
+         lcd.locate(0,1);
+      lcd.printf(" Transferred ");
+      wait(5);
+		}
+	
+	}
+	
+	
+	}  	
+	}
+	}
+