P2-2 Harris Barton

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
DCchico
Date:
Fri Oct 23 16:18:39 2020 -0400
Revision:
1:10330bce85cb
shell-code

Who changed what in which revision?

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