Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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