Ellis Blackford Stroud 201155309

Dependencies:   mbed FATFileSystem

main.cpp

Committer:
ellisbhastroud
Date:
2019-05-03
Revision:
12:7f7fadb5c106
Parent:
10:9f54a6366e94
Child:
13:681192091568

File content as of revision 12:7f7fadb5c106:

/*
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 "Menu.h"
#include "GolfEngine.h"

// objects 
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Menu menu;
GolfEngine golf;
Ticker ticker_frame;

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

int frame_rate; //frame rate got from menu class - can be changed in settings
//flag set in isr 
volatile int g_frame_flag = 0;

int main()
{
    init();
    lcd.clear();
    menu.print_welcome(lcd); 
    lcd.clear();
    bool start_game = false; 
    
    while(start_game == false) {   //menu loop navigates menu until game started
        menu.menu_screen(pad, lcd); 
        start_game = menu.menu_change(lcd, pad); //changes menu screen to selected screen the returns true when start game 
    }  
    
    lcd.clear();
    golf.printLevel(lcd); 
    lcd.refresh();
    wait(1);
    frame_rate = menu.get_frame_rate(); //changes frame rate to the value chosen in settings
    golf.init(frame_rate); //resets variables for game and sets frame rate
    ticker_frame.attach(&frame_isr,1.0f/frame_rate); //calls isr code 

    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(); //moves ball to new level position and increments level
                golf.reset_hole_flag(); //so that the game loop can continue
                golf.printLevel(lcd); //prints level number 
                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);
}