My ELEC2645 project. Nikollao Sulollari. 200804685
Dependencies: N5110 SoftPWM mbed
main.cpp
- Committer:
- Nikollao
- Date:
- 2016-03-24
- Revision:
- 4:f31bdc212241
- Parent:
- 3:d2cc054e8605
- Child:
- 5:ffa498d7071f
File content as of revision 4:f31bdc212241:
//Fadia #include "mbed.h" #include "N5110.h" #include "stdlib.h" #include "main.h" N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); Serial pc(USBTX,USBRX); #define DIRECTION_TOLERANCE 0.05 Ticker ticker; Ticker game_ticker; Timeout timeout; enum DirectionName { UP, DOWN, LEFT, RIGHT, CENTRE, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; typedef struct JoyStick Joystick; struct JoyStick { double x; // current x value double x0; // 'centred' x value double y; // current y value double 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 main() { lcd.init(); init_K64F(); init_serial(); button.rise(&button_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(); enemies(); obstacles(); if (heroY < -45) { heroY = 0; level++; } if (reset < level) { reset = level; rectX = rand() % 84; rectY = 0; circleX = 0; circleY = rand() % 47; } /*for (int i=0; i<84; i++) { for (int j=0; j<48; j++) { // loop through the cells on the grid bool n = intersection(i,j); if ( n > 0) { lcd.clear(); lcd.printString("enemy touched",0,0); break; } } }*/ 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 == UP_RIGHT) pc.printf(" UP - RIGHT\n"); if (joystick.direction == UP_LEFT) pc.printf(" UP - LEFT\n"); if (joystick.direction == DOWN_RIGHT) pc.printf(" DOWN - RIGHT\n"); if (joystick.direction == DOWN_LEFT) pc.printf(" DOWN - LEFT\n"); } updateJoystick(); //callibrateJoystick(); 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 sw3_isr() { g_sw3_flag = 1; } void button_isr() { g_button_flag =1; } void timeout_isr() { if (button) { pc.printf("button is pressed! \n"); } } void enemyRect() { lcd.drawRect(rectX,rectY,5,4,1); rectX = rectX + rand()%4 - 2; //rectY = rand()%4-2; //rectX++; rectY++; } void hero() { heroX = heroX + 5*xPot; heroY = heroY - 5*yPot; if (heroX > 35) { heroX = 35; } if (heroX < 5) { heroX = 5; } lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1); lcd.drawLine(40+heroX, 43+heroY,48+heroX, 47+heroY,1); lcd.drawLine(44+heroX, 45+heroY,44+heroX, 41+heroY,1); lcd.drawCircle(44+heroX, 39+heroY,2,0); } void enemyCircle() { lcd.drawCircle(circleX,circleY,4,1); circleY = circleY + rand() %4 - 2; circleX++; } void init_game() { //button.mode(PullNone); srand(time(NULL)); rectY = 0; rectX = rand() %40 + 20; circleY = rand() %20 + 10; circleX = 0; if ( play == 0) { lcd.setBrightness(0.5); // put LED backlight on 50% timeout.attach(&timeout_isr,2); lcd.printString("Welcome to",11,1); lcd.printString("Treasure Hunt!",1,3); lcd.refresh(); sleep(); lcd.clear(); } game_ticker.attach(game_timer_isr,0.2); while (1) { if (g_game_timer_flag) { g_game_timer_flag = 0; updateJoystick(); lcd.clear(); menu(); //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_button_flag) { g_button_flag = 0; if (option == 0) { game_ticker.detach(); break; } else if (option == 1) { //settings_menu(); pc.printf("Modify Settings!"); } else { lcd.turnOff(); deepsleep(); } } 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.printString("F",42,0); } else if (level == 8) { ticker.detach(); lcd.clear(); lcd.printString("Well done!",0,0); lcd.refresh(); timeout.attach(&timeout_isr,2); sleep(); lcd.clear(); lcd.printString("Play again <",0,0); lcd.refresh(); sleep(); if (g_button_flag) { g_button_flag = 0; level = 0; play++; lcd.clear(); main(); } } } 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 + rand() %4 - 2,15 + rand() %4 - 2,2,2,1); lcd.drawRect(10 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1); lcd.drawRect(74 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1); lcd.drawRect(30 + rand() %4 - 2,15,2,2,1); lcd.drawRect(54,15 + rand() %4 - 2,2,2,1); lcd.drawRect(30,28 + rand() %4 - 2,2,2,1); lcd.drawRect(54 + rand() %4 - 2,28,2,2,1); } else if ( level == 5) { lcd.drawLine(50,35,65,35,1); lcd.drawLine(38,30,28,30,1); lcd.drawLine(60,25,70,25,1); lcd.drawLine(28,25,18,25,1); lcd.drawLine(70,20,80,20,1); lcd.drawLine(18,20,8,20,1); } else if (level == 6) { } else if (level == 7) { /* lcd.drawLine(50,15,30,15,1); lcd.drawLine(30,15,30,35,1); lcd.drawLine(30,25,45,25,1); */ } } void enemies() { if (level == 0) { enemyRect(); //enemyCircle(); } else if (level == 1) { enemyRect(); //enemyCircle(); } else if (level == 2) { } else if (level == 3) { } enemyCircle(); } void calibrateJoystick() { // 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 if (joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = UP_LEFT; } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = UP_RIGHT; } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { joystick.direction = DOWN_LEFT; } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = DOWN_RIGHT; } } int menu() { if (joystick.y <= 0.33) { option = 0; } else if (joystick.y <= 0.66) { option = 1; } else { option = 2; } return option; } bool intersection (int i, int j) { bool n=0; // set n (number of neigbours) as 0 if (lcd.getPixel(i-1,j-1)) //pixel to the top-left n = 1; // increase n by 1 if (lcd.getPixel(i-1,j)) //pixel to the left n = 1; // increase n by 1 if (lcd.getPixel(i-1,j+1)) //pixel to the bottom-left n = 1; // increase n by 1 if (lcd.getPixel(i,j-1)) // pixel to the top n = 1; // increase n by 1 if (lcd.getPixel(i,j+1)) //pixel to the bottom n = 1; // increase n by 1 if (lcd.getPixel(i+1,j-1)) //pixel to the top-right n = 1; // increase n by 1 if (lcd.getPixel(i+1,j)) // pixel to the right n = 1; // increase n by 1 if (lcd.getPixel(i+1,j+1)) //pixel to the bottom right n = 1; // increase n by 1 //pc.printf("Number of neighbours = %d \n",n); return n; } void error() { while (1) { lcd.printString("Error!",0,0); r_led = 0; wait(0.2); r_led = 1; wait(0.2); } }