Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Wed May 08 18:51:59 2019 +0000
Revision:
15:d855e8c666e7
Parent:
14:08ac9aaa34c3
Child:
16:c8d68cbd1ae2
Updated aesthetics. Testing 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 5:0b31909caf7f 10
ellisbhastroud 0:c6ceddb241df 11 #include "mbed.h"
ellisbhastroud 1:6179c2d67d19 12 #include "Gamepad.h"
ellisbhastroud 1:6179c2d67d19 13 #include "N5110.h"
ellisbhastroud 13:681192091568 14 #include "SDFileSystem.h"
ellisbhastroud 1:6179c2d67d19 15 #include "Menu.h"
ellisbhastroud 5:0b31909caf7f 16 #include "GolfEngine.h"
ellisbhastroud 0:c6ceddb241df 17
ellisbhastroud 1:6179c2d67d19 18 // objects
ellisbhastroud 1:6179c2d67d19 19 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
ellisbhastroud 13:681192091568 20 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
ellisbhastroud 1:6179c2d67d19 21 Gamepad pad;
ellisbhastroud 2:81cfa8310f55 22 Menu menu;
ellisbhastroud 5:0b31909caf7f 23 GolfEngine golf;
ellisbhastroud 12:7f7fadb5c106 24 Ticker ticker_frame;
ellisbhastroud 9:bc34f2243e43 25
ellisbhastroud 13:681192091568 26 //prototypes
ellisbhastroud 14:08ac9aaa34c3 27 void frame_isr();
ellisbhastroud 2:81cfa8310f55 28 void init();
ellisbhastroud 14:08ac9aaa34c3 29
ellisbhastroud 12:7f7fadb5c106 30
ellisbhastroud 13:681192091568 31 //global variables
ellisbhastroud 14:08ac9aaa34c3 32 volatile int g_frame_flag = 0; //flag set in isr to control running frame rate of game
ellisbhastroud 0:c6ceddb241df 33
ellisbhastroud 13:681192091568 34 //functions
ellisbhastroud 0:c6ceddb241df 35 int main()
ellisbhastroud 0:c6ceddb241df 36 {
ellisbhastroud 14:08ac9aaa34c3 37 init(); //Initialises pad, lcd and menu objects. Sets up ticker
ellisbhastroud 14:08ac9aaa34c3 38 menu.welcome_loop(pad, lcd); //Prints welcome screen and loops until start pressed
ellisbhastroud 14:08ac9aaa34c3 39 while(1) {
ellisbhastroud 14:08ac9aaa34c3 40 //Menu Loop - Naviagates series of loops for menu screens
ellisbhastroud 14:08ac9aaa34c3 41 if(menu.get_start_game_flag() == false) { //if start flag false stay in menu loop
ellisbhastroud 14:08ac9aaa34c3 42 menu.menu_loop(pad, lcd); //main menu screen - loops until user chooses next screen
ellisbhastroud 14:08ac9aaa34c3 43 switch (menu.get_screen()) { //uses user choice to enter next loop in menu
ellisbhastroud 14:08ac9aaa34c3 44 case START:
ellisbhastroud 15:d855e8c666e7 45 menu.start_loop(pad, lcd); //loops until A pressed then game begins
ellisbhastroud 14:08ac9aaa34c3 46 break;
ellisbhastroud 14:08ac9aaa34c3 47 case HIGHSCORES:
ellisbhastroud 14:08ac9aaa34c3 48 menu.highscores_loop(pad, lcd, sd); //loops until back pressed then returns to main menu screen
ellisbhastroud 14:08ac9aaa34c3 49 break;
ellisbhastroud 14:08ac9aaa34c3 50 case SETTINGS:
ellisbhastroud 14:08ac9aaa34c3 51 menu.settings_loop(pad, lcd); //loops until back pressed then returns to main menu screen
ellisbhastroud 14:08ac9aaa34c3 52 break;
ellisbhastroud 14:08ac9aaa34c3 53 } //when user exits sub-menu loop check game_start_flag - if false then return to main menu loop
ellisbhastroud 14:08ac9aaa34c3 54 } else { //Game Loop - If start flag = true
ellisbhastroud 14:08ac9aaa34c3 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 14:08ac9aaa34c3 58 lcd.clear();
ellisbhastroud 14:08ac9aaa34c3 59 golf.printLevel(lcd); //Indicating start of game
ellisbhastroud 14:08ac9aaa34c3 60 lcd.refresh();
ellisbhastroud 14:08ac9aaa34c3 61 wait(3.0f);
ellisbhastroud 14:08ac9aaa34c3 62 golf.reset_game_over_flag();
ellisbhastroud 14:08ac9aaa34c3 63 while(golf.get_game_over_flag() == false){ //Main Game Loop
ellisbhastroud 9:bc34f2243e43 64
ellisbhastroud 15:d855e8c666e7 65 if(g_frame_flag) { //If ticker calls isr and game not over
ellisbhastroud 14:08ac9aaa34c3 66
ellisbhastroud 14:08ac9aaa34c3 67 g_frame_flag = 0; //reset flag
ellisbhastroud 14:08ac9aaa34c3 68 lcd.clear(); //clears lcd buffer
ellisbhastroud 15:d855e8c666e7 69 golf.read_input(pad); //reads input from gamepad
ellisbhastroud 15:d855e8c666e7 70 golf.update_ball(pad, lcd); //moves ball and checks for bounces/shots/holes/game over
ellisbhastroud 14:08ac9aaa34c3 71 golf.drawGame(lcd); //draws ball, walls etc. - draws game from previous frame
ellisbhastroud 14:08ac9aaa34c3 72 golf.check_end_level(lcd, pad, sd); //checks if level comleted and if game over
ellisbhastroud 14:08ac9aaa34c3 73 lcd.refresh(); //updates lcd display
ellisbhastroud 14:08ac9aaa34c3 74 sleep(); //sleeps mcu until ticker wakes it up
ellisbhastroud 14:08ac9aaa34c3 75 }
ellisbhastroud 12:7f7fadb5c106 76 }
ellisbhastroud 9:bc34f2243e43 77 }
ellisbhastroud 2:81cfa8310f55 78 }
ellisbhastroud 1:6179c2d67d19 79 }
ellisbhastroud 1:6179c2d67d19 80
ellisbhastroud 12:7f7fadb5c106 81 void frame_isr() //called by ticker at rate of frame rate
ellisbhastroud 1:6179c2d67d19 82 {
ellisbhastroud 12:7f7fadb5c106 83 g_frame_flag = 1; // set flag in ISR
ellisbhastroud 12:7f7fadb5c106 84 }
ellisbhastroud 12:7f7fadb5c106 85
ellisbhastroud 12:7f7fadb5c106 86 void init() //initialises lcd and pad peripherals
ellisbhastroud 12:7f7fadb5c106 87 {
ellisbhastroud 1:6179c2d67d19 88 lcd.init();
ellisbhastroud 1:6179c2d67d19 89 pad.init();
ellisbhastroud 4:035448357749 90 menu.init();
ellisbhastroud 14:08ac9aaa34c3 91 ticker_frame.attach(&frame_isr,1.0f/menu.get_frame_rate()); //sets up ticker used to control running speed/frame rate
ellisbhastroud 15:d855e8c666e7 92 lcd.setContrast(0.55f); //can alter this in settings
ellisbhastroud 15:d855e8c666e7 93 lcd.setBrightness(1.0f); //can alter this in settings
ellisbhastroud 15:d855e8c666e7 94
ellisbhastroud 14:08ac9aaa34c3 95 }