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.
Dependencies: N5110 PinDetect PowerControl mbed
Fork of DocTest by
main.h
00001 /** 00002 @file main.h 00003 @brief Header file containing functions prototypes, defines and global variables. 00004 @brief Game of Snake 00005 @brief Revision 1.0. 00006 @author Michael Birney 00007 @date April 2015 00008 */ 00009 00010 #include <vector> 00011 #include "mbed.h" 00012 #include "N5110.h" 00013 #include "PinDetect.h" 00014 #include "PowerControl/PowerControl.h" 00015 #include "PowerControl/EthernetPowerControl.h" 00016 #define DIRECTION_TOLERANCE 0.1 00017 00018 #define USR_POWERDOWN (0x104) 00019 int semihost_powerdown(){ 00020 00021 uint32_t arg; 00022 return __semihost(USR_POWERDOWN,& arg); 00023 } 00024 00025 AnalogIn pot(p20); 00026 PwmOut Buzzer(p21); 00027 00028 // VCC,SCE,RST,D/C,MOSI,SCLK,LED 00029 00030 /** 00031 @namespace lcd 00032 @brief connections lcd mosi connections for nokia N5110 00033 */ 00034 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); 00035 00036 /** 00037 @namespace myleds 00038 @brief busout to onboard mbed LEDS for debug 00039 */ 00040 00041 BusOut myleds(LED1,LED2,LED3,LED4); 00042 00043 /** 00044 @namespace leds 00045 @brief bus output for game indicator leds 00046 */ 00047 BusOut leds(p22,p23,p24); 00048 00049 /** 00050 @namespace startButton 00051 @PinDetect for start button ISR 00052 */ 00053 PinDetect startButton(p5); 00054 /** 00055 @namespace resetButton 00056 @Interrupt in for reset button ISR 00057 */ 00058 InterruptIn resetButton(p6); 00059 00060 int score =5; /*!< integer to store user score */ 00061 00062 /** 00063 @namespace startGame 00064 @brief Ticker to set updateGameFlag 00065 */ 00066 Ticker startGame; 00067 00068 00069 char buffer [14]; /*!< stores data to print to screen */ 00070 int length = sprintf(buffer ,"%2d",score ); // print formatted data to buffer 00071 // it is important the format specifier ensures the length will fit in the buffer 00072 // connections for joystick 00073 00074 /** 00075 @namespace button 00076 @brief Joysitck button GPIO 00077 */ 00078 PinDetect button(p17); 00079 /** 00080 @namespace xPot 00081 @brief joystick x pot GPIO 00082 */ 00083 AnalogIn xPot(p15); 00084 /** 00085 @namespace yPot 00086 @brief joystick y pot GPIO 00087 */ 00088 AnalogIn yPot(p16); 00089 //for debug 00090 00091 /** 00092 @namespace serial 00093 @brief Print to terminal for debug 00094 */ 00095 Serial serial(USBTX,USBRX); 00096 00097 //forGame 00098 00099 bool gamePaused =0; /*!< bool to store paused state */ 00100 vector<int> snakeX (5); /*!<vector to store all x coordinates of snake */ 00101 vector<int> snakeY (5,27);/*!< vector to store all y coordinates of snake */ 00102 int foodX [1];/*!< int to store x value of food */ 00103 int foodY [1];/*!< int to store y value of food */ 00104 int gameState =0;/*!<indicator for various game states */ 00105 00106 00107 float gameSpeed ;/*!< changes depending on difficulty selected */ 00108 int updateGameFlag =0; /*!< ISR to update game */ 00109 00110 00111 // GAME MENU 00112 00113 /** 00114 Displays a Snake Logo Splash Screen 00115 */ 00116 void displaySplash(); 00117 00118 /** 00119 Reads default positions of the joystick to calibrate later readings 00120 */ 00121 void calibrateJoystick(); 00122 00123 /** 00124 Reads X and Y pots and determines what direction joystick is in 00125 */ 00126 void updateJoystick(); 00127 00128 /** 00129 Displays easy selected on display and updates gameSpeed variable 00130 */ 00131 void easySelected(); 00132 /** 00133 Displays Medium selected on display and updates gameSpeed variable 00134 */ 00135 void mediumSelected(); 00136 /** 00137 Displays Hard selected on display and updates gameSpeed variable 00138 */ 00139 void hardSelected() ; 00140 00141 /** 00142 Displays classicMode selected on display and updates gameMode variable 00143 */ 00144 void classicModeSelected(); 00145 00146 /** 00147 Displays BoundaryMode selected on display and updates gameMode variable 00148 */ 00149 void boundaryModeSelected(); 00150 00151 /** 00152 Uses joystick direction to determine what difficulty too update to 00153 */ 00154 void checkSelectedGameMode(); 00155 00156 /** 00157 Uses joystick direction to determine what game mode too update to 00158 */ 00159 void checkSelectedDifficulty(); 00160 00161 00162 //GAMEPLAY 00163 00164 00165 /** 00166 resizes the vector back to 5 incase game is reset 00167 prints to screen using coordinates stored in the snakeX and snakeY vectors 00168 drawRect used for 2x2 snake 00169 */ 00170 void startingSnake(); 00171 00172 00173 /** 00174 Funtion to generate random food 00175 */ 00176 void randomiseFood(); 00177 00178 00179 /** 00180 prints a solid boundary around screen for use in BOUNDARY mode 00181 */ 00182 void hardBoundary(); 00183 00184 /** 00185 Prints a dotted boundary around the screen for use in CLASSIC mode 00186 */ 00187 void classicBoundary(); 00188 00189 00190 /** 00191 Uses joystick direction and previous joysitck direction to determine new direction of snake 00192 Clears Tail of Snake 00193 Elements in SnakeX and Y are shifted along one to the right 00194 New snake head cordinates are added to last element of vector depending on direction 00195 */ 00196 void updateSnakeVector(); 00197 00198 /** 00199 triggers Buzzer to play when gameover 00200 */ 00201 void gameOverTune(); 00202 00203 /** 00204 called when game is over. 00205 displays final score for user 00206 */ 00207 void gameOver(); 00208 00209 00210 /** 00211 called in boundary mode 00212 everytime snake updates checks for a collision with boundary 00213 */ 00214 void checkForCollision(); 00215 00216 00217 /** 00218 Everytime snake is updated 00219 compares coordinates of snake to the food to see if they match 00220 */ 00221 void checkForFood(); 00222 00223 00224 /** 00225 function called when pindetect startButton is triggered 00226 */ 00227 void startButtonPressed(); 00228 00229 00230 /** 00231 Reads from brightness potentiometer to update lcd brightness 00232 */ 00233 void updateBrightness(); 00234 00235 00236 /** 00237 function called by updateGame timer 00238 */ 00239 void updateGameISR(); 00240 00241 00242 /** 00243 For debug. Prints content of snakeX and snakeY vectors 00244 */ 00245 void printVectorContent(); 00246 00247 /** 00248 function called when interupt occurs on joystick button 00249 */ 00250 void pauseButtonPressed(); 00251 00252 00253 /** 00254 Compares all elements in snake vectors against head of snake 00255 @param i - integer signifying element in snake vector 00256 */ 00257 void checkForCollisionWithSelf(int i); 00258 00259 00260
Generated on Thu Jul 14 2022 02:03:08 by
1.7.2
