James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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