SD card interface
mbed-export/LocalFileSystem.h@0:22612ae617a0, 2012-10-08 (annotated)
- Committer:
- lharoon
- Date:
- Mon Oct 08 11:14:07 2012 +0000
- Revision:
- 0:22612ae617a0
1st edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lharoon | 0:22612ae617a0 | 1 | /* mbed Microcontroller Library - LocalFileSystem |
lharoon | 0:22612ae617a0 | 2 | * Copyright (c) 2008-2009 ARM Limited. All rights reserved. |
lharoon | 0:22612ae617a0 | 3 | */ |
lharoon | 0:22612ae617a0 | 4 | |
lharoon | 0:22612ae617a0 | 5 | #ifndef MBED_LOCALFILESYSTEM_H |
lharoon | 0:22612ae617a0 | 6 | #define MBED_LOCALFILESYSTEM_H |
lharoon | 0:22612ae617a0 | 7 | |
lharoon | 0:22612ae617a0 | 8 | #include "FileSystemLike.h" |
lharoon | 0:22612ae617a0 | 9 | |
lharoon | 0:22612ae617a0 | 10 | namespace mbed { |
lharoon | 0:22612ae617a0 | 11 | |
lharoon | 0:22612ae617a0 | 12 | FILEHANDLE local_file_open(const char* name, int flags); |
lharoon | 0:22612ae617a0 | 13 | |
lharoon | 0:22612ae617a0 | 14 | class LocalFileHandle : public FileHandle { |
lharoon | 0:22612ae617a0 | 15 | |
lharoon | 0:22612ae617a0 | 16 | public: |
lharoon | 0:22612ae617a0 | 17 | LocalFileHandle(FILEHANDLE fh); |
lharoon | 0:22612ae617a0 | 18 | |
lharoon | 0:22612ae617a0 | 19 | virtual int close(); |
lharoon | 0:22612ae617a0 | 20 | |
lharoon | 0:22612ae617a0 | 21 | virtual ssize_t write(const void *buffer, size_t length); |
lharoon | 0:22612ae617a0 | 22 | |
lharoon | 0:22612ae617a0 | 23 | virtual ssize_t read(void *buffer, size_t length); |
lharoon | 0:22612ae617a0 | 24 | |
lharoon | 0:22612ae617a0 | 25 | virtual int isatty(); |
lharoon | 0:22612ae617a0 | 26 | |
lharoon | 0:22612ae617a0 | 27 | virtual off_t lseek(off_t position, int whence); |
lharoon | 0:22612ae617a0 | 28 | |
lharoon | 0:22612ae617a0 | 29 | virtual int fsync(); |
lharoon | 0:22612ae617a0 | 30 | |
lharoon | 0:22612ae617a0 | 31 | virtual off_t flen(); |
lharoon | 0:22612ae617a0 | 32 | |
lharoon | 0:22612ae617a0 | 33 | protected: |
lharoon | 0:22612ae617a0 | 34 | FILEHANDLE _fh; |
lharoon | 0:22612ae617a0 | 35 | int pos; |
lharoon | 0:22612ae617a0 | 36 | }; |
lharoon | 0:22612ae617a0 | 37 | |
lharoon | 0:22612ae617a0 | 38 | /* Class: LocalFileSystem |
lharoon | 0:22612ae617a0 | 39 | * A filesystem for accessing the local mbed Microcontroller USB disk drive |
lharoon | 0:22612ae617a0 | 40 | * |
lharoon | 0:22612ae617a0 | 41 | * This allows programs to read and write files on the same disk drive that is used to program the |
lharoon | 0:22612ae617a0 | 42 | * mbed Microcontroller. Once created, the standard C file access functions are used to open, |
lharoon | 0:22612ae617a0 | 43 | * read and write files. |
lharoon | 0:22612ae617a0 | 44 | * |
lharoon | 0:22612ae617a0 | 45 | * Example: |
lharoon | 0:22612ae617a0 | 46 | * > #include "mbed.h" |
lharoon | 0:22612ae617a0 | 47 | * > |
lharoon | 0:22612ae617a0 | 48 | * > LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
lharoon | 0:22612ae617a0 | 49 | * > |
lharoon | 0:22612ae617a0 | 50 | * > int main() { |
lharoon | 0:22612ae617a0 | 51 | * > FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing |
lharoon | 0:22612ae617a0 | 52 | * > fprintf(fp, "Hello World!"); |
lharoon | 0:22612ae617a0 | 53 | * > fclose(fp); |
lharoon | 0:22612ae617a0 | 54 | * > remove("/local/out.txt"); // Removes the file "out.txt" from the local file system |
lharoon | 0:22612ae617a0 | 55 | * > |
lharoon | 0:22612ae617a0 | 56 | * > DIR *d = opendir("/local"); // Opens the root directory of the local file system |
lharoon | 0:22612ae617a0 | 57 | * > struct dirent *p; |
lharoon | 0:22612ae617a0 | 58 | * > while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
lharoon | 0:22612ae617a0 | 59 | * > printf("%s\n", p->d_name); // to stdout. |
lharoon | 0:22612ae617a0 | 60 | * > } |
lharoon | 0:22612ae617a0 | 61 | * > closedir(d); |
lharoon | 0:22612ae617a0 | 62 | * > } |
lharoon | 0:22612ae617a0 | 63 | * |
lharoon | 0:22612ae617a0 | 64 | * Implementation Notes: |
lharoon | 0:22612ae617a0 | 65 | * If the microcontroller program makes an access to the local drive, it will be marked as "removed" |
lharoon | 0:22612ae617a0 | 66 | * on the Host computer. This means it is no longer accessible from the Host Computer. |
lharoon | 0:22612ae617a0 | 67 | * |
lharoon | 0:22612ae617a0 | 68 | * The drive will only re-appear when the microcontroller program exists. Note that if the program does |
lharoon | 0:22612ae617a0 | 69 | * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again! |
lharoon | 0:22612ae617a0 | 70 | */ |
lharoon | 0:22612ae617a0 | 71 | class LocalFileSystem : public FileSystemLike { |
lharoon | 0:22612ae617a0 | 72 | |
lharoon | 0:22612ae617a0 | 73 | public: |
lharoon | 0:22612ae617a0 | 74 | |
lharoon | 0:22612ae617a0 | 75 | LocalFileSystem(const char* n) : FileSystemLike(n) { |
lharoon | 0:22612ae617a0 | 76 | |
lharoon | 0:22612ae617a0 | 77 | } |
lharoon | 0:22612ae617a0 | 78 | |
lharoon | 0:22612ae617a0 | 79 | virtual FileHandle *open(const char* name, int flags); |
lharoon | 0:22612ae617a0 | 80 | virtual int remove(const char *filename); |
lharoon | 0:22612ae617a0 | 81 | virtual DirHandle *opendir(const char *name); |
lharoon | 0:22612ae617a0 | 82 | }; |
lharoon | 0:22612ae617a0 | 83 | |
lharoon | 0:22612ae617a0 | 84 | } // namespace mbed |
lharoon | 0:22612ae617a0 | 85 | |
lharoon | 0:22612ae617a0 | 86 | #endif |