Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

main.cpp

Committer:
ellisbhastroud
Date:
2019-05-06
Revision:
13:681192091568
Parent:
12:7f7fadb5c106
Child:
14:08ac9aaa34c3

File content as of revision 13:681192091568:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Ellis Blackford Stroud
Username: el17ebs
Student ID Number: 201155309
Date: 09/05/19
*/

#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "SDFileSystem.h"
#include "Menu.h"
#include "GolfEngine.h"

// objects 
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
Gamepad pad;
Menu menu;
GolfEngine golf;
Ticker ticker_frame;

//prototypes
void init();
void inverseFlash();
void frame_isr();

//global variables
volatile int g_frame_flag = 0; //flag set in isr to control running frequency/frame rate of game

//functions 
int main()
{
    init(); //initialises pad, lcd and menu objects
    lcd.clear();
    menu.print_welcome(lcd);  //Prints welcome message
    lcd.clear();
    bool start_game = false; 
    
    while(start_game == false) {   //navigates menu via a series of loops until user chooses to start game
    
        start_game = menu.menu_loop(pad, lcd, sd); //series of loop functions used to navigate the menu
    }  
    
    lcd.clear();
    golf.printLevel(lcd);  //Indicating start of game
    lcd.refresh();
    wait(1);
    
    int frame_rate = menu.get_frame_rate(); //initialises frame rate variable and sets to frame rate chosen in settings
    golf.init(frame_rate); //initialises golf objects, sets variables for game and sets frame rate
    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 

    while(1){ 

        //Game Loop - 
        if(g_frame_flag) { //Causes game loop code to run at frame rate 
        
            g_frame_flag = 0; //reset flag
        
            if(golf.get_hole_flag() == false) { //if ball not in hole run game loop
            
                lcd.clear();
                golf.read_input(pad); //reads input from gamepad 
                golf.update_ball(pad); //moves ball and checks for bounces/shots/holes
                golf.drawGame(lcd, pad); //draws ball, walls etc.
                lcd.refresh(); //updates lc display
                sleep(); //sleeps mcu until ticker wakes it up
            
            } else if(golf.get_hole_flag() == true) { //if ball in hole then end level 
            
                inverseFlash(); //flashes screen pixels on and off to indicate end of level
                inverseFlash();
                lcd.clear();
                golf.new_level(lcd, sd); //increments level count, prints level for user, checks if all levels complete
                golf.reset_hole_flag(); //so that the game loop can continue
                lcd.refresh();
                wait(1);
            }   
        }
    }
}

void frame_isr() //called by ticker at rate of frame rate 
{
    g_frame_flag = 1;   // set flag in ISR
}

void init() //initialises lcd and pad peripherals
{ 
    lcd.init();
    pad.init();
    menu.init();
    lcd.setContrast(0.5f); //can alter this in settings
}

void inverseFlash() // at end of level to indicate ball in hole
{ 
    lcd.inverseMode();
    lcd.refresh();
    wait(0.25);
    lcd.normalMode();
    lcd.refresh();
    wait(0.25);
    lcd.inverseMode();
    lcd.refresh();
    wait(0.25);
    lcd.normalMode();
    lcd.refresh();
    wait(0.25);
}