Adam Baker 201166301

Dependencies:   mbed Gamepad N5110

Committer:
adambakerwa
Date:
Thu May 09 10:51:42 2019 +0000
Revision:
44:03059810630d
Parent:
43:11c5d098ce9d
Child:
45:e5b69581d7a1
Final Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adambakerwa 43:11c5d098ce9d 1 /*
adambakerwa 43:11c5d098ce9d 2 ELEC2645 Embedded Systems Project
adambakerwa 43:11c5d098ce9d 3 School of Electronic & Electrical Engineering University of Leeds
adambakerwa 43:11c5d098ce9d 4 Name: Adam P. Baker
adambakerwa 43:11c5d098ce9d 5 Username: el17apb
adambakerwa 43:11c5d098ce9d 6 Student ID Number: 201166301
adambakerwa 43:11c5d098ce9d 7 Date: 8 May 2019
adambakerwa 43:11c5d098ce9d 8 */
adambakerwa 43:11c5d098ce9d 9
adambakerwa 43:11c5d098ce9d 10
adambakerwa 41:4b20f909bbcb 11 ///////// pre-processor directives ////////
adambakerwa 1:a3cce487dffe 12 #include "mbed.h"
adambakerwa 1:a3cce487dffe 13 #include "Gamepad.h"
adambakerwa 1:a3cce487dffe 14 #include "N5110.h"
adambakerwa 25:230e4163cb6b 15 #include "BlockheadEngine.h"
adambakerwa 36:6f452777b9ce 16 #include "Menu.h"
adambakerwa 1:a3cce487dffe 17
adambakerwa 41:4b20f909bbcb 18 /////////////// objects ///////////////
adambakerwa 1:a3cce487dffe 19 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
adambakerwa 1:a3cce487dffe 20 Gamepad pad;
adambakerwa 25:230e4163cb6b 21 BlockheadEngine blockhead;
adambakerwa 13:35a459a33bbd 22 Ticker ticker;
adambakerwa 36:6f452777b9ce 23 Menu menu;
adambakerwa 13:35a459a33bbd 24
adambakerwa 41:4b20f909bbcb 25 ///////////// prototypes ///////////////
adambakerwa 13:35a459a33bbd 26 void timer_isr();
adambakerwa 31:bf765e3961f1 27 void init();
adambakerwa 37:23b6312c8315 28 void intro(N5110 &lcd, Gamepad &pad);
adambakerwa 26:3319ddd5111d 29 void main_menu(N5110 &lcd, Gamepad &pad);
adambakerwa 36:6f452777b9ce 30 int select_menu_item(Gamepad &pad, N5110 &lcd);
adambakerwa 36:6f452777b9ce 31 void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input);
adambakerwa 26:3319ddd5111d 32 void menu_playgame(N5110 &lcd, Gamepad &pad);
adambakerwa 36:6f452777b9ce 33 void menu_highscore(N5110 &lcd, Gamepad &pad);
adambakerwa 36:6f452777b9ce 34 void menu_settings(N5110 &lcd, Gamepad &pad);
adambakerwa 36:6f452777b9ce 35 void menu_quit(N5110 &lcd, Gamepad &pad);
adambakerwa 31:bf765e3961f1 36 void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input);
adambakerwa 31:bf765e3961f1 37 void menu_continue(N5110 &lcd, Gamepad &pad);
adambakerwa 31:bf765e3961f1 38
adambakerwa 41:4b20f909bbcb 39 volatile int timer_flag = 0; //sets timer_flag to 0
adambakerwa 31:bf765e3961f1 40
adambakerwa 41:4b20f909bbcb 41
adambakerwa 41:4b20f909bbcb 42 ///////////// functions ////////////////
adambakerwa 13:35a459a33bbd 43 int main()
adambakerwa 13:35a459a33bbd 44 {
adambakerwa 13:35a459a33bbd 45
adambakerwa 41:4b20f909bbcb 46 int fps = 6; //sets fps to 6
adambakerwa 31:bf765e3961f1 47 init();
adambakerwa 41:4b20f909bbcb 48 ticker.attach(&timer_isr,1.0f/fps); //creates a ticker and attaches it to timer_isr (ticks 6 * per second)
adambakerwa 36:6f452777b9ce 49
adambakerwa 41:4b20f909bbcb 50 lcd.setContrast(0.55); //intialy sets contrast to 0.55
adambakerwa 24:f5dd1f3db693 51
adambakerwa 41:4b20f909bbcb 52 intro(lcd, pad); //runs intro sequence
adambakerwa 37:23b6312c8315 53
adambakerwa 44:03059810630d 54 main_menu(lcd, pad); //runs main menu ( finite state machine, states also include "play game" "highscore" "settings" and "quit..." )
adambakerwa 41:4b20f909bbcb 55
adambakerwa 13:35a459a33bbd 56 }
adambakerwa 13:35a459a33bbd 57
adambakerwa 37:23b6312c8315 58
adambakerwa 41:4b20f909bbcb 59 //sets the timer_flag to 1
adambakerwa 41:4b20f909bbcb 60 void timer_isr()
adambakerwa 13:35a459a33bbd 61 {
adambakerwa 13:35a459a33bbd 62
adambakerwa 41:4b20f909bbcb 63 timer_flag = 1; // set flag in ISR
adambakerwa 1:a3cce487dffe 64 }
adambakerwa 1:a3cce487dffe 65
adambakerwa 41:4b20f909bbcb 66
adambakerwa 41:4b20f909bbcb 67 //intialises variables of below classes
adambakerwa 41:4b20f909bbcb 68 void init()
adambakerwa 31:bf765e3961f1 69 {
adambakerwa 41:4b20f909bbcb 70 pad.init(); //intialise Gamepad class variables
adambakerwa 41:4b20f909bbcb 71 lcd.init(); //intialise N5110 class variables
adambakerwa 41:4b20f909bbcb 72 menu.init(); //intialise menu variables
adambakerwa 31:bf765e3961f1 73 }
adambakerwa 31:bf765e3961f1 74
adambakerwa 41:4b20f909bbcb 75
adambakerwa 41:4b20f909bbcb 76 //runs the intro sequence
adambakerwa 41:4b20f909bbcb 77 void intro(N5110 &lcd, Gamepad &pad)
adambakerwa 37:23b6312c8315 78 {
adambakerwa 41:4b20f909bbcb 79 menu.title_intro(lcd, pad); //runs 'BLOCK HEAD' intro sequence
adambakerwa 37:23b6312c8315 80 int start = 0;
adambakerwa 37:23b6312c8315 81
adambakerwa 37:23b6312c8315 82 do {
adambakerwa 41:4b20f909bbcb 83 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 37:23b6312c8315 84
adambakerwa 37:23b6312c8315 85 timer_flag = 0;
adambakerwa 37:23b6312c8315 86
adambakerwa 41:4b20f909bbcb 87 start = menu.press_start(lcd, pad); //flashes start untill start button is pressed
adambakerwa 37:23b6312c8315 88
adambakerwa 37:23b6312c8315 89 } else {
adambakerwa 37:23b6312c8315 90
adambakerwa 41:4b20f909bbcb 91 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 37:23b6312c8315 92
adambakerwa 37:23b6312c8315 93 }
adambakerwa 37:23b6312c8315 94
adambakerwa 37:23b6312c8315 95
adambakerwa 37:23b6312c8315 96 } while (start == 0);
adambakerwa 37:23b6312c8315 97
adambakerwa 41:4b20f909bbcb 98 menu.init(); //intilises menu variables
adambakerwa 26:3319ddd5111d 99 }
adambakerwa 26:3319ddd5111d 100
adambakerwa 26:3319ddd5111d 101
adambakerwa 41:4b20f909bbcb 102 //main menu state
adambakerwa 41:4b20f909bbcb 103 void main_menu(N5110 &lcd, Gamepad &pad)
adambakerwa 41:4b20f909bbcb 104 {
adambakerwa 41:4b20f909bbcb 105
adambakerwa 41:4b20f909bbcb 106 blockhead.init(); //intialise blockhead engine variables for new game
adambakerwa 41:4b20f909bbcb 107 int input = select_menu_item(pad, lcd); //reads input and prints the main menu
adambakerwa 41:4b20f909bbcb 108 go_to_main_menu_item(lcd, pad, input); //peforms what ever input is chosen
adambakerwa 41:4b20f909bbcb 109 }
adambakerwa 36:6f452777b9ce 110
adambakerwa 26:3319ddd5111d 111
adambakerwa 41:4b20f909bbcb 112 //reads input and prints main menu
adambakerwa 41:4b20f909bbcb 113 int select_menu_item(Gamepad &pad, N5110 &lcd)
adambakerwa 36:6f452777b9ce 114 {
adambakerwa 37:23b6312c8315 115 int input = 0;
adambakerwa 37:23b6312c8315 116 do {
adambakerwa 40:f53c70793975 117
adambakerwa 41:4b20f909bbcb 118 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 37:23b6312c8315 119
adambakerwa 40:f53c70793975 120 timer_flag = 0;
adambakerwa 40:f53c70793975 121
adambakerwa 41:4b20f909bbcb 122 input = menu.select_input_main(pad, lcd); //runs until input is selected
adambakerwa 40:f53c70793975 123
adambakerwa 37:23b6312c8315 124 } else {
adambakerwa 40:f53c70793975 125
adambakerwa 41:4b20f909bbcb 126 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 41:4b20f909bbcb 127
adambakerwa 40:f53c70793975 128 }
adambakerwa 41:4b20f909bbcb 129 } while (input == 0); //repeats untill an input is chosen
adambakerwa 26:3319ddd5111d 130
adambakerwa 25:230e4163cb6b 131 return input;
adambakerwa 25:230e4163cb6b 132 }
adambakerwa 25:230e4163cb6b 133
adambakerwa 41:4b20f909bbcb 134
adambakerwa 41:4b20f909bbcb 135 //depending on input, goes to following menu items
adambakerwa 41:4b20f909bbcb 136 void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input)
adambakerwa 26:3319ddd5111d 137 {
adambakerwa 41:4b20f909bbcb 138 switch (input) {
adambakerwa 26:3319ddd5111d 139 case 1:
adambakerwa 26:3319ddd5111d 140 menu_playgame(lcd, pad);
adambakerwa 26:3319ddd5111d 141 break;
adambakerwa 26:3319ddd5111d 142 case 2:
adambakerwa 36:6f452777b9ce 143 menu_highscore(lcd, pad);
adambakerwa 26:3319ddd5111d 144 break;
adambakerwa 36:6f452777b9ce 145 case 3:
adambakerwa 36:6f452777b9ce 146 menu_settings(lcd, pad);
adambakerwa 36:6f452777b9ce 147 case 4:
adambakerwa 36:6f452777b9ce 148 menu_quit(lcd, pad);
adambakerwa 26:3319ddd5111d 149 default:
adambakerwa 26:3319ddd5111d 150 exit(1);
adambakerwa 26:3319ddd5111d 151 break;
adambakerwa 26:3319ddd5111d 152 }
adambakerwa 25:230e4163cb6b 153 }
adambakerwa 25:230e4163cb6b 154
adambakerwa 41:4b20f909bbcb 155
adambakerwa 41:4b20f909bbcb 156 //runs playgame menu state
adambakerwa 41:4b20f909bbcb 157 void menu_playgame(N5110 &lcd, Gamepad &pad)
adambakerwa 26:3319ddd5111d 158 {
adambakerwa 26:3319ddd5111d 159 int gameover = 0;
adambakerwa 31:bf765e3961f1 160
adambakerwa 26:3319ddd5111d 161 do {
adambakerwa 41:4b20f909bbcb 162 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 26:3319ddd5111d 163
adambakerwa 41:4b20f909bbcb 164 timer_flag = 0; //if it has, clear the flag
adambakerwa 26:3319ddd5111d 165
adambakerwa 41:4b20f909bbcb 166 gameover = blockhead.playgame(lcd, pad); //run game untill gameover
adambakerwa 26:3319ddd5111d 167
adambakerwa 26:3319ddd5111d 168 } else {
adambakerwa 26:3319ddd5111d 169
adambakerwa 41:4b20f909bbcb 170 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 26:3319ddd5111d 171
adambakerwa 26:3319ddd5111d 172 }
adambakerwa 26:3319ddd5111d 173 } while (gameover == 0);
adambakerwa 26:3319ddd5111d 174
adambakerwa 41:4b20f909bbcb 175 menu_continue(lcd, pad); //once gameover occurs, go to continue menu
adambakerwa 26:3319ddd5111d 176
adambakerwa 26:3319ddd5111d 177 }
adambakerwa 26:3319ddd5111d 178
adambakerwa 41:4b20f909bbcb 179
adambakerwa 41:4b20f909bbcb 180 //runs highscore menu state
adambakerwa 41:4b20f909bbcb 181 void menu_highscore(N5110 &lcd, Gamepad &pad)
adambakerwa 25:230e4163cb6b 182 {
adambakerwa 25:230e4163cb6b 183
adambakerwa 26:3319ddd5111d 184 int goback = 0;
adambakerwa 36:6f452777b9ce 185
adambakerwa 26:3319ddd5111d 186 do {
adambakerwa 25:230e4163cb6b 187
adambakerwa 41:4b20f909bbcb 188 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 26:3319ddd5111d 189
adambakerwa 41:4b20f909bbcb 190 timer_flag = 0; //if it has, clear the flag
adambakerwa 26:3319ddd5111d 191
adambakerwa 41:4b20f909bbcb 192 int highscore = blockhead.highscore(); //gets high score from blockhead engine
adambakerwa 36:6f452777b9ce 193
adambakerwa 41:4b20f909bbcb 194 menu.print_highscore(lcd, highscore); //prints high score menu, with high score
adambakerwa 26:3319ddd5111d 195
adambakerwa 41:4b20f909bbcb 196 if (pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)) { //B or Back button to go back to main menu
adambakerwa 26:3319ddd5111d 197 goback = 1;
adambakerwa 26:3319ddd5111d 198 }
adambakerwa 26:3319ddd5111d 199
adambakerwa 26:3319ddd5111d 200 } else {
adambakerwa 26:3319ddd5111d 201
adambakerwa 41:4b20f909bbcb 202 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 26:3319ddd5111d 203
adambakerwa 26:3319ddd5111d 204 }
adambakerwa 26:3319ddd5111d 205 } while (goback == 0);
adambakerwa 37:23b6312c8315 206
adambakerwa 41:4b20f909bbcb 207 main_menu(lcd, pad); //once b or back pressed, go to main menu
adambakerwa 36:6f452777b9ce 208 }
adambakerwa 26:3319ddd5111d 209
adambakerwa 41:4b20f909bbcb 210
adambakerwa 41:4b20f909bbcb 211 //runs settings menu state
adambakerwa 41:4b20f909bbcb 212 void menu_settings(N5110 &lcd, Gamepad &pad)
adambakerwa 36:6f452777b9ce 213 {
adambakerwa 36:6f452777b9ce 214
adambakerwa 36:6f452777b9ce 215 int goback = 0;
adambakerwa 40:f53c70793975 216 float contrast;
adambakerwa 36:6f452777b9ce 217
adambakerwa 36:6f452777b9ce 218 do {
adambakerwa 36:6f452777b9ce 219
adambakerwa 41:4b20f909bbcb 220 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 36:6f452777b9ce 221
adambakerwa 41:4b20f909bbcb 222 timer_flag = 0; //if it has, clear the flag
adambakerwa 36:6f452777b9ce 223
adambakerwa 41:4b20f909bbcb 224 contrast = menu.print_settings(pad, lcd); //run comtast menu and return contast
adambakerwa 41:4b20f909bbcb 225 lcd.setContrast(contrast); //update lcd contast
adambakerwa 40:f53c70793975 226
adambakerwa 42:0dad7c359fa5 227 if (pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)) {
adambakerwa 36:6f452777b9ce 228 goback = 1;
adambakerwa 36:6f452777b9ce 229 }
adambakerwa 36:6f452777b9ce 230
adambakerwa 36:6f452777b9ce 231 } else {
adambakerwa 36:6f452777b9ce 232
adambakerwa 41:4b20f909bbcb 233 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 36:6f452777b9ce 234
adambakerwa 36:6f452777b9ce 235 }
adambakerwa 36:6f452777b9ce 236 } while (goback == 0);
adambakerwa 41:4b20f909bbcb 237 main_menu(lcd, pad); //go to main menu once b or back is pressed
adambakerwa 26:3319ddd5111d 238 }
adambakerwa 25:230e4163cb6b 239
adambakerwa 36:6f452777b9ce 240 void menu_quit(N5110 &lcd, Gamepad &pad)
adambakerwa 36:6f452777b9ce 241 {
adambakerwa 36:6f452777b9ce 242 lcd.turnOff();
adambakerwa 36:6f452777b9ce 243 sleep();
adambakerwa 37:23b6312c8315 244
adambakerwa 36:6f452777b9ce 245 }
adambakerwa 36:6f452777b9ce 246
adambakerwa 25:230e4163cb6b 247
adambakerwa 41:4b20f909bbcb 248 //depending on input, goes to following continue menu items
adambakerwa 41:4b20f909bbcb 249 void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input)
adambakerwa 31:bf765e3961f1 250 {
adambakerwa 31:bf765e3961f1 251 switch (input) {
adambakerwa 31:bf765e3961f1 252 case 1:
adambakerwa 41:4b20f909bbcb 253 menu_playgame(lcd, pad); //if play game, level will be the one you were just on
adambakerwa 31:bf765e3961f1 254 break;
adambakerwa 31:bf765e3961f1 255 case 2:
adambakerwa 41:4b20f909bbcb 256 main_menu(lcd, pad); //if menu, game resets
adambakerwa 31:bf765e3961f1 257 break;
adambakerwa 31:bf765e3961f1 258 default:
adambakerwa 31:bf765e3961f1 259 exit(1);
adambakerwa 31:bf765e3961f1 260 break;
adambakerwa 31:bf765e3961f1 261 }
adambakerwa 31:bf765e3961f1 262 }
adambakerwa 31:bf765e3961f1 263
adambakerwa 41:4b20f909bbcb 264
adambakerwa 41:4b20f909bbcb 265 //runs continue game state
adambakerwa 41:4b20f909bbcb 266 void menu_continue(N5110 &lcd, Gamepad &pad)
adambakerwa 31:bf765e3961f1 267 {
adambakerwa 44:03059810630d 268 blockhead.continue_init(); //intialised continue game variables (level does not change)
adambakerwa 40:f53c70793975 269 menu.init();
adambakerwa 37:23b6312c8315 270
adambakerwa 36:6f452777b9ce 271 int input = 0;
adambakerwa 37:23b6312c8315 272 do {
adambakerwa 37:23b6312c8315 273
adambakerwa 41:4b20f909bbcb 274 if (timer_flag == true) { //only run when timer flag is true (6fps)
adambakerwa 41:4b20f909bbcb 275
adambakerwa 41:4b20f909bbcb 276 timer_flag = 0; //if it has, clear the flag
adambakerwa 40:f53c70793975 277
adambakerwa 41:4b20f909bbcb 278 input = menu.select_input_continue(pad, lcd); //prints continue menu, and selects coninue menu item
adambakerwa 37:23b6312c8315 279
adambakerwa 41:4b20f909bbcb 280 } else {
adambakerwa 41:4b20f909bbcb 281
adambakerwa 41:4b20f909bbcb 282 sleep(); //sleep when timer_flag not true in order to conserve energy
adambakerwa 40:f53c70793975 283
adambakerwa 37:23b6312c8315 284 }
adambakerwa 40:f53c70793975 285 } while (input == 0);
adambakerwa 41:4b20f909bbcb 286 select_continue_menu_item(lcd, pad, input); //go to what ever item has been selected
adambakerwa 31:bf765e3961f1 287 }
adambakerwa 31:bf765e3961f1 288