Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PowerControl mbed
main.cpp
- Committer:
- el13ks
- Date:
- 2015-04-20
- Revision:
- 1:98cc4f4a20dc
- Parent:
- 0:1c3f3fe862f2
- Child:
- 2:830feffe8462
File content as of revision 1:98cc4f4a20dc:
#include "mbed.h" #include "N5110.h" #define DIRECTION_TOLERANCE 0.05// Tolerance of Joystick #define PACMAN 3 //Radius size of pacman #define CHECKPIXEL 5 // Number of pixels checked ahead BusOut myleds(p24,p23,p22); //External LEDS InterruptIn StartButton(p18);//Start button N5110 lcd(p7,p8,p9,p10,p11,p13,p26); //PWR, SCE, RST, DC, MOSI, SCLK, LED AnalogIn Bright(p20);//Pot to adjust brightness Ticker timer; //Create ticker object DigitalIn button(p17);// Joystick Button AnalogIn xPot(p15);// Joystick x-direction AnalogIn yPot(p16);// Joystick y-direction int i=42; // Pacman's starting position int j = 24; volatile bool win=false; //True when pacman has eaten all coins volatile bool gameover=false; //True when the position of the ghost and pacman are the same Ticker pollJoystick; // timer to regularly read the joystick int next[84][48]= {0}; enum DirectionName { // create enumerated type (0,1,2,3 etc. for direction) UP, // could be extended for diagonals etc. DOWN, LEFT, RIGHT, CENTRE, UNKNOWN }; typedef struct JoyStick Joystick; // struct for 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 }; Joystick joystick; // create struct variable void calibrateJoystick() // read default positions of the joystick to calibrate later readings { 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() { joystick.x = xPot - joystick.x0; // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.y = yPot - joystick.y0; joystick.button = button; // read button state // 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 { joystick.direction = UNKNOWN; } } void drawMap1()// Furthest Left Map { lcd.drawRect(0,0,1,47,1); //x-coordinate of origin (top-left),y-coordinate of origin (top-left),width , height ,0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline), Left Border lcd.drawRect(0,0,83,1,1); // Top Border lcd.drawRect(0,46,83,1,1); // Bottom Border lcd.drawRect(82,0,1,47,1);// Right border lcd.drawRect(41,0,2,10,1); } void Getpixels() { for(int i=1; i<83; i++) { for(int j=1; j<47; j++) { int n = 0; if(lcd.getPixel (i-1,j-1)) //Pixel to top left n++; if(lcd.getPixel (i-1,j)) // Pixel to left n++; if(lcd.getPixel (i-1,j+1))//Pixel to bottom left n++; if(lcd.getPixel (i,j-1)) //Pixel directly above n++; if(lcd.getPixel (i,j+1))//Pixel to directly below n++; if(lcd.getPixel (i+1,j-1))//Pixel to top right n++; if(lcd.getPixel (i+1,j))// Pixel to right n++; if(lcd.getPixel (i+1,j+1)) // Pixel to bottom right n++; if (n>3) { next[i][j]=0; } } } } void nextgen() { for(int i=0; i<84 ; i++) { for(int j=0; j<48; j++) { if(next[i][j]) { lcd.setPixel(i,j); } else { lcd.clearPixel(i,j); } } lcd.refresh(); //Need to refresh everytime } } int main() { lcd.init(); calibrateJoystick(); // get centred values of joystick pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second while(1) { lcd.clear(); drawMap1(); lcd.drawCircle(i,j,2,1); //x-coordinate of origin (top-left),y-coordinate of origin (top-left),width , height ,0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline) if (joystick.direction == UP) { j--; } if (joystick.direction == DOWN) { j++; } if (joystick.direction == LEFT) { i--; } if (joystick.direction == RIGHT) { i++; } if (i<4) { i=4; } if (j<4) { j=4; } if (j>43) { j=43; } } }