repair
Dependencies: FatFileSystemCpp mbed
Fork of MSCUsbHost by
main.cpp@0:e294af8d0e07, 2010-01-26 (annotated)
- Committer:
- igorsk
- Date:
- Tue Jan 26 23:25:56 2010 +0000
- Revision:
- 0:e294af8d0e07
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:e294af8d0e07 | 1 | #include "mbed.h" |
igorsk | 0:e294af8d0e07 | 2 | #include "MSCFileSystem.h" |
igorsk | 0:e294af8d0e07 | 3 | //#include <stat.h> |
igorsk | 0:e294af8d0e07 | 4 | |
igorsk | 0:e294af8d0e07 | 5 | #define FSNAME "msc" |
igorsk | 0:e294af8d0e07 | 6 | MSCFileSystem msc(FSNAME); |
igorsk | 0:e294af8d0e07 | 7 | |
igorsk | 0:e294af8d0e07 | 8 | int main() |
igorsk | 0:e294af8d0e07 | 9 | { |
igorsk | 0:e294af8d0e07 | 10 | DIR *d; |
igorsk | 0:e294af8d0e07 | 11 | struct dirent *p; |
igorsk | 0:e294af8d0e07 | 12 | //struct stat st; |
igorsk | 0:e294af8d0e07 | 13 | //char path[PATH_MAX]; |
igorsk | 0:e294af8d0e07 | 14 | |
igorsk | 0:e294af8d0e07 | 15 | printf("\n\n================================\n"); |
igorsk | 0:e294af8d0e07 | 16 | printf("USB Mass storage demo program for mbed LPC1768\n"); |
igorsk | 0:e294af8d0e07 | 17 | printf("================================\n\n"); |
igorsk | 0:e294af8d0e07 | 18 | |
igorsk | 0:e294af8d0e07 | 19 | d = opendir("/" FSNAME); |
igorsk | 0:e294af8d0e07 | 20 | |
igorsk | 0:e294af8d0e07 | 21 | printf("\nList of files on the flash drive:\n"); |
igorsk | 0:e294af8d0e07 | 22 | if ( d != NULL ) |
igorsk | 0:e294af8d0e07 | 23 | { |
igorsk | 0:e294af8d0e07 | 24 | while ( (p = readdir(d)) != NULL ) |
igorsk | 0:e294af8d0e07 | 25 | { |
igorsk | 0:e294af8d0e07 | 26 | printf(" - %s\n", p->d_name); |
igorsk | 0:e294af8d0e07 | 27 | /* no <stat.h> on mbed, it seems :/ |
igorsk | 0:e294af8d0e07 | 28 | sprintf(path, "/"FSNAME"/%s", p->d_name); |
igorsk | 0:e294af8d0e07 | 29 | if ( stat(path, &st) == 0 ) |
igorsk | 0:e294af8d0e07 | 30 | { |
igorsk | 0:e294af8d0e07 | 31 | if ( S_ISDIR(st.st_mode) ) |
igorsk | 0:e294af8d0e07 | 32 | printf(" <directory>\n"); |
igorsk | 0:e294af8d0e07 | 33 | else |
igorsk | 0:e294af8d0e07 | 34 | printf(" %d\n", st.st_size); |
igorsk | 0:e294af8d0e07 | 35 | } |
igorsk | 0:e294af8d0e07 | 36 | else |
igorsk | 0:e294af8d0e07 | 37 | { |
igorsk | 0:e294af8d0e07 | 38 | printf(" ???\n"); |
igorsk | 0:e294af8d0e07 | 39 | }*/ |
igorsk | 0:e294af8d0e07 | 40 | } |
igorsk | 0:e294af8d0e07 | 41 | } |
igorsk | 0:e294af8d0e07 | 42 | else |
igorsk | 0:e294af8d0e07 | 43 | { |
igorsk | 0:e294af8d0e07 | 44 | error("Could not open directory!"); |
igorsk | 0:e294af8d0e07 | 45 | } |
igorsk | 0:e294af8d0e07 | 46 | printf("\nTesting file write:\n"); |
igorsk | 0:e294af8d0e07 | 47 | FILE *fp = fopen( "/" FSNAME "/msctest.txt", "w"); |
igorsk | 0:e294af8d0e07 | 48 | if ( fp == NULL ) |
igorsk | 0:e294af8d0e07 | 49 | { |
igorsk | 0:e294af8d0e07 | 50 | error("Could not open file for write\n"); |
igorsk | 0:e294af8d0e07 | 51 | } |
igorsk | 0:e294af8d0e07 | 52 | fprintf(fp, "Hello mass storage!"); |
igorsk | 0:e294af8d0e07 | 53 | fclose(fp); |
igorsk | 0:e294af8d0e07 | 54 | printf("\n - OK\n"); |
igorsk | 0:e294af8d0e07 | 55 | |
igorsk | 0:e294af8d0e07 | 56 | printf("\nTesting file read:\n"); |
igorsk | 0:e294af8d0e07 | 57 | fp = fopen( "/" FSNAME "/msctest.txt", "r"); |
igorsk | 0:e294af8d0e07 | 58 | if ( fp == NULL ) |
igorsk | 0:e294af8d0e07 | 59 | { |
igorsk | 0:e294af8d0e07 | 60 | error("Could not open file for read\n"); |
igorsk | 0:e294af8d0e07 | 61 | } |
igorsk | 0:e294af8d0e07 | 62 | char buf[256]; |
igorsk | 0:e294af8d0e07 | 63 | if ( NULL == fgets(buf, sizeof(buf), fp) ) |
igorsk | 0:e294af8d0e07 | 64 | { |
igorsk | 0:e294af8d0e07 | 65 | error("Error reading from file\n"); |
igorsk | 0:e294af8d0e07 | 66 | } |
igorsk | 0:e294af8d0e07 | 67 | fclose(fp); |
igorsk | 0:e294af8d0e07 | 68 | printf("\n - OK, read string: '%s'\n\n", buf); |
igorsk | 0:e294af8d0e07 | 69 | } |