Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Thu May 09 09:58:51 2019 +0000
Revision:
16:c8d68cbd1ae2
Parent:
15:d855e8c666e7
Child:
17:98127ac75195
Penultimate Commit - Tidied a little and checking documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellisbhastroud 0:c6ceddb241df 1 /*
ellisbhastroud 0:c6ceddb241df 2 ELEC2645 Embedded Systems Project
ellisbhastroud 0:c6ceddb241df 3 School of Electronic & Electrical Engineering
ellisbhastroud 0:c6ceddb241df 4 University of Leeds
ellisbhastroud 0:c6ceddb241df 5 Name: Ellis Blackford Stroud
ellisbhastroud 0:c6ceddb241df 6 Username: el17ebs
ellisbhastroud 0:c6ceddb241df 7 Student ID Number: 201155309
ellisbhastroud 1:6179c2d67d19 8 Date: 09/05/19
ellisbhastroud 0:c6ceddb241df 9 */
ellisbhastroud 0:c6ceddb241df 10 #include "mbed.h"
ellisbhastroud 1:6179c2d67d19 11 #include "Gamepad.h"
ellisbhastroud 1:6179c2d67d19 12 #include "N5110.h"
ellisbhastroud 13:681192091568 13 #include "SDFileSystem.h"
ellisbhastroud 1:6179c2d67d19 14 #include "Menu.h"
ellisbhastroud 5:0b31909caf7f 15 #include "GolfEngine.h"
ellisbhastroud 0:c6ceddb241df 16
ellisbhastroud 1:6179c2d67d19 17 // objects
ellisbhastroud 1:6179c2d67d19 18 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ellisbhastroud 13:681192091568 19 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
ellisbhastroud 1:6179c2d67d19 20 Gamepad pad;
ellisbhastroud 2:81cfa8310f55 21 Menu menu;
ellisbhastroud 5:0b31909caf7f 22 GolfEngine golf;
ellisbhastroud 12:7f7fadb5c106 23 Ticker ticker_frame;
ellisbhastroud 9:bc34f2243e43 24
ellisbhastroud 13:681192091568 25 //prototypes
ellisbhastroud 14:08ac9aaa34c3 26 void frame_isr();
ellisbhastroud 2:81cfa8310f55 27 void init();
ellisbhastroud 14:08ac9aaa34c3 28
ellisbhastroud 12:7f7fadb5c106 29
ellisbhastroud 13:681192091568 30 //global variables
ellisbhastroud 14:08ac9aaa34c3 31 volatile int g_frame_flag = 0; //flag set in isr to control running frame rate of game
ellisbhastroud 0:c6ceddb241df 32
ellisbhastroud 13:681192091568 33 //functions
ellisbhastroud 0:c6ceddb241df 34 int main()
ellisbhastroud 0:c6ceddb241df 35 {
ellisbhastroud 14:08ac9aaa34c3 36 init(); //Initialises pad, lcd and menu objects. Sets up ticker
ellisbhastroud 14:08ac9aaa34c3 37 menu.welcome_loop(pad, lcd); //Prints welcome screen and loops until start pressed
ellisbhastroud 14:08ac9aaa34c3 38 while(1) {
ellisbhastroud 14:08ac9aaa34c3 39 //Menu Loop - Naviagates series of loops for menu screens
ellisbhastroud 14:08ac9aaa34c3 40 if(menu.get_start_game_flag() == false) { //if start flag false stay in menu loop
ellisbhastroud 16:c8d68cbd1ae2 41 menu.menu_loop(pad, lcd); //main menu screen - loops until user chooses next screen
ellisbhastroud 14:08ac9aaa34c3 42 switch (menu.get_screen()) { //uses user choice to enter next loop in menu
ellisbhastroud 14:08ac9aaa34c3 43 case START:
ellisbhastroud 15:d855e8c666e7 44 menu.start_loop(pad, lcd); //loops until A pressed then game begins
ellisbhastroud 14:08ac9aaa34c3 45 break;
ellisbhastroud 14:08ac9aaa34c3 46 case HIGHSCORES:
ellisbhastroud 14:08ac9aaa34c3 47 menu.highscores_loop(pad, lcd, sd); //loops until back pressed then returns to main menu screen
ellisbhastroud 14:08ac9aaa34c3 48 break;
ellisbhastroud 14:08ac9aaa34c3 49 case SETTINGS:
ellisbhastroud 14:08ac9aaa34c3 50 menu.settings_loop(pad, lcd); //loops until back pressed then returns to main menu screen
ellisbhastroud 14:08ac9aaa34c3 51 break;
ellisbhastroud 14:08ac9aaa34c3 52 } //when user exits sub-menu loop check game_start_flag - if false then return to main menu loop
ellisbhastroud 14:08ac9aaa34c3 53 } else { //Game Loop - If start flag = true
ellisbhastroud 14:08ac9aaa34c3 54
ellisbhastroud 16:c8d68cbd1ae2 55
ellisbhastroud 14:08ac9aaa34c3 56 menu.reset_start_game_flag(); //resets start flag to return to menu after course complete
ellisbhastroud 14:08ac9aaa34c3 57 golf.init(menu.get_frame_rate()); //initialises golf objects, sets variables for game and sets frame rate
ellisbhastroud 16:c8d68cbd1ae2 58 ticker_frame.attach(&frame_isr,1.0f/menu.get_frame_rate()); //sets up ticker used to control running speed/frame rate
ellisbhastroud 14:08ac9aaa34c3 59 lcd.clear();
ellisbhastroud 14:08ac9aaa34c3 60 golf.printLevel(lcd); //Indicating start of game
ellisbhastroud 14:08ac9aaa34c3 61 lcd.refresh();
ellisbhastroud 14:08ac9aaa34c3 62 wait(3.0f);
ellisbhastroud 14:08ac9aaa34c3 63 golf.reset_game_over_flag();
ellisbhastroud 16:c8d68cbd1ae2 64 /* UNCOMMENT FOR TESTING
ellisbhastroud 16:c8d68cbd1ae2 65 printf("Game Initialised\n",_level-1);
ellisbhastroud 16:c8d68cbd1ae2 66 print("Frame Rate = %i Hz\n",menu.get_frame_rate()));
ellisbhastroud 16:c8d68cbd1ae2 67 print("Begin Level 1 \n",menu.get_frame_rate()));
ellisbhastroud 16:c8d68cbd1ae2 68 */
ellisbhastroud 16:c8d68cbd1ae2 69 while(golf.get_game_over_flag() == false){ //Main Game Loop - When game is over returns to start at menu screen
ellisbhastroud 9:bc34f2243e43 70
ellisbhastroud 15:d855e8c666e7 71 if(g_frame_flag) { //If ticker calls isr and game not over
ellisbhastroud 14:08ac9aaa34c3 72
ellisbhastroud 14:08ac9aaa34c3 73 g_frame_flag = 0; //reset flag
ellisbhastroud 14:08ac9aaa34c3 74 lcd.clear(); //clears lcd buffer
ellisbhastroud 15:d855e8c666e7 75 golf.read_input(pad); //reads input from gamepad
ellisbhastroud 15:d855e8c666e7 76 golf.update_ball(pad, lcd); //moves ball and checks for bounces/shots/holes/game over
ellisbhastroud 14:08ac9aaa34c3 77 golf.drawGame(lcd); //draws ball, walls etc. - draws game from previous frame
ellisbhastroud 14:08ac9aaa34c3 78 golf.check_end_level(lcd, pad, sd); //checks if level comleted and if game over
ellisbhastroud 14:08ac9aaa34c3 79 lcd.refresh(); //updates lcd display
ellisbhastroud 14:08ac9aaa34c3 80 sleep(); //sleeps mcu until ticker wakes it up
ellisbhastroud 14:08ac9aaa34c3 81 }
ellisbhastroud 12:7f7fadb5c106 82 }
ellisbhastroud 9:bc34f2243e43 83 }
ellisbhastroud 2:81cfa8310f55 84 }
ellisbhastroud 1:6179c2d67d19 85 }
ellisbhastroud 1:6179c2d67d19 86
ellisbhastroud 12:7f7fadb5c106 87 void frame_isr() //called by ticker at rate of frame rate
ellisbhastroud 1:6179c2d67d19 88 {
ellisbhastroud 12:7f7fadb5c106 89 g_frame_flag = 1; // set flag in ISR
ellisbhastroud 12:7f7fadb5c106 90 }
ellisbhastroud 12:7f7fadb5c106 91
ellisbhastroud 12:7f7fadb5c106 92 void init() //initialises lcd and pad peripherals
ellisbhastroud 12:7f7fadb5c106 93 {
ellisbhastroud 1:6179c2d67d19 94 lcd.init();
ellisbhastroud 1:6179c2d67d19 95 pad.init();
ellisbhastroud 4:035448357749 96 menu.init();
ellisbhastroud 15:d855e8c666e7 97 lcd.setContrast(0.55f); //can alter this in settings
ellisbhastroud 15:d855e8c666e7 98 lcd.setBrightness(1.0f); //can alter this in settings
ellisbhastroud 15:d855e8c666e7 99
ellisbhastroud 14:08ac9aaa34c3 100 }