
Completed Snake Program
Dependencies: N5110 PinDetect PowerControl mbed
Fork of DocTest by
Revision 3:478b090b7e1b, committed 2015-04-09
- Comitter:
- MBirney
- Date:
- Thu Apr 09 13:10:14 2015 +0000
- Parent:
- 2:deb61a34ac31
- Child:
- 4:551dea241d0a
- Commit message:
- Start Menu working (easy medium hard selectable) need to include start button to enter gameplay
Changed in this revision
PinDetect.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Apr 09 13:10:14 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- a/main.cpp Wed Apr 08 20:00:37 2015 +0000 +++ b/main.cpp Thu Apr 09 13:10:14 2015 +0000 @@ -1,20 +1,121 @@ #include "mbed.h" #include "N5110.h" +#include "PinDetect.h" + +// change this to alter tolerance of joystick direction +#define DIRECTION_TOLERANCE 0.2 // VCC,SCE,RST,D/C,MOSI,SCLK,LED N5110 lcd(p7,p8,p9,p10,p11,p13,p21); -//d +DigitalOut led2(LED2); +InterruptIn pb(p16); +// connections for joystick +DigitalIn button(p18); +AnalogIn xPot(p19); +AnalogIn yPot(p20); + +bool gamePlaying =0; + +// timer to regularly read the joystick +Ticker pollJoystick; +/*pin.mode( PullDown ); + pin.attach_asserted( &keyPressed ); + pin.attach_deasserted( &keyReleased ); + pin.attach_asserted_held( &keyPressedHeld ); + pin.attach_deasserted_held( &keyReleasedHeld ); + pin.setSampleFrequency();*/ + -// Can also power (VCC) directly from VOUT (3.3 V) - -// Can give better performance due to current limitation from GPIO pin +/* +void keyPressed( void ) { +joystick.direction =DOWN; +led2 = 1; +} + +void keyReleased( void ) { +joystick.direction =UNKNOWN; +led2 = 0; +} + +void keyPressedHeld( void ) { +led2 = 1; +} + +void keyReleasedHeld( void) { +}*/ + + enum Difficulty { EASY, MEDIUM, HARD, }; + int gameSpeed;//change depending on difficulty selected -Difficulty currentDifficulty =EASY; +Difficulty currentDifficulty=EASY; + + +// 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; + + + +// read default positions of the joystick to calibrate later readings +void calibrateJoystick() +{ + 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() +{ + // 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 = LEFT; // remember switched this with right + } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = RIGHT; + } else { + joystick.direction = UNKNOWN; + } +} void displaySplash() { @@ -44,6 +145,7 @@ void easySelected() // display when easy is selected { + currentDifficulty=EASY; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); @@ -59,6 +161,7 @@ } void mediumSelected() // display when medium is selected { + currentDifficulty=MEDIUM; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); @@ -74,6 +177,7 @@ void hardSelected() // display when hard is selected { + currentDifficulty=HARD; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); @@ -91,66 +195,76 @@ int main() { + // first need to initialise display lcd.init(); + // pb.rise(&flip); displaySplash(); wait(1); - easySelected(); - wait(2); - mediumSelected(); - wait(2); - hardSelected(); - - while(1) { + joystick.direction=UNKNOWN; + calibrateJoystick(); // get centred values of joystick + pollJoystick.attach(&updateJoystick,1.0/5.0); // read joystick 10 times per second - switch(currentDifficulty) { - case EASY: - switch (joystick.direction) - - case UP: - hardSelected; - break; + + while(1) { + + if (gamePlaying==0) { - case DOWN: - mediumSelected; - break; + switch(currentDifficulty) { + case EASY: - break; + switch (joystick.direction) { + + case UP: + hardSelected(); + break; - case MEDIUM: - - switch (joystick.direction) - - case UP: - easySelected; - break; + case DOWN: + mediumSelected(); + break; + } + break; - case DOWN: - hardSelected; - break; + case MEDIUM: + + switch (joystick.direction) { + + case UP: + easySelected(); + break; + + + case DOWN: + hardSelected(); + break; + } - break; - case HARD: - switch (joystick.direction) + break; + case HARD: + switch (joystick.direction) { - case UP: - mediumSelected; - break; + case UP: + mediumSelected(); + break; - case DOWN: - easySelected; - break; - break; + case DOWN: + easySelected(); + break; + } + break; + wait(0.1); + } + } + } - } } \ No newline at end of file