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.
main.h
00001 /** 00002 @file main.h 00003 @brief Header file containing functions prototypes, defines and global variables. 00004 @see "Joystick" by Craig Evans 00005 @author Jordan Harper 00006 @date April 2015 00007 */ 00008 00009 #include "mbed.h" 00010 #include "N5110.h" 00011 #include "PowerControl/PowerControl.h" 00012 #include "PowerControl/EthernetPowerControl.h" 00013 00014 #define DIRECTION_TOLERANCE 0.1 /*!< change this to alter tolerance of joystick direction */ 00015 00016 #define USR_POWERDOWN (0x104) 00017 00018 /** 00019 @namespace button 00020 @brief sends digital signal to mbed when joystick is pressed, can interrupt at any point in code where there is a button flag 00021 */ 00022 InterruptIn button(p17); 00023 00024 /** 00025 @namespace xPot 00026 @brief sends analog signal to mbed from X-axis of joystick 00027 */ 00028 AnalogIn xPot(p15); 00029 00030 /** 00031 @namespace yPot 00032 @brief sends analog signal to mbed from Y-axis of joystick 00033 */ 00034 AnalogIn yPot(p16); 00035 00036 /** 00037 @namespace lcd 00038 @brief recieves and sends data between LCD connections and mbed 00039 */ 00040 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); 00041 00042 /** 00043 @namespace power 00044 @brief PWM Output to LED to signal that the power is On/Off 00045 */ 00046 PwmOut power(p24); 00047 00048 /** 00049 @namespace buz 00050 @brief PWM Output to Piezo Buzzer to play a song 00051 */ 00052 PwmOut buz(p21); 00053 00054 00055 /* LCD dimentions */ 00056 int nx = 84; /*!< screen is 84 pixels across */ 00057 int ny = 48; /*!< by 48 pixels downwards */ 00058 00059 00060 int x = 0; /*!< wall starts from the coordinates (0,0) */ 00061 int y = 0; /*!< wall starts from the coordinates (0,0) */ 00062 int z = 30; /*!< gap in the wall starts near the middle of the screen */ 00063 00064 /* initial coordinates for player object (i,j) */ 00065 int i =24; /*!< initial coordinates for player object (24,42) */ 00066 int j =42; /*!< initial coordinates for player object (24,42) */ 00067 00068 00069 int score = 0; /*!< initial value for the score at beginning of game */ 00070 00071 00072 int printFlag = 0; /*!< print flag initial value */ 00073 00074 00075 int FLAG = 0; /*!< initial value for flag that are used for wall movement */ 00076 int FLAG2 = 0; /*!< initial value for flag that are used for wall movement 2 */ 00077 int FLAG3 = 0; /*!< initial value for flag that are used for wall movement 3 */ 00078 00079 int c = 0; /*!< initial value for position of option in menu*/ 00080 00081 int bright = 0; /*!< initial case for brightness function in switch loop */ 00082 00083 int vol = 0; /*!< initial case for volume function in switch loop */ 00084 00085 int u = 0; /*!< initial value for u (used to select next frequency of PWM in song) */ 00086 00087 int soundFlag = 1; /*!< flag for toggling mute */ 00088 00089 int flashFlag = 0; /*!< flag for calling "Game Over!" phrase */ 00090 00091 int BFlag = 0; /*!< flag for joystick button */ 00092 00093 int sleepCounter = 0; /*!< initial value set for timer to save power */ 00094 00095 00096 int a = 0; /*!< wall starts from the coordinates (0,47) */ 00097 int b = 47; /*!< wall starts from the coordinates (0,47) */ 00098 int d = 30; /*!< gap in wall is near the middle of screen */ 00099 int f = 0; /*!< wall starts from the coordinates (0,0) */ 00100 int g = 0; /*!< wall starts from the coordinates (0,0) */ 00101 int h = 24; /*!< gap in wall is near the top left corner */ 00102 int q = 83; /*!< wall starts from the coordinates (83,0)*/ 00103 int w = 0; /*!< wall starts from the coordinates (83,0) */ 00104 int e = 24; /*!< gap in wall is near the top left corner */ 00105 00106 float frequency []= {880,880,0,0,440,880,0,0,440,880,600,1047,0,0,0,0,880,880,0,0,440,880,0,0,440,880,600,587,0,0,0,0,0}; /*!< frequency array for gameplay song */ 00107 float frequency2 []= {600,600,400,400,200}; /*!< frequency array for gameover song */ 00108 00109 00110 Ticker pollJoystick ; /*!< timer to regularly read the joystick */ 00111 Ticker wallMovement ; /*!< timers for moving the walls */ 00112 Ticker wallMovement2 ; /*!< timers for moving the walls */ 00113 Ticker wallMovement3 ; /*!< timers for moving the walls*/ 00114 Ticker flash ; /*!< timer for "Game Over!" phrase and sleep intterupt */ 00115 00116 00117 00118 00119 Serial serial (USBTX,USBRX); /*!< Serial for debug */ 00120 00121 LocalFileSystem local ("local"); /*!< create local filesystem */ 00122 00123 /* functions */ 00124 00125 /** reads default positions of the joystick to calibrate later readings. 00126 @param joystick.x0 - float value in X-axis to read 00127 @param joystick.y0 - float value in Y-axis to read 00128 @returns a definition for the centre value of joystick 00129 */ 00130 void calibrateJoystick(); 00131 00132 /** reads current joystick values relative to the calibrated centre value and calculates direction of joystick. 00133 @param joystick.x - new float value in X-axis to read 00134 @param joystick.y - new float value in Y-axis to read 00135 @param joystick.button - state of joystick button to read 00136 @param joystick.direction - calculated direction state of joystick 00137 @returns direction of joystick 00138 */ 00139 void updateJoystick(); 00140 00141 /** Turns all cells on the display off 00142 @param i - all cells in x direction on screen 00143 @param j - all cells in y direction on screen 00144 @returns a blank screen 00145 */ 00146 void clearCells(); 00147 00148 00149 void fallingWall(); 00150 00151 /** flag function for moving wall to call later */ 00152 void flagForWall(); 00153 00154 /** flag function for moving wall to call later */ 00155 void flagForWall2(); 00156 00157 /** flag function for moving wall to call later */ 00158 void flagForWall3(); 00159 00160 /** displays menu of options that can be selected with joystick 00161 @param case 0 - displays the "Play Game" option 00162 @param case 1 - displays the "Settings" option 00163 @param case 2 - displays the "Highscore" option 00164 @returns a menu of options that can be selected to initiate other functions 00165 */ 00166 void menu(); 00167 00168 /** initiates game and detects collisions with walls 00169 @param i - x coordinate of player object 00170 @param j - y coordinate of player object 00171 @param m - value of specific case 00172 @param case 0 - displays YES option 00173 @param case 1 - displays restart question 00174 @param case 2 - displays NO option 00175 */ 00176 void playGame(); 00177 00178 /** uses UNIX time to set time and date of Real Time Clock 00179 */ 00180 void setTime(); 00181 00182 /** 00183 writes data to on board Flash memory. 00184 data such as the score and date (if it is better than the previous highscore) is printed to a text file. 00185 */ 00186 void writeDataToFile(); 00187 00188 /** menu for brightness of LCD and volume of buzzer 00189 @param t - value of specific case 00190 @param case 0 - brightness option 00191 @param case 1 - volume option 00192 @param case 2 - instructions option 00193 */ 00194 void BandVMenu(); 00195 00196 /** sets brightness of LCD 00197 @param bright - value of specific case 00198 @param case 0 - sets LCD backlight to 60% 00199 @param case 1 - sets LCD backlight to 80% 00200 @param case 2 - sets LCD backlight to 90% 00201 @param case 3 - sets LCD backlight to 100% 00202 @param case -1 - sets LCD backlight to 40% 00203 @param case -2 - sets LCD backlight to 20% 00204 @param case -3 - turns LCD backlight off 00205 */ 00206 void brightness(); 00207 00208 /** sets volume of buzzer 00209 @param vol - value of specific case 00210 @param case 0 - sets PWM duty ratio to buzzer to 0.6 00211 @param case 1 - sets PWM duty ratio to buzzer to 0.8 00212 @param case 2 - sets PWM duty ratio to buzzer to 0.9 00213 @param case 3 - sets PWM duty ratio to buzzer to 1.0 00214 @param case -1 - sets PWM duty ratio to buzzer to 0.4 00215 @param case -2 - sets PWM duty ratio to buzzer to 0.2 00216 @param case -3 - sets PWM duty ratio to buzzer to zero (MUTE) 00217 */ 00218 void volume(); 00219 00220 /** 00221 plays song using different frequencies of PWM through buzzer 00222 used to play gameplay music 00223 @param u - variable to represent each frequency in the array 00224 */ 00225 void tone(); 00226 00227 /** 00228 plays song using different frequencies of PWM through buzzer 00229 used to play a 'game over' tone 00230 @param l - variable to represent each frequency in the array 00231 */ 00232 void deadTone(); 00233 00234 /** reads data from the on board Flash memory. 00235 */ 00236 void readDataFromFile(); 00237 00238 /** checks current score against the highscore and decides whether to record it or not. 00239 this is done by scanning the text file saved to the on board Flash memory and then comparing it to the score that has been achieved after the current game is over. 00240 */ 00241 void scoreCheck(); 00242 00243 /** flag function for when the game is over */ 00244 void flagForFlash(); 00245 00246 /** displays "Game Over!" for 3 seconds 00247 uses a timer instead of wait() function to save power 00248 */ 00249 void GameOverFlash(); 00250 00251 /** Puts mbed into sleep mode and powers down the LCD if the game is left for more than 20 seconds. 00252 this saves power. it can be turned back on again by pushing the joystick button. 00253 */ 00254 void powerSave(); 00255 00256 /** puts mbed into sleep mode */ 00257 void Sleep(); 00258 00259 /** 00260 rules to prevent player object going off the screen 00261 */ 00262 void boundries(); 00263 00264 /** moves player object on LCD screen to new position. 00265 if joystick direction is a diagonal the player object moves one space in y direction and one space in x direction. 00266 i.e. upRight direction would cause the player object to move up first, then to the right. 00267 */ 00268 void playerMovement(); 00269 00270 /** sets different wall objects that move across the screen. 00271 each wall has one gap in it which the player must move through to survive. 00272 as the score increases, more walls are added to the screen making it more difficult to survive. 00273 */ 00274 void fallingWalls(); 00275 00276 /** 00277 gives instructions on how to navigate in menu 00278 */ 00279 void instructions(); 00280 00281 /** 00282 flag function for joystick button 00283 */ 00284 void buttonFlag(); 00285 00286 /* create enumerated type (0,1,2,3 etc. for direction) */ 00287 enum DirectionName { 00288 UP, 00289 DOWN, 00290 LEFT, 00291 upLeft, 00292 downLeft, 00293 RIGHT, 00294 upRight, 00295 downRight, 00296 CENTRE, 00297 UNKNOWN 00298 }; 00299 00300 00301 00302 typedef struct JoyStick Joystick ; /*!< struct for Joystick */ 00303 00304 struct JoyStick { 00305 float x ; /*!< current x value */ 00306 float x0; /*!< 'centred' x value */ 00307 float y ; /*!< current y value */ 00308 float y0; /*!< 'centred' y value */ 00309 int button; /*!< button state (assume pull-down used, so 1 = pressed, 0 = unpressed) */ 00310 DirectionName direction; /*!< current direction */ 00311 }; 00312 00313 00314 Joystick joystick ; /*!< create struct variable */ 00315 00316 00317 00318 00319
Generated on Sat Jul 30 2022 09:41:41 by
1.7.2