Only Yesterday 制御プログラム

Dependencies:   mbed FATFileSystem

Fork of OnlyYestaerday by Junichi Katsu

Committer:
jksoft
Date:
Wed Apr 02 01:43:55 2014 +0000
Revision:
0:5975af170e43
1st Prototype

Who changed what in which revision?

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