My ELEC2645 project. Nikollao Sulollari. 200804685
Dependencies: N5110 SoftPWM mbed
Fork of Treasure_Hunt by
Diff: main.cpp
- Revision:
- 1:12803d645d32
- Parent:
- 0:2d0f77943105
- Child:
- 2:af5b5d42e835
--- a/main.cpp Mon Mar 07 09:38:00 2016 +0000 +++ b/main.cpp Mon Mar 07 12:09:22 2016 +0000 @@ -7,12 +7,13 @@ #define DIRECTION_TOLERANCE 0.05 -DigitalIn button(PTB2); +InterruptIn button(PTB2); AnalogIn xPot(PTB3); AnalogIn yPot(PTB10); Ticker ticker; -Ticker game_timer; +Ticker game_ticker; +Timeout timeout; DigitalOut r_led(LED_RED); DigitalOut g_led(LED_GREEN); @@ -28,15 +29,18 @@ LEFT, RIGHT, CENTRE, - UNKNOWN + UP_LEFT, + UP_RIGHT, + DOWN_LEFT, + DOWN_RIGHT }; 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 + 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 }; @@ -58,6 +62,8 @@ volatile int g_timer_flag = 0; volatile int g_game_timer_flag = 0; volatile int g_sw2_flag = 0; +volatile int g_sw3_flag = 0; +volatile int g_button_flag = 0; void error(); // setup serial port @@ -68,6 +74,10 @@ void timer_isr(); void game_timer_isr(); void sw2_isr(); +void sw3_isr(); +void button_isr(); +void timeout_isr(); + void calibrateJoystick(); void updateJoystick(); @@ -86,6 +96,8 @@ init_K64F(); init_serial(); sw2.fall(&sw2_isr); + button.fall(&button_isr); + sw3.fall(&sw3_isr); init_game(); calibrateJoystick(); @@ -147,10 +159,17 @@ pc.printf(" RIGHT\n"); if (joystick.direction == CENTRE) pc.printf(" CENTRE\n"); - if (joystick.direction == UNKNOWN) - pc.printf(" Unsupported direction\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(); } @@ -194,6 +213,26 @@ 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!!!"); + } +} + void enemyRect() { @@ -232,12 +271,13 @@ circleY = rand() %48 + 1; circleX = 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(); - wait(1); + sleep(); lcd.clear(); + game_ticker.attach(game_timer_isr,0.5); while (1) { @@ -246,6 +286,7 @@ g_game_timer_flag = 0; updateJoystick(); lcd.clear(); + menu(); int option = menu(); if (option == 0) { @@ -264,12 +305,29 @@ lcd.printString("Settings",0,2); lcd.printString("Exit <",0,4); } + + if (g_sw3_flag) { + + g_sw3_flag = 0; + + if (option == 0) { + + pc.printf("continue!"); + } else if (option == 1) { + + pc.printf("display Settings!"); + } else if (option == 2) { + + lcd.turnOff(); + } + } } if (g_sw2_flag) { + + game_ticker.detach(); break; } - - sleep(); + sleep(); } } void guidance() @@ -325,9 +383,22 @@ } 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); + 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); + } } @@ -358,8 +429,14 @@ joystick.direction = RIGHT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; - } else { - joystick.direction = UNKNOWN; + } 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; } }