Snake and Physics based jumping game with high scores and different difficulties.
Dependencies: N5110 SDFileSystem mbed
main.h@3:02d9072a2507, 2016-04-25 (annotated)
- Committer:
- el14jw
- Date:
- Mon Apr 25 09:36:08 2016 +0000
- Revision:
- 3:02d9072a2507
- Parent:
- 2:80a91a737e17
- Child:
- 5:ae641b1d04fa
- Modularized Plink function using struct; - Written more deoxygen documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14jw | 0:23a749719479 | 1 | /** |
el14jw | 0:23a749719479 | 2 | @file main.h |
el14jw | 0:23a749719479 | 3 | |
el14jw | 0:23a749719479 | 4 | @brief Header file containing functions prototypes, defines and global variables. |
el14jw | 0:23a749719479 | 5 | @brief Revision 1.0. |
el14jw | 0:23a749719479 | 6 | @author Joel W. Webb |
el14jw | 0:23a749719479 | 7 | @date March 2016 |
el14jw | 0:23a749719479 | 8 | */ |
el14jw | 0:23a749719479 | 9 | |
el14jw | 0:23a749719479 | 10 | #ifndef MAIN_H |
el14jw | 0:23a749719479 | 11 | #define MAIN_H |
el14jw | 0:23a749719479 | 12 | |
el14jw | 0:23a749719479 | 13 | #define PI 3.14159265359 |
el14jw | 2:80a91a737e17 | 14 | #define DIRECTION_TOLERANCE 0.2 |
el14jw | 0:23a749719479 | 15 | |
el14jw | 0:23a749719479 | 16 | #include "mbed.h" |
el14jw | 0:23a749719479 | 17 | #include "N5110.h" |
el14jw | 1:c4928de1f922 | 18 | #include "SDFileSystem.h" |
el14jw | 0:23a749719479 | 19 | |
el14jw | 0:23a749719479 | 20 | |
el14jw | 0:23a749719479 | 21 | |
el14jw | 0:23a749719479 | 22 | // GPIO Objects |
el14jw | 0:23a749719479 | 23 | /* |
el14jw | 0:23a749719479 | 24 | @namespace lcd |
el14jw | 0:23a749719479 | 25 | @brief N5110 object for interfacing with Nokia 5110 LCD |
el14jw | 0:23a749719479 | 26 | @param VCC - PTE26 |
el14jw | 0:23a749719479 | 27 | @param SCE - PTA0 |
el14jw | 0:23a749719479 | 28 | @param RST - PTC4 |
el14jw | 0:23a749719479 | 29 | @param D/C - PTD0 |
el14jw | 0:23a749719479 | 30 | @param MOSI - PTD2 |
el14jw | 0:23a749719479 | 31 | @param SCLK - PTD1 |
el14jw | 0:23a749719479 | 32 | @param LED - PTC3 |
el14jw | 0:23a749719479 | 33 | */ |
el14jw | 0:23a749719479 | 34 | N5110 lcd(PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3); |
el14jw | 0:23a749719479 | 35 | |
el14jw | 1:c4928de1f922 | 36 | /* |
el14jw | 1:c4928de1f922 | 37 | @namespace sd |
el14jw | 1:c4928de1f922 | 38 | @param MOSI - PTE3 |
el14jw | 1:c4928de1f922 | 39 | @param MISO - PTE1 |
el14jw | 1:c4928de1f922 | 40 | @param SCK - PTE4 |
el14jw | 1:c4928de1f922 | 41 | @param CS - PTE4 |
el14jw | 1:c4928de1f922 | 42 | */ |
el14jw | 1:c4928de1f922 | 43 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
el14jw | 1:c4928de1f922 | 44 | |
el14jw | 0:23a749719479 | 45 | /** |
el14jw | 0:23a749719479 | 46 | @namespace errorLED |
el14jw | 0:23a749719479 | 47 | @brief GPIO output for error LED |
el14jw | 0:23a749719479 | 48 | */ |
el14jw | 0:23a749719479 | 49 | DigitalOut errorLED(LED1); |
el14jw | 0:23a749719479 | 50 | |
el14jw | 0:23a749719479 | 51 | /** |
el14jw | 0:23a749719479 | 52 | @namespace buzzer |
el14jw | 0:23a749719479 | 53 | @brief PWM output for buzzer |
el14jw | 0:23a749719479 | 54 | */ |
el14jw | 0:23a749719479 | 55 | PwmOut buzzer(PTC10); |
el14jw | 0:23a749719479 | 56 | |
el14jw | 0:23a749719479 | 57 | /** |
el14jw | 0:23a749719479 | 58 | @namespace xpot |
el14jw | 0:23a749719479 | 59 | @brief The joystick x axis potentiometer |
el14jw | 0:23a749719479 | 60 | */ |
el14jw | 0:23a749719479 | 61 | AnalogIn xPot(PTB11); |
el14jw | 0:23a749719479 | 62 | |
el14jw | 0:23a749719479 | 63 | /** |
el14jw | 0:23a749719479 | 64 | @namespace ypot |
el14jw | 0:23a749719479 | 65 | @brief The joystick y axis potentiometer |
el14jw | 0:23a749719479 | 66 | */ |
el14jw | 0:23a749719479 | 67 | AnalogIn yPot(PTB10); |
el14jw | 0:23a749719479 | 68 | |
el14jw | 0:23a749719479 | 69 | /** |
el14jw | 0:23a749719479 | 70 | @namespace buttonjoy |
el14jw | 0:23a749719479 | 71 | @brief Interrupt input for Joystick button |
el14jw | 0:23a749719479 | 72 | */ |
el14jw | 0:23a749719479 | 73 | InterruptIn buttonjoy(PTB9); |
el14jw | 0:23a749719479 | 74 | |
el14jw | 0:23a749719479 | 75 | /** |
el14jw | 0:23a749719479 | 76 | @namespace buttonA |
el14jw | 0:23a749719479 | 77 | @brief Interrupt input for Button A |
el14jw | 0:23a749719479 | 78 | */ |
el14jw | 1:c4928de1f922 | 79 | InterruptIn buttonA(PTB23); |
el14jw | 0:23a749719479 | 80 | |
el14jw | 0:23a749719479 | 81 | /** |
el14jw | 0:23a749719479 | 82 | @namespace buttonB |
el14jw | 0:23a749719479 | 83 | @brief Interrupt input for Button B |
el14jw | 0:23a749719479 | 84 | */ |
el14jw | 1:c4928de1f922 | 85 | InterruptIn buttonB(PTA1); |
el14jw | 0:23a749719479 | 86 | |
el14jw | 0:23a749719479 | 87 | |
el14jw | 0:23a749719479 | 88 | |
el14jw | 0:23a749719479 | 89 | //Data Structures |
el14jw | 0:23a749719479 | 90 | |
el14jw | 0:23a749719479 | 91 | /// stringList struct for dealing with printing strings to LCD easier |
el14jw | 0:23a749719479 | 92 | struct stringList { |
el14jw | 0:23a749719479 | 93 | char str[14]; |
el14jw | 0:23a749719479 | 94 | int offset; |
el14jw | 0:23a749719479 | 95 | }; |
el14jw | 0:23a749719479 | 96 | typedef stringList stringList; |
el14jw | 0:23a749719479 | 97 | |
el14jw | 0:23a749719479 | 98 | /// cell struct for dealing with coordinates |
el14jw | 0:23a749719479 | 99 | struct cell { |
el14jw | 0:23a749719479 | 100 | int x; |
el14jw | 0:23a749719479 | 101 | int y; |
el14jw | 0:23a749719479 | 102 | }; |
el14jw | 0:23a749719479 | 103 | typedef cell cell; |
el14jw | 0:23a749719479 | 104 | |
el14jw | 1:c4928de1f922 | 105 | /// vector struct for dealing with vectors |
el14jw | 1:c4928de1f922 | 106 | struct vector { |
el14jw | 1:c4928de1f922 | 107 | float x; |
el14jw | 1:c4928de1f922 | 108 | float y; |
el14jw | 1:c4928de1f922 | 109 | }; |
el14jw | 1:c4928de1f922 | 110 | typedef vector vector; |
el14jw | 1:c4928de1f922 | 111 | |
el14jw | 0:23a749719479 | 112 | |
el14jw | 3:02d9072a2507 | 113 | struct plinkvar { |
el14jw | 3:02d9072a2507 | 114 | int difficulty; |
el14jw | 3:02d9072a2507 | 115 | vector pos; |
el14jw | 3:02d9072a2507 | 116 | vector vel; |
el14jw | 3:02d9072a2507 | 117 | vector acc; |
el14jw | 3:02d9072a2507 | 118 | int height; |
el14jw | 3:02d9072a2507 | 119 | int gameOver; |
el14jw | 3:02d9072a2507 | 120 | int platformWidth; |
el14jw | 3:02d9072a2507 | 121 | cell powerUp; |
el14jw | 3:02d9072a2507 | 122 | int powerUpHeight; |
el14jw | 3:02d9072a2507 | 123 | int powerUpRadius; |
el14jw | 3:02d9072a2507 | 124 | int ballRadius; |
el14jw | 3:02d9072a2507 | 125 | cell platforms[20]; |
el14jw | 3:02d9072a2507 | 126 | float tickerDelay; |
el14jw | 3:02d9072a2507 | 127 | }; |
el14jw | 3:02d9072a2507 | 128 | typedef plinkvar plinkvar; |
el14jw | 3:02d9072a2507 | 129 | |
el14jw | 0:23a749719479 | 130 | |
el14jw | 0:23a749719479 | 131 | // Global variables |
el14jw | 0:23a749719479 | 132 | |
el14jw | 0:23a749719479 | 133 | volatile int g_buttonA_flag; /*!< Button A flag set in ISR */ |
el14jw | 0:23a749719479 | 134 | volatile int g_buttonB_flag; /*!< Button B flag set in ISR */ |
el14jw | 0:23a749719479 | 135 | volatile int g_buttonjoy_flag; /*!< Joystick Button flag set in ISR */ |
el14jw | 0:23a749719479 | 136 | volatile int g_joystick_flag; /*!< Joystick x and y values have been polled in ISR */ |
el14jw | 1:c4928de1f922 | 137 | volatile int g_gametick_flag; /*!< gametick flag is set in gametick_isr by Ticker gametick */ |
el14jw | 0:23a749719479 | 138 | |
el14jw | 0:23a749719479 | 139 | volatile const float* noteArray; /*!< Float pointer to next note in array. Used in sound Timeout ISR */ |
el14jw | 0:23a749719479 | 140 | |
el14jw | 0:23a749719479 | 141 | // Menu stringList arrays kept in FLASH |
el14jw | 1:c4928de1f922 | 142 | const stringList menuList[] = {{"Games",20},{"Snake",0},{"Plink",0}}; |
el14jw | 1:c4928de1f922 | 143 | const stringList snakeList[] = {{"Snake",20},{"Start",0},{"Difficulty",0},{"Highscore",0},{"Back",0}}; |
el14jw | 0:23a749719479 | 144 | const stringList difficultyList[] = {{"Difficulty",10},{"Easy",0},{"Medium",0},{"Hard",0}}; |
el14jw | 1:c4928de1f922 | 145 | const stringList plinkList[] = {{"Plink",20},{"Start",0},{"Difficulty",0},{"Highscore",0},{"Back",0}}; |
el14jw | 0:23a749719479 | 146 | |
el14jw | 0:23a749719479 | 147 | |
el14jw | 0:23a749719479 | 148 | |
el14jw | 0:23a749719479 | 149 | // Function Prototypes |
el14jw | 0:23a749719479 | 150 | |
el14jw | 0:23a749719479 | 151 | //ISR |
el14jw | 0:23a749719479 | 152 | /** |
el14jw | 0:23a749719479 | 153 | Interrupt service routine for button A |
el14jw | 0:23a749719479 | 154 | @brief Sets g_buttonA_flag |
el14jw | 0:23a749719479 | 155 | */ |
el14jw | 0:23a749719479 | 156 | void buttonA_isr(); |
el14jw | 0:23a749719479 | 157 | /** |
el14jw | 0:23a749719479 | 158 | Interrupt service routine for button B |
el14jw | 0:23a749719479 | 159 | @brief Sets g_buttonB_flag |
el14jw | 0:23a749719479 | 160 | */ |
el14jw | 0:23a749719479 | 161 | void buttonB_isr(); |
el14jw | 0:23a749719479 | 162 | /** |
el14jw | 0:23a749719479 | 163 | Interrupt service routine for buttonjoy |
el14jw | 0:23a749719479 | 164 | @brief Set g_buttonjoy_flag |
el14jw | 0:23a749719479 | 165 | */ |
el14jw | 0:23a749719479 | 166 | void buttonjoy_isr(); |
el14jw | 0:23a749719479 | 167 | |
el14jw | 0:23a749719479 | 168 | /** |
el14jw | 0:23a749719479 | 169 | Interrupt service routine for playing the current note |
el14jw | 0:23a749719479 | 170 | @brief Alters the current PWM signal to the current note frequency read from noteArray |
el14jw | 0:23a749719479 | 171 | */ |
el14jw | 0:23a749719479 | 172 | void sound_isr(); |
el14jw | 0:23a749719479 | 173 | |
el14jw | 0:23a749719479 | 174 | /** |
el14jw | 0:23a749719479 | 175 | Interrupt service routine for Ticker gametick |
el14jw | 0:23a749719479 | 176 | @brief Used to control the update speed of games |
el14jw | 0:23a749719479 | 177 | */ |
el14jw | 0:23a749719479 | 178 | void gametick_isr(); |
el14jw | 0:23a749719479 | 179 | |
el14jw | 0:23a749719479 | 180 | |
el14jw | 0:23a749719479 | 181 | // Snake functions |
el14jw | 0:23a749719479 | 182 | /** |
el14jw | 0:23a749719479 | 183 | Main Snake game function |
el14jw | 0:23a749719479 | 184 | @brief The snake function is called from the main loop and contains everything needed to play the snake game |
el14jw | 0:23a749719479 | 185 | @param difficulty - The difficulty is easy(0) medium(1) or hard(2) and it alters how fast the game ticker is applied |
el14jw | 0:23a749719479 | 186 | */ |
el14jw | 0:23a749719479 | 187 | void snake(int difficulty); |
el14jw | 1:c4928de1f922 | 188 | |
el14jw | 0:23a749719479 | 189 | |
el14jw | 1:c4928de1f922 | 190 | // Plink functions |
el14jw | 3:02d9072a2507 | 191 | /** |
el14jw | 3:02d9072a2507 | 192 | Main Plink game function |
el14jw | 3:02d9072a2507 | 193 | @brief The plink function calls its sub-functions to allow the user to play a physics based jumping game |
el14jw | 3:02d9072a2507 | 194 | @param difficulty - The difficulty is easy(0) medium(1) or hard(2) and it alters how long the platforms are |
el14jw | 3:02d9072a2507 | 195 | */ |
el14jw | 1:c4928de1f922 | 196 | void plink(int difficulty); |
el14jw | 3:02d9072a2507 | 197 | /** |
el14jw | 3:02d9072a2507 | 198 | Initialising plink variables function |
el14jw | 3:02d9072a2507 | 199 | @brief The initpvar function sets all of the intiial plink variables |
el14jw | 3:02d9072a2507 | 200 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 201 | */ |
el14jw | 3:02d9072a2507 | 202 | plinkvar initpvar(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 203 | /** |
el14jw | 3:02d9072a2507 | 204 | Scrolls the objects on the LCD display |
el14jw | 3:02d9072a2507 | 205 | @brief Handles when the ball exceeds a certain height and moves all objects up in Y value |
el14jw | 3:02d9072a2507 | 206 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 207 | */ |
el14jw | 3:02d9072a2507 | 208 | plinkvar plinkScroll(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 209 | /** |
el14jw | 3:02d9072a2507 | 210 | Platform Generation function |
el14jw | 3:02d9072a2507 | 211 | @brief By default only 20 platforms are saved at any one time so new platforms need to be generated when old ones are no longer needed |
el14jw | 3:02d9072a2507 | 212 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 213 | */ |
el14jw | 3:02d9072a2507 | 214 | plinkvar plinkPlatGen(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 215 | /** |
el14jw | 3:02d9072a2507 | 216 | Object collision handling function |
el14jw | 3:02d9072a2507 | 217 | @brief When objects such as the ball, platforms, walls and power ups collide this function detects it and performs the corresponding action |
el14jw | 3:02d9072a2507 | 218 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 219 | */ |
el14jw | 3:02d9072a2507 | 220 | plinkvar plinkCollisions(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 221 | /** |
el14jw | 3:02d9072a2507 | 222 | Plink Physics Engine function |
el14jw | 3:02d9072a2507 | 223 | @brief Handles all the physics related movements with newtons laws of motion every game iteration |
el14jw | 3:02d9072a2507 | 224 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 225 | */ |
el14jw | 3:02d9072a2507 | 226 | plinkvar plinkPhysicsEngine(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 227 | /** |
el14jw | 3:02d9072a2507 | 228 | LCD Screen drawing function |
el14jw | 3:02d9072a2507 | 229 | @brief This function takes all objects and draws them on the LCD display |
el14jw | 3:02d9072a2507 | 230 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 231 | */ |
el14jw | 3:02d9072a2507 | 232 | plinkvar plinkDrawScreen(plinkvar pvar); |
el14jw | 3:02d9072a2507 | 233 | /** |
el14jw | 3:02d9072a2507 | 234 | Game Over Function |
el14jw | 3:02d9072a2507 | 235 | @brief This function handles all the high score saving and printing to LCD display |
el14jw | 3:02d9072a2507 | 236 | @param pvar - Struct that contains all necessary plink variables |
el14jw | 3:02d9072a2507 | 237 | */ |
el14jw | 3:02d9072a2507 | 238 | plinkvar plinkGameOver(plinkvar pvar); |
el14jw | 0:23a749719479 | 239 | |
el14jw | 0:23a749719479 | 240 | |
el14jw | 0:23a749719479 | 241 | // Menu functions |
el14jw | 0:23a749719479 | 242 | /** |
el14jw | 0:23a749719479 | 243 | Menu screen Controller |
el14jw | 0:23a749719479 | 244 | @brief menu is a function that handles the selection of different menu selections |
el14jw | 3:02d9072a2507 | 245 | @param menuList - The pointer to the array of strings handled in the menu (Must be 6 elements or less) |
el14jw | 0:23a749719479 | 246 | @param line - The number of lines in the menu list (Must be 6 or less) |
el14jw | 0:23a749719479 | 247 | @returns Selected menu element (0-4) |
el14jw | 0:23a749719479 | 248 | */ |
el14jw | 0:23a749719479 | 249 | int menu(const stringList* menuList,int lines); |
el14jw | 0:23a749719479 | 250 | /** |
el14jw | 0:23a749719479 | 251 | The drawString function |
el14jw | 0:23a749719479 | 252 | @brief This function is used to simplify the menu function |
el14jw | 0:23a749719479 | 253 | @param stringList - a const string array containing the strings needed to be written on each line |
el14jw | 0:23a749719479 | 254 | @param lines - Integer representing how many strings should be written |
el14jw | 0:23a749719479 | 255 | */ |
el14jw | 0:23a749719479 | 256 | void drawStrings(const stringList* list,int lines); |
el14jw | 0:23a749719479 | 257 | |
el14jw | 0:23a749719479 | 258 | |
el14jw | 0:23a749719479 | 259 | // Sound and error functions |
el14jw | 0:23a749719479 | 260 | /** |
el14jw | 0:23a749719479 | 261 | Attaches Timeout to play sounds immediately |
el14jw | 0:23a749719479 | 262 | @brief playSound is a function to provide ease fo use when calling sound arrays |
el14jw | 0:23a749719479 | 263 | @param sound - The pointer to the noteArray |
el14jw | 0:23a749719479 | 264 | */ |
el14jw | 0:23a749719479 | 265 | void playSound(const float* sound); |
el14jw | 0:23a749719479 | 266 | /** |
el14jw | 0:23a749719479 | 267 | Error function |
el14jw | 0:23a749719479 | 268 | @brief Hangs while flashing errorLED |
el14jw | 0:23a749719479 | 269 | */ |
el14jw | 0:23a749719479 | 270 | void error(); |
el14jw | 0:23a749719479 | 271 | |
el14jw | 0:23a749719479 | 272 | |
el14jw | 0:23a749719479 | 273 | // Initialising and joystick |
el14jw | 0:23a749719479 | 274 | /** |
el14jw | 0:23a749719479 | 275 | Initializes Inputs |
el14jw | 0:23a749719479 | 276 | @brief Used to group code that will run only once at startup |
el14jw | 0:23a749719479 | 277 | */ |
el14jw | 0:23a749719479 | 278 | void initInputs(); |
el14jw | 0:23a749719479 | 279 | /** |
el14jw | 0:23a749719479 | 280 | Initializes the joystick |
el14jw | 0:23a749719479 | 281 | @brief Aquires xpot and ypot default values |
el14jw | 0:23a749719479 | 282 | */ |
el14jw | 0:23a749719479 | 283 | void calibrateJoystick(); |
el14jw | 0:23a749719479 | 284 | /** |
el14jw | 0:23a749719479 | 285 | updateJoystick |
el14jw | 0:23a749719479 | 286 | @brief Updates direction the joystick is pointing in on a Ticker event |
el14jw | 0:23a749719479 | 287 | */ |
el14jw | 0:23a749719479 | 288 | void updateJoystick(); |
el14jw | 0:23a749719479 | 289 | |
el14jw | 0:23a749719479 | 290 | |
el14jw | 0:23a749719479 | 291 | |
el14jw | 0:23a749719479 | 292 | #include "sounds.h" |
el14jw | 0:23a749719479 | 293 | |
el14jw | 0:23a749719479 | 294 | #endif |
el14jw | 0:23a749719479 | 295 |