A project that creates falling asteroids at random positions along the top of the screen for a space ship at the bottom to shoot, using a Nokia N5110 LCD, joystick and pushbuttons

Dependencies:   DebounceIn N5110 PowerControl mbed

Committer:
wray2303
Date:
Fri May 08 09:24:50 2015 +0000
Revision:
0:6eba0e66ce01
Child:
2:8d28a2f491eb
pre-update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wray2303 0:6eba0e66ce01 1 /**
wray2303 0:6eba0e66ce01 2 @file main.h
wray2303 0:6eba0e66ce01 3 @brief Header file containing functions prototypes, defines and global variables.
wray2303 0:6eba0e66ce01 4 @author Louis Wray
wray2303 0:6eba0e66ce01 5 @date 14th April 2015
wray2303 0:6eba0e66ce01 6 */
wray2303 0:6eba0e66ce01 7
wray2303 0:6eba0e66ce01 8 #ifndef MAIN_H
wray2303 0:6eba0e66ce01 9 #define MAIN_H
wray2303 0:6eba0e66ce01 10 #include "mbed.h"
wray2303 0:6eba0e66ce01 11 #include "N5110.h"
wray2303 0:6eba0e66ce01 12 #include "PowerControl/PowerControl.h"
wray2303 0:6eba0e66ce01 13 #include "PowerControl/EthernetPowerControl.h"
wray2303 0:6eba0e66ce01 14 #include "DebounceIn.h"
wray2303 0:6eba0e66ce01 15 #define DIRECTION_TOLERANCE 0.05
wray2303 0:6eba0e66ce01 16
wray2303 0:6eba0e66ce01 17 /**
wray2303 0:6eba0e66ce01 18 @namespace button
wray2303 0:6eba0e66ce01 19 @brief represents button input pin from joystick
wray2303 0:6eba0e66ce01 20 */
wray2303 0:6eba0e66ce01 21 DebounceIn button(p17);
wray2303 0:6eba0e66ce01 22
wray2303 0:6eba0e66ce01 23 /**
wray2303 0:6eba0e66ce01 24 @namespace buzzer
wray2303 0:6eba0e66ce01 25 @brief represents buzzer output pin from pwm
wray2303 0:6eba0e66ce01 26 */
wray2303 0:6eba0e66ce01 27 PwmOut PWM1(p25);
wray2303 0:6eba0e66ce01 28
wray2303 0:6eba0e66ce01 29 /**
wray2303 0:6eba0e66ce01 30 @namespace xPot
wray2303 0:6eba0e66ce01 31 @brief analog input from potentiometer reading x position
wray2303 0:6eba0e66ce01 32 */
wray2303 0:6eba0e66ce01 33 AnalogIn xPot(p15);
wray2303 0:6eba0e66ce01 34
wray2303 0:6eba0e66ce01 35 /**
wray2303 0:6eba0e66ce01 36 @namespace yPot
wray2303 0:6eba0e66ce01 37 @brief analog input from potentiometer reading y position
wray2303 0:6eba0e66ce01 38 */
wray2303 0:6eba0e66ce01 39 AnalogIn yPot(p16);
wray2303 0:6eba0e66ce01 40
wray2303 0:6eba0e66ce01 41 /**
wray2303 0:6eba0e66ce01 42 @namespace randomPin
wray2303 0:6eba0e66ce01 43 @brief analog input to seed random function
wray2303 0:6eba0e66ce01 44 */
wray2303 0:6eba0e66ce01 45 AnalogIn randomPin(p19);
wray2303 0:6eba0e66ce01 46
wray2303 0:6eba0e66ce01 47 /**
wray2303 0:6eba0e66ce01 48 @namespace serial
wray2303 0:6eba0e66ce01 49 @brief serial connection established
wray2303 0:6eba0e66ce01 50 */
wray2303 0:6eba0e66ce01 51 Serial serial(USBTX,USBRX);
wray2303 0:6eba0e66ce01 52
wray2303 0:6eba0e66ce01 53 /**
wray2303 0:6eba0e66ce01 54 @namespace myled
wray2303 0:6eba0e66ce01 55 @brief GPIO for on board mbed LED1
wray2303 0:6eba0e66ce01 56 */
wray2303 0:6eba0e66ce01 57 DigitalOut myled(LED1);
wray2303 0:6eba0e66ce01 58
wray2303 0:6eba0e66ce01 59 /**
wray2303 0:6eba0e66ce01 60 @namespace lcd
wray2303 0:6eba0e66ce01 61 @brief creates object for display.
wray2303 0:6eba0e66ce01 62 @brief shows pins used and there configuration
wray2303 0:6eba0e66ce01 63 */
wray2303 0:6eba0e66ce01 64 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); // pin settings
wray2303 0:6eba0e66ce01 65
wray2303 0:6eba0e66ce01 66 /**
wray2303 0:6eba0e66ce01 67 @namespace buttonS
wray2303 0:6eba0e66ce01 68 @brief creates interrupt in for shooting function.
wray2303 0:6eba0e66ce01 69 */
wray2303 0:6eba0e66ce01 70 DebounceIn buttonS(p22); // create interrupt button shoot
wray2303 0:6eba0e66ce01 71
wray2303 0:6eba0e66ce01 72 /**
wray2303 0:6eba0e66ce01 73 @namespace buttonP
wray2303 0:6eba0e66ce01 74 @brief creates interrupt in for other functions.
wray2303 0:6eba0e66ce01 75 */
wray2303 0:6eba0e66ce01 76 DebounceIn buttonP(p23); // create interrupt button
wray2303 0:6eba0e66ce01 77
wray2303 0:6eba0e66ce01 78 /**
wray2303 0:6eba0e66ce01 79 @brief class created for user component ship.
wray2303 0:6eba0e66ce01 80 *code will access the components of this ship class to control user component on the screen
wray2303 0:6eba0e66ce01 81 */
wray2303 0:6eba0e66ce01 82 class Ship
wray2303 0:6eba0e66ce01 83 {
wray2303 0:6eba0e66ce01 84 public:
wray2303 0:6eba0e66ce01 85 int xs; /**< ships integer x coordinate */
wray2303 0:6eba0e66ce01 86 int ys; /**< ships integer y coordinate */
wray2303 0:6eba0e66ce01 87
wray2303 0:6eba0e66ce01 88 /**
wray2303 0:6eba0e66ce01 89 *set the values for the ship, used for drawing and moving etc
wray2303 0:6eba0e66ce01 90 */
wray2303 0:6eba0e66ce01 91 void SetValues(int,int);
wray2303 0:6eba0e66ce01 92
wray2303 0:6eba0e66ce01 93 /**
wray2303 0:6eba0e66ce01 94 *function to draw ships shape
wray2303 0:6eba0e66ce01 95 @param xs - ships integer x coordinate
wray2303 0:6eba0e66ce01 96 @param ys - ships integer y coordinate
wray2303 0:6eba0e66ce01 97 */
wray2303 0:6eba0e66ce01 98 void shape() {
wray2303 0:6eba0e66ce01 99 lcd.drawLine(xs,ys,xs-5,ys+6,1);
wray2303 0:6eba0e66ce01 100 lcd.drawLine(xs,ys,xs+5,ys+6,1);
wray2303 0:6eba0e66ce01 101 lcd.drawLine(xs-5,ys+6,xs+5,ys+6,1);
wray2303 0:6eba0e66ce01 102 lcd.setPixel(xs-3,ys+7);
wray2303 0:6eba0e66ce01 103 lcd.setPixel(xs+3,ys+7);
wray2303 0:6eba0e66ce01 104 // lcd.refresh();
wray2303 0:6eba0e66ce01 105 }
wray2303 0:6eba0e66ce01 106
wray2303 0:6eba0e66ce01 107 /**
wray2303 0:6eba0e66ce01 108 *function for moving the ship to the left
wray2303 0:6eba0e66ce01 109 */
wray2303 0:6eba0e66ce01 110 void ShipLeft() {
wray2303 0:6eba0e66ce01 111 xs = xs-1;
wray2303 0:6eba0e66ce01 112 clearShapeL();
wray2303 0:6eba0e66ce01 113 shape();
wray2303 0:6eba0e66ce01 114 }
wray2303 0:6eba0e66ce01 115
wray2303 0:6eba0e66ce01 116 /**
wray2303 0:6eba0e66ce01 117 *function for moving the ship to the right
wray2303 0:6eba0e66ce01 118 */
wray2303 0:6eba0e66ce01 119 void ShipRight() {
wray2303 0:6eba0e66ce01 120 xs = xs+1;
wray2303 0:6eba0e66ce01 121 clearShapeR();
wray2303 0:6eba0e66ce01 122 shape();
wray2303 0:6eba0e66ce01 123 }
wray2303 0:6eba0e66ce01 124
wray2303 0:6eba0e66ce01 125 /**
wray2303 0:6eba0e66ce01 126 *function for clearing the previous shape when moved to the left
wray2303 0:6eba0e66ce01 127 */
wray2303 0:6eba0e66ce01 128 void clearShapeL() {
wray2303 0:6eba0e66ce01 129 lcd.drawLine(xs+1,ys,xs-4,ys+6,0);
wray2303 0:6eba0e66ce01 130 lcd.drawLine(xs+1,ys,xs+6,ys+6,0);
wray2303 0:6eba0e66ce01 131 lcd.drawLine(xs-4,ys+6,xs+6,ys+6,0);
wray2303 0:6eba0e66ce01 132 lcd.clearPixel(xs-2,ys+7);
wray2303 0:6eba0e66ce01 133 lcd.clearPixel(xs+4,ys+7);
wray2303 0:6eba0e66ce01 134 }
wray2303 0:6eba0e66ce01 135
wray2303 0:6eba0e66ce01 136 /**
wray2303 0:6eba0e66ce01 137 *function for clearing the previous shape when moved to the right
wray2303 0:6eba0e66ce01 138 */
wray2303 0:6eba0e66ce01 139 void clearShapeR() {
wray2303 0:6eba0e66ce01 140 lcd.drawLine(xs-1,ys,xs-6,ys+6,0);
wray2303 0:6eba0e66ce01 141 lcd.drawLine(xs-1,ys,xs+4,ys+6,0);
wray2303 0:6eba0e66ce01 142 lcd.drawLine(xs-6,ys+6,xs+4,ys+6,0);
wray2303 0:6eba0e66ce01 143 lcd.clearPixel(xs-4,ys+7);
wray2303 0:6eba0e66ce01 144 lcd.clearPixel(xs+2,ys+7);
wray2303 0:6eba0e66ce01 145 }
wray2303 0:6eba0e66ce01 146 } ship;
wray2303 0:6eba0e66ce01 147
wray2303 0:6eba0e66ce01 148 /** function which sets the ships xs and ys coordinates, used in the main program as an initialisation
wray2303 0:6eba0e66ce01 149 @param X - integer value to set xs
wray2303 0:6eba0e66ce01 150 @param Y - integer value to set ys
wray2303 0:6eba0e66ce01 151 */
wray2303 0:6eba0e66ce01 152 void Ship::SetValues(int X,int Y)
wray2303 0:6eba0e66ce01 153 {
wray2303 0:6eba0e66ce01 154 xs = X;
wray2303 0:6eba0e66ce01 155 ys = Y;
wray2303 0:6eba0e66ce01 156 }
wray2303 0:6eba0e66ce01 157
wray2303 0:6eba0e66ce01 158 /**
wray2303 0:6eba0e66ce01 159 @brief class created for bullet fired.
wray2303 0:6eba0e66ce01 160 *code will access the components of this Bullet class to control the bullet
wray2303 0:6eba0e66ce01 161 */
wray2303 0:6eba0e66ce01 162 class Bullet
wray2303 0:6eba0e66ce01 163 {
wray2303 0:6eba0e66ce01 164 public:
wray2303 0:6eba0e66ce01 165 int x; /**< bullets integer x coordinate */
wray2303 0:6eba0e66ce01 166 int y; /**< bullets integer y coordinate */
wray2303 0:6eba0e66ce01 167
wray2303 0:6eba0e66ce01 168 /**
wray2303 0:6eba0e66ce01 169 *set the values for the bullet, used when first fired for other commands to work off
wray2303 0:6eba0e66ce01 170 */
wray2303 0:6eba0e66ce01 171 void SetValues(int,int);
wray2303 0:6eba0e66ce01 172
wray2303 0:6eba0e66ce01 173 /**
wray2303 0:6eba0e66ce01 174 *function to draw bullets shape
wray2303 0:6eba0e66ce01 175 @param x - bullets integer x coordinate
wray2303 0:6eba0e66ce01 176 @param y - bullets integer y coordinate
wray2303 0:6eba0e66ce01 177 */
wray2303 0:6eba0e66ce01 178 void createBullet() {
wray2303 0:6eba0e66ce01 179 lcd.setPixel(x,y);
wray2303 0:6eba0e66ce01 180 lcd.setPixel(x+1,y);
wray2303 0:6eba0e66ce01 181 lcd.setPixel(x-1,y);
wray2303 0:6eba0e66ce01 182 lcd.setPixel(x,y-1);
wray2303 0:6eba0e66ce01 183 lcd.refresh();
wray2303 0:6eba0e66ce01 184 }
wray2303 0:6eba0e66ce01 185
wray2303 0:6eba0e66ce01 186 /**
wray2303 0:6eba0e66ce01 187 *function to erase previous bullets when moving forward
wray2303 0:6eba0e66ce01 188 *always followed by create bullet so no need for lcd refresh when drawing
wray2303 0:6eba0e66ce01 189 */
wray2303 0:6eba0e66ce01 190 void erasePrevBullet() {
wray2303 0:6eba0e66ce01 191 lcd.clearPixel(x,y+1);
wray2303 0:6eba0e66ce01 192 lcd.clearPixel(x+1,y+1);
wray2303 0:6eba0e66ce01 193 lcd.clearPixel(x-1,y+1);
wray2303 0:6eba0e66ce01 194 }
wray2303 0:6eba0e66ce01 195
wray2303 0:6eba0e66ce01 196 /**
wray2303 0:6eba0e66ce01 197 *bool function, which determines whether the bullet has collided with any enemies returns true or false
wray2303 0:6eba0e66ce01 198 */
wray2303 0:6eba0e66ce01 199 bool collision() {
wray2303 0:6eba0e66ce01 200 if(lcd.getPixel(x+1,y-1)||lcd.getPixel(x-1,y-1)||lcd.getPixel(x,y-2)) {
wray2303 0:6eba0e66ce01 201 clearBullet();
wray2303 0:6eba0e66ce01 202 return true;
wray2303 0:6eba0e66ce01 203 } else {return false;}
wray2303 0:6eba0e66ce01 204 }
wray2303 0:6eba0e66ce01 205 /**
wray2303 0:6eba0e66ce01 206 *bool function, which determines whether the bullet has reached the edge of the screen returns true or false
wray2303 0:6eba0e66ce01 207 */
wray2303 0:6eba0e66ce01 208 bool offScreen() {
wray2303 0:6eba0e66ce01 209 if(y<4) {
wray2303 0:6eba0e66ce01 210 clearBullet();
wray2303 0:6eba0e66ce01 211 return true;
wray2303 0:6eba0e66ce01 212 } else {
wray2303 0:6eba0e66ce01 213 return false;
wray2303 0:6eba0e66ce01 214 }
wray2303 0:6eba0e66ce01 215 }
wray2303 0:6eba0e66ce01 216 /**
wray2303 0:6eba0e66ce01 217 *function to clear bullets current position
wray2303 0:6eba0e66ce01 218 */
wray2303 0:6eba0e66ce01 219 void clearBullet() {
wray2303 0:6eba0e66ce01 220 lcd.clearPixel(x,y);
wray2303 0:6eba0e66ce01 221 lcd.clearPixel(x+1,y);
wray2303 0:6eba0e66ce01 222 lcd.clearPixel(x-1,y);
wray2303 0:6eba0e66ce01 223 lcd.clearPixel(x,y-1);
wray2303 0:6eba0e66ce01 224 lcd.refresh();
wray2303 0:6eba0e66ce01 225 }
wray2303 0:6eba0e66ce01 226 } bullet;
wray2303 0:6eba0e66ce01 227
wray2303 0:6eba0e66ce01 228 /** function which sets the bullets x and y coordinates, used in the shooting program to initialise where the bullet was fired
wray2303 0:6eba0e66ce01 229 @param Xb - integer value to set x
wray2303 0:6eba0e66ce01 230 @param Yb - integer value to set y
wray2303 0:6eba0e66ce01 231 */
wray2303 0:6eba0e66ce01 232 void Bullet::SetValues(int Xb,int Yb)
wray2303 0:6eba0e66ce01 233 {
wray2303 0:6eba0e66ce01 234 x = Xb;
wray2303 0:6eba0e66ce01 235 y = Yb;
wray2303 0:6eba0e66ce01 236 }
wray2303 0:6eba0e66ce01 237
wray2303 0:6eba0e66ce01 238 /**
wray2303 0:6eba0e66ce01 239 @brief class created for Asteroids.
wray2303 0:6eba0e66ce01 240 *code will access the components of this Asteroid class to control the Asteroid
wray2303 0:6eba0e66ce01 241 */
wray2303 0:6eba0e66ce01 242 class Asteroid
wray2303 0:6eba0e66ce01 243 {
wray2303 0:6eba0e66ce01 244 public:
wray2303 0:6eba0e66ce01 245 int xa; /**< asteroids integer x coordinate */
wray2303 0:6eba0e66ce01 246 int ya; /**< asteroids integer y coordinate */
wray2303 0:6eba0e66ce01 247 int nscore; /**< an integer value score that increments every time the player has destroyed an asteroid */
wray2303 0:6eba0e66ce01 248
wray2303 0:6eba0e66ce01 249 /**
wray2303 0:6eba0e66ce01 250 *set the x value for the asteroid, used at intervals called by a ticker
wray2303 0:6eba0e66ce01 251 */
wray2303 0:6eba0e66ce01 252 void SetAsteroidValue(int,int);
wray2303 0:6eba0e66ce01 253
wray2303 0:6eba0e66ce01 254 /**
wray2303 0:6eba0e66ce01 255 *function to draw and asteroid according to given coordinates
wray2303 0:6eba0e66ce01 256 */
wray2303 0:6eba0e66ce01 257 void createAsteroid() {
wray2303 0:6eba0e66ce01 258 lcd.drawCircle(xa,ya,3,0,1);
wray2303 0:6eba0e66ce01 259 lcd.refresh();
wray2303 0:6eba0e66ce01 260 }
wray2303 0:6eba0e66ce01 261
wray2303 0:6eba0e66ce01 262 /**
wray2303 0:6eba0e66ce01 263 *function to clear previous asteroid when moving forward(forward being from the aseroids point of view, looking at
wray2303 0:6eba0e66ce01 264 *the screen would be downwards)
wray2303 0:6eba0e66ce01 265 */
wray2303 0:6eba0e66ce01 266 void erasePrevAsteroid() {
wray2303 0:6eba0e66ce01 267 lcd.drawCircle(xa,ya-1,3,0,0);
wray2303 0:6eba0e66ce01 268 lcd.refresh();
wray2303 0:6eba0e66ce01 269 }
wray2303 0:6eba0e66ce01 270
wray2303 0:6eba0e66ce01 271 /**
wray2303 0:6eba0e66ce01 272 *function to eliminate asteroid, this will be called when a bullet has made a collision with the asteroid
wray2303 0:6eba0e66ce01 273 */
wray2303 0:6eba0e66ce01 274 void destroyAsteroid() {
wray2303 0:6eba0e66ce01 275 lcd.drawCircle(xa,ya,3,0,0);
wray2303 0:6eba0e66ce01 276 nscore = nscore+1;
wray2303 0:6eba0e66ce01 277 bullet.clearBullet();
wray2303 0:6eba0e66ce01 278 lcd.refresh();
wray2303 0:6eba0e66ce01 279 }
wray2303 0:6eba0e66ce01 280
wray2303 0:6eba0e66ce01 281 /**
wray2303 0:6eba0e66ce01 282 *boolean function which returns true or false, determining whether the asteroid has reached the bottom of the screen or not
wray2303 0:6eba0e66ce01 283 */
wray2303 0:6eba0e66ce01 284 bool armageddon() {
wray2303 0:6eba0e66ce01 285 if(ya+4 == 47) {
wray2303 0:6eba0e66ce01 286 return true;
wray2303 0:6eba0e66ce01 287 } else {
wray2303 0:6eba0e66ce01 288 return false;
wray2303 0:6eba0e66ce01 289 }
wray2303 0:6eba0e66ce01 290 }
wray2303 0:6eba0e66ce01 291 } asteroid;
wray2303 0:6eba0e66ce01 292
wray2303 0:6eba0e66ce01 293 /** function which sets the asteroids x position (y posisition is set to top of the screen)
wray2303 0:6eba0e66ce01 294 @param XA - integer value to set xa
wray2303 0:6eba0e66ce01 295 @param YA - integer value to set ya
wray2303 0:6eba0e66ce01 296 */
wray2303 0:6eba0e66ce01 297 void Asteroid::SetAsteroidValue(int XA, int YA)
wray2303 0:6eba0e66ce01 298 {
wray2303 0:6eba0e66ce01 299 xa = XA;
wray2303 0:6eba0e66ce01 300 ya = YA;
wray2303 0:6eba0e66ce01 301 }
wray2303 0:6eba0e66ce01 302
wray2303 0:6eba0e66ce01 303 /**
wray2303 0:6eba0e66ce01 304 *function prototype to caibrate the joystick and centre values
wray2303 0:6eba0e66ce01 305 */
wray2303 0:6eba0e66ce01 306 void calibrateJoystick();
wray2303 0:6eba0e66ce01 307
wray2303 0:6eba0e66ce01 308 /**
wray2303 0:6eba0e66ce01 309 *function which when called upon will update the joystick position
wray2303 0:6eba0e66ce01 310 */
wray2303 0:6eba0e66ce01 311 void updateJoystick();
wray2303 0:6eba0e66ce01 312
wray2303 0:6eba0e66ce01 313 /**
wray2303 0:6eba0e66ce01 314 *function which shows initial display and sleeps(low power sleep) untill interrupt made and game started
wray2303 0:6eba0e66ce01 315 */
wray2303 0:6eba0e66ce01 316 void startUpDisplay();
wray2303 0:6eba0e66ce01 317
wray2303 0:6eba0e66ce01 318 /**
wray2303 0:6eba0e66ce01 319 *function for gameplay
wray2303 0:6eba0e66ce01 320 */
wray2303 0:6eba0e66ce01 321 void Game();
wray2303 0:6eba0e66ce01 322
wray2303 0:6eba0e66ce01 323 /**
wray2303 0:6eba0e66ce01 324 *function which reads the position of the bullet on screen and reprints according to position also checking
wray2303 0:6eba0e66ce01 325 *whether it has made contact with enemies or the end of the screen
wray2303 0:6eba0e66ce01 326 */
wray2303 0:6eba0e66ce01 327 void ReadBullet();
wray2303 0:6eba0e66ce01 328
wray2303 0:6eba0e66ce01 329 /**
wray2303 0:6eba0e66ce01 330 *function which reads the position and state of the asteroid and refeshes accordingly
wray2303 0:6eba0e66ce01 331 */
wray2303 0:6eba0e66ce01 332 void ReadAsteroid();
wray2303 0:6eba0e66ce01 333
wray2303 0:6eba0e66ce01 334 /**
wray2303 0:6eba0e66ce01 335 *function which shows end display with score, and sleeps(low power sleep) untill interrupt made and game re-started
wray2303 0:6eba0e66ce01 336 */
wray2303 0:6eba0e66ce01 337 void EndDisplay();
wray2303 0:6eba0e66ce01 338
wray2303 0:6eba0e66ce01 339 /**
wray2303 0:6eba0e66ce01 340 *function which shows Pause Display and will restart when shoot button pressed , sleep() mode untill then
wray2303 0:6eba0e66ce01 341 */
wray2303 0:6eba0e66ce01 342 void PauseDisplay();
wray2303 0:6eba0e66ce01 343
wray2303 0:6eba0e66ce01 344 #endif