ELEC2645 (2015/16) / Mbed 2 deprecated 2645_ProjectCode

Dependencies:   N5110 mbed

main.cpp

Committer:
el15s3p
Date:
2016-04-27
Revision:
1:9d69901e18d0
Parent:
0:d3a050703801
Child:
2:9e791f33c49f

File content as of revision 1:9d69901e18d0:

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

#define DIRECTION_TOLERANCE 0.05

//         VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);

// connections for joystick
DigitalIn button(PTB11);
AnalogIn xPot(PTB2);
AnalogIn yPot(PTB3);

AnalogIn Pot1(PTB10);

BusOut leds(LED1,LED2);
DigitalOut led1(LED1);
DigitalOut led2(LED2);


InterruptIn sw2 (SW2);
DigitalIn buttonA(PTB18);
DigitalIn buttonB(PTB19);
PwmOut buzzer(PTA2);

// timer to regularly read the joystick
Ticker pollJoystick;

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 menuSelect = 2;

int main()
{
    for (; 2 > menuSelect < 5;) {
        calibrateJoystick();  // get centred values of joystick
        pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
        lcd.init();
        lcd.clear();
        lcd.normalMode();
        lcd.printString("Comet Crusher!",0,0);
        lcd.printString("Start",10,2);
        lcd.printString("Instructions",10,3);
        lcd.printString("Difficulty",10,4);
        lcd.printString("Scores",10,5);
        lcd.printString(">",5,menuSelect);
        lcd.refresh();

        if (menuSelect == 2) {
            if (joystick.direction == DOWN) {
                menuSelect = 3;
                sleep();
            } else if (joystick.direction == UP) {
                menuSelect = 5;
                sleep();
            }
            lcd.refresh();
            sleep();
        }
        if (menuSelect == 3) {
            if (joystick.direction == DOWN) {
                menuSelect = 4;
                sleep();
            } else if (joystick.direction == UP) {
                menuSelect = 2;
                sleep();
            }
            lcd.refresh();
            sleep();
        }
        if (menuSelect == 4) {
            if (joystick.direction == DOWN) {
                menuSelect = 5; 
                sleep();
            } else if (joystick.direction == UP) {
                menuSelect = 3;
                sleep();
            }
            lcd.refresh();
            sleep();
        }
        if (menuSelect == 5) {
            if (joystick.direction == DOWN) {
                menuSelect = 2;
                sleep();
            } else if (joystick.direction == UP) {
                menuSelect = 4;
                sleep();
            }
            lcd.refresh();
            sleep();
        }
        sleep();
    }
}
void game()
{
}

void difficultySet()
{
}

void highScore()
{
}

void instructions()
{
}

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 = DOWN;
    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
        joystick.direction = UP;
    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = LEFT;
    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
        joystick.direction = RIGHT;
    } else {
        joystick.direction = UNKNOWN;
    }

    // set flag for printing
    printFlag = 1;
}