Carlos Alberto Nascimento Filho / Mbed 2 deprecated MSCUsbHost_v2

Dependencies:   mbed FATFileSystem

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 
00005 #define FSNAME "msc"
00006 MSCFileSystem msc(FSNAME);
00007 
00008 int main()
00009 {
00010     DIR *d;
00011     struct dirent *p;
00012     //struct stat st;
00013     //char path[PATH_MAX];
00014     
00015     printf("\n\n================================\n");
00016     printf("USB Mass storage demo program for mbed LPC1768\n");
00017     printf("================================\n\n");
00018     
00019     d = opendir("/" FSNAME);
00020     
00021     printf("\nList of files on the flash drive:\n");
00022     if ( d != NULL )
00023     {
00024         while ( (p = readdir(d)) != NULL )
00025         {
00026             printf(" - %s\n", p->d_name);
00027             /* no <stat.h> on mbed, it seems :/
00028             sprintf(path, "/"FSNAME"/%s", p->d_name);
00029             if ( stat(path, &st) == 0 )
00030             {
00031               if ( S_ISDIR(st.st_mode) )
00032                 printf(" <directory>\n");
00033               else
00034                 printf(" %d\n", st.st_size);
00035             }
00036             else
00037             {
00038               printf(" ???\n");
00039             }*/
00040         }
00041     }
00042     else
00043     {
00044         error("Could not open directory!");
00045     }
00046     printf("\nTesting file write:\n");
00047     FILE *fp = fopen( "/" FSNAME "/msctest.txt", "w");
00048     if ( fp == NULL )
00049     {
00050         error("Could not open file for write\n");
00051     }
00052     fprintf(fp, "Hello mass storage!");
00053     fclose(fp); 
00054     printf("\n - OK\n");
00055 
00056     printf("\nTesting file read:\n");
00057     fp = fopen( "/" FSNAME "/msctest.txt", "r");
00058     if ( fp == NULL )
00059     {
00060         error("Could not open file for read\n");
00061     }
00062     char buf[256];
00063     if ( NULL == fgets(buf, sizeof(buf), fp) )
00064     {
00065         error("Error reading from file\n");
00066     }
00067     fclose(fp); 
00068     printf("\n - OK, read string: '%s'\n\n", buf);
00069 }