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