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