Mbed project to control a hoverboard

Dependencies:   mbed wave_player Servo 4DGL-uLCD-SE LSM9DS1_Library_cal HC_SR04_Ultrasonic_Library

Committer:
msafwan3
Date:
Wed Apr 29 22:42:25 2020 +0000
Revision:
3:bf0c46975248
Parent:
0:808403b99108
removed rtos

Who changed what in which revision?

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