Racing Cars game using N5110 LCD and thumb Joystick

Dependencies:   N5110 PowerControl beep mbed

Committer:
el13gs
Date:
Fri May 08 00:38:01 2015 +0000
Revision:
6:289f237b8d90
Parent:
5:243718c3cd8b
Child:
7:edf0f1fcb16b
VERSION 3-LATEST ONE;

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 5:243718c3cd8b 102 int gamePlays=1;
el13gs 5:243718c3cd8b 103 int sounds=1;
el13gs 5:243718c3cd8b 104 int coinAppear=1;
el13gs 5:243718c3cd8b 105 int optionsPointer=1;
el13gs 5:243718c3cd8b 106
el13gs 5:243718c3cd8b 107 /**initial enemies Y positions (p,q,c) */
el13gs 5:243718c3cd8b 108 int p=-20;
el13gs 5:243718c3cd8b 109 int q=-100;
el13gs 5:243718c3cd8b 110 int c=0;
el13gs 5:243718c3cd8b 111 int j=0;
el13gs 5:243718c3cd8b 112
el13gs 5:243718c3cd8b 113 /** enemies X positions (p,q,c)*/
el13gs 5:243718c3cd8b 114 int enemy1x=46;
el13gs 5:243718c3cd8b 115 int enemy2x=6;
el13gs 5:243718c3cd8b 116 int enemy3x=26;
el13gs 5:243718c3cd8b 117
el13gs 5:243718c3cd8b 118
el13gs 5:243718c3cd8b 119 /** initial player's Car position and its dimensions(w,h) */
el13gs 5:243718c3cd8b 120 int x=26; /** x-position*/
el13gs 5:243718c3cd8b 121 int v=30; /** y-position*/
el13gs 5:243718c3cd8b 122 int w=8; /** width*/
el13gs 5:243718c3cd8b 123 int h=12; /** height*/
el13gs 5:243718c3cd8b 124
el13gs 5:243718c3cd8b 125 /**initial Coin Xposition */
el13gs 5:243718c3cd8b 126 int xPos=6;
el13gs 5:243718c3cd8b 127
el13gs 5:243718c3cd8b 128 /** initial Game States, Number of lives, Number of coins, Round Number */
el13gs 5:243718c3cd8b 129 int lives=5;
el13gs 5:243718c3cd8b 130 int coins=0;
el13gs 5:243718c3cd8b 131 int round=1;
el13gs 5:243718c3cd8b 132
el13gs 5:243718c3cd8b 133 /** brightness Initial Values, brightnessDisplay is the value displayed to the user */
el13gs 5:243718c3cd8b 134 float brightness=0.9;
el13gs 5:243718c3cd8b 135 int brightnessDisplay=9;
el13gs 5:243718c3cd8b 136
el13gs 5:243718c3cd8b 137 /** table array that will store cell conditions (either 0,1,2) */
el13gs 5:243718c3cd8b 138 int table[84][48];
el13gs 5:243718c3cd8b 139
el13gs 6:289f237b8d90 140 /** enemies acceleration initial value */
el13gs 6:289f237b8d90 141 int a=4;
el13gs 6:289f237b8d90 142
el13gs 5:243718c3cd8b 143 /* FUNCTION DECLERATIONS */
el13gs 5:243718c3cd8b 144
el13gs 5:243718c3cd8b 145
el13gs 5:243718c3cd8b 146 /** Initialise GameScreen
el13gs 5:243718c3cd8b 147 * Draws the lanes of the road, that are fixed.
el13gs 5:243718c3cd8b 148 * This function is called when the player moves on the screen and pixels need to be reset
el13gs 5:243718c3cd8b 149 */
el13gs 5:243718c3cd8b 150 void gameInitial();
el13gs 5:243718c3cd8b 151
el13gs 5:243718c3cd8b 152
el13gs 5:243718c3cd8b 153 /** Initialise Table Array
el13gs 5:243718c3cd8b 154 * It initialises the array used in the game by setting all the array cells value equal to 0
el13gs 5:243718c3cd8b 155 */
el13gs 5:243718c3cd8b 156 void initTable();
el13gs 5:243718c3cd8b 157
el13gs 5:243718c3cd8b 158
el13gs 5:243718c3cd8b 159 /** Calibrate Joystick Function
el13gs 5:243718c3cd8b 160 * read default positions of the joystick to calibrate later readings
el13gs 5:243718c3cd8b 161 */
el13gs 5:243718c3cd8b 162 void calibrateJoystick();
el13gs 5:243718c3cd8b 163
el13gs 5:243718c3cd8b 164
el13gs 5:243718c3cd8b 165 /** Update Joystick Function
el13gs 5:243718c3cd8b 166 * read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el13gs 5:243718c3cd8b 167 */
el13gs 5:243718c3cd8b 168 void updateJoystick();
el13gs 5:243718c3cd8b 169
el13gs 5:243718c3cd8b 170
el13gs 5:243718c3cd8b 171 /** Clear Rectangle Function
el13gs 5:243718c3cd8b 172 * This function clears the rectangle
el13gs 5:243718c3cd8b 173 * @param x - x-coordinate of top left point
el13gs 5:243718c3cd8b 174 * @param v - y-coordinate of top left point
el13gs 5:243718c3cd8b 175 * @param w - width of the Rectangle
el13gs 5:243718c3cd8b 176 * @param h - height of the Rectangle
el13gs 5:243718c3cd8b 177 * */
el13gs 5:243718c3cd8b 178 void clearRect(int x,int v,int w,int h); //clear position
el13gs 5:243718c3cd8b 179
el13gs 5:243718c3cd8b 180
el13gs 5:243718c3cd8b 181 /** MovePlayer Function
el13gs 5:243718c3cd8b 182 * This function is being called every 0.1 sec, checks the direction of the joystick
el13gs 5:243718c3cd8b 183 * and moves the Player by clearing and drawing Rectangle
el13gs 5:243718c3cd8b 184 */
el13gs 5:243718c3cd8b 185 void movePlayer();
el13gs 5:243718c3cd8b 186
el13gs 5:243718c3cd8b 187
el13gs 5:243718c3cd8b 188 /** Loose Function
el13gs 5:243718c3cd8b 189 * This function is being called when the player loose (i.e touches an enemy)
el13gs 5:243718c3cd8b 190 */
el13gs 5:243718c3cd8b 191 void loose();
el13gs 5:243718c3cd8b 192
el13gs 6:289f237b8d90 193
el13gs 5:243718c3cd8b 194 /** Check Player Function
el13gs 5:243718c3cd8b 195 * Function can be used to check if the Player touched an enemy
el13gs 5:243718c3cd8b 196 * or if he has touched a coin. Depending on the situation calls the
el13gs 5:243718c3cd8b 197 * appropriate function and implements some certain tasks
el13gs 5:243718c3cd8b 198 */
el13gs 5:243718c3cd8b 199 void checkPlayerPos();//Constantly checking player's position
el13gs 5:243718c3cd8b 200
el13gs 5:243718c3cd8b 201
el13gs 5:243718c3cd8b 202 /** Enemy Move Functions
el13gs 5:243718c3cd8b 203 * 3 Enemy Functions, each one is responsible to move the Enemy downwards
el13gs 5:243718c3cd8b 204 * The functions use an integer that defines the y position of the enemy
el13gs 5:243718c3cd8b 205 * As this integer is increasing, rectangle is being drawn and cleared continuously
el13gs 5:243718c3cd8b 206 * This integer variable restarts from a random value after reaching a specific point
el13gs 5:243718c3cd8b 207 */
el13gs 5:243718c3cd8b 208 void enemy1MovesDown();
el13gs 5:243718c3cd8b 209 void enemy2MovesDown();
el13gs 5:243718c3cd8b 210 void enemy3MovesDown();
el13gs 5:243718c3cd8b 211
el13gs 5:243718c3cd8b 212
el13gs 5:243718c3cd8b 213 /** Coin Moves Function
el13gs 5:243718c3cd8b 214 * Function that causes a coin to move downwards
el13gs 5:243718c3cd8b 215 * It calls the drawCoin and clearCoin functions so the coin is continuously moving down when it appears
el13gs 5:243718c3cd8b 216 */
el13gs 5:243718c3cd8b 217 void coinMoves();
el13gs 5:243718c3cd8b 218
el13gs 5:243718c3cd8b 219
el13gs 5:243718c3cd8b 220 /** Draw Coin Function
el13gs 5:243718c3cd8b 221 * Function that draws a coin on the screen
el13gs 5:243718c3cd8b 222 * This is a circle with a 'c' symbol at the centre of the circle
el13gs 5:243718c3cd8b 223 * It calls the drawCircle function and setPixel function to draw the coin
el13gs 5:243718c3cd8b 224 * It also sets the array cells value equal to 2, indicating there is a coin
el13gs 5:243718c3cd8b 225 * @param c - the y coordinate of the centre of the coin
el13gs 5:243718c3cd8b 226 */
el13gs 5:243718c3cd8b 227 void drawCoin(int c);
el13gs 5:243718c3cd8b 228
el13gs 5:243718c3cd8b 229
el13gs 5:243718c3cd8b 230 /** Clear Coin Function
el13gs 5:243718c3cd8b 231 * Function that clears the coin that was drawn previously
el13gs 5:243718c3cd8b 232 * This is a circle with a 'c' symbol at the centre of the circle
el13gs 5:243718c3cd8b 233 * It calls the clearCircle function and clearPixel function to clear the coin
el13gs 5:243718c3cd8b 234 * It also clears the array cells (set their values to 0), indicating there is nothing in these cells
el13gs 5:243718c3cd8b 235 * @param c - the y coordinate of the centre of the coin
el13gs 5:243718c3cd8b 236 */
el13gs 5:243718c3cd8b 237 void clearCoin(int c);
el13gs 5:243718c3cd8b 238
el13gs 5:243718c3cd8b 239
el13gs 5:243718c3cd8b 240 /** Set Circle Cells Function
el13gs 5:243718c3cd8b 241 * Function is responsible to set the values of the Table Array cells within a Circle range equal to 2
el13gs 5:243718c3cd8b 242 * This is done to identify where the coin is at a certain time
el13gs 5:243718c3cd8b 243 * @param x0-the x coordinate of the Circle's centre
el13gs 5:243718c3cd8b 244 * @param y0-the y coordinate of the Circle's centre
el13gs 5:243718c3cd8b 245 * @param radius-radius of the circle
el13gs 5:243718c3cd8b 246 */
el13gs 5:243718c3cd8b 247 void setCircleCells(int x0,int y0,int radius); //Set selected Circle cells to 1
el13gs 5:243718c3cd8b 248
el13gs 5:243718c3cd8b 249
el13gs 5:243718c3cd8b 250 /** Clear Circle Cells Function
el13gs 5:243718c3cd8b 251 * Function that clears the values of the Table Array cells within a Circle (set them equal to 0)
el13gs 5:243718c3cd8b 252 * @param x0-the x coordinate of the Circle's centre
el13gs 5:243718c3cd8b 253 * @param y0-the y coordinate of the Circle's centre
el13gs 5:243718c3cd8b 254 * @param radius-radius of the circle
el13gs 5:243718c3cd8b 255 */
el13gs 5:243718c3cd8b 256 void clearCircleCells(int x0,int y0,int radius);//Set selected Circle cells to 0
el13gs 5:243718c3cd8b 257
el13gs 5:243718c3cd8b 258
el13gs 6:289f237b8d90 259 /** Set Rectangle Cells Function
el13gs 6:289f237b8d90 260 * Function that cells the values of the Table Array cells within a Rectanlge Shape equal to 1
el13gs 6:289f237b8d90 261 * This is done for all enemies moving downwards, thus we can check where the enemies are located
el13gs 6:289f237b8d90 262 * @param x-the x-direction of the Rectangle's top left corner
el13gs 6:289f237b8d90 263 * @param v-the y-direction of the Rectangle's top right corner
el13gs 6:289f237b8d90 264 * @param w-the width of the Rectangle
el13gs 6:289f237b8d90 265 * @param h-the height of the Rectangle
el13gs 6:289f237b8d90 266 */
el13gs 5:243718c3cd8b 267 void setRectCells(int x,int v,int w,int h); //set Cells range to 1
el13gs 6:289f237b8d90 268
el13gs 6:289f237b8d90 269
el13gs 6:289f237b8d90 270 /** Clear Cells Function
el13gs 6:289f237b8d90 271 * 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 272 * @param x-x coordinate of the array to clear
el13gs 6:289f237b8d90 273 * @param y-y coordinate of the array to clear
el13gs 6:289f237b8d90 274 */
el13gs 5:243718c3cd8b 275 void clearCells(int x,int y); //set Cells range to 0
el13gs 6:289f237b8d90 276
el13gs 6:289f237b8d90 277
el13gs 6:289f237b8d90 278 /** Start Button Pressed Function
el13gs 6:289f237b8d90 279 * Flips the Start Button Flag indicator showing that button was pressed
el13gs 6:289f237b8d90 280 */
el13gs 6:289f237b8d90 281 void startButtonPressed();//Function that checks for an Interupt at pin 24
el13gs 6:289f237b8d90 282
el13gs 6:289f237b8d90 283 void resetButtonPressed();//Function that checks for an Interupt at pin 25
el13gs 6:289f237b8d90 284
el13gs 6:289f237b8d90 285 void setLineCells(int x0,int y0,int x1,int y1,int type);
el13gs 6:289f237b8d90 286
el13gs 6:289f237b8d90 287 void clearLineCells(int x0,int y0,int x1,int y1,int type);
el13gs 6:289f237b8d90 288
el13gs 6:289f237b8d90 289 extern "C" void mbed_reset();
el13gs 6:289f237b8d90 290
el13gs 6:289f237b8d90 291 void gameInitialise();