Ben Evans University Second Year Project. Game Called Defender.
Hello, soldier, you have been specially selected as the defender of planet earth.
Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.
But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.
As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.
SDFileSystem/FATFileSystem/FATFileSystem.cpp@48:e308067cfea5, 2020-05-19 (annotated)
- Committer:
- evanso
- Date:
- Tue May 19 15:13:47 2020 +0000
- Revision:
- 48:e308067cfea5
Added sd card class folder to project folder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evanso | 48:e308067cfea5 | 1 | /* mbed Microcontroller Library |
evanso | 48:e308067cfea5 | 2 | * Copyright (c) 2006-2012 ARM Limited |
evanso | 48:e308067cfea5 | 3 | * |
evanso | 48:e308067cfea5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
evanso | 48:e308067cfea5 | 5 | * of this software and associated documentation files (the "Software"), to deal |
evanso | 48:e308067cfea5 | 6 | * in the Software without restriction, including without limitation the rights |
evanso | 48:e308067cfea5 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
evanso | 48:e308067cfea5 | 8 | * copies of the Software, and to permit persons to whom the Software is |
evanso | 48:e308067cfea5 | 9 | * furnished to do so, subject to the following conditions: |
evanso | 48:e308067cfea5 | 10 | * |
evanso | 48:e308067cfea5 | 11 | * The above copyright notice and this permission notice shall be included in |
evanso | 48:e308067cfea5 | 12 | * all copies or substantial portions of the Software. |
evanso | 48:e308067cfea5 | 13 | * |
evanso | 48:e308067cfea5 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
evanso | 48:e308067cfea5 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
evanso | 48:e308067cfea5 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
evanso | 48:e308067cfea5 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
evanso | 48:e308067cfea5 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
evanso | 48:e308067cfea5 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
evanso | 48:e308067cfea5 | 20 | * SOFTWARE. |
evanso | 48:e308067cfea5 | 21 | */ |
evanso | 48:e308067cfea5 | 22 | #include "mbed.h" |
evanso | 48:e308067cfea5 | 23 | |
evanso | 48:e308067cfea5 | 24 | #include "ffconf.h" |
evanso | 48:e308067cfea5 | 25 | #include "mbed_debug.h" |
evanso | 48:e308067cfea5 | 26 | |
evanso | 48:e308067cfea5 | 27 | #include "FATFileSystem.h" |
evanso | 48:e308067cfea5 | 28 | #include "FATFileHandle.h" |
evanso | 48:e308067cfea5 | 29 | #include "FATDirHandle.h" |
evanso | 48:e308067cfea5 | 30 | |
evanso | 48:e308067cfea5 | 31 | DWORD get_fattime(void) { |
evanso | 48:e308067cfea5 | 32 | time_t rawtime; |
evanso | 48:e308067cfea5 | 33 | time(&rawtime); |
evanso | 48:e308067cfea5 | 34 | struct tm *ptm = localtime(&rawtime); |
evanso | 48:e308067cfea5 | 35 | return (DWORD)(ptm->tm_year - 80) << 25 |
evanso | 48:e308067cfea5 | 36 | | (DWORD)(ptm->tm_mon + 1 ) << 21 |
evanso | 48:e308067cfea5 | 37 | | (DWORD)(ptm->tm_mday ) << 16 |
evanso | 48:e308067cfea5 | 38 | | (DWORD)(ptm->tm_hour ) << 11 |
evanso | 48:e308067cfea5 | 39 | | (DWORD)(ptm->tm_min ) << 5 |
evanso | 48:e308067cfea5 | 40 | | (DWORD)(ptm->tm_sec/2 ); |
evanso | 48:e308067cfea5 | 41 | } |
evanso | 48:e308067cfea5 | 42 | |
evanso | 48:e308067cfea5 | 43 | FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; |
evanso | 48:e308067cfea5 | 44 | |
evanso | 48:e308067cfea5 | 45 | FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { |
evanso | 48:e308067cfea5 | 46 | debug_if(FFS_DBG, "FATFileSystem(%s)\n", n); |
evanso | 48:e308067cfea5 | 47 | for(int i=0; i<_VOLUMES; i++) { |
evanso | 48:e308067cfea5 | 48 | if(_ffs[i] == 0) { |
evanso | 48:e308067cfea5 | 49 | _ffs[i] = this; |
evanso | 48:e308067cfea5 | 50 | _fsid[0] = '0' + i; |
evanso | 48:e308067cfea5 | 51 | _fsid[1] = '\0'; |
evanso | 48:e308067cfea5 | 52 | debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid); |
evanso | 48:e308067cfea5 | 53 | f_mount(&_fs, _fsid, 0); |
evanso | 48:e308067cfea5 | 54 | return; |
evanso | 48:e308067cfea5 | 55 | } |
evanso | 48:e308067cfea5 | 56 | } |
evanso | 48:e308067cfea5 | 57 | error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n); |
evanso | 48:e308067cfea5 | 58 | } |
evanso | 48:e308067cfea5 | 59 | |
evanso | 48:e308067cfea5 | 60 | FATFileSystem::~FATFileSystem() { |
evanso | 48:e308067cfea5 | 61 | for (int i=0; i<_VOLUMES; i++) { |
evanso | 48:e308067cfea5 | 62 | if (_ffs[i] == this) { |
evanso | 48:e308067cfea5 | 63 | _ffs[i] = 0; |
evanso | 48:e308067cfea5 | 64 | f_mount(NULL, _fsid, 0); |
evanso | 48:e308067cfea5 | 65 | } |
evanso | 48:e308067cfea5 | 66 | } |
evanso | 48:e308067cfea5 | 67 | } |
evanso | 48:e308067cfea5 | 68 | |
evanso | 48:e308067cfea5 | 69 | FileHandle *FATFileSystem::open(const char* name, int flags) { |
evanso | 48:e308067cfea5 | 70 | debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid); |
evanso | 48:e308067cfea5 | 71 | char n[64]; |
evanso | 48:e308067cfea5 | 72 | sprintf(n, "%s:/%s", _fsid, name); |
evanso | 48:e308067cfea5 | 73 | |
evanso | 48:e308067cfea5 | 74 | /* POSIX flags -> FatFS open mode */ |
evanso | 48:e308067cfea5 | 75 | BYTE openmode; |
evanso | 48:e308067cfea5 | 76 | if (flags & O_RDWR) { |
evanso | 48:e308067cfea5 | 77 | openmode = FA_READ|FA_WRITE; |
evanso | 48:e308067cfea5 | 78 | } else if(flags & O_WRONLY) { |
evanso | 48:e308067cfea5 | 79 | openmode = FA_WRITE; |
evanso | 48:e308067cfea5 | 80 | } else { |
evanso | 48:e308067cfea5 | 81 | openmode = FA_READ; |
evanso | 48:e308067cfea5 | 82 | } |
evanso | 48:e308067cfea5 | 83 | if(flags & O_CREAT) { |
evanso | 48:e308067cfea5 | 84 | if(flags & O_TRUNC) { |
evanso | 48:e308067cfea5 | 85 | openmode |= FA_CREATE_ALWAYS; |
evanso | 48:e308067cfea5 | 86 | } else { |
evanso | 48:e308067cfea5 | 87 | openmode |= FA_OPEN_ALWAYS; |
evanso | 48:e308067cfea5 | 88 | } |
evanso | 48:e308067cfea5 | 89 | } |
evanso | 48:e308067cfea5 | 90 | |
evanso | 48:e308067cfea5 | 91 | FIL fh; |
evanso | 48:e308067cfea5 | 92 | FRESULT res = f_open(&fh, n, openmode); |
evanso | 48:e308067cfea5 | 93 | if (res) { |
evanso | 48:e308067cfea5 | 94 | debug_if(FFS_DBG, "f_open('w') failed: %d\n", res); |
evanso | 48:e308067cfea5 | 95 | return NULL; |
evanso | 48:e308067cfea5 | 96 | } |
evanso | 48:e308067cfea5 | 97 | if (flags & O_APPEND) { |
evanso | 48:e308067cfea5 | 98 | f_lseek(&fh, fh.fsize); |
evanso | 48:e308067cfea5 | 99 | } |
evanso | 48:e308067cfea5 | 100 | return new FATFileHandle(fh); |
evanso | 48:e308067cfea5 | 101 | } |
evanso | 48:e308067cfea5 | 102 | |
evanso | 48:e308067cfea5 | 103 | int FATFileSystem::open(FileHandle **file, const char *name, int flags) { |
evanso | 48:e308067cfea5 | 104 | FileHandle *temp = open(name, flags); |
evanso | 48:e308067cfea5 | 105 | if (!temp) { |
evanso | 48:e308067cfea5 | 106 | return -1; |
evanso | 48:e308067cfea5 | 107 | } |
evanso | 48:e308067cfea5 | 108 | |
evanso | 48:e308067cfea5 | 109 | *file = temp; |
evanso | 48:e308067cfea5 | 110 | return 0; |
evanso | 48:e308067cfea5 | 111 | } |
evanso | 48:e308067cfea5 | 112 | |
evanso | 48:e308067cfea5 | 113 | int FATFileSystem::remove(const char *filename) { |
evanso | 48:e308067cfea5 | 114 | FRESULT res = f_unlink(filename); |
evanso | 48:e308067cfea5 | 115 | if (res) { |
evanso | 48:e308067cfea5 | 116 | debug_if(FFS_DBG, "f_unlink() failed: %d\n", res); |
evanso | 48:e308067cfea5 | 117 | return -1; |
evanso | 48:e308067cfea5 | 118 | } |
evanso | 48:e308067cfea5 | 119 | return 0; |
evanso | 48:e308067cfea5 | 120 | } |
evanso | 48:e308067cfea5 | 121 | |
evanso | 48:e308067cfea5 | 122 | int FATFileSystem::rename(const char *oldname, const char *newname) { |
evanso | 48:e308067cfea5 | 123 | FRESULT res = f_rename(oldname, newname); |
evanso | 48:e308067cfea5 | 124 | if (res) { |
evanso | 48:e308067cfea5 | 125 | debug_if(FFS_DBG, "f_rename() failed: %d\n", res); |
evanso | 48:e308067cfea5 | 126 | return -1; |
evanso | 48:e308067cfea5 | 127 | } |
evanso | 48:e308067cfea5 | 128 | return 0; |
evanso | 48:e308067cfea5 | 129 | } |
evanso | 48:e308067cfea5 | 130 | |
evanso | 48:e308067cfea5 | 131 | int FATFileSystem::format() { |
evanso | 48:e308067cfea5 | 132 | FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) |
evanso | 48:e308067cfea5 | 133 | if (res) { |
evanso | 48:e308067cfea5 | 134 | debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); |
evanso | 48:e308067cfea5 | 135 | return -1; |
evanso | 48:e308067cfea5 | 136 | } |
evanso | 48:e308067cfea5 | 137 | return 0; |
evanso | 48:e308067cfea5 | 138 | } |
evanso | 48:e308067cfea5 | 139 | |
evanso | 48:e308067cfea5 | 140 | DirHandle *FATFileSystem::opendir(const char *name) { |
evanso | 48:e308067cfea5 | 141 | FATFS_DIR dir; |
evanso | 48:e308067cfea5 | 142 | FRESULT res = f_opendir(&dir, name); |
evanso | 48:e308067cfea5 | 143 | if (res != 0) { |
evanso | 48:e308067cfea5 | 144 | return NULL; |
evanso | 48:e308067cfea5 | 145 | } |
evanso | 48:e308067cfea5 | 146 | return new FATDirHandle(dir); |
evanso | 48:e308067cfea5 | 147 | } |
evanso | 48:e308067cfea5 | 148 | |
evanso | 48:e308067cfea5 | 149 | int FATFileSystem::open(DirHandle **dir, const char *name) { |
evanso | 48:e308067cfea5 | 150 | DirHandle *temp = opendir(name); |
evanso | 48:e308067cfea5 | 151 | if (!temp) { |
evanso | 48:e308067cfea5 | 152 | return -1; |
evanso | 48:e308067cfea5 | 153 | } |
evanso | 48:e308067cfea5 | 154 | |
evanso | 48:e308067cfea5 | 155 | *dir = temp; |
evanso | 48:e308067cfea5 | 156 | return 0; |
evanso | 48:e308067cfea5 | 157 | } |
evanso | 48:e308067cfea5 | 158 | |
evanso | 48:e308067cfea5 | 159 | int FATFileSystem::mkdir(const char *name, mode_t mode) { |
evanso | 48:e308067cfea5 | 160 | FRESULT res = f_mkdir(name); |
evanso | 48:e308067cfea5 | 161 | return res == 0 ? 0 : -1; |
evanso | 48:e308067cfea5 | 162 | } |
evanso | 48:e308067cfea5 | 163 | |
evanso | 48:e308067cfea5 | 164 | int FATFileSystem::mount() { |
evanso | 48:e308067cfea5 | 165 | FRESULT res = f_mount(&_fs, _fsid, 1); |
evanso | 48:e308067cfea5 | 166 | return res == 0 ? 0 : -1; |
evanso | 48:e308067cfea5 | 167 | } |
evanso | 48:e308067cfea5 | 168 | |
evanso | 48:e308067cfea5 | 169 | int FATFileSystem::unmount() { |
evanso | 48:e308067cfea5 | 170 | if (disk_sync()) |
evanso | 48:e308067cfea5 | 171 | return -1; |
evanso | 48:e308067cfea5 | 172 | FRESULT res = f_mount(NULL, _fsid, 0); |
evanso | 48:e308067cfea5 | 173 | return res == 0 ? 0 : -1; |
evanso | 48:e308067cfea5 | 174 | } |