Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Mon May 06 10:00:57 2019 +0000
Revision:
13:681192091568
Parent:
12:7f7fadb5c106
Child:
14:08ac9aaa34c3
Highscores now fully implemented. Scores written to file at end of game. Highscores section in menu now reads, sorts and displays scores stored in SD card file.

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 2:81cfa8310f55 27 void init();
ellisbhastroud 9:bc34f2243e43 28 void inverseFlash();
ellisbhastroud 12:7f7fadb5c106 29 void frame_isr();
ellisbhastroud 12:7f7fadb5c106 30
ellisbhastroud 13:681192091568 31 //global variables
ellisbhastroud 13:681192091568 32 volatile int g_frame_flag = 0; //flag set in isr to control running frequency/frame rate of game
ellisbhastroud 0:c6ceddb241df 33
ellisbhastroud 13:681192091568 34 //functions
ellisbhastroud 0:c6ceddb241df 35 int main()
ellisbhastroud 0:c6ceddb241df 36 {
ellisbhastroud 13:681192091568 37 init(); //initialises pad, lcd and menu objects
ellisbhastroud 2:81cfa8310f55 38 lcd.clear();
ellisbhastroud 13:681192091568 39 menu.print_welcome(lcd); //Prints welcome message
ellisbhastroud 5:0b31909caf7f 40 lcd.clear();
ellisbhastroud 3:a8960004d261 41 bool start_game = false;
ellisbhastroud 3:a8960004d261 42
ellisbhastroud 13:681192091568 43 while(start_game == false) { //navigates menu via a series of loops until user chooses to start game
ellisbhastroud 13:681192091568 44
ellisbhastroud 13:681192091568 45 start_game = menu.menu_loop(pad, lcd, sd); //series of loop functions used to navigate the menu
ellisbhastroud 3:a8960004d261 46 }
ellisbhastroud 12:7f7fadb5c106 47
ellisbhastroud 9:bc34f2243e43 48 lcd.clear();
ellisbhastroud 13:681192091568 49 golf.printLevel(lcd); //Indicating start of game
ellisbhastroud 9:bc34f2243e43 50 lcd.refresh();
ellisbhastroud 9:bc34f2243e43 51 wait(1);
ellisbhastroud 13:681192091568 52
ellisbhastroud 13:681192091568 53 int frame_rate = menu.get_frame_rate(); //initialises frame rate variable and sets to frame rate chosen in settings
ellisbhastroud 13:681192091568 54 golf.init(frame_rate); //initialises golf objects, sets variables for game and sets frame rate
ellisbhastroud 13:681192091568 55 ticker_frame.attach(&frame_isr,1.0f/frame_rate); //sets up ticker to call fram_isr with interval of the time period of the fps
ellisbhastroud 10:9f54a6366e94 56
ellisbhastroud 3:a8960004d261 57 while(1){
ellisbhastroud 12:7f7fadb5c106 58
ellisbhastroud 12:7f7fadb5c106 59 //Game Loop -
ellisbhastroud 12:7f7fadb5c106 60 if(g_frame_flag) { //Causes game loop code to run at frame rate
ellisbhastroud 9:bc34f2243e43 61
ellisbhastroud 12:7f7fadb5c106 62 g_frame_flag = 0; //reset flag
ellisbhastroud 12:7f7fadb5c106 63
ellisbhastroud 12:7f7fadb5c106 64 if(golf.get_hole_flag() == false) { //if ball not in hole run game loop
ellisbhastroud 9:bc34f2243e43 65
ellisbhastroud 12:7f7fadb5c106 66 lcd.clear();
ellisbhastroud 12:7f7fadb5c106 67 golf.read_input(pad); //reads input from gamepad
ellisbhastroud 12:7f7fadb5c106 68 golf.update_ball(pad); //moves ball and checks for bounces/shots/holes
ellisbhastroud 12:7f7fadb5c106 69 golf.drawGame(lcd, pad); //draws ball, walls etc.
ellisbhastroud 12:7f7fadb5c106 70 lcd.refresh(); //updates lc display
ellisbhastroud 12:7f7fadb5c106 71 sleep(); //sleeps mcu until ticker wakes it up
ellisbhastroud 9:bc34f2243e43 72
ellisbhastroud 12:7f7fadb5c106 73 } else if(golf.get_hole_flag() == true) { //if ball in hole then end level
ellisbhastroud 9:bc34f2243e43 74
ellisbhastroud 12:7f7fadb5c106 75 inverseFlash(); //flashes screen pixels on and off to indicate end of level
ellisbhastroud 12:7f7fadb5c106 76 inverseFlash();
ellisbhastroud 12:7f7fadb5c106 77 lcd.clear();
ellisbhastroud 13:681192091568 78 golf.new_level(lcd, sd); //increments level count, prints level for user, checks if all levels complete
ellisbhastroud 12:7f7fadb5c106 79 golf.reset_hole_flag(); //so that the game loop can continue
ellisbhastroud 12:7f7fadb5c106 80 lcd.refresh();
ellisbhastroud 12:7f7fadb5c106 81 wait(1);
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 12:7f7fadb5c106 97 lcd.setContrast(0.5f); //can alter this in settings
ellisbhastroud 1:6179c2d67d19 98 }
ellisbhastroud 9:bc34f2243e43 99
ellisbhastroud 12:7f7fadb5c106 100 void inverseFlash() // at end of level to indicate ball in hole
ellisbhastroud 12:7f7fadb5c106 101 {
ellisbhastroud 9:bc34f2243e43 102 lcd.inverseMode();
ellisbhastroud 9:bc34f2243e43 103 lcd.refresh();
ellisbhastroud 9:bc34f2243e43 104 wait(0.25);
ellisbhastroud 9:bc34f2243e43 105 lcd.normalMode();
ellisbhastroud 9:bc34f2243e43 106 lcd.refresh();
ellisbhastroud 9:bc34f2243e43 107 wait(0.25);
ellisbhastroud 9:bc34f2243e43 108 lcd.inverseMode();
ellisbhastroud 9:bc34f2243e43 109 lcd.refresh();
ellisbhastroud 9:bc34f2243e43 110 wait(0.25);
ellisbhastroud 9:bc34f2243e43 111 lcd.normalMode();
ellisbhastroud 9:bc34f2243e43 112 lcd.refresh();
ellisbhastroud 9:bc34f2243e43 113 wait(0.25);
ellisbhastroud 9:bc34f2243e43 114 }