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 "mbed.h"
jamesheavey 0:92b180c8d407 23
jamesheavey 0:92b180c8d407 24 #include "ffconf.h"
jamesheavey 0:92b180c8d407 25 #include "mbed_debug.h"
jamesheavey 0:92b180c8d407 26
jamesheavey 0:92b180c8d407 27 #include "FATFileSystem.h"
jamesheavey 0:92b180c8d407 28 #include "FATFileHandle.h"
jamesheavey 0:92b180c8d407 29 #include "FATDirHandle.h"
jamesheavey 0:92b180c8d407 30
jamesheavey 0:92b180c8d407 31 DWORD get_fattime(void) {
jamesheavey 0:92b180c8d407 32 time_t rawtime;
jamesheavey 0:92b180c8d407 33 time(&rawtime);
jamesheavey 0:92b180c8d407 34 struct tm *ptm = localtime(&rawtime);
jamesheavey 0:92b180c8d407 35 return (DWORD)(ptm->tm_year - 80) << 25
jamesheavey 0:92b180c8d407 36 | (DWORD)(ptm->tm_mon + 1 ) << 21
jamesheavey 0:92b180c8d407 37 | (DWORD)(ptm->tm_mday ) << 16
jamesheavey 0:92b180c8d407 38 | (DWORD)(ptm->tm_hour ) << 11
jamesheavey 0:92b180c8d407 39 | (DWORD)(ptm->tm_min ) << 5
jamesheavey 0:92b180c8d407 40 | (DWORD)(ptm->tm_sec/2 );
jamesheavey 0:92b180c8d407 41 }
jamesheavey 0:92b180c8d407 42
jamesheavey 0:92b180c8d407 43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
jamesheavey 0:92b180c8d407 44
jamesheavey 0:92b180c8d407 45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
jamesheavey 0:92b180c8d407 46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
jamesheavey 0:92b180c8d407 47 for(int i=0; i<_VOLUMES; i++) {
jamesheavey 0:92b180c8d407 48 if(_ffs[i] == 0) {
jamesheavey 0:92b180c8d407 49 _ffs[i] = this;
jamesheavey 0:92b180c8d407 50 _fsid[0] = '0' + i;
jamesheavey 0:92b180c8d407 51 _fsid[1] = '\0';
jamesheavey 0:92b180c8d407 52 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
jamesheavey 0:92b180c8d407 53 f_mount(&_fs, _fsid, 0);
jamesheavey 0:92b180c8d407 54 return;
jamesheavey 0:92b180c8d407 55 }
jamesheavey 0:92b180c8d407 56 }
jamesheavey 0:92b180c8d407 57 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
jamesheavey 0:92b180c8d407 58 }
jamesheavey 0:92b180c8d407 59
jamesheavey 0:92b180c8d407 60 FATFileSystem::~FATFileSystem() {
jamesheavey 0:92b180c8d407 61 for (int i=0; i<_VOLUMES; i++) {
jamesheavey 0:92b180c8d407 62 if (_ffs[i] == this) {
jamesheavey 0:92b180c8d407 63 _ffs[i] = 0;
jamesheavey 0:92b180c8d407 64 f_mount(NULL, _fsid, 0);
jamesheavey 0:92b180c8d407 65 }
jamesheavey 0:92b180c8d407 66 }
jamesheavey 0:92b180c8d407 67 }
jamesheavey 0:92b180c8d407 68
jamesheavey 0:92b180c8d407 69 FileHandle *FATFileSystem::open(const char* name, int flags) {
jamesheavey 0:92b180c8d407 70 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
jamesheavey 0:92b180c8d407 71 char n[64];
jamesheavey 0:92b180c8d407 72 sprintf(n, "%s:/%s", _fsid, name);
jamesheavey 0:92b180c8d407 73
jamesheavey 0:92b180c8d407 74 /* POSIX flags -> FatFS open mode */
jamesheavey 0:92b180c8d407 75 BYTE openmode;
jamesheavey 0:92b180c8d407 76 if (flags & O_RDWR) {
jamesheavey 0:92b180c8d407 77 openmode = FA_READ|FA_WRITE;
jamesheavey 0:92b180c8d407 78 } else if(flags & O_WRONLY) {
jamesheavey 0:92b180c8d407 79 openmode = FA_WRITE;
jamesheavey 0:92b180c8d407 80 } else {
jamesheavey 0:92b180c8d407 81 openmode = FA_READ;
jamesheavey 0:92b180c8d407 82 }
jamesheavey 0:92b180c8d407 83 if(flags & O_CREAT) {
jamesheavey 0:92b180c8d407 84 if(flags & O_TRUNC) {
jamesheavey 0:92b180c8d407 85 openmode |= FA_CREATE_ALWAYS;
jamesheavey 0:92b180c8d407 86 } else {
jamesheavey 0:92b180c8d407 87 openmode |= FA_OPEN_ALWAYS;
jamesheavey 0:92b180c8d407 88 }
jamesheavey 0:92b180c8d407 89 }
jamesheavey 0:92b180c8d407 90
jamesheavey 0:92b180c8d407 91 FIL fh;
jamesheavey 0:92b180c8d407 92 FRESULT res = f_open(&fh, n, openmode);
jamesheavey 0:92b180c8d407 93 if (res) {
jamesheavey 0:92b180c8d407 94 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
jamesheavey 0:92b180c8d407 95 return NULL;
jamesheavey 0:92b180c8d407 96 }
jamesheavey 0:92b180c8d407 97 if (flags & O_APPEND) {
jamesheavey 0:92b180c8d407 98 f_lseek(&fh, fh.fsize);
jamesheavey 0:92b180c8d407 99 }
jamesheavey 0:92b180c8d407 100 return new FATFileHandle(fh);
jamesheavey 0:92b180c8d407 101 }
jamesheavey 0:92b180c8d407 102
jamesheavey 0:92b180c8d407 103 int FATFileSystem::remove(const char *filename) {
jamesheavey 0:92b180c8d407 104 FRESULT res = f_unlink(filename);
jamesheavey 0:92b180c8d407 105 if (res) {
jamesheavey 0:92b180c8d407 106 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
jamesheavey 0:92b180c8d407 107 return -1;
jamesheavey 0:92b180c8d407 108 }
jamesheavey 0:92b180c8d407 109 return 0;
jamesheavey 0:92b180c8d407 110 }
jamesheavey 0:92b180c8d407 111
jamesheavey 0:92b180c8d407 112 int FATFileSystem::rename(const char *oldname, const char *newname) {
jamesheavey 0:92b180c8d407 113 FRESULT res = f_rename(oldname, newname);
jamesheavey 0:92b180c8d407 114 if (res) {
jamesheavey 0:92b180c8d407 115 debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
jamesheavey 0:92b180c8d407 116 return -1;
jamesheavey 0:92b180c8d407 117 }
jamesheavey 0:92b180c8d407 118 return 0;
jamesheavey 0:92b180c8d407 119 }
jamesheavey 0:92b180c8d407 120
jamesheavey 0:92b180c8d407 121 int FATFileSystem::format() {
jamesheavey 0:92b180c8d407 122 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
jamesheavey 0:92b180c8d407 123 if (res) {
jamesheavey 0:92b180c8d407 124 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
jamesheavey 0:92b180c8d407 125 return -1;
jamesheavey 0:92b180c8d407 126 }
jamesheavey 0:92b180c8d407 127 return 0;
jamesheavey 0:92b180c8d407 128 }
jamesheavey 0:92b180c8d407 129
jamesheavey 0:92b180c8d407 130 DirHandle *FATFileSystem::opendir(const char *name) {
jamesheavey 0:92b180c8d407 131 FATFS_DIR dir;
jamesheavey 0:92b180c8d407 132 FRESULT res = f_opendir(&dir, name);
jamesheavey 0:92b180c8d407 133 if (res != 0) {
jamesheavey 0:92b180c8d407 134 return NULL;
jamesheavey 0:92b180c8d407 135 }
jamesheavey 0:92b180c8d407 136 return new FATDirHandle(dir);
jamesheavey 0:92b180c8d407 137 }
jamesheavey 0:92b180c8d407 138
jamesheavey 0:92b180c8d407 139 int FATFileSystem::mkdir(const char *name, mode_t mode) {
jamesheavey 0:92b180c8d407 140 FRESULT res = f_mkdir(name);
jamesheavey 0:92b180c8d407 141 return res == 0 ? 0 : -1;
jamesheavey 0:92b180c8d407 142 }
jamesheavey 0:92b180c8d407 143
jamesheavey 0:92b180c8d407 144 int FATFileSystem::mount() {
jamesheavey 0:92b180c8d407 145 FRESULT res = f_mount(&_fs, _fsid, 1);
jamesheavey 0:92b180c8d407 146 return res == 0 ? 0 : -1;
jamesheavey 0:92b180c8d407 147 }
jamesheavey 0:92b180c8d407 148
jamesheavey 0:92b180c8d407 149 int FATFileSystem::unmount() {
jamesheavey 0:92b180c8d407 150 if (disk_sync())
jamesheavey 0:92b180c8d407 151 return -1;
jamesheavey 0:92b180c8d407 152 FRESULT res = f_mount(NULL, _fsid, 0);
jamesheavey 0:92b180c8d407 153 return res == 0 ? 0 : -1;
jamesheavey 0:92b180c8d407 154 }