ELEC2645 (2015/16) / Mbed 2 deprecated Gameinitial

Dependencies:   N5110 mbed

main.cpp

Committer:
Honeymeister
Date:
2016-04-29
Revision:
1:2ab3e61fa258
Parent:
0:46009d41b72b
Child:
2:fae6fef4f5ad

File content as of revision 1:2ab3e61fa258:

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

//  VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
BusOut leds(PTC5 , PTC7);
AnalogIn pot(PTB10);

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

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


int nx= 84;
int ny= 48;
int i;
int j;
int NextGen[84][48]= {0};
void splashScreen();
void clearCells();
void Wall();
void snakeBody(char x,char y);

// timer to regularly read the joystick
Ticker pollJoystick;

// 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()
{
    srand(time(NULL));

    wait(2);
    lcd.init();
    splashScreen();
    wait(2);
    clearCells();
    Wall();
    wait(0.5);
    snakeBody(42,24);
    calibrateJoystick();  // get centred values of joystick
    pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
    while (1){
      
       lcd.setBrightness(pot*0.001f);
   }
   }


int main()
{


    while(1) {

        if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
            printFlag = 0;
            serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);

            // check joystick direction
            if (joystick.direction == UP)
                serial.printf(" UP\n");
            if (joystick.direction == DOWN)
                serial.printf(" DOWN\n");
            if (joystick.direction == LEFT)
                serial.printf(" LEFT\n");
            if (joystick.direction == RIGHT)
                serial.printf(" RIGHT\n");
            if (joystick.direction == CENTRE)
                serial.printf(" CENTRE\n");
            if (joystick.direction == UNKNOWN)
                serial.printf(" Unsupported direction\n");

        }

    }
}


void splashScreen() //This is the first screen the user sees when the game is turned on
{

    lcd.printString("Welcome To",15,1);
    wait(1);
    lcd.printString("D.Honeywell's",3,2);
    lcd.printString("------------------",1,3);
    wait(2);
    lcd.printString("E",15,4);
    wait(0.3);
    lcd.printString("EP",15,4);
    wait(0.3);
    lcd.printString("EPI",15,4);
    wait(0.3);
    lcd.printString("EPIC",15,4);
    lcd.printString("EPIC ",15,4);
    wait(0.3);
    lcd.printString("EPIC G",15,4);
    wait(0.3);
    lcd.printString("EPIC GA",15,4);
    wait(0.3);
    lcd.printString("EPIC GAM",15,4);
    wait(0.3);
    lcd.printString("EPIC GAME",15,4);
    wait(0.3);
//    lcd.drawRect(0,0,82,46,0);
//    wait(0.1);
//    lcd.drawRect(0,0,78,42,0);
//    wait(0.1);
//    lcd.drawRect(0,0,74,38,0);
//    wait(0.1);
//    lcd.drawRect(0,0,70,34,0);
//    wait(0.1);
//    lcd.drawRect(0,0,66,30,0);
//    wait(0.1);
//    lcd.drawRect(0,0,62,26,0);
//    wait(0.1);
//    lcd.drawRect(0,0,58,22,0);
//    wait(0.1);
//    lcd.drawRect(0,0,54,18,0);
//    wait(0.1);
//    lcd.drawRect(0,0,50,14,0);
//    wait(0.1);
//    lcd.drawRect(0,0,46,10,0);
//   wait(0.1);
//    lcd.drawRect(0,0,42,6,0);
//    wait(0.1);
//    lcd.drawRect(0,0,38,2,0);
//    wait(0.1);
    
    
    
    }


void clearCells()
{
    for (int i = 0; i < nx ; i++) {
        for (int j = 0; j < ny ; j++) {
            lcd.clearPixel(i,j);

        }
    }
    lcd.refresh();

}


 
void snakeBody(char x, char y)

{
    lcd.setPixel(x,y);
    lcd.setPixel(x+1,y);
    lcd.setPixel(x+2,y);
    lcd.setPixel(x+3,y);
    lcd.setPixel(x+4,y);
    lcd.setPixel(x+5,y);
    lcd.setPixel(x+6,y);
    lcd.setPixel(x+7,y);
    lcd.setPixel(x+8,y);
    lcd.setPixel(x+9,y);
    lcd.setPixel(x+10,y);
    lcd.setPixel(x,y+1);
    lcd.setPixel(x+1,y+1);
    lcd.setPixel(x+2,y+1);
    lcd.setPixel(x+3,y+1);
    lcd.setPixel(x+4,y+1);
    lcd.setPixel(x+5,y+1);
    lcd.setPixel(x+6,y+1);
    lcd.setPixel(x+7,y+1);
    lcd.setPixel(x+8,y+1);
    lcd.setPixel(x+9,y+1);
    lcd.setPixel(x+10,y+1);
    
    
    lcd.refresh();
    
}

int Rand (int Min = 1, int Max = 83)
            {
                static bool First = true;
                if (First) srand (time (NULL)), First = false;
                while (7)
                    {
                        int S = rand () % (Max - Min + 1) + Min;
                        if (!((S - 1)  % 3)) return S;
                    }
}

void Wall ()

{
    
    lcd.drawRect(0,0,82,46,0);
    lcd.drawRect(1,1,80,44,0);
    
}