Racing Cars game using N5110 LCD and thumb Joystick
Dependencies: N5110 PowerControl beep mbed
main.h@5:243718c3cd8b, 2015-05-06 (annotated)
- Committer:
- el13gs
- Date:
- Wed May 06 00:16:31 2015 +0000
- Revision:
- 5:243718c3cd8b
- Child:
- 6:289f237b8d90
VERSION 3(WITH HEADER FILE)
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:243718c3cd8b | 25 | DigitalIn 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 | 5:243718c3cd8b | 99 | int buttonFlag=0; |
el13gs | 5:243718c3cd8b | 100 | |
el13gs | 5:243718c3cd8b | 101 | int gamePlays=1; |
el13gs | 5:243718c3cd8b | 102 | int sounds=1; |
el13gs | 5:243718c3cd8b | 103 | int coinAppear=1; |
el13gs | 5:243718c3cd8b | 104 | int menuPointer=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 | 5:243718c3cd8b | 140 | /* FUNCTION DECLERATIONS */ |
el13gs | 5:243718c3cd8b | 141 | |
el13gs | 5:243718c3cd8b | 142 | |
el13gs | 5:243718c3cd8b | 143 | /** Initialise GameScreen |
el13gs | 5:243718c3cd8b | 144 | * Draws the lanes of the road, that are fixed. |
el13gs | 5:243718c3cd8b | 145 | * This function is called when the player moves on the screen and pixels need to be reset |
el13gs | 5:243718c3cd8b | 146 | */ |
el13gs | 5:243718c3cd8b | 147 | void gameInitial(); |
el13gs | 5:243718c3cd8b | 148 | |
el13gs | 5:243718c3cd8b | 149 | |
el13gs | 5:243718c3cd8b | 150 | /** Initialise Table Array |
el13gs | 5:243718c3cd8b | 151 | * It initialises the array used in the game by setting all the array cells value equal to 0 |
el13gs | 5:243718c3cd8b | 152 | */ |
el13gs | 5:243718c3cd8b | 153 | void initTable(); |
el13gs | 5:243718c3cd8b | 154 | |
el13gs | 5:243718c3cd8b | 155 | |
el13gs | 5:243718c3cd8b | 156 | /** Calibrate Joystick Function |
el13gs | 5:243718c3cd8b | 157 | * read default positions of the joystick to calibrate later readings |
el13gs | 5:243718c3cd8b | 158 | */ |
el13gs | 5:243718c3cd8b | 159 | void calibrateJoystick(); |
el13gs | 5:243718c3cd8b | 160 | |
el13gs | 5:243718c3cd8b | 161 | |
el13gs | 5:243718c3cd8b | 162 | /** Update Joystick Function |
el13gs | 5:243718c3cd8b | 163 | * read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el13gs | 5:243718c3cd8b | 164 | */ |
el13gs | 5:243718c3cd8b | 165 | void updateJoystick(); |
el13gs | 5:243718c3cd8b | 166 | |
el13gs | 5:243718c3cd8b | 167 | |
el13gs | 5:243718c3cd8b | 168 | /** Clear Rectangle Function |
el13gs | 5:243718c3cd8b | 169 | * This function clears the rectangle |
el13gs | 5:243718c3cd8b | 170 | * @param x - x-coordinate of top left point |
el13gs | 5:243718c3cd8b | 171 | * @param v - y-coordinate of top left point |
el13gs | 5:243718c3cd8b | 172 | * @param w - width of the Rectangle |
el13gs | 5:243718c3cd8b | 173 | * @param h - height of the Rectangle |
el13gs | 5:243718c3cd8b | 174 | * */ |
el13gs | 5:243718c3cd8b | 175 | void clearRect(int x,int v,int w,int h); //clear position |
el13gs | 5:243718c3cd8b | 176 | |
el13gs | 5:243718c3cd8b | 177 | |
el13gs | 5:243718c3cd8b | 178 | /** MovePlayer Function |
el13gs | 5:243718c3cd8b | 179 | * This function is being called every 0.1 sec, checks the direction of the joystick |
el13gs | 5:243718c3cd8b | 180 | * and moves the Player by clearing and drawing Rectangle |
el13gs | 5:243718c3cd8b | 181 | */ |
el13gs | 5:243718c3cd8b | 182 | void movePlayer(); |
el13gs | 5:243718c3cd8b | 183 | |
el13gs | 5:243718c3cd8b | 184 | |
el13gs | 5:243718c3cd8b | 185 | /** Loose Function |
el13gs | 5:243718c3cd8b | 186 | * This function is being called when the player loose (i.e touches an enemy) |
el13gs | 5:243718c3cd8b | 187 | */ |
el13gs | 5:243718c3cd8b | 188 | void loose(); |
el13gs | 5:243718c3cd8b | 189 | |
el13gs | 5:243718c3cd8b | 190 | /** Check Player Function |
el13gs | 5:243718c3cd8b | 191 | * Function can be used to check if the Player touched an enemy |
el13gs | 5:243718c3cd8b | 192 | * or if he has touched a coin. Depending on the situation calls the |
el13gs | 5:243718c3cd8b | 193 | * appropriate function and implements some certain tasks |
el13gs | 5:243718c3cd8b | 194 | */ |
el13gs | 5:243718c3cd8b | 195 | void checkPlayerPos();//Constantly checking player's position |
el13gs | 5:243718c3cd8b | 196 | |
el13gs | 5:243718c3cd8b | 197 | |
el13gs | 5:243718c3cd8b | 198 | /** Enemy Move Functions |
el13gs | 5:243718c3cd8b | 199 | * 3 Enemy Functions, each one is responsible to move the Enemy downwards |
el13gs | 5:243718c3cd8b | 200 | * The functions use an integer that defines the y position of the enemy |
el13gs | 5:243718c3cd8b | 201 | * As this integer is increasing, rectangle is being drawn and cleared continuously |
el13gs | 5:243718c3cd8b | 202 | * This integer variable restarts from a random value after reaching a specific point |
el13gs | 5:243718c3cd8b | 203 | */ |
el13gs | 5:243718c3cd8b | 204 | void enemy1MovesDown(); |
el13gs | 5:243718c3cd8b | 205 | void enemy2MovesDown(); |
el13gs | 5:243718c3cd8b | 206 | void enemy3MovesDown(); |
el13gs | 5:243718c3cd8b | 207 | |
el13gs | 5:243718c3cd8b | 208 | |
el13gs | 5:243718c3cd8b | 209 | /** Coin Moves Function |
el13gs | 5:243718c3cd8b | 210 | * Function that causes a coin to move downwards |
el13gs | 5:243718c3cd8b | 211 | * It calls the drawCoin and clearCoin functions so the coin is continuously moving down when it appears |
el13gs | 5:243718c3cd8b | 212 | */ |
el13gs | 5:243718c3cd8b | 213 | void coinMoves(); |
el13gs | 5:243718c3cd8b | 214 | |
el13gs | 5:243718c3cd8b | 215 | |
el13gs | 5:243718c3cd8b | 216 | /** Draw Coin Function |
el13gs | 5:243718c3cd8b | 217 | * Function that draws a coin on the screen |
el13gs | 5:243718c3cd8b | 218 | * This is a circle with a 'c' symbol at the centre of the circle |
el13gs | 5:243718c3cd8b | 219 | * It calls the drawCircle function and setPixel function to draw the coin |
el13gs | 5:243718c3cd8b | 220 | * It also sets the array cells value equal to 2, indicating there is a coin |
el13gs | 5:243718c3cd8b | 221 | * @param c - the y coordinate of the centre of the coin |
el13gs | 5:243718c3cd8b | 222 | */ |
el13gs | 5:243718c3cd8b | 223 | void drawCoin(int c); |
el13gs | 5:243718c3cd8b | 224 | |
el13gs | 5:243718c3cd8b | 225 | |
el13gs | 5:243718c3cd8b | 226 | /** Clear Coin Function |
el13gs | 5:243718c3cd8b | 227 | * Function that clears the coin that was drawn previously |
el13gs | 5:243718c3cd8b | 228 | * This is a circle with a 'c' symbol at the centre of the circle |
el13gs | 5:243718c3cd8b | 229 | * It calls the clearCircle function and clearPixel function to clear the coin |
el13gs | 5:243718c3cd8b | 230 | * It also clears the array cells (set their values to 0), indicating there is nothing in these cells |
el13gs | 5:243718c3cd8b | 231 | * @param c - the y coordinate of the centre of the coin |
el13gs | 5:243718c3cd8b | 232 | */ |
el13gs | 5:243718c3cd8b | 233 | void clearCoin(int c); |
el13gs | 5:243718c3cd8b | 234 | |
el13gs | 5:243718c3cd8b | 235 | |
el13gs | 5:243718c3cd8b | 236 | /** Set Circle Cells Function |
el13gs | 5:243718c3cd8b | 237 | * Function is responsible to set the values of the Table Array cells within a Circle range equal to 2 |
el13gs | 5:243718c3cd8b | 238 | * This is done to identify where the coin is at a certain time |
el13gs | 5:243718c3cd8b | 239 | * @param x0-the x coordinate of the Circle's centre |
el13gs | 5:243718c3cd8b | 240 | * @param y0-the y coordinate of the Circle's centre |
el13gs | 5:243718c3cd8b | 241 | * @param radius-radius of the circle |
el13gs | 5:243718c3cd8b | 242 | */ |
el13gs | 5:243718c3cd8b | 243 | void setCircleCells(int x0,int y0,int radius); //Set selected Circle cells to 1 |
el13gs | 5:243718c3cd8b | 244 | |
el13gs | 5:243718c3cd8b | 245 | |
el13gs | 5:243718c3cd8b | 246 | /** Clear Circle Cells Function |
el13gs | 5:243718c3cd8b | 247 | * Function that clears the values of the Table Array cells within a Circle (set them equal to 0) |
el13gs | 5:243718c3cd8b | 248 | * @param x0-the x coordinate of the Circle's centre |
el13gs | 5:243718c3cd8b | 249 | * @param y0-the y coordinate of the Circle's centre |
el13gs | 5:243718c3cd8b | 250 | * @param radius-radius of the circle |
el13gs | 5:243718c3cd8b | 251 | */ |
el13gs | 5:243718c3cd8b | 252 | void clearCircleCells(int x0,int y0,int radius);//Set selected Circle cells to 0 |
el13gs | 5:243718c3cd8b | 253 | |
el13gs | 5:243718c3cd8b | 254 | |
el13gs | 5:243718c3cd8b | 255 | |
el13gs | 5:243718c3cd8b | 256 | void setRectCells(int x,int v,int w,int h); //set Cells range to 1 |
el13gs | 5:243718c3cd8b | 257 | void clearCells(int x,int y); //set Cells range to 0 |
el13gs | 5:243718c3cd8b | 258 | void buttonPressed();//Function that checks for an Interupt at pin 24 |