Racing Cars game using N5110 LCD and thumb Joystick

Dependencies:   N5110 PowerControl beep mbed

Committer:
el13gs
Date:
Fri May 08 12:58:22 2015 +0000
Revision:
7:edf0f1fcb16b
Parent:
6:289f237b8d90
Child:
8:699055e89c7d
*EXCELLENT WORKING VERSION *

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el13gs 5:243718c3cd8b 1 /**
el13gs 5:243718c3cd8b 2 @file main.h
el13gs 5:243718c3cd8b 3 @brief Header File containing function prototypes,defines and global variables.
el13gs 5:243718c3cd8b 4 @author Giorgos Savvides SID:200805533
el13gs 5:243718c3cd8b 5 @date May 2015
el13gs 5:243718c3cd8b 6 */
el13gs 5:243718c3cd8b 7
el13gs 5:243718c3cd8b 8 #include "N5110.h"
el13gs 5:243718c3cd8b 9 #include "PowerControl.h"
el13gs 5:243718c3cd8b 10 #include "EthernetPowerControl.h"
el13gs 5:243718c3cd8b 11 #include "beep.h"
el13gs 5:243718c3cd8b 12
el13gs 5:243718c3cd8b 13 #define DIRECTION_TOLERANCE 0.05
el13gs 5:243718c3cd8b 14
el13gs 5:243718c3cd8b 15 /**
el13gs 5:243718c3cd8b 16 @namespace Joystick button
el13gs 5:243718c3cd8b 17 @brief Digital Input for button status
el13gs 5:243718c3cd8b 18 */
el13gs 5:243718c3cd8b 19 DigitalIn button(p17);
el13gs 5:243718c3cd8b 20
el13gs 5:243718c3cd8b 21 /**
el13gs 5:243718c3cd8b 22 @namespace Reset Button
el13gs 5:243718c3cd8b 23 @brief Digital Input for Reset Button Status
el13gs 5:243718c3cd8b 24 */
el13gs 6:289f237b8d90 25 InterruptIn reset(p25);
el13gs 5:243718c3cd8b 26
el13gs 5:243718c3cd8b 27 /**
el13gs 5:243718c3cd8b 28 @namespace Reset Button
el13gs 5:243718c3cd8b 29 @brief Interrupt for Start Button Status
el13gs 5:243718c3cd8b 30 */
el13gs 5:243718c3cd8b 31 InterruptIn start(p24);
el13gs 5:243718c3cd8b 32
el13gs 5:243718c3cd8b 33 /**
el13gs 5:243718c3cd8b 34 @namespace AnalogIn y-position
el13gs 5:243718c3cd8b 35 @brief AnalogIn input to read potentiometer value
el13gs 5:243718c3cd8b 36 */
el13gs 5:243718c3cd8b 37 AnalogIn yPot(p18);
el13gs 5:243718c3cd8b 38
el13gs 5:243718c3cd8b 39 /**
el13gs 5:243718c3cd8b 40 @namespace AnalogIn x-position
el13gs 5:243718c3cd8b 41 @brief AnalogIn input to read potentiometer value
el13gs 5:243718c3cd8b 42 */
el13gs 5:243718c3cd8b 43 AnalogIn xPot(p19);
el13gs 5:243718c3cd8b 44
el13gs 5:243718c3cd8b 45 /**
el13gs 5:243718c3cd8b 46 @namespace Buzzer
el13gs 5:243718c3cd8b 47 @brief Buzzer object of Beep.h class to control the buzzer component
el13gs 5:243718c3cd8b 48 */
el13gs 5:243718c3cd8b 49 Beep buzzer(p21);
el13gs 5:243718c3cd8b 50
el13gs 5:243718c3cd8b 51
el13gs 5:243718c3cd8b 52 /**
el13gs 5:243718c3cd8b 53 @namespace LCD
el13gs 5:243718c3cd8b 54 @brief LCD object of N5110.h class, sets the LCD pin connections
el13gs 5:243718c3cd8b 55 */
el13gs 5:243718c3cd8b 56 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
el13gs 5:243718c3cd8b 57
el13gs 5:243718c3cd8b 58
el13gs 5:243718c3cd8b 59 /**
el13gs 5:243718c3cd8b 60 @namespace pollJoystick
el13gs 5:243718c3cd8b 61 @brief timer that reads Joystick value every certain amount of time
el13gs 5:243718c3cd8b 62 */
el13gs 5:243718c3cd8b 63 Ticker pollJoystick;
el13gs 5:243718c3cd8b 64
el13gs 5:243718c3cd8b 65 /**
el13gs 5:243718c3cd8b 66 @namespace Player Timer
el13gs 5:243718c3cd8b 67 @brief timer that reads Player's movements every certain amount of time
el13gs 5:243718c3cd8b 68 */
el13gs 5:243718c3cd8b 69 Ticker timer;
el13gs 5:243718c3cd8b 70
el13gs 5:243718c3cd8b 71
el13gs 5:243718c3cd8b 72 /** List of Directions within the game enum class */
el13gs 5:243718c3cd8b 73 enum DirectionName {
el13gs 5:243718c3cd8b 74 UP,
el13gs 5:243718c3cd8b 75 DOWN,
el13gs 5:243718c3cd8b 76 LEFT,
el13gs 5:243718c3cd8b 77 RIGHT,
el13gs 5:243718c3cd8b 78 CENTRE,
el13gs 5:243718c3cd8b 79 UNKNOWN
el13gs 5:243718c3cd8b 80 };
el13gs 5:243718c3cd8b 81
el13gs 5:243718c3cd8b 82 /** A structure that includes joystick properties
el13gs 5:243718c3cd8b 83 */
el13gs 5:243718c3cd8b 84 typedef struct JoyStick Joystick;
el13gs 5:243718c3cd8b 85 struct JoyStick {
el13gs 5:243718c3cd8b 86 float x; /** current x value */
el13gs 5:243718c3cd8b 87 float x0; /** 'centred' x value */
el13gs 5:243718c3cd8b 88 float y; /** current y value */
el13gs 5:243718c3cd8b 89 float y0; /** 'centred' y value */
el13gs 5:243718c3cd8b 90 int button; /** button state (assume pull-down used, so 1 = pressed, 0 = unpressed) */
el13gs 5:243718c3cd8b 91 DirectionName direction; /** current direction */
el13gs 5:243718c3cd8b 92 };
el13gs 5:243718c3cd8b 93
el13gs 5:243718c3cd8b 94 /** create Joystick struct variable */
el13gs 5:243718c3cd8b 95 Joystick joystick;
el13gs 5:243718c3cd8b 96
el13gs 5:243718c3cd8b 97 /**flag pointers when interrupt comes in */
el13gs 5:243718c3cd8b 98 int printFlag = 0;
el13gs 6:289f237b8d90 99 int startButtonFlag=0;
el13gs 6:289f237b8d90 100 int resetButtonFlag=0;
el13gs 5:243718c3cd8b 101
el13gs 7:edf0f1fcb16b 102
el13gs 7:edf0f1fcb16b 103 char livesBuffer[1];
el13gs 7:edf0f1fcb16b 104 char roundBuffer[1];
el13gs 7:edf0f1fcb16b 105 char coinsBuffer[2];
el13gs 7:edf0f1fcb16b 106 char soundString[3]; // Create buffer String that stores the sound state String("YES","NO")
el13gs 7:edf0f1fcb16b 107 char brightnessBuffer[2];
el13gs 7:edf0f1fcb16b 108
el13gs 5:243718c3cd8b 109 int gamePlays=1;
el13gs 5:243718c3cd8b 110 int sounds=1;
el13gs 5:243718c3cd8b 111 int coinAppear=1;
el13gs 5:243718c3cd8b 112 int optionsPointer=1;
el13gs 5:243718c3cd8b 113
el13gs 5:243718c3cd8b 114 /**initial enemies Y positions (p,q,c) */
el13gs 5:243718c3cd8b 115 int p=-20;
el13gs 5:243718c3cd8b 116 int q=-100;
el13gs 5:243718c3cd8b 117 int c=0;
el13gs 5:243718c3cd8b 118 int j=0;
el13gs 5:243718c3cd8b 119
el13gs 5:243718c3cd8b 120 /** enemies X positions (p,q,c)*/
el13gs 5:243718c3cd8b 121 int enemy1x=46;
el13gs 5:243718c3cd8b 122 int enemy2x=6;
el13gs 5:243718c3cd8b 123 int enemy3x=26;
el13gs 5:243718c3cd8b 124
el13gs 5:243718c3cd8b 125
el13gs 5:243718c3cd8b 126 /** initial player's Car position and its dimensions(w,h) */
el13gs 5:243718c3cd8b 127 int x=26; /** x-position*/
el13gs 5:243718c3cd8b 128 int v=30; /** y-position*/
el13gs 5:243718c3cd8b 129 int w=8; /** width*/
el13gs 5:243718c3cd8b 130 int h=12; /** height*/
el13gs 5:243718c3cd8b 131
el13gs 5:243718c3cd8b 132 /**initial Coin Xposition */
el13gs 5:243718c3cd8b 133 int xPos=6;
el13gs 5:243718c3cd8b 134
el13gs 5:243718c3cd8b 135 /** initial Game States, Number of lives, Number of coins, Round Number */
el13gs 7:edf0f1fcb16b 136 int lives;
el13gs 7:edf0f1fcb16b 137 int coins;
el13gs 7:edf0f1fcb16b 138 int round;
el13gs 5:243718c3cd8b 139
el13gs 5:243718c3cd8b 140 /** brightness Initial Values, brightnessDisplay is the value displayed to the user */
el13gs 5:243718c3cd8b 141 float brightness=0.9;
el13gs 5:243718c3cd8b 142 int brightnessDisplay=9;
el13gs 5:243718c3cd8b 143
el13gs 5:243718c3cd8b 144 /** table array that will store cell conditions (either 0,1,2) */
el13gs 5:243718c3cd8b 145 int table[84][48];
el13gs 5:243718c3cd8b 146
el13gs 6:289f237b8d90 147 /** enemies acceleration initial value */
el13gs 6:289f237b8d90 148 int a=4;
el13gs 6:289f237b8d90 149
el13gs 5:243718c3cd8b 150 /* FUNCTION DECLERATIONS */
el13gs 5:243718c3cd8b 151
el13gs 5:243718c3cd8b 152
el13gs 5:243718c3cd8b 153 /** Initialise GameScreen
el13gs 5:243718c3cd8b 154 * Draws the lanes of the road, that are fixed.
el13gs 5:243718c3cd8b 155 * This function is called when the player moves on the screen and pixels need to be reset
el13gs 5:243718c3cd8b 156 */
el13gs 7:edf0f1fcb16b 157 void gameLanes();
el13gs 5:243718c3cd8b 158
el13gs 5:243718c3cd8b 159
el13gs 5:243718c3cd8b 160 /** Initialise Table Array
el13gs 5:243718c3cd8b 161 * It initialises the array used in the game by setting all the array cells value equal to 0
el13gs 5:243718c3cd8b 162 */
el13gs 5:243718c3cd8b 163 void initTable();
el13gs 5:243718c3cd8b 164
el13gs 5:243718c3cd8b 165
el13gs 5:243718c3cd8b 166 /** Calibrate Joystick Function
el13gs 5:243718c3cd8b 167 * read default positions of the joystick to calibrate later readings
el13gs 5:243718c3cd8b 168 */
el13gs 5:243718c3cd8b 169 void calibrateJoystick();
el13gs 5:243718c3cd8b 170
el13gs 5:243718c3cd8b 171
el13gs 5:243718c3cd8b 172 /** Update Joystick Function
el13gs 5:243718c3cd8b 173 * read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el13gs 5:243718c3cd8b 174 */
el13gs 5:243718c3cd8b 175 void updateJoystick();
el13gs 5:243718c3cd8b 176
el13gs 5:243718c3cd8b 177
el13gs 5:243718c3cd8b 178 /** Clear Rectangle Function
el13gs 5:243718c3cd8b 179 * This function clears the rectangle
el13gs 5:243718c3cd8b 180 * @param x - x-coordinate of top left point
el13gs 5:243718c3cd8b 181 * @param v - y-coordinate of top left point
el13gs 5:243718c3cd8b 182 * @param w - width of the Rectangle
el13gs 5:243718c3cd8b 183 * @param h - height of the Rectangle
el13gs 5:243718c3cd8b 184 * */
el13gs 5:243718c3cd8b 185 void clearRect(int x,int v,int w,int h); //clear position
el13gs 5:243718c3cd8b 186
el13gs 5:243718c3cd8b 187
el13gs 5:243718c3cd8b 188 /** MovePlayer Function
el13gs 5:243718c3cd8b 189 * This function is being called every 0.1 sec, checks the direction of the joystick
el13gs 5:243718c3cd8b 190 * and moves the Player by clearing and drawing Rectangle
el13gs 5:243718c3cd8b 191 */
el13gs 5:243718c3cd8b 192 void movePlayer();
el13gs 5:243718c3cd8b 193
el13gs 5:243718c3cd8b 194
el13gs 5:243718c3cd8b 195 /** Loose Function
el13gs 5:243718c3cd8b 196 * This function is being called when the player loose (i.e touches an enemy)
el13gs 5:243718c3cd8b 197 */
el13gs 5:243718c3cd8b 198 void loose();
el13gs 5:243718c3cd8b 199
el13gs 6:289f237b8d90 200
el13gs 5:243718c3cd8b 201 /** Check Player Function
el13gs 5:243718c3cd8b 202 * Function can be used to check if the Player touched an enemy
el13gs 5:243718c3cd8b 203 * or if he has touched a coin. Depending on the situation calls the
el13gs 5:243718c3cd8b 204 * appropriate function and implements some certain tasks
el13gs 5:243718c3cd8b 205 */
el13gs 5:243718c3cd8b 206 void checkPlayerPos();//Constantly checking player's position
el13gs 5:243718c3cd8b 207
el13gs 5:243718c3cd8b 208
el13gs 5:243718c3cd8b 209 /** Enemy Move Functions
el13gs 5:243718c3cd8b 210 * 3 Enemy Functions, each one is responsible to move the Enemy downwards
el13gs 5:243718c3cd8b 211 * The functions use an integer that defines the y position of the enemy
el13gs 5:243718c3cd8b 212 * As this integer is increasing, rectangle is being drawn and cleared continuously
el13gs 5:243718c3cd8b 213 * This integer variable restarts from a random value after reaching a specific point
el13gs 5:243718c3cd8b 214 */
el13gs 5:243718c3cd8b 215 void enemy1MovesDown();
el13gs 5:243718c3cd8b 216 void enemy2MovesDown();
el13gs 5:243718c3cd8b 217 void enemy3MovesDown();
el13gs 5:243718c3cd8b 218
el13gs 5:243718c3cd8b 219
el13gs 5:243718c3cd8b 220 /** Coin Moves Function
el13gs 5:243718c3cd8b 221 * Function that causes a coin to move downwards
el13gs 5:243718c3cd8b 222 * It calls the drawCoin and clearCoin functions so the coin is continuously moving down when it appears
el13gs 5:243718c3cd8b 223 */
el13gs 5:243718c3cd8b 224 void coinMoves();
el13gs 5:243718c3cd8b 225
el13gs 5:243718c3cd8b 226
el13gs 5:243718c3cd8b 227 /** Draw Coin Function
el13gs 5:243718c3cd8b 228 * Function that draws a coin on the screen
el13gs 5:243718c3cd8b 229 * This is a circle with a 'c' symbol at the centre of the circle
el13gs 5:243718c3cd8b 230 * It calls the drawCircle function and setPixel function to draw the coin
el13gs 5:243718c3cd8b 231 * It also sets the array cells value equal to 2, indicating there is a coin
el13gs 5:243718c3cd8b 232 * @param c - the y coordinate of the centre of the coin
el13gs 5:243718c3cd8b 233 */
el13gs 5:243718c3cd8b 234 void drawCoin(int c);
el13gs 5:243718c3cd8b 235
el13gs 5:243718c3cd8b 236
el13gs 5:243718c3cd8b 237 /** Clear Coin Function
el13gs 5:243718c3cd8b 238 * Function that clears the coin that was drawn previously
el13gs 5:243718c3cd8b 239 * This is a circle with a 'c' symbol at the centre of the circle
el13gs 5:243718c3cd8b 240 * It calls the clearCircle function and clearPixel function to clear the coin
el13gs 5:243718c3cd8b 241 * It also clears the array cells (set their values to 0), indicating there is nothing in these cells
el13gs 5:243718c3cd8b 242 * @param c - the y coordinate of the centre of the coin
el13gs 5:243718c3cd8b 243 */
el13gs 5:243718c3cd8b 244 void clearCoin(int c);
el13gs 5:243718c3cd8b 245
el13gs 5:243718c3cd8b 246
el13gs 5:243718c3cd8b 247 /** Set Circle Cells Function
el13gs 5:243718c3cd8b 248 * Function is responsible to set the values of the Table Array cells within a Circle range equal to 2
el13gs 5:243718c3cd8b 249 * This is done to identify where the coin is at a certain time
el13gs 5:243718c3cd8b 250 * @param x0-the x coordinate of the Circle's centre
el13gs 5:243718c3cd8b 251 * @param y0-the y coordinate of the Circle's centre
el13gs 5:243718c3cd8b 252 * @param radius-radius of the circle
el13gs 5:243718c3cd8b 253 */
el13gs 5:243718c3cd8b 254 void setCircleCells(int x0,int y0,int radius); //Set selected Circle cells to 1
el13gs 5:243718c3cd8b 255
el13gs 5:243718c3cd8b 256
el13gs 5:243718c3cd8b 257 /** Clear Circle Cells Function
el13gs 5:243718c3cd8b 258 * Function that clears the values of the Table Array cells within a Circle (set them equal to 0)
el13gs 5:243718c3cd8b 259 * @param x0-the x coordinate of the Circle's centre
el13gs 5:243718c3cd8b 260 * @param y0-the y coordinate of the Circle's centre
el13gs 5:243718c3cd8b 261 * @param radius-radius of the circle
el13gs 5:243718c3cd8b 262 */
el13gs 5:243718c3cd8b 263 void clearCircleCells(int x0,int y0,int radius);//Set selected Circle cells to 0
el13gs 5:243718c3cd8b 264
el13gs 5:243718c3cd8b 265
el13gs 6:289f237b8d90 266 /** Set Rectangle Cells Function
el13gs 6:289f237b8d90 267 * Function that cells the values of the Table Array cells within a Rectanlge Shape equal to 1
el13gs 6:289f237b8d90 268 * This is done for all enemies moving downwards, thus we can check where the enemies are located
el13gs 6:289f237b8d90 269 * @param x-the x-direction of the Rectangle's top left corner
el13gs 6:289f237b8d90 270 * @param v-the y-direction of the Rectangle's top right corner
el13gs 6:289f237b8d90 271 * @param w-the width of the Rectangle
el13gs 6:289f237b8d90 272 * @param h-the height of the Rectangle
el13gs 6:289f237b8d90 273 */
el13gs 5:243718c3cd8b 274 void setRectCells(int x,int v,int w,int h); //set Cells range to 1
el13gs 6:289f237b8d90 275
el13gs 6:289f237b8d90 276
el13gs 6:289f237b8d90 277 /** Clear Cells Function
el13gs 6:289f237b8d90 278 * Clears the pixel and sets the array table cells value equal to 0 , from 0 to x and from 0 to y
el13gs 6:289f237b8d90 279 * @param x-x coordinate of the array to clear
el13gs 6:289f237b8d90 280 * @param y-y coordinate of the array to clear
el13gs 6:289f237b8d90 281 */
el13gs 5:243718c3cd8b 282 void clearCells(int x,int y); //set Cells range to 0
el13gs 6:289f237b8d90 283
el13gs 6:289f237b8d90 284
el13gs 6:289f237b8d90 285 /** Start Button Pressed Function
el13gs 6:289f237b8d90 286 * Flips the Start Button Flag indicator showing that button was pressed
el13gs 6:289f237b8d90 287 */
el13gs 6:289f237b8d90 288 void startButtonPressed();//Function that checks for an Interupt at pin 24
el13gs 6:289f237b8d90 289
el13gs 7:edf0f1fcb16b 290 /** Start Button Pressed Function
el13gs 7:edf0f1fcb16b 291 * Flips the Start Button Flag indicator showing that button was pressed
el13gs 7:edf0f1fcb16b 292 */
el13gs 6:289f237b8d90 293 void resetButtonPressed();//Function that checks for an Interupt at pin 25
el13gs 6:289f237b8d90 294
el13gs 7:edf0f1fcb16b 295 /** Set Line's Cells Function
el13gs 7:edf0f1fcb16b 296 * Sets the cells of a line equal to 2
el13gs 7:edf0f1fcb16b 297 * @param x0 - x-coordinate of first point
el13gs 7:edf0f1fcb16b 298 * @param y0 - y-coordinate of first point
el13gs 7:edf0f1fcb16b 299 * @param x1 - x-coordinate of last point
el13gs 7:edf0f1fcb16b 300 * @param y1 - y-coordinate of last point
el13gs 7:edf0f1fcb16b 301 * @param type - 0 white,1 black,2 dotted
el13gs 7:edf0f1fcb16b 302 */
el13gs 6:289f237b8d90 303 void setLineCells(int x0,int y0,int x1,int y1,int type);
el13gs 6:289f237b8d90 304
el13gs 7:edf0f1fcb16b 305
el13gs 7:edf0f1fcb16b 306 /** Clear Line's Cells Function
el13gs 7:edf0f1fcb16b 307 * Clears the cells of a line(set them equal to 0)
el13gs 7:edf0f1fcb16b 308 * @param x0 - x-coordinate of first point
el13gs 7:edf0f1fcb16b 309 * @param y0 - y-coordinate of first point
el13gs 7:edf0f1fcb16b 310 * @param x1 - x-coordinate of last point
el13gs 7:edf0f1fcb16b 311 * @param y1 - y-coordinate of last point
el13gs 7:edf0f1fcb16b 312 * @param type - 0 white,1 black,2 dotted
el13gs 7:edf0f1fcb16b 313 */
el13gs 6:289f237b8d90 314 void clearLineCells(int x0,int y0,int x1,int y1,int type);
el13gs 6:289f237b8d90 315
el13gs 6:289f237b8d90 316
el13gs 7:edf0f1fcb16b 317 /** Initialisation Game Function
el13gs 7:edf0f1fcb16b 318 * Sets the initial game values
el13gs 7:edf0f1fcb16b 319 */
el13gs 7:edf0f1fcb16b 320 void gameReset();