A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

main.cpp

Committer:
el13drt
Date:
2015-04-16
Revision:
12:eedda6554615
Parent:
11:6b8416a8ddb3
Child:
13:a1b3a373c5a4

File content as of revision 12:eedda6554615:

/**
@file main.cpp
@brief Program implementation
*/

#include "mbed.h"
#include "N5110.h"
#include "beep.h"
#include "tower.h"

#include <ctime>
#include <cstdlib>

// change this to alter tolerance of joystick direction
#define DIRECTION_TOLERANCE 0.05



// create buzzer objecct
Beep buzzer(p21);

// create local file system
//LocalFileSytem local("local");

// navigation/action buttons
DigitalIn buttonA(p19);
DigitalIn buttonB(p20);

// LED indicators
AnalogOut ledR(p18);// RED LED
DigitalOut ledY(p24);// YELLOW LED

// connections for joystick
DigitalIn button(p17);
AnalogIn xPot(p15);
AnalogIn yPot(p16);

// timer to regularly read the joystick
Ticker pollJoystick;

// serial for debug
Serial serial(USBTX,USBRX);

// create enumerated type (0,1,2,3 etc. for direction)
// could be extended for diagonals etc.
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; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
    DirectionName direction;  // current direction
};

// create struct variable
Joystick joystick;

int printFlag = 0;

// function prototypes
void calibrateJoystick();
void updateJoystick();


int main()
{
    ledR = 1;//power LED on

    srand (time(NULL));//initial seed for randomisation

    // initial random x co-ordinates
    // for falling hazards
    // (values between 3 and 76)
    randX1 = rand() % 74 + 5;
    randX2 = rand() % 74 + 5;
    randX3 = rand() % 74 + 5;
    randX4 = rand() % 74 + 5;
    randX5 = rand() % 74 + 5;
    randX6 = rand() % 74 + 5;

    calibrateJoystick();//get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);//read joystick 10 times per second

    lcd.init();//initialise screen
    welcome();//welcome screen
    lcd.clear();//clear pixels

    int exitFlag = 0;//exit flag
    int mainOption = 0;//counter for main menu
    int exitOption = 0;//counter for exit menu
    int option = 0;//counter for options menu
    int subOption = 0;//counter for sub options menu

    while(1) {
        drawMainMenu();//draws main menu
        mainMenu(mainOption);//presents main menu options

        // if 'Play Game' selected
        if ((mainOption == 0)&&(buttonA == 1)) {
            game(exitFlag, exitOption);//actual game
        }
        // if 'Scores' selected
        if((mainOption == 1)&&(buttonA == 1)) {
            scores();
        }

        // if 'option' selected
        if((mainOption == 2)&&(buttonA == 1)) {
            actionButton();
            backButton();
            drawOptionsMenu();//draws options menu

            while(1) {
                actionButton();
                backButton();
                optionsMenu(option);//presents options

////////////////////// difficulty menu ////////////////////////////////////
                if ((option == 0)&&(buttonA == 1)) {
                    actionButton();
                    backButton();
                    drawDifficultyMenu();//draws difficulty menu

                    while(1) {
                        actionButton();
                        backButton();
                        difficultyMenu(subOption);//presents difficulty options

                        if(buttonB == 1) {
                            lcd.clear();
                            break;
                        }
                    }
                }

                // back to menu
                if(buttonB == 1) {
                    lcd.clear();
                    break;
                }
            }
        }
    }
}



// read default positions of the joystick to calibrate later readings
void calibrateJoystick()
{
    button.mode(PullDown);
    // must not move during calibration
    joystick.x0 = xPot; //initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
    joystick.y0 = yPot;
}

void updateJoystick()
{
    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
    joystick.x = xPot - joystick.x0;
    joystick.y = yPot - joystick.y0;
    // 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 && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = CENTRE;
    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = UP;
    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = DOWN;
    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = RIGHT;
    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = LEFT;
    } else {
        joystick.direction = UNKNOWN;
    }

    // set flag for printing
    printFlag = 1;
}