ELEC2645 (2015/16) / Mbed 2 deprecated el14jpps_2645_Ping_Pong_Project

Dependencies:   N5110 Ping_Pong beep mbed Servo

main.cpp

Committer:
el14jpps
Date:
2017-04-15
Revision:
9:80898ce9d4d5
Parent:
6:70830f1ed62e

File content as of revision 9:80898ce9d4d5:

/**
@file main.cpp
@brief On this code the main has been used as at the pricipal code for the menu and the funtions needed for the selection are brought from their original folders. Pong and Game of life.
 Revision 1.0.
@author Jefferson Sanchez
@date  April 2016
*/

#include "N5110.h"      // @see Author Dr Evans 
#include "mbed.h"      // @see Arm 
#include "beep.h"     //  @seeAuthor Peter Drescher
#include "Game_of_life.h"//Jefferson Sanchez 
#include "menu.h"       //Jefferson Sanchez 
/**@code Joystick by Dr Evans*/
/**@code LCD by Dr Evans*/
/**@code Interrupsts by Dr Evans*/
//This changes the tolerance of joystick direction
#define DIRECTION_TOLERANCE 0.010L

// LEDs connections
DigitalOut red_led(LED_RED);
DigitalOut green_led(LED_GREEN);
DigitalOut blue_led(LED_BLUE);

//Buttons
DigitalIn button(PTB11);
InterruptIn switch2(SW3);
//Joystick
AnalogIn xPot(PTB2);
AnalogIn yPot(PTB3);
DigitalOut yellow(PTA1);
DigitalOut green(PTC2);
//LCD
N5110 display(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
// Timer to regularly read the joystick.
Ticker pollJoystick;
// Serial used For debugging Inputs and Outputs.
Serial serial(USBTX,USBRX);

volatile int g_switch2_flag = 0;
void switch2_isr();

// Used for to show the direction of the joystick.
enum DirectionName {
    UP,
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};

// Struct for Joystick
typedef struct JoyStick Joystick;
struct JoyStick {
    float x;    // current x value
    float x0;   // 'centred' x value
    float y;    // current y value
    float y0;   // 'centred' y value
    int button;

    int switch2; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
    DirectionName direction;  // current direction
};

/// Tone for the push button

Joystick joystick;

// function prototypes
int printFlag = 0;
int Option_seclected = 1;
int star = 1;
void light();
void light()
{

    display.setBrightness(0);
    wait(0.3);
    display.setBrightness(1);
    wait(0.5);
    display.setBrightness(0);
    wait(0.3);
    display.setBrightness(1);
    wait(0.5);
    display.setBrightness(0);
    wait(0.3);
    display.setBrightness(1);
    wait(0.5);
    display.setBrightness(0);
    wait(0.3);
    display.setBrightness(1);


}
int main()
{
    menu_intro();
    calibrateJoystick();                         // get centred values of joystick
    pollJoystick.attach(&updateJoystick,0.23);  // read joystick 10 times per second
    switch2.fall(&switch2_isr);
    switch2.mode(PullUp);
    while(1) {
        if(joystick.direction == DOWN) {
            star=0;
            serial.printf("up\n");
            /** the following leds will light up to give a visual*/
            blue_led =1;
            red_led= 1;
            green_led = 0;

        } else if (joystick.direction == CENTRE) {
            serial.printf(" middle\n");
            star=1;
            blue_led=0;
            red_led =1;
            green_led=1;

        } else if (joystick.direction == UP) {
            star=2;
            red_led= 0;
            blue_led =1;
            green_led = 1;


        }
        switch(Option_seclected) {
            case 0:
                display.clear();
                display.printString("=-- MENU --=",6,1);
                display.printString("PING PONG[o]",5,2);
                display.printString("G of Life",5,3);
                display.printString("Credits",5,4);
                
                serial.printf("ping pong \n");
                /**
                *As shown bellow I have used a interrupt for the g_switch2_flag to show that the selection can be done using two diferent methods.
                This funtion is controlled by the sw2 switch.
                */
                if (g_switch2_flag) {
                    g_switch2_flag = 0;
                    pongGame();
                   
                }
                /**
                 * As described above the button funtion is controlled by PTB11 Joy stick button
                  */
                if(button==1) { // if it is a external switch then it needs to be a 1
                    serial.printf("button == 1  ping pong has been selected\n ");
                    //tone1();
                    yellow =1;
                    light();
                     buzzer.beep(500,0.5);
                    pongGame();
                }

                switch (star) {
                    case 0 :
                        Option_seclected = 2;

                        break;
                    case 1:
                        Option_seclected =0;

                        break;
                    case 2:
                        Option_seclected =1;

                        break;
                }
                break;
            case 1:
                display.clear();

                display.printString("-=- MENU -=-",6,1);
                display.printString("PING PONG",5,2);
                display.printString("G of Life[.]",5,3);
                display.printString("Credits ",5,4);
            //    buzzer.beep(500,0.5);
                serial.printf("g of life\n");

                if(button==1) {// if it is a external switch then it needs to be a 1

                    serial.printf("button ==1  game of life has been selected \n");
                   tone2();
                    green=1;
                   light();
                   
                    gameOfLife();

                }
                switch (star) {
                    case 0 :
                        Option_seclected = 0;

                        break;
                    case 1:
                        Option_seclected =1;

                        break;
                    case 2:
                        Option_seclected =2;

                        break;
                }
                break;
            case 2:
                display.clear();
                display.printString("--= MENU =--",6,1);
                display.printString("PING PONG ",5,2);
                display.printString("G of Life ",5,3);
                display.printString("Credits  [*]",5,4);
               // buzzer.beep(500,0.5);
                serial.printf("credits \n");

                if (button==1) {
                    light();
                    display.clear();
                    display.printString("Thank you ",2,1);
                    buzzer.beep(1000,0.5);
                    wait(1);
                    display.printString("   for ",2,2);
                    buzzer.beep(1000,0.5);
                    wait(1);
                    display.printString("  playing  ",2,3);
                    buzzer.beep(1000,0.5);
                    wait(1);
                    display.printString("see you later ",2,4);
                    buzzer.beep(1000,0.5);

                    wait(1);
                }
                switch (star) {
                    case 0 :
                        Option_seclected = 1;
                        break;
                    case 1:
                        Option_seclected =2;
                        break;
                    case 2:
                        Option_seclected =0;

                        break;
                }
                break;

        }
        sleep();
    }
}
void calibrateJoystick()
{
    button.mode(PullDown);


    joystick.y0 = yPot;
    joystick.x0 = xPot;
}
void updateJoystick()
{
    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)

    joystick.y = yPot - joystick.y0;
    joystick.x = xPot - joystick.x0;
    // read button state
    joystick.button = button ;

    // calculate direction depending on x,y values
    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
    if ( fabs(joystick.y)  < DIRECTION_TOLERANCE) {
        joystick.direction = CENTRE;
    } else if ( joystick.y >  DIRECTION_TOLERANCE) {
        joystick.direction = UP;
    } else if ( joystick.y <  DIRECTION_TOLERANCE) {
        joystick.direction = DOWN;
    } else if ( joystick.x >  DIRECTION_TOLERANCE) {
        joystick.direction = RIGHT;
    } else if ( joystick.x < DIRECTION_TOLERANCE) {
        joystick.direction = LEFT;
    } else {
        joystick.direction = UNKNOWN;
    }
    printFlag = 1;
}
void switch2_isr()
{
    g_switch2_flag = 1;
}