SNAKE GAME

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
congvu
Date:
Wed Nov 25 04:25:25 2020 +0000
Revision:
0:24041b847eb5
ECE2035 SNAKE GAME;

Who changed what in which revision?

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