help :(
Dependencies: FFT
FATFileSystem/FATFileHandle.cpp@0:d6c9b09b4042, 2020-12-02 (annotated)
- Committer:
- annieluo2
- Date:
- Wed Dec 02 18:02:03 2020 +0000
- Revision:
- 0:d6c9b09b4042
boo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
annieluo2 | 0:d6c9b09b4042 | 1 | /* mbed Microcontroller Library |
annieluo2 | 0:d6c9b09b4042 | 2 | * Copyright (c) 2006-2012 ARM Limited |
annieluo2 | 0:d6c9b09b4042 | 3 | * |
annieluo2 | 0:d6c9b09b4042 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
annieluo2 | 0:d6c9b09b4042 | 5 | * of this software and associated documentation files (the "Software"), to deal |
annieluo2 | 0:d6c9b09b4042 | 6 | * in the Software without restriction, including without limitation the rights |
annieluo2 | 0:d6c9b09b4042 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
annieluo2 | 0:d6c9b09b4042 | 8 | * copies of the Software, and to permit persons to whom the Software is |
annieluo2 | 0:d6c9b09b4042 | 9 | * furnished to do so, subject to the following conditions: |
annieluo2 | 0:d6c9b09b4042 | 10 | * |
annieluo2 | 0:d6c9b09b4042 | 11 | * The above copyright notice and this permission notice shall be included in |
annieluo2 | 0:d6c9b09b4042 | 12 | * all copies or substantial portions of the Software. |
annieluo2 | 0:d6c9b09b4042 | 13 | * |
annieluo2 | 0:d6c9b09b4042 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
annieluo2 | 0:d6c9b09b4042 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
annieluo2 | 0:d6c9b09b4042 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
annieluo2 | 0:d6c9b09b4042 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
annieluo2 | 0:d6c9b09b4042 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
annieluo2 | 0:d6c9b09b4042 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
annieluo2 | 0:d6c9b09b4042 | 20 | * SOFTWARE. |
annieluo2 | 0:d6c9b09b4042 | 21 | */ |
annieluo2 | 0:d6c9b09b4042 | 22 | #include "ff.h" |
annieluo2 | 0:d6c9b09b4042 | 23 | #include "ffconf.h" |
annieluo2 | 0:d6c9b09b4042 | 24 | #include "mbed_debug.h" |
annieluo2 | 0:d6c9b09b4042 | 25 | |
annieluo2 | 0:d6c9b09b4042 | 26 | #include "FATFileHandle.h" |
annieluo2 | 0:d6c9b09b4042 | 27 | |
annieluo2 | 0:d6c9b09b4042 | 28 | FATFileHandle::FATFileHandle(FIL fh) { |
annieluo2 | 0:d6c9b09b4042 | 29 | _fh = fh; |
annieluo2 | 0:d6c9b09b4042 | 30 | } |
annieluo2 | 0:d6c9b09b4042 | 31 | |
annieluo2 | 0:d6c9b09b4042 | 32 | int FATFileHandle::close() { |
annieluo2 | 0:d6c9b09b4042 | 33 | int retval = f_close(&_fh); |
annieluo2 | 0:d6c9b09b4042 | 34 | delete this; |
annieluo2 | 0:d6c9b09b4042 | 35 | return retval; |
annieluo2 | 0:d6c9b09b4042 | 36 | } |
annieluo2 | 0:d6c9b09b4042 | 37 | |
annieluo2 | 0:d6c9b09b4042 | 38 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
annieluo2 | 0:d6c9b09b4042 | 39 | UINT n; |
annieluo2 | 0:d6c9b09b4042 | 40 | FRESULT res = f_write(&_fh, buffer, length, &n); |
annieluo2 | 0:d6c9b09b4042 | 41 | if (res) { |
annieluo2 | 0:d6c9b09b4042 | 42 | debug_if(FFS_DBG, "f_write() failed: %d", res); |
annieluo2 | 0:d6c9b09b4042 | 43 | return -1; |
annieluo2 | 0:d6c9b09b4042 | 44 | } |
annieluo2 | 0:d6c9b09b4042 | 45 | return n; |
annieluo2 | 0:d6c9b09b4042 | 46 | } |
annieluo2 | 0:d6c9b09b4042 | 47 | |
annieluo2 | 0:d6c9b09b4042 | 48 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
annieluo2 | 0:d6c9b09b4042 | 49 | debug_if(FFS_DBG, "read(%d)\n", length); |
annieluo2 | 0:d6c9b09b4042 | 50 | UINT n; |
annieluo2 | 0:d6c9b09b4042 | 51 | FRESULT res = f_read(&_fh, buffer, length, &n); |
annieluo2 | 0:d6c9b09b4042 | 52 | if (res) { |
annieluo2 | 0:d6c9b09b4042 | 53 | debug_if(FFS_DBG, "f_read() failed: %d\n", res); |
annieluo2 | 0:d6c9b09b4042 | 54 | return -1; |
annieluo2 | 0:d6c9b09b4042 | 55 | } |
annieluo2 | 0:d6c9b09b4042 | 56 | return n; |
annieluo2 | 0:d6c9b09b4042 | 57 | } |
annieluo2 | 0:d6c9b09b4042 | 58 | |
annieluo2 | 0:d6c9b09b4042 | 59 | int FATFileHandle::isatty() { |
annieluo2 | 0:d6c9b09b4042 | 60 | return 0; |
annieluo2 | 0:d6c9b09b4042 | 61 | } |
annieluo2 | 0:d6c9b09b4042 | 62 | |
annieluo2 | 0:d6c9b09b4042 | 63 | off_t FATFileHandle::lseek(off_t position, int whence) { |
annieluo2 | 0:d6c9b09b4042 | 64 | if (whence == SEEK_END) { |
annieluo2 | 0:d6c9b09b4042 | 65 | position += _fh.fsize; |
annieluo2 | 0:d6c9b09b4042 | 66 | } else if(whence==SEEK_CUR) { |
annieluo2 | 0:d6c9b09b4042 | 67 | position += _fh.fptr; |
annieluo2 | 0:d6c9b09b4042 | 68 | } |
annieluo2 | 0:d6c9b09b4042 | 69 | FRESULT res = f_lseek(&_fh, position); |
annieluo2 | 0:d6c9b09b4042 | 70 | if (res) { |
annieluo2 | 0:d6c9b09b4042 | 71 | debug_if(FFS_DBG, "lseek failed: %d\n", res); |
annieluo2 | 0:d6c9b09b4042 | 72 | return -1; |
annieluo2 | 0:d6c9b09b4042 | 73 | } else { |
annieluo2 | 0:d6c9b09b4042 | 74 | debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); |
annieluo2 | 0:d6c9b09b4042 | 75 | return _fh.fptr; |
annieluo2 | 0:d6c9b09b4042 | 76 | } |
annieluo2 | 0:d6c9b09b4042 | 77 | } |
annieluo2 | 0:d6c9b09b4042 | 78 | |
annieluo2 | 0:d6c9b09b4042 | 79 | int FATFileHandle::fsync() { |
annieluo2 | 0:d6c9b09b4042 | 80 | FRESULT res = f_sync(&_fh); |
annieluo2 | 0:d6c9b09b4042 | 81 | if (res) { |
annieluo2 | 0:d6c9b09b4042 | 82 | debug_if(FFS_DBG, "f_sync() failed: %d\n", res); |
annieluo2 | 0:d6c9b09b4042 | 83 | return -1; |
annieluo2 | 0:d6c9b09b4042 | 84 | } |
annieluo2 | 0:d6c9b09b4042 | 85 | return 0; |
annieluo2 | 0:d6c9b09b4042 | 86 | } |
annieluo2 | 0:d6c9b09b4042 | 87 | |
annieluo2 | 0:d6c9b09b4042 | 88 | off_t FATFileHandle::flen() { |
annieluo2 | 0:d6c9b09b4042 | 89 | return _fh.fsize; |
annieluo2 | 0:d6c9b09b4042 | 90 | } |