el15mh 200929957

Dependencies:   mbed

main.cpp

Committer:
el15mh
Date:
2017-03-30
Revision:
0:d7701c5c20e6
Child:
1:8ce2586b5965

File content as of revision 0:d7701c5c20e6:

#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"

// CREATE OBJECTS //
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;

// FUNCTION PROTOTYPES //
void init();
void play();
void menu();
void options();
void lcdSettings();
void soundSettings();
void drawBox();

int main()
{
    init(); // initialise devices
    
    while(1){
        
        menu();
        
    }
}

void init()
{
    lcd.init();
    pad.init();
}

void play()
{
    int exit = 0;
    
    while(exit == 0){
        
        // coordinates for centre of lcd
        float x = 41.0;
        float y = 23.0;
        
        lcd.clear();
        
        double size = pad.read_pot() * 10.0;
        
        if (pad.check_event(Gamepad::A_PRESSED)){
            pad.leds_on();
        }
        if (pad.check_event(Gamepad::B_PRESSED)){
            pad.leds_off();
        }
        
        // check four sides of ball (+x, -x, +y, -y)
        float leftSide = x - size;
        float rightSide = x + size;
        float topSide = y - size;
        float bottomSide = y + size;
        
        // default values for bool values are true
        bool MOVE_X = true;
        bool MOVE_Y = true;
        
        if (leftSide == 1) {
            MOVE_X = false; // only set bool values false if circle on the edge
        }
        
        if (rightSide == 82) {
            MOVE_X = false;
        }
        
        if (topSide == 1) {
            MOVE_Y = false;
        }
        
        if (bottomSide == 46){
            MOVE_Y = false;
        }
        
        drawBox();
        lcd.drawCircle(x, y, size, FILL_TRANSPARENT);
        lcd.refresh();
        
        Vector2D position = pad.get_mapped_coord();
        
        if (MOVE_X == true){
            x += position.x;
        }
        if (MOVE_Y == true){
            y -= position.y;
        }
        
        // printf("x = %f, y = %f \n", position.x, position.y);
        // printf("ball X = %f, ball Y = %f \n\n", x, y);
        
        wait_ms(20);
        
        if (pad.check_event(Gamepad::BACK_PRESSED)){
            
            exit = 1;
        }
    }
}

void drawBox()
{
    for (int i = 0; i < WIDTH; i++){
        lcd.setPixel(i, 0);
        lcd.setPixel(i, HEIGHT - 1);
    }
    
    for (int j = 0; j < HEIGHT; j++){
        lcd.setPixel(0, j);
        lcd.setPixel(WIDTH - 1, j);
    }
    
    lcd.refresh();
}


void menu()
{
    int selected = 0;
    
    while(1) {
        
        // lcd.printString("Testing", 0, 2);
        
        switch (selected) {
                
            case 1:
                
                lcd.clear();
                lcd.printString(">Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                // either clicking joystick or pressing A selects function
                if (pad.check_event(Gamepad::A_PRESSED) ||
                    pad.check_event(Gamepad::JOY_PRESSED)){
                    
                    play(); // call the game function
                }
                
                wait_ms(250);    // 1s propogation delay
                
                break;
                
            case 2:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(">Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                
                if (pad.check_event(Gamepad::A_PRESSED) ||
                    pad.check_event(Gamepad::JOY_PRESSED)){
                    
                    options();
                }
                
                wait_ms(250);    // 1s propogation delay
                
                break;
                
            case 3:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(">LCD settings", 0, 2);
                lcd.printString(" Sound", 0, 3);
                
                lcd.refresh();
                
                
                if (pad.check_event(Gamepad::A_PRESSED) ||
                    pad.check_event(Gamepad::JOY_PRESSED)){
                    
                    lcdSettings();
                }
                
                wait_ms(250);    // 1s propogation delay
                
                break;
                
            case 4:
                
                lcd.clear();
                lcd.printString(" Play game", 0, 0);
                lcd.printString(" Game options", 0, 1);
                lcd.printString(" LCD settings", 0, 2);
                lcd.printString(">Sound", 0, 3);
                
                lcd.refresh();
                
                
                if (pad.check_event(Gamepad::A_PRESSED) ||
                    pad.check_event(Gamepad::JOY_PRESSED)){
                    
                    soundSettings();
                }
                
                wait_ms(250);    // 1s propogation delay
                
                break;
                
            default:
                
                selected = 0;
                
                break;
        }
        
        // menuSelector(selector);
        
        char d = pad.get_direction();
        
        if ((d == NW) ||
            (d == N)  ||
            (d == NE)){
            
            selected -= 1;
        }
        
        if ((d == SW) ||
            (d == S)  ||
            (d == SE)){
            
            selected += 1;
        }
        
        // printf("Joystick position = %c \n", d);
        // printf("Selected = %i \n", selected);
        
    }
}

void options()
{
    int selected = 0;
    int exit = 0;
    
    while(exit == 0){
        
        int back = 0;
        
        switch (selected) {
                
            case 1:
                
                lcd.clear();
                // displays options page with indicator on first
                lcd.printString("Game Options:", 0, 0);
                lcd.printString(">Difficulty", 0, 2);
                lcd.printString(" Ball Colour", 0, 3);
                lcd.refresh();
                
                while (back == 0){
                    
                    // if first option selected
                    if (pad.check_event(Gamepad::A_PRESSED) ||
                        pad.check_event(Gamepad::JOY_PRESSED)){
                        
                        switch(selected){
                                
                            case 1:
                                
                                lcd.clear();
                                lcd.printString("Difficulty: ", 0, 0);
                                lcd.printString(">Easy", 0, 2);
                                lcd.printString(" Hard", 0, 3);
                                lcd.refresh();
                                
                                if (pad.check_event(Gamepad::A_PRESSED) ||
                                    pad.check_event(Gamepad::JOY_PRESSED)){
                                    
                                    // maze.mazeIndex = 0;
                                }
                                
                                wait_ms(250);
                                
                                break;
                                
                            case 2:
                                
                                lcd.clear();
                                lcd.printString("Difficulty: ", 0, 0);
                                lcd.printString(" Easy", 0, 2);
                                lcd.printString(">Hard", 0, 3);
                                lcd.refresh();
                                
                                if (pad.check_event(Gamepad::A_PRESSED) ||
                                    pad.check_event(Gamepad::JOY_PRESSED)){
                                    
                                    // maze.mazeIndex = 1;
                                    lcd.printString("Hard selected", 0, 1);
                                    lcd.refresh();
                                }
                                
                                wait_ms(250);
                                
                                break;
                                
                            default:
                                
                                selected = 0;
                                
                                break;
                                
                        }
                    }
                    
                    if (pad.check_event(Gamepad::BACK_PRESSED)){
                        
                        back = 1;
                    }
                }
                
                wait(1);    // 1s propogation delay
                
            case 2:
                
                lcd.clear();
                // displays options page with indicator on second
                lcd.printString("Game Options:", 0, 0);
                lcd.printString(" Difficulty", 0, 2);
                lcd.printString(">Ball Colour", 0, 3);
                lcd.refresh();
                
                while (back == 0){
                    
                    // if second option selected
                    if (pad.check_event(Gamepad::A_PRESSED) ||
                        pad.check_event(Gamepad::JOY_PRESSED)){
                        
                        switch (selected){
                                
                            case 1:
                                
                                lcd.clear();
                                lcd.printString("Ball colour: ", 0, 0);
                                lcd.printString(">Transparent", 0, 2);
                                lcd.printString(" Solid", 0, 3);
                                lcd.refresh();
                                
                                if (pad.check_event(Gamepad::A_PRESSED) ||
                                    pad.check_event(Gamepad::JOY_PRESSED)){
                                    
                                    // ball.ballColour = 0;
                                    lcd.clear();
                                    lcd.printString("Transparent", 0, 1);
                                    lcd.refresh();
                                    
                                    wait(2);
                                }
                                
                                break;
                                
                            case 2:
                                
                                lcd.printString("Ball colour: ", 0, 0);
                                lcd.printString(" Transparent", 0, 2);
                                lcd.printString(">Solid", 0, 3);
                                lcd.refresh();
                                
                                if (pad.check_event(Gamepad::A_PRESSED) ||
                                    pad.check_event(Gamepad::JOY_PRESSED)){
                                    
                                    // ball.ballColour = 1;
                                    lcd.clear();
                                    lcd.printString("Solid", 0, 1);
                                    lcd.refresh();
                                    
                                    wait(2);
                                }
                                
                                break;
                                
                            default:
                                
                                selected = 0;
                                break;
                        }
                        
                        if (pad.check_event(Gamepad::BACK_PRESSED)){
                            
                            back = 1;
                        }
                    }
                    
                    wait(1);    // 1s propogation delay
                    
                }
                
                if (pad.check_event(Gamepad::BACK_PRESSED)){
                    
                    exit = 1;
                }
        }
    }
}


void soundSettings()
{
    pad.tone(750.0,0.1);
    lcd.clear();
    lcd.printString("Sound settings", 0, 2);
    lcd.refresh();
    
    wait(2);
}

void lcdSettings()
{
    int exit = 0;
    
    while(exit == 0){
        
        lcd.clear();
        lcd.printString("LCD Settings", 0, 2);
        lcd.refresh();
        
        if(pad.check_event(Gamepad::BACK_PRESSED)){
            
            exit = 1;
        }
    }
}