Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Committer:
joshdavy
Date:
Mon May 06 14:43:01 2019 +0000
Revision:
11:db27d3838514
Parent:
10:58cf89dd878c
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joshdavy 0:4916a63a6cbf 1 /*
joshdavy 0:4916a63a6cbf 2 ELEC2645 Embedded Systems Project
joshdavy 0:4916a63a6cbf 3 School of Electronic & Electrical Engineering
joshdavy 0:4916a63a6cbf 4 University of Leeds
joshdavy 0:4916a63a6cbf 5 Name: Joshua Davy
joshdavy 0:4916a63a6cbf 6 Username: el17jd
joshdavy 0:4916a63a6cbf 7 Student ID Number: 201148379
joshdavy 0:4916a63a6cbf 8 Date: 12/03/2019
joshdavy 1:37802772843e 9 */
joshdavy 2:b62e8be35a5d 10
joshdavy 1:37802772843e 11
joshdavy 1:37802772843e 12 ///////// pre-processor directives ////////
joshdavy 1:37802772843e 13 #include "mbed.h"
joshdavy 1:37802772843e 14 #include "Gamepad.h"
joshdavy 1:37802772843e 15 #include "N5110.h"
joshdavy 1:37802772843e 16 #include "Sprite.h"
joshdavy 1:37802772843e 17 #include "Game.h"
joshdavy 5:b9cf407bcc63 18 #include "Music.h"
joshdavy 5:b9cf407bcc63 19 #include "SoundData.h"
joshdavy 8:21b6d4dbce44 20 #include "SplashScreen.h"
joshdavy 8:21b6d4dbce44 21 #include "MenuScreen.h"
joshdavy 9:96969b1c6bde 22 #include "WinScreen.h"
joshdavy 9:96969b1c6bde 23
joshdavy 1:37802772843e 24
joshdavy 2:b62e8be35a5d 25 ////// Constants //////
joshdavy 3:b34685dbdb8d 26
joshdavy 10:58cf89dd878c 27 // The tick times for both timers
joshdavy 10:58cf89dd878c 28 const int MUSIC_TICK_TIME = 100; //100 microseconds
joshdavy 10:58cf89dd878c 29 const int GAME_TICK_TIME = 100; // 100 millieconds
joshdavy 2:b62e8be35a5d 30
joshdavy 1:37802772843e 31 /////////////// objects ///////////////
joshdavy 1:37802772843e 32 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
joshdavy 1:37802772843e 33 Gamepad pad;
joshdavy 1:37802772843e 34 Game game;
joshdavy 9:96969b1c6bde 35 Music music;
joshdavy 10:58cf89dd878c 36 Timer game_timer;
joshdavy 10:58cf89dd878c 37 Timer music_timer;
joshdavy 2:b62e8be35a5d 38
joshdavy 1:37802772843e 39 ///////////// prototypes ///////////////
joshdavy 1:37802772843e 40 void init();
joshdavy 1:37802772843e 41 void welcome();
joshdavy 9:96969b1c6bde 42 void menu();
joshdavy 9:96969b1c6bde 43 void how_to_play();
joshdavy 9:96969b1c6bde 44 void you_win();
joshdavy 9:96969b1c6bde 45 int main();
joshdavy 10:58cf89dd878c 46
joshdavy 11:db27d3838514 47 /**
joshdavy 11:db27d3838514 48 * @brief initialies all classes and libraries
joshdavy 11:db27d3838514 49 */
joshdavy 1:37802772843e 50 void init()
joshdavy 1:37802772843e 51 {
joshdavy 8:21b6d4dbce44 52 // need to initialise LCD and Gamepad
joshdavy 1:37802772843e 53 lcd.init();
joshdavy 2:b62e8be35a5d 54 lcd.setContrast(0.4);
joshdavy 1:37802772843e 55 pad.init();
joshdavy 9:96969b1c6bde 56 // Initalises Music with data from SoundData.h and game
joshdavy 10:58cf89dd878c 57 music.init(sound_data,NUM_ELEMENTS);
joshdavy 7:68e06dda79f7 58 game.init();
joshdavy 3:b34685dbdb8d 59
joshdavy 1:37802772843e 60 }
joshdavy 11:db27d3838514 61
joshdavy 11:db27d3838514 62 /**
joshdavy 11:db27d3838514 63 * @brief simple splash screen displayed on start-up
joshdavy 11:db27d3838514 64 */
joshdavy 8:21b6d4dbce44 65 void welcome()
joshdavy 8:21b6d4dbce44 66 {
joshdavy 8:21b6d4dbce44 67
joshdavy 9:96969b1c6bde 68 // Draws Splash Screen
joshdavy 8:21b6d4dbce44 69 lcd.drawSprite(0,0,48,84, (int *) splashScreen);
joshdavy 1:37802772843e 70 lcd.refresh();
joshdavy 8:21b6d4dbce44 71
joshdavy 8:21b6d4dbce44 72 // wait flashing LEDs until start button is pressed
joshdavy 9:96969b1c6bde 73 while (!pad.check_event(Gamepad::START_PRESSED)) {
joshdavy 10:58cf89dd878c 74 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 75
joshdavy 9:96969b1c6bde 76 pad.leds_on();
joshdavy 9:96969b1c6bde 77 wait(0.1);
joshdavy 9:96969b1c6bde 78 pad.leds_off();
joshdavy 9:96969b1c6bde 79 wait(0.1);
joshdavy 9:96969b1c6bde 80 }
joshdavy 9:96969b1c6bde 81
joshdavy 9:96969b1c6bde 82 }
joshdavy 11:db27d3838514 83 /**
joshdavy 11:db27d3838514 84 * @brief You Win! Screen
joshdavy 11:db27d3838514 85 */
joshdavy 9:96969b1c6bde 86 void you_win()
joshdavy 9:96969b1c6bde 87 {
joshdavy 9:96969b1c6bde 88 // Draws Win Screen
joshdavy 9:96969b1c6bde 89 lcd.drawSprite(0,0,48,84, (int *) winScreen);
joshdavy 9:96969b1c6bde 90 lcd.refresh();
joshdavy 9:96969b1c6bde 91
joshdavy 10:58cf89dd878c 92 // wait flashing LEDs until START button is pressed
joshdavy 10:58cf89dd878c 93 while (!pad.check_event(Gamepad::START_PRESSED)) {
joshdavy 1:37802772843e 94 pad.leds_on();
joshdavy 1:37802772843e 95 wait(0.1);
joshdavy 1:37802772843e 96 pad.leds_off();
joshdavy 1:37802772843e 97 wait(0.1);
joshdavy 1:37802772843e 98 }
joshdavy 10:58cf89dd878c 99
joshdavy 9:96969b1c6bde 100 main();
joshdavy 2:b62e8be35a5d 101
joshdavy 1:37802772843e 102 }
joshdavy 11:db27d3838514 103 /**
joshdavy 11:db27d3838514 104 * @brief Displays instructions on how to play.
joshdavy 11:db27d3838514 105 */
joshdavy 8:21b6d4dbce44 106 void how_to_play()
joshdavy 8:21b6d4dbce44 107 {
joshdavy 9:96969b1c6bde 108 // Gives instructions on how to play
joshdavy 9:96969b1c6bde 109 lcd.clear();
joshdavy 9:96969b1c6bde 110 lcd.printString("Use the joy -",0,0);
joshdavy 9:96969b1c6bde 111 lcd.printString("stick to move.",0,1);
joshdavy 9:96969b1c6bde 112 lcd.printString("Press A to ",0,2);
joshdavy 9:96969b1c6bde 113 lcd.printString("flip gravity.",0,3);
joshdavy 9:96969b1c6bde 114 lcd.printString("Get the flags",0,4);
joshdavy 9:96969b1c6bde 115 lcd.printString("to win!",0,5);
joshdavy 9:96969b1c6bde 116 lcd.refresh();
joshdavy 10:58cf89dd878c 117
joshdavy 9:96969b1c6bde 118 // Loop until A button pressed
joshdavy 9:96969b1c6bde 119 while ( !pad.check_event(Gamepad::A_PRESSED) ) {}
joshdavy 10:58cf89dd878c 120
joshdavy 9:96969b1c6bde 121 // Return to menu
joshdavy 11:db27d3838514 122 menu();
joshdavy 8:21b6d4dbce44 123 }
joshdavy 11:db27d3838514 124 /**
joshdavy 11:db27d3838514 125 * @brief Displays main menu
joshdavy 11:db27d3838514 126 */
joshdavy 8:21b6d4dbce44 127 void menu()
joshdavy 8:21b6d4dbce44 128 {
joshdavy 9:96969b1c6bde 129 // Main Menu
joshdavy 10:58cf89dd878c 130 int menu_option = 0;
joshdavy 8:21b6d4dbce44 131 while ( !pad.check_event(Gamepad::A_PRESSED) ) {
joshdavy 10:58cf89dd878c 132 //Allow adjusting of the contrast
joshdavy 10:58cf89dd878c 133 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 134
joshdavy 10:58cf89dd878c 135 // Read the joystick value and relate it to a menu option
joshdavy 9:96969b1c6bde 136 if (pad.get_coord().y > 0.7f) {
joshdavy 10:58cf89dd878c 137 menu_option = 0;
joshdavy 8:21b6d4dbce44 138 }
joshdavy 8:21b6d4dbce44 139 if (pad.get_coord().y < -0.7f) {
joshdavy 10:58cf89dd878c 140 menu_option = 1;
joshdavy 8:21b6d4dbce44 141 }
joshdavy 9:96969b1c6bde 142 // Draws main menu background image
joshdavy 8:21b6d4dbce44 143 lcd.drawSprite(0,0,48,84, (int *) menuScreen);
joshdavy 10:58cf89dd878c 144
joshdavy 9:96969b1c6bde 145 // Draws the arrow on the menu
joshdavy 10:58cf89dd878c 146 if (menu_option == 0) {
joshdavy 8:21b6d4dbce44 147 lcd.drawSprite(40,10,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 148 } else {
joshdavy 8:21b6d4dbce44 149 lcd.drawSprite(40,25,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 150 }
joshdavy 10:58cf89dd878c 151
joshdavy 8:21b6d4dbce44 152 lcd.refresh();
joshdavy 8:21b6d4dbce44 153
joshdavy 8:21b6d4dbce44 154 }
joshdavy 9:96969b1c6bde 155 // If the "how to play" option is selected run its display function
joshdavy 10:58cf89dd878c 156 if (menu_option == 1) {
joshdavy 8:21b6d4dbce44 157 how_to_play();
joshdavy 8:21b6d4dbce44 158 }
joshdavy 8:21b6d4dbce44 159 }
joshdavy 8:21b6d4dbce44 160
joshdavy 5:b9cf407bcc63 161
joshdavy 11:db27d3838514 162 /**
joshdavy 11:db27d3838514 163 * @brief Main Loop
joshdavy 11:db27d3838514 164 */
joshdavy 1:37802772843e 165 int main()
joshdavy 1:37802772843e 166 {
joshdavy 8:21b6d4dbce44 167
joshdavy 1:37802772843e 168 init(); // initialise and then display welcome screen...
joshdavy 1:37802772843e 169 welcome(); // waiting for the user to start
joshdavy 8:21b6d4dbce44 170 menu(); // main menu
joshdavy 1:37802772843e 171
joshdavy 10:58cf89dd878c 172 // Resets and starts the timers responsible for the game and music loops
joshdavy 8:21b6d4dbce44 173 game_timer.reset();
joshdavy 8:21b6d4dbce44 174 music_timer.reset();
joshdavy 8:21b6d4dbce44 175 game_timer.start();
joshdavy 8:21b6d4dbce44 176 music_timer.start();
joshdavy 10:58cf89dd878c 177
joshdavy 10:58cf89dd878c 178
joshdavy 9:96969b1c6bde 179 while (true) {
joshdavy 10:58cf89dd878c 180
joshdavy 9:96969b1c6bde 181 // If the game timer is above the game tick time then main game process
joshdavy 10:58cf89dd878c 182 // is ran
joshdavy 8:21b6d4dbce44 183 if (game_timer.read_ms() > GAME_TICK_TIME) {
joshdavy 10:58cf89dd878c 184 // Allows adjusting on the contrast
joshdavy 10:58cf89dd878c 185 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 186 // Main update states
joshdavy 8:21b6d4dbce44 187 game.update(pad);
joshdavy 8:21b6d4dbce44 188 game.draw(lcd);
joshdavy 10:58cf89dd878c 189
joshdavy 10:58cf89dd878c 190 // If the games won goes to the you_win screen.
joshdavy 9:96969b1c6bde 191 if (game.game_won()) {
joshdavy 9:96969b1c6bde 192 you_win();
joshdavy 10:58cf89dd878c 193
joshdavy 9:96969b1c6bde 194 }
joshdavy 10:58cf89dd878c 195 game_timer.reset();
joshdavy 8:21b6d4dbce44 196 }
joshdavy 9:96969b1c6bde 197 // If the music timer is above the music tick time update the music object
joshdavy 8:21b6d4dbce44 198 if (music_timer.read_us() > MUSIC_TICK_TIME) {
joshdavy 10:58cf89dd878c 199 music.play_next();
joshdavy 10:58cf89dd878c 200 music_timer.reset();
joshdavy 8:21b6d4dbce44 201 }
joshdavy 10:58cf89dd878c 202 }
joshdavy 10:58cf89dd878c 203 }