mbed library for slider v2
LocalFileSystem.h@0:b7116bd48af6, 2016-09-14 (annotated)
- Committer:
- mturner5
- Date:
- Wed Sep 14 07:04:27 2016 +0000
- Revision:
- 0:b7116bd48af6
Tried to use the timer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mturner5 | 0:b7116bd48af6 | 1 | /* mbed Microcontroller Library |
mturner5 | 0:b7116bd48af6 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mturner5 | 0:b7116bd48af6 | 3 | * |
mturner5 | 0:b7116bd48af6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mturner5 | 0:b7116bd48af6 | 5 | * you may not use this file except in compliance with the License. |
mturner5 | 0:b7116bd48af6 | 6 | * You may obtain a copy of the License at |
mturner5 | 0:b7116bd48af6 | 7 | * |
mturner5 | 0:b7116bd48af6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mturner5 | 0:b7116bd48af6 | 9 | * |
mturner5 | 0:b7116bd48af6 | 10 | * Unless required by applicable law or agreed to in writing, software |
mturner5 | 0:b7116bd48af6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mturner5 | 0:b7116bd48af6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mturner5 | 0:b7116bd48af6 | 13 | * See the License for the specific language governing permissions and |
mturner5 | 0:b7116bd48af6 | 14 | * limitations under the License. |
mturner5 | 0:b7116bd48af6 | 15 | */ |
mturner5 | 0:b7116bd48af6 | 16 | #ifndef MBED_LOCALFILESYSTEM_H |
mturner5 | 0:b7116bd48af6 | 17 | #define MBED_LOCALFILESYSTEM_H |
mturner5 | 0:b7116bd48af6 | 18 | |
mturner5 | 0:b7116bd48af6 | 19 | #include "platform.h" |
mturner5 | 0:b7116bd48af6 | 20 | |
mturner5 | 0:b7116bd48af6 | 21 | #if DEVICE_LOCALFILESYSTEM |
mturner5 | 0:b7116bd48af6 | 22 | |
mturner5 | 0:b7116bd48af6 | 23 | #include "FileSystemLike.h" |
mturner5 | 0:b7116bd48af6 | 24 | #include "PlatformMutex.h" |
mturner5 | 0:b7116bd48af6 | 25 | |
mturner5 | 0:b7116bd48af6 | 26 | namespace mbed { |
mturner5 | 0:b7116bd48af6 | 27 | |
mturner5 | 0:b7116bd48af6 | 28 | FILEHANDLE local_file_open(const char* name, int flags); |
mturner5 | 0:b7116bd48af6 | 29 | |
mturner5 | 0:b7116bd48af6 | 30 | class LocalFileHandle : public FileHandle { |
mturner5 | 0:b7116bd48af6 | 31 | |
mturner5 | 0:b7116bd48af6 | 32 | public: |
mturner5 | 0:b7116bd48af6 | 33 | LocalFileHandle(FILEHANDLE fh); |
mturner5 | 0:b7116bd48af6 | 34 | |
mturner5 | 0:b7116bd48af6 | 35 | virtual int close(); |
mturner5 | 0:b7116bd48af6 | 36 | |
mturner5 | 0:b7116bd48af6 | 37 | virtual ssize_t write(const void *buffer, size_t length); |
mturner5 | 0:b7116bd48af6 | 38 | |
mturner5 | 0:b7116bd48af6 | 39 | virtual ssize_t read(void *buffer, size_t length); |
mturner5 | 0:b7116bd48af6 | 40 | |
mturner5 | 0:b7116bd48af6 | 41 | virtual int isatty(); |
mturner5 | 0:b7116bd48af6 | 42 | |
mturner5 | 0:b7116bd48af6 | 43 | virtual off_t lseek(off_t position, int whence); |
mturner5 | 0:b7116bd48af6 | 44 | |
mturner5 | 0:b7116bd48af6 | 45 | virtual int fsync(); |
mturner5 | 0:b7116bd48af6 | 46 | |
mturner5 | 0:b7116bd48af6 | 47 | virtual off_t flen(); |
mturner5 | 0:b7116bd48af6 | 48 | |
mturner5 | 0:b7116bd48af6 | 49 | protected: |
mturner5 | 0:b7116bd48af6 | 50 | virtual void lock(); |
mturner5 | 0:b7116bd48af6 | 51 | virtual void unlock(); |
mturner5 | 0:b7116bd48af6 | 52 | FILEHANDLE _fh; |
mturner5 | 0:b7116bd48af6 | 53 | int pos; |
mturner5 | 0:b7116bd48af6 | 54 | PlatformMutex _mutex; |
mturner5 | 0:b7116bd48af6 | 55 | }; |
mturner5 | 0:b7116bd48af6 | 56 | |
mturner5 | 0:b7116bd48af6 | 57 | /** A filesystem for accessing the local mbed Microcontroller USB disk drive |
mturner5 | 0:b7116bd48af6 | 58 | * |
mturner5 | 0:b7116bd48af6 | 59 | * This allows programs to read and write files on the same disk drive that is used to program the |
mturner5 | 0:b7116bd48af6 | 60 | * mbed Microcontroller. Once created, the standard C file access functions are used to open, |
mturner5 | 0:b7116bd48af6 | 61 | * read and write files. |
mturner5 | 0:b7116bd48af6 | 62 | * |
mturner5 | 0:b7116bd48af6 | 63 | * @Note Synchronization level: Thread safe |
mturner5 | 0:b7116bd48af6 | 64 | * |
mturner5 | 0:b7116bd48af6 | 65 | * Example: |
mturner5 | 0:b7116bd48af6 | 66 | * @code |
mturner5 | 0:b7116bd48af6 | 67 | * #include "mbed.h" |
mturner5 | 0:b7116bd48af6 | 68 | * |
mturner5 | 0:b7116bd48af6 | 69 | * LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
mturner5 | 0:b7116bd48af6 | 70 | * |
mturner5 | 0:b7116bd48af6 | 71 | * int main() { |
mturner5 | 0:b7116bd48af6 | 72 | * FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing |
mturner5 | 0:b7116bd48af6 | 73 | * fprintf(fp, "Hello World!"); |
mturner5 | 0:b7116bd48af6 | 74 | * fclose(fp); |
mturner5 | 0:b7116bd48af6 | 75 | * remove("/local/out.txt"); // Removes the file "out.txt" from the local file system |
mturner5 | 0:b7116bd48af6 | 76 | * |
mturner5 | 0:b7116bd48af6 | 77 | * DIR *d = opendir("/local"); // Opens the root directory of the local file system |
mturner5 | 0:b7116bd48af6 | 78 | * struct dirent *p; |
mturner5 | 0:b7116bd48af6 | 79 | * while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
mturner5 | 0:b7116bd48af6 | 80 | * printf("%s\n", p->d_name); // to stdout. |
mturner5 | 0:b7116bd48af6 | 81 | * } |
mturner5 | 0:b7116bd48af6 | 82 | * closedir(d); |
mturner5 | 0:b7116bd48af6 | 83 | * } |
mturner5 | 0:b7116bd48af6 | 84 | * @endcode |
mturner5 | 0:b7116bd48af6 | 85 | * |
mturner5 | 0:b7116bd48af6 | 86 | * @note |
mturner5 | 0:b7116bd48af6 | 87 | * If the microcontroller program makes an access to the local drive, it will be marked as "removed" |
mturner5 | 0:b7116bd48af6 | 88 | * on the Host computer. This means it is no longer accessible from the Host Computer. |
mturner5 | 0:b7116bd48af6 | 89 | * |
mturner5 | 0:b7116bd48af6 | 90 | * The drive will only re-appear when the microcontroller program exists. Note that if the program does |
mturner5 | 0:b7116bd48af6 | 91 | * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again! |
mturner5 | 0:b7116bd48af6 | 92 | */ |
mturner5 | 0:b7116bd48af6 | 93 | class LocalFileSystem : public FileSystemLike { |
mturner5 | 0:b7116bd48af6 | 94 | // No modifiable state |
mturner5 | 0:b7116bd48af6 | 95 | |
mturner5 | 0:b7116bd48af6 | 96 | public: |
mturner5 | 0:b7116bd48af6 | 97 | LocalFileSystem(const char* n) : FileSystemLike(n) { |
mturner5 | 0:b7116bd48af6 | 98 | |
mturner5 | 0:b7116bd48af6 | 99 | } |
mturner5 | 0:b7116bd48af6 | 100 | |
mturner5 | 0:b7116bd48af6 | 101 | virtual FileHandle *open(const char* name, int flags); |
mturner5 | 0:b7116bd48af6 | 102 | virtual int remove(const char *filename); |
mturner5 | 0:b7116bd48af6 | 103 | virtual DirHandle *opendir(const char *name); |
mturner5 | 0:b7116bd48af6 | 104 | }; |
mturner5 | 0:b7116bd48af6 | 105 | |
mturner5 | 0:b7116bd48af6 | 106 | } // namespace mbed |
mturner5 | 0:b7116bd48af6 | 107 | |
mturner5 | 0:b7116bd48af6 | 108 | #endif |
mturner5 | 0:b7116bd48af6 | 109 | |
mturner5 | 0:b7116bd48af6 | 110 | #endif |