ELEC2645 Embedded Systems Project (JOYSTICK)
Dependencies: N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 0:1719e11ef162
- Child:
- 1:06958b7bbf65
diff -r 000000000000 -r 1719e11ef162 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed May 04 23:47:13 2016 +0000 @@ -0,0 +1,397 @@ +/* +Progress update: +Easter -- Added ticker for joystick update + joystick initial position checker + +week 22 -- main logic behind snake game added + to do : wraparound (optional in main menu) + menu system - play game, settings(joy calibration, wraparound on//off) ; game over screen +*/ + + +#include "mbed.h" +#include "N5110.h" +#include "SDFileSystem.h" +#define JOYSTICK_TOLERANCE 0.05 //direction tolerance of joystick + +// VCC, SCE, RST, D/C, MOSI, SCLK, LED +N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); + +void init_display(); +void draw_screen(); +void calibrate_joystick(); +void joystick_pos(); +void game_timer_isr(); +void food_pos(); +void snake_move(); +float refresh_rate = 10; //refresh screen in Hz +volatile int g_timer_flag = 0; +bool gameOver; //if game has ended +int pdirection; +int gamemode; + +const int width = 84 ; // screen width +const int height = 48 ; // screen height + +Ticker gameTimer; +Ticker gameTimer1; +Ticker joystickTimer; // ticker timer for joystick poisition update +int fx,fy,Button,score; // global variables to hold data (control) +AnalogIn xAxis(A0); +AnalogIn yAxis(A1); +AnalogIn pot(A2); + + + +enum DirectionName { //joystick directions + CENTRE, + UP, + LEFT, + DOWN, + RIGHT, + UNKNOWN +}; + +; +// struct for joystick +typedef struct joystick1 Joystick; +struct joystick1 { + 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; + +typedef struct snake1 Snake; +struct snake1 { + int hx; // head x value + int hy; // head y value + int length; // snake length + int tailx[100],taily[100]; //tail coordinates array +}; + +Snake snake; + + + +void init_display() +{ + lcd.init(); + lcd.normalMode(); // normal colour mode + lcd.setBrightness(0.5); // put LED backlight on 50% + lcd.clear(); // clear screen (init) +} + +void adjust_brightness() +{ + int x = pot; + lcd.setBrightness(x); +} + + +void draw_screen() +{ + lcd.clear(); + // for (int i = 0; i < width; i++) { // borders across + // lcd.setPixel(i,47); + // lcd.setPixel(i,0); + // } + // for (int j = 0; j < height; j++) { // borders vertical + // lcd.setPixel(0,j); + // lcd.setPixel(83,j); + // } + lcd.drawRect(0,0,83,47,0); // borders square + //lcd.setPixel(snake.hx,snake.hy); //print snake head position + + for (int q = 0; q < snake.length ; q++) { //print snake + lcd.setPixel (snake.tailx[q],snake.taily[q]); + } + lcd.setPixel(snake.hx,snake.hy); //print snake head position + lcd.setPixel (fx,fy); //print food position + lcd.refresh(); // update display + +} + +void game_timer_isr() +{ + g_timer_flag = 1; +} + +void calibrate_joystick() +{ + //get initial joystick position as offset + joystick.x0 = xAxis; + joystick.y0 = yAxis; + +} + +void joystick_pos() +{ + joystick.x = xAxis - joystick.x0; + joystick.y = yAxis - joystick.y0; + + if ( fabs(joystick.y) <= JOYSTICK_TOLERANCE && fabs(joystick.x) < JOYSTICK_TOLERANCE) { + joystick.direction = CENTRE; + } else if ( joystick.y > JOYSTICK_TOLERANCE && fabs(joystick.x) < JOYSTICK_TOLERANCE) { + joystick.direction = UP; + } else if ( joystick.y < JOYSTICK_TOLERANCE && fabs(joystick.x) < JOYSTICK_TOLERANCE) { + joystick.direction = DOWN; + } else if ( joystick.x > JOYSTICK_TOLERANCE && fabs(joystick.y) < JOYSTICK_TOLERANCE) { + joystick.direction = RIGHT; + } else if ( joystick.x < JOYSTICK_TOLERANCE && fabs(joystick.y) < JOYSTICK_TOLERANCE) { + joystick.direction = LEFT; + } else { + joystick.direction = UNKNOWN; + } + +} + + +void snake_move() +{ + + if (joystick.direction == CENTRE) { + if (pdirection == 0) { + snake.hx-- ; + } else if (pdirection == 1) { + snake.hy++ ; + } else if (pdirection == 2) { + snake.hx++ ; + } else if (pdirection == 3) { + snake.hy-- ; + } else { + } + } else { + if (joystick.direction == LEFT) { + snake.hx-- ; + pdirection = 0; + } else if (joystick.direction == UP) { + snake.hy++ ; + pdirection = 1; + } else if (joystick.direction == RIGHT) { + snake.hx++ ; + pdirection = 2; + } else if (joystick.direction == DOWN) { + snake.hy-- ; + pdirection = 3; + } else { + } + + } +} + +void game() +{ + //brightnessTimer.attach(adjust_brightness,0.1); + //gamemode = 0; + gameOver = false; + snake.hx = width / 2; // initial position of snake head + snake.hy = height / 2; + food_pos(); + score = 0; + pdirection = 1; + snake.length = 4; //length including head and tail + for (int w = 0; w < snake.length ; w++) { //set initial snake position + snake.tailx[w] = (snake.hx - w) ; + snake.taily[w] = snake.hy; + } + //draw_screen(); + + joystickTimer.attach(&joystick_pos, 0.1); + gameTimer.attach(&snake_move, 0.2); + gameTimer1.attach(&draw_screen, 0.1); + + + if (gamemode == 0) { + while (!gameOver) { + + + for (int d = snake.length ; d > 0 ; d--) { //move all snake body one position back + snake.tailx[d] = snake.tailx[d - 1]; + } + snake.hx = snake.tailx[0]; + snake.hy = snake.taily[0]; + for (int pp = 1; pp < snake.length + 1; pp++) { //snake dies when come in contact with tail + if (snake.tailx[0] == snake.tailx[pp] && snake.taily[0] == snake.taily[pp]) { + gameOver = true; + } + + } + if (snake.tailx[0] == fx && snake.taily[0] == fy) { // snake grows when in contact with food + score++; + food_pos(); + snake.length++; + } + //snake_move(); + } + + } + + + + + /*int prevx = snake.tailx[0]; + int prevy = snake.taily[0]; + int prev2x, prev2y; + // snake.tailx[0] = snake.hx; + // snake.taily[0] = snake.hy; + + for (int i = 1; i < snake.length ; i++) { + + prev2x = snake.tailx[i]; + prev2y = snake.taily[i]; + snake.tailx[i] = prevx; + snake.taily[i] = prevy; + prevx = prev2x; + prevy = prev2y; + draw_screen(); + */ + +// for (int w = 1; w < snake.length; w++) { +// if (snake.tailx[w] == snake.hx && snake.taily[w] == snake.hy) { +// gameOver = 1; +//} +// } + + + +} + +/*} else { + while (!gameOver) { + int prevx = snake.tailx[0]; + int prevy = snake.taily[0]; + int prev2x, prev2y; + snake.tailx[0] = snake.hx; + + snake.taily[0] = snake.hy; + + + for (int i = 1; i < snake.length ; i++) { + + prev2x = snake.tailx[i]; + prev2y = snake.taily[i]; + snake.tailx[i] = prevx; + snake.taily[i] = prevy; + prevx = prev2x; + prevy = prev2y; + draw_screen(); + + if (snake.hx == fx && snake.hy == fy) { + score++; + food_pos(); + snake.length++; + + + } + for (int w = 1; w < snake.length; w++) { + if (snake.tailx[w] == snake.hx && snake.taily[w] == snake.hy) { + gameOver = 1; + } + } + } + } + + // if (snake.hx == 0) { + // gameOver = 1; + //} else if (snake.hx == 83) { + // gameOver = 1; + //} else if (snake.hy == 0) { + // gameOver = 1; + //} else if (snake.hy == 47) { + // gameOver = 1; + //} + +*/ + + +void food_pos() +{ + srand(time(NULL)); + fx = rand() % 83 + 1; // random x from 0 to width - 1 for x coordinate + fy = rand() % 47 + 1; // random y from 0 to height - 1 for y coordinate + +} + +void main_menu() +{ + //brightnessTimer.attach(adjust_brightness,0.1); + joystickTimer.attach(joystick_pos,0.1); + int select = 0; + while (1) { + switch(select) { + case 0: + switch(joystick.direction) { + case UP: + select = 1; + break; + case DOWN: + select = 1; + break; + } + break; + case 1: + switch(joystick.direction) { + case UP: + select = 0; + break; + case DOWN: + select = 0; + break; + } + break; + } + wait(0.1); + if (select == 0) { + lcd.clear(); + lcd.printString("Start Game",7,0); + lcd.printString("Settings",7,1); + lcd.drawCircle(3,3,2,1); + lcd.refresh(); + } else if (select == 1) { + lcd.clear(); + lcd.printString("Start Game", 7,0); + lcd.printString("Settings",7,1); + lcd.drawCircle(3,11,2,1); + lcd.refresh(); + } + } +} + +int main() +{ + init_display(); + //brightnessTimer.attach(adjust_brightness,0.1); + joystickTimer.attach(joystick_pos,0.1); + calibrate_joystick(); + gamemode = 0; + //lcd.printString("Hello, World!",0,0); + //main_menu(); + game(); + /*while(1) { + + lcd.clear(); + if (joystick.direction == UP) { + lcd.printString ("UP",1,0); + } else if (joystick.direction == CENTRE) { + lcd.printString ("CENTRE",1,0); + } else if (joystick.direction == DOWN) { + lcd.printString ("DOWN",1,0); + } else if (joystick.direction == LEFT) { + lcd.printString ("LEFT",1,0); + } else if (joystick.direction == RIGHT) { + lcd.printString ("RIGHT",1,0); + } else { + lcd.printString ("UNKNOWN",1,0); + } + lcd.refresh(); + wait(1); + } + */ + + +}