CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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