Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

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.

Committer:
evanso
Date:
Wed May 27 02:06:05 2020 +0000
Revision:
87:832ca78426b5
Parent:
48:e308067cfea5
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 48:e308067cfea5 1 /* SD/MMC File System Library
evanso 48:e308067cfea5 2 * Copyright (c) 2016 Neil Thiessen
evanso 48:e308067cfea5 3 *
evanso 48:e308067cfea5 4 * Licensed under the Apache License, Version 2.0 (the "License");
evanso 48:e308067cfea5 5 * you may not use this file except in compliance with the License.
evanso 48:e308067cfea5 6 * You may obtain a copy of the License at
evanso 48:e308067cfea5 7 *
evanso 48:e308067cfea5 8 * http://www.apache.org/licenses/LICENSE-2.0
evanso 48:e308067cfea5 9 *
evanso 48:e308067cfea5 10 * Unless required by applicable law or agreed to in writing, software
evanso 48:e308067cfea5 11 * distributed under the License is distributed on an "AS IS" BASIS,
evanso 48:e308067cfea5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
evanso 48:e308067cfea5 13 * See the License for the specific language governing permissions and
evanso 48:e308067cfea5 14 * limitations under the License.
evanso 48:e308067cfea5 15 */
evanso 48:e308067cfea5 16
evanso 48:e308067cfea5 17 #ifndef SD_FILE_SYSTEM_H
evanso 48:e308067cfea5 18 #define SD_FILE_SYSTEM_H
evanso 48:e308067cfea5 19
evanso 48:e308067cfea5 20 #include "mbed.h"
evanso 48:e308067cfea5 21 #include "FATFileSystem.h"
evanso 48:e308067cfea5 22
evanso 48:e308067cfea5 23 /** SDFileSystem class.
evanso 48:e308067cfea5 24 * Used for creating a virtual file system for accessing SD/MMC cards via SPI.
evanso 48:e308067cfea5 25 *
evanso 48:e308067cfea5 26 * Example:
evanso 48:e308067cfea5 27 * @code
evanso 48:e308067cfea5 28 * #include "mbed.h"
evanso 48:e308067cfea5 29 * #include "SDFileSystem.h"
evanso 48:e308067cfea5 30 *
evanso 48:e308067cfea5 31 * //Create an SDFileSystem object
evanso 48:e308067cfea5 32 * SDFileSystem sd(p5, p6, p7, p20, "sd");
evanso 48:e308067cfea5 33 *
evanso 48:e308067cfea5 34 * int main()
evanso 48:e308067cfea5 35 * {
evanso 48:e308067cfea5 36 * //Mount the filesystem
evanso 48:e308067cfea5 37 * sd.mount();
evanso 48:e308067cfea5 38 *
evanso 48:e308067cfea5 39 * //Perform a write test
evanso 48:e308067cfea5 40 * printf("\nWriting to SD card...");
evanso 48:e308067cfea5 41 * FILE *fp = fopen("/sd/sdtest.txt", "w");
evanso 48:e308067cfea5 42 * if (fp != NULL) {
evanso 48:e308067cfea5 43 * fprintf(fp, "We're writing to an SD card!");
evanso 48:e308067cfea5 44 * fclose(fp);
evanso 48:e308067cfea5 45 * printf("success!\n");
evanso 48:e308067cfea5 46 * } else {
evanso 48:e308067cfea5 47 * printf("failed!\n");
evanso 48:e308067cfea5 48 * }
evanso 48:e308067cfea5 49 *
evanso 48:e308067cfea5 50 * //Perform a read test
evanso 48:e308067cfea5 51 * printf("Reading from SD card...");
evanso 48:e308067cfea5 52 * fp = fopen("/sd/sdtest.txt", "r");
evanso 48:e308067cfea5 53 * if (fp != NULL) {
evanso 48:e308067cfea5 54 * char c = fgetc(fp);
evanso 48:e308067cfea5 55 * if (c == 'W')
evanso 48:e308067cfea5 56 * printf("success!\n");
evanso 48:e308067cfea5 57 * else
evanso 48:e308067cfea5 58 * printf("incorrect char (%c)!\n", c);
evanso 48:e308067cfea5 59 * fclose(fp);
evanso 48:e308067cfea5 60 * } else {
evanso 48:e308067cfea5 61 * printf("failed!\n");
evanso 48:e308067cfea5 62 * }
evanso 48:e308067cfea5 63 *
evanso 48:e308067cfea5 64 * //Unmount the filesystem
evanso 48:e308067cfea5 65 * sd.unmount();
evanso 48:e308067cfea5 66 * }
evanso 48:e308067cfea5 67 * @endcode
evanso 48:e308067cfea5 68 */
evanso 48:e308067cfea5 69 class SDFileSystem : public FATFileSystem
evanso 48:e308067cfea5 70 {
evanso 48:e308067cfea5 71 public:
evanso 48:e308067cfea5 72 /** Represents the different card detect switch types
evanso 48:e308067cfea5 73 */
evanso 48:e308067cfea5 74 enum SwitchType {
evanso 48:e308067cfea5 75 SWITCH_NONE, /**< No card detect switch (assumes socket is always occupied) */
evanso 48:e308067cfea5 76 SWITCH_POS_NO, /**< Switch shorts to VDD when the socket is occupied (positive logic, normally open) */
evanso 48:e308067cfea5 77 SWITCH_POS_NC, /**< Switch shorts to VDD when the socket is empty (positive logic, normally closed) */
evanso 48:e308067cfea5 78 SWITCH_NEG_NO, /**< Switch shorts to GND when the socket is occupied (negative logic, normally open) */
evanso 48:e308067cfea5 79 SWITCH_NEG_NC /**< Switch shorts to GND when the socket is empty (negative logic, normally closed) */
evanso 48:e308067cfea5 80 };
evanso 48:e308067cfea5 81
evanso 48:e308067cfea5 82 /** Represents the different SD/MMC card types
evanso 48:e308067cfea5 83 */
evanso 48:e308067cfea5 84 enum CardType {
evanso 48:e308067cfea5 85 CARD_NONE, /**< No card is present */
evanso 48:e308067cfea5 86 CARD_MMC, /**< MMC card */
evanso 48:e308067cfea5 87 CARD_SD, /**< Standard capacity SD card */
evanso 48:e308067cfea5 88 CARD_SDHC, /**< High capacity SD card */
evanso 48:e308067cfea5 89 CARD_UNKNOWN /**< Unknown or unsupported card */
evanso 48:e308067cfea5 90 };
evanso 48:e308067cfea5 91
evanso 48:e308067cfea5 92 /** Create a virtual file system for accessing SD/MMC cards via SPI
evanso 48:e308067cfea5 93 *
evanso 48:e308067cfea5 94 * @param mosi The SPI data out pin.
evanso 48:e308067cfea5 95 * @param miso The SPI data in pin.
evanso 48:e308067cfea5 96 * @param sclk The SPI clock pin.
evanso 48:e308067cfea5 97 * @param cs The SPI chip select pin.
evanso 48:e308067cfea5 98 * @param name The name used to access the virtual filesystem.
evanso 48:e308067cfea5 99 * @param cd The card detect pin.
evanso 48:e308067cfea5 100 * @param cdtype The type of card detect switch.
evanso 48:e308067cfea5 101 * @param hz The SPI bus frequency (defaults to 1MHz).
evanso 48:e308067cfea5 102 */
evanso 48:e308067cfea5 103 SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, SwitchType cdtype = SWITCH_NONE, int hz = 1000000);
evanso 48:e308067cfea5 104
evanso 48:e308067cfea5 105 /** Determine whether or not a card is present
evanso 48:e308067cfea5 106 *
evanso 48:e308067cfea5 107 * @returns
evanso 48:e308067cfea5 108 * 'true' if a card is present,
evanso 48:e308067cfea5 109 * 'false' if no card is present.
evanso 48:e308067cfea5 110 */
evanso 48:e308067cfea5 111 bool card_present();
evanso 48:e308067cfea5 112
evanso 48:e308067cfea5 113 /** Get the detected SD/MMC card type
evanso 48:e308067cfea5 114 *
evanso 48:e308067cfea5 115 * @returns The detected card type as a CardType enum.
evanso 48:e308067cfea5 116 *
evanso 48:e308067cfea5 117 * @note Valid after the filesystem has been mounted.
evanso 48:e308067cfea5 118 */
evanso 48:e308067cfea5 119 SDFileSystem::CardType card_type();
evanso 48:e308067cfea5 120
evanso 48:e308067cfea5 121 /** Get whether or not CRC is enabled for commands and data
evanso 48:e308067cfea5 122 *
evanso 48:e308067cfea5 123 * @returns
evanso 48:e308067cfea5 124 * 'true' if CRC is enabled for commands and data,
evanso 48:e308067cfea5 125 * 'false' if CRC is disabled for commands and data.
evanso 48:e308067cfea5 126 */
evanso 48:e308067cfea5 127 bool crc();
evanso 48:e308067cfea5 128
evanso 48:e308067cfea5 129 /** Set whether or not CRC is enabled for commands and data
evanso 48:e308067cfea5 130 *
evanso 48:e308067cfea5 131 * @param enabled Whether or not to enable CRC for commands and data.
evanso 48:e308067cfea5 132 */
evanso 48:e308067cfea5 133 void crc(bool enabled);
evanso 48:e308067cfea5 134
evanso 48:e308067cfea5 135 /** Get whether or not 16-bit frames are enabled for data read/write operations
evanso 48:e308067cfea5 136 *
evanso 48:e308067cfea5 137 * @returns
evanso 48:e308067cfea5 138 * 'true' if 16-bit frames will be used during data read/write operations,
evanso 48:e308067cfea5 139 * 'false' if 8-bit frames will be used during data read/write operations.
evanso 48:e308067cfea5 140 */
evanso 48:e308067cfea5 141 bool large_frames();
evanso 48:e308067cfea5 142
evanso 48:e308067cfea5 143 /** Set whether or not 16-bit frames are enabled for data read/write operations
evanso 48:e308067cfea5 144 *
evanso 48:e308067cfea5 145 * @param enabled Whether or not 16-bit frames are enabled for data read/write operations.
evanso 48:e308067cfea5 146 */
evanso 48:e308067cfea5 147 void large_frames(bool enabled);
evanso 48:e308067cfea5 148
evanso 48:e308067cfea5 149 /** Get whether or not write validation is enabled for data write operations
evanso 48:e308067cfea5 150 *
evanso 48:e308067cfea5 151 * @returns
evanso 48:e308067cfea5 152 * 'true' if data writes will be verified using CMD13,
evanso 48:e308067cfea5 153 * 'false' if data writes will not be verified.
evanso 48:e308067cfea5 154 */
evanso 48:e308067cfea5 155 bool write_validation();
evanso 48:e308067cfea5 156
evanso 48:e308067cfea5 157 /** Set whether or not write validation is enabled for data write operations
evanso 48:e308067cfea5 158 *
evanso 48:e308067cfea5 159 * @param enabled Whether or not write validation is enabled for data write operations.
evanso 48:e308067cfea5 160 */
evanso 48:e308067cfea5 161 void write_validation(bool enabled);
evanso 48:e308067cfea5 162
evanso 48:e308067cfea5 163 virtual int unmount();
evanso 48:e308067cfea5 164 virtual int disk_initialize();
evanso 48:e308067cfea5 165 virtual int disk_status();
evanso 48:e308067cfea5 166 virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count);
evanso 48:e308067cfea5 167 virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count);
evanso 48:e308067cfea5 168 virtual int disk_sync();
evanso 48:e308067cfea5 169 virtual uint32_t disk_sectors();
evanso 48:e308067cfea5 170
evanso 48:e308067cfea5 171 private:
evanso 48:e308067cfea5 172 //Commands
evanso 48:e308067cfea5 173 enum Command {
evanso 48:e308067cfea5 174 CMD0 = (0x40 | 0), /**< GO_IDLE_STATE */
evanso 48:e308067cfea5 175 CMD1 = (0x40 | 1), /**< SEND_OP_COND */
evanso 48:e308067cfea5 176 CMD6 = (0x40 | 6), /**< SWITCH_FUNC */
evanso 48:e308067cfea5 177 CMD8 = (0x40 | 8), /**< SEND_IF_COND */
evanso 48:e308067cfea5 178 CMD9 = (0x40 | 9), /**< SEND_CSD */
evanso 48:e308067cfea5 179 CMD12 = (0x40 | 12), /**< STOP_TRANSMISSION */
evanso 48:e308067cfea5 180 CMD13 = (0x40 | 13), /**< SEND_STATUS */
evanso 48:e308067cfea5 181 CMD16 = (0x40 | 16), /**< SET_BLOCKLEN */
evanso 48:e308067cfea5 182 CMD17 = (0x40 | 17), /**< READ_SINGLE_BLOCK */
evanso 48:e308067cfea5 183 CMD18 = (0x40 | 18), /**< READ_MULTIPLE_BLOCK */
evanso 48:e308067cfea5 184 ACMD22 = (0x40 | 22), /**< SEND_NUM_WR_BLOCKS */
evanso 48:e308067cfea5 185 ACMD23 = (0x40 | 23), /**< SET_WR_BLK_ERASE_COUNT */
evanso 48:e308067cfea5 186 CMD24 = (0x40 | 24), /**< WRITE_BLOCK */
evanso 48:e308067cfea5 187 CMD25 = (0x40 | 25), /**< WRITE_MULTIPLE_BLOCK */
evanso 48:e308067cfea5 188 ACMD41 = (0x40 | 41), /**< SD_SEND_OP_COND */
evanso 48:e308067cfea5 189 ACMD42 = (0x40 | 42), /**< SET_CLR_CARD_DETECT */
evanso 48:e308067cfea5 190 CMD55 = (0x40 | 55), /**< APP_CMD */
evanso 48:e308067cfea5 191 CMD58 = (0x40 | 58), /**< READ_OCR */
evanso 48:e308067cfea5 192 CMD59 = (0x40 | 59) /**< CRC_ON_OFF */
evanso 48:e308067cfea5 193 };
evanso 48:e308067cfea5 194
evanso 48:e308067cfea5 195 //Member variables
evanso 48:e308067cfea5 196 Timer m_Timer;
evanso 48:e308067cfea5 197 SPI m_Spi;
evanso 48:e308067cfea5 198 DigitalOut m_Cs;
evanso 48:e308067cfea5 199 InterruptIn m_Cd;
evanso 48:e308067cfea5 200 int m_CdAssert;
evanso 48:e308067cfea5 201 const int m_FREQ;
evanso 48:e308067cfea5 202 SDFileSystem::CardType m_CardType;
evanso 48:e308067cfea5 203 bool m_Crc;
evanso 48:e308067cfea5 204 bool m_LargeFrames;
evanso 48:e308067cfea5 205 bool m_WriteValidation;
evanso 48:e308067cfea5 206 int m_Status;
evanso 48:e308067cfea5 207
evanso 48:e308067cfea5 208 //Internal methods
evanso 48:e308067cfea5 209 void onCardRemoval();
evanso 48:e308067cfea5 210 void checkSocket();
evanso 48:e308067cfea5 211 bool waitReady(int timeout);
evanso 48:e308067cfea5 212 bool select();
evanso 48:e308067cfea5 213 void deselect();
evanso 48:e308067cfea5 214 char commandTransaction(char cmd, unsigned int arg, unsigned int* resp = NULL);
evanso 48:e308067cfea5 215 char writeCommand(char cmd, unsigned int arg, unsigned int* resp = NULL);
evanso 48:e308067cfea5 216 bool readData(char* buffer, int length);
evanso 48:e308067cfea5 217 char writeData(const char* buffer, char token);
evanso 48:e308067cfea5 218 bool readBlock(char* buffer, unsigned int lba);
evanso 48:e308067cfea5 219 bool readBlocks(char* buffer, unsigned int lba, unsigned int count);
evanso 48:e308067cfea5 220 bool writeBlock(const char* buffer, unsigned int lba);
evanso 48:e308067cfea5 221 bool writeBlocks(const char* buffer, unsigned int lba, unsigned int count);
evanso 48:e308067cfea5 222 bool enableHighSpeedMode();
evanso 48:e308067cfea5 223 };
evanso 48:e308067cfea5 224
evanso 48:e308067cfea5 225 #endif