Konlatee Sittichaivijit / Mbed 2 deprecated PacManII

Dependencies:   N5110 PowerControl mbed

main.cpp

Committer:
el13ks
Date:
2015-04-21
Revision:
4:75c7277a1b88
Parent:
3:83c1d9cd97f1
Child:
5:61c3bbef19f0

File content as of revision 4:75c7277a1b88:

#include "mbed.h"
#include "N5110.h"
#define DIRECTION_TOLERANCE 0.05// Tolerance of Joystick 

BusOut myleds(p24,p23,p22); //External LEDS
InterruptIn StartButton(p18);//Start button
N5110 lcd(p7,p8,p9,p10,p11,p13,p26); //PWR, SCE, RST, DC, MOSI, SCLK, LED
AnalogIn Bright(p20);//Pot to adjust brightness
Ticker timer; //Create ticker object
DigitalIn button(p17);// Joystick Button
AnalogIn xPot(p15);// Joystick x-direction
AnalogIn yPot(p16);// Joystick y-direction
int i=3; // Pacman's starting position
int j = 5;

Ticker pollJoystick; // timer to regularly read the joystick
int next[84][48]= {0};
int coinflag = 1;
int coinflag2 = 1;
int coinflag3 = 1;
int coinflag4 = 1;
int coinflag5 = 1;
bool canRight = 1;

enum DirectionName { // create enumerated type (0,1,2,3 etc. for direction)
    UP,         // could be extended for diagonals etc.
    DOWN,
    LEFT,
    RIGHT,
    CENTRE,
    UNKNOWN
};

typedef struct JoyStick Joystick; // struct for 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
};

Joystick joystick; // create struct variable


void calibrateJoystick() // read default positions of the joystick to calibrate later readings
{
    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()
{

    joystick.x = xPot - joystick.x0; // 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.button = button; // read button state

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



}

void drawMap()
{

    lcd.drawRect(41,0,2,10,1);



}

void checkRight()
{


    int r = 0;//Variable for right movement check
    if(lcd.getPixel (i+4,j))//Check 4 pixels to the right
        r++;
    if(lcd.getPixel (i+5,j))//Check 5 pixels to the right
        r++;
    if (r==2) { // Condition for solid obstacles
        i--;

    }
}
void checkLeft()
{
    int l = 0 ; // Variable for left movement check
    if(lcd.getPixel (i-4,j))//Check 4 pixels to the left
        l++;
    if(lcd.getPixel (i-5,j))//Check 5 pixels to the left
        l++;
    if (l==2) { // Condition for solid obstacles
        i++;


    }
}
void drawCoin() {

        if (coinflag) {
            lcd.drawCircle(10,3,2,0);
        }
        if (coinflag2) {
            lcd.drawCircle(20,3,2,0);
        }
        if (coinflag3) {
            lcd.drawCircle(30,3,2,0);
        }
        if (coinflag4) {
            lcd.drawCircle(50,3,2,0);
        }
        if (coinflag5) {
            lcd.drawCircle(60,3,2,0);
        }
    }
    void checkCoin() {
        if (i==10 &&j==3) {
            coinflag=0;
        }
        if (i==20 &&j==3) {
            coinflag2=0;
        }
        if (i==30 &&j==3) {
            coinflag3=0;
        }
        if (i==40 &&j==3) {
            coinflag4=0;
        }
        if (i==50 &&j==3) {
            coinflag5=0;
        }

    }

    void Joystickcheck() {
        if (joystick.direction == UP) {
            j--;
        }
        if (joystick.direction == DOWN) {
            j++;
        }
        if (joystick.direction == LEFT) {
            i--;
        }
        if ((joystick.direction == RIGHT)&canRight) {
            i++;
        }

    }

    void Boundarycheck() {
        if (i<3) {
            i=3;
        }
        if (j<3) {
            j=3;
        }
        if (j>44) {
            j=44;
        }
        if (i>80) {
            i=80;
        }
    }






    int main() {

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

        while(1) {
            lcd.clear();
            lcd.drawCircle(i,j,3,1); //x-coordinate of centre,y-coordinate of centre,radius ;0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
            drawMap();
            drawCoin();
            checkRight();
            checkLeft();
            Joystickcheck();
            Boundarycheck();
            checkCoin();
            lcd.refresh();
        }
    }