
Completed Snake Program
Dependencies: N5110 PinDetect PowerControl mbed
Fork of DocTest by
main.cpp
- Committer:
- MBirney
- Date:
- 2015-04-09
- Revision:
- 3:478b090b7e1b
- Parent:
- 2:deb61a34ac31
- Child:
- 4:551dea241d0a
File content as of revision 3:478b090b7e1b:
#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); 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();*/ /* 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; // 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() { // these are default settings so not strictly needed lcd.inverseMode(); // normal colour mode lcd.setBrightness(0.5); // put LED backlight on 50% //Draw S lcd.drawRect(28,10,2,5,1); lcd.drawRect(15,10,15,2,1); lcd.drawRect(15,10,2,10,1); lcd.drawRect(15,20,15,2,1); lcd.drawRect(28,20,2,10,1); lcd.drawRect(15,28,15,2,1); lcd.drawRect(15,25,2,3,1); lcd.printString("NAKE ",34,3); lcd.printString("By M.Birney",10,5); lcd.drawRect(10,5,65,30,0); // need to refresh display after setting pixels lcd.refresh(); } void easySelected() // display when easy is selected { currentDifficulty=EASY; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); lcd.printString("Easy",20,3); lcd.printString("Medium",20,4); lcd.printString("Hard",20,5); lcd.refresh(); lcd.drawCircle(10,27,2,1); lcd.drawCircle(10,35,2,0); lcd.drawCircle(10,43,2,0); gameSpeed= 1.0; } void mediumSelected() // display when medium is selected { currentDifficulty=MEDIUM; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); lcd.printString("Easy",20,3); lcd.printString("Medium",20,4); lcd.printString("Hard",20,5); lcd.refresh(); lcd.drawCircle(10,27,2,0); lcd.drawCircle(10,35,2,1); lcd.drawCircle(10,43,2,0); gameSpeed=1.0/4; } void hardSelected() // display when hard is selected { currentDifficulty=HARD; lcd.clear(); lcd.printString("Please Select",2,0); lcd.printString("Difficulty:",2,1); lcd.printString("Easy",20,3); lcd.printString("Medium",20,4); lcd.printString("Hard",20,5); lcd.refresh(); lcd.drawCircle(10,27,2,0); lcd.drawCircle(10,35,2,0); lcd.drawCircle(10,43,2,1); gameSpeed=1.0/8; } int main() { // first need to initialise display lcd.init(); // pb.rise(&flip); displaySplash(); wait(1); easySelected(); joystick.direction=UNKNOWN; calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/5.0); // read joystick 10 times per second while(1) { if (gamePlaying==0) { switch(currentDifficulty) { case EASY: switch (joystick.direction) { case UP: hardSelected(); break; case DOWN: mediumSelected(); break; } break; case MEDIUM: switch (joystick.direction) { case UP: easySelected(); break; case DOWN: hardSelected(); break; } break; case HARD: switch (joystick.direction) { case UP: mediumSelected(); break; case DOWN: easySelected(); break; } break; wait(0.1); } } } }