My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

main.cpp

Committer:
Nikollao
Date:
2016-03-07
Revision:
0:2d0f77943105
Child:
1:12803d645d32

File content as of revision 0:2d0f77943105:

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

N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
Serial pc(USBTX,USBRX);

#define DIRECTION_TOLERANCE 0.05

DigitalIn button(PTB2);
AnalogIn xPot(PTB3);
AnalogIn yPot(PTB10);

Ticker ticker;
Ticker game_timer;

DigitalOut r_led(LED_RED);
DigitalOut g_led(LED_GREEN);
DigitalOut b_led(LED_BLUE);
// K64F on-board switches
InterruptIn sw2(SW2);
InterruptIn sw3(SW3);
PwmOut speaker(PTC11);

enum DirectionName {
    UP,
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};

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;

// error function hangs flashing an LED

volatile int rectY;
volatile int rectX ;
volatile int circleX;
volatile int circleY;
volatile int heroX;
volatile int heroY;

volatile int level = 0;
volatile int count = 0;
volatile int reset = 0;
volatile int g_timer_flag = 0;
volatile int g_game_timer_flag = 0;
volatile int g_sw2_flag = 0;

void error();
// setup serial port
void init_serial();
// set-up the on-board LEDs and switches
void init_K64F();
void init_game();
void timer_isr();
void game_timer_isr();
void sw2_isr();
void calibrateJoystick();
void updateJoystick();

void enemyRect();
void enemyCircle();
void hero();
void guidance();
void obstacles();
int menu();

int main()
{


    lcd.init();
    init_K64F();
    init_serial();
    sw2.fall(&sw2_isr);
    init_game();
    calibrateJoystick();

    ticker.attach(&timer_isr,0.1);
    reset = level;

    while (1) {

        if (g_timer_flag) {

            g_timer_flag = 0;
            lcd.clear();
            guidance();
            hero();
            if (level == 0) {

                enemyRect();
                enemyCircle();
            } else if (level == 1) {

                enemyRect();
                enemyCircle();
                enemyRect();
            } else if (level == 2) {

            } else if (level == 3) {

            }
            obstacles();
            count++;
            //heroX++;
            heroY--;

            if (heroY < -45) {
                heroY = 0;
                level++;
            }

            if (reset < level) {

                reset = level;
                count = 0;
                rectX = rand() % 84;
                rectY = 0;
                circleX = 0;
                circleY = rand() % 47;
            }

            pc.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);

            // check joystick direction
            if (joystick.direction == UP)
                pc.printf(" UP\n");
            if (joystick.direction == DOWN)
                pc.printf(" DOWN\n");
            if (joystick.direction == LEFT)
                pc.printf(" LEFT\n");
            if (joystick.direction == RIGHT)
                pc.printf(" RIGHT\n");
            if (joystick.direction == CENTRE)
                pc.printf(" CENTRE\n");
            if (joystick.direction == UNKNOWN)
                pc.printf(" Unsupported direction\n");
        }
        updateJoystick();
        lcd.refresh();
        sleep();
    }
}

void init_serial()
{
    // set to highest baud - ensure terminal software matches
    pc.baud(115200);
}

void init_K64F()
{
    // on-board LEDs are active-low, so set pin high to turn them off.
    r_led = 1;
    g_led = 1;
    b_led = 1;

    // since the on-board switches have external pull-ups, we should disable the internal pull-down
    // resistors that are enabled by default using InterruptIn
    sw2.mode(PullNone);
    sw3.mode(PullNone);
}

void timer_isr ()
{

    g_timer_flag = 1;
    // lcd.refresh();
}

void game_timer_isr()
{

    g_game_timer_flag = 1;
}

void sw2_isr()
{

    g_sw2_flag = 1;
}

void enemyRect()
{

    lcd.drawRect(rectX,rectY,5,4,1);
    rectX = rectX + rand()%4 - 2;
    //rectY = rand()%4-2;
    //rectX++;
    rectY++;
}

void hero()
{

    lcd.drawLine(40,47+heroY,48,43+heroY,1);
    lcd.drawLine(40,43+heroY,48,47+heroY,1);
    lcd.drawLine(44,45+heroY,44,41+heroY,1);
    lcd.drawCircle(44,39+heroY,2,0);
}

void enemyCircle()
{
    if (count > 10) {
        lcd.drawCircle(circleX,circleY,4,1);
        circleY = circleY + rand() %4 - 2;

        circleX++;
    }
}

void init_game()
{

    srand(time(NULL));
    rectY = 0;
    rectX = rand() %84 + 1;
    circleY = rand() %48 + 1;
    circleX = 0;
    lcd.setBrightness(0.5); // put LED backlight on 50%

    lcd.printString("Welcome to",11,1);
    lcd.printString("Treasure Hunt!",1,3);
    lcd.refresh();
    wait(1);
    lcd.clear();

    while (1) {

        if (g_game_timer_flag) {

            g_game_timer_flag = 0;
            updateJoystick();
            lcd.clear();
            int option = menu();

            if (option == 0) {

                lcd.printString("Start Game <",0,0);
                lcd.printString("Settings",0,2);
                lcd.printString("Exit",0,4);
            } else if (option == 1) {

                lcd.printString("Start Game",0,0);
                lcd.printString("Settings <",0,2);
                lcd.printString("Exit",0,4);
            } else {

                lcd.printString("Start Game",0,0);
                lcd.printString("Settings",0,2);
                lcd.printString("Exit <",0,4);
            }
        }
        if (g_sw2_flag) {
            break;
        }

        sleep();
    }
}
void guidance()
{

    if (level < 7) {

        lcd.drawLine(42,0,42,4,1);
        lcd.drawLine(42,0,40,2,1);
        lcd.drawLine(42,0,44,2,1);
    } else if (level == 7) {

        lcd.drawLine(42,0,42,6,1);
        lcd.drawLine(40,3,44,3,1);
        lcd.drawLine(40,0,44,6,1);
        lcd.drawLine(40,6,44,0,1);
    } else if (level == 8) {

        ticker.detach();
        lcd.clear();
        lcd.printString("Well done!",0,0);
        lcd.refresh();
        wait(2);
        lcd.clear();
        lcd.printString("Play again",0,0);
        lcd.refresh();
    }
}

void obstacles()
{

    if (level == 1) {

        lcd.drawRect(10,15,2,2,1);
        lcd.drawRect(74,15,2,2,1);
    } else if (level == 2) {

        lcd.drawRect(10,15,2,2,1);
        lcd.drawRect(74,15,2,2,1);
        lcd.drawRect(10,28,2,2,1);
        lcd.drawRect(74,28,2,2,1);
    } else if (level == 3) {

        lcd.drawRect(10,15,2,2,1);
        lcd.drawRect(74,15,2,2,1);
        lcd.drawRect(10,28,2,2,1);
        lcd.drawRect(74,28,2,2,1);
        lcd.drawRect(30,15,2,2,1);
        lcd.drawRect(54,15,2,2,1);
        lcd.drawRect(30,28,2,2,1);
        lcd.drawRect(54,28,2,2,1);
    } else if (level == 4) {

        lcd.drawRect(10 + rand() %4 - 2,15 + rand() &4 -2,2,2,1);
        lcd.drawRect(74,15,2,2,1);
        lcd.drawRect(10,28,2,2,1);
        lcd.drawRect(74,28,2,2,1);
    }
}

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;
    }
}

int menu()
{

    int option;

    if (joystick.y <= 0.33) {
        option = 0;
    } else if (joystick.y <= 0.66) {
        option = 1;
    } else {
        option = 2;
    }
    return option;
}
void error()
{

    while (1) {

        lcd.printString("Error!",0,0);

        r_led = 0;
        wait(0.2);
        r_led = 1;
        wait(0.2);
    }
}