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