ELEC2645 Joseph Allison 200860349

Dependencies:   N5110 Tetris mbed

Committer:
el14ja
Date:
Thu May 05 10:41:45 2016 +0000
Revision:
7:dfb290c7958c
Parent:
6:9cb8171ca6df
submitted 1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14ja 1:2b9ba34131ca 1 /**
el14ja 1:2b9ba34131ca 2 @file main.h
el14ja 7:dfb290c7958c 3 @breif Version 1.2
el14ja 1:2b9ba34131ca 4 @author Joseph Allison
el14ja 1:2b9ba34131ca 5 @date 27/3/2016
el14ja 1:2b9ba34131ca 6 */
el14ja 1:2b9ba34131ca 7
el14ja 3:8c5362716f4c 8 #ifndef MAIN_H
el14ja 3:8c5362716f4c 9 #define MAIN_H
el14ja 3:8c5362716f4c 10
el14ja 1:2b9ba34131ca 11 #include "mbed.h"
el14ja 3:8c5362716f4c 12
el14ja 3:8c5362716f4c 13 #include "time.h"
el14ja 1:2b9ba34131ca 14 #include "N5110.h"
el14ja 3:8c5362716f4c 15
el14ja 1:2b9ba34131ca 16 #include "Tetris.h"
el14ja 1:2b9ba34131ca 17
el14ja 1:2b9ba34131ca 18 #define E 3034
el14ja 1:2b9ba34131ca 19 #define GSharp 2408
el14ja 1:2b9ba34131ca 20 #define G 2551
el14ja 1:2b9ba34131ca 21 #define A 2273
el14ja 1:2b9ba34131ca 22 #define B 2025
el14ja 1:2b9ba34131ca 23 #define C 1911
el14ja 1:2b9ba34131ca 24 #define HD 1703
el14ja 1:2b9ba34131ca 25 #define HE 1517
el14ja 1:2b9ba34131ca 26 #define HF 1431
el14ja 1:2b9ba34131ca 27 #define HG 1276
el14ja 1:2b9ba34131ca 28 #define HA 1136
el14ja 1:2b9ba34131ca 29
el14ja 1:2b9ba34131ca 30 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
el14ja 1:2b9ba34131ca 31 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
el14ja 2:61caca4dd3f0 32
el14ja 1:2b9ba34131ca 33 /** Creates Tertis instance*/
el14ja 2:61caca4dd3f0 34 Tetris tetris;
el14ja 1:2b9ba34131ca 35
el14ja 1:2b9ba34131ca 36 /** Creates Ticker instance*/
el14ja 1:2b9ba34131ca 37 Ticker ticker;
el14ja 1:2b9ba34131ca 38
el14ja 1:2b9ba34131ca 39 /** Creates music ticker instace*/
el14ja 1:2b9ba34131ca 40 Ticker musicticker;
el14ja 1:2b9ba34131ca 41
el14ja 3:8c5362716f4c 42 /** Creates Timeout instances that is used to stop the left button double clicks */
el14ja 1:2b9ba34131ca 43 Timeout lefttimeout;
el14ja 3:8c5362716f4c 44 /** Creates Timeout instances that is used to stop the right button double clicks */
el14ja 1:2b9ba34131ca 45 Timeout righttimeout;
el14ja 3:8c5362716f4c 46 /** Creates Timeout instances that is used to stop the spin button double clicks */
el14ja 1:2b9ba34131ca 47 Timeout spintimeout;
el14ja 3:8c5362716f4c 48 /** Creates Timeout instances that is used to stop the down button double clicks */
el14ja 1:2b9ba34131ca 49 Timeout downtimeout;
el14ja 4:7160c74529f7 50 /** Creates Timeout instances that is ised for the pause menu */
el14ja 4:7160c74529f7 51 Timeout pausemenu;
el14ja 1:2b9ba34131ca 52
el14ja 1:2b9ba34131ca 53
el14ja 1:2b9ba34131ca 54 /**
el14ja 1:2b9ba34131ca 55 @namespace ain
el14ja 1:2b9ba34131ca 56 @breif Analog input that is used for the seed for the randomiser
el14ja 1:2b9ba34131ca 57 */
el14ja 1:2b9ba34131ca 58 AnalogIn ain(A0);
el14ja 1:2b9ba34131ca 59
el14ja 1:2b9ba34131ca 60 /**
el14ja 1:2b9ba34131ca 61 @namespace buzzer
el14ja 1:2b9ba34131ca 62 @brief Buzzer output to play tetris music
el14ja 1:2b9ba34131ca 63 */
el14ja 4:7160c74529f7 64 PwmOut buzzer(PTA2);
el14ja 1:2b9ba34131ca 65
el14ja 1:2b9ba34131ca 66
el14ja 1:2b9ba34131ca 67 /**
el14ja 3:8c5362716f4c 68 @namespace left
el14ja 3:8c5362716f4c 69 @brief The interrupt input of the left button
el14ja 3:8c5362716f4c 70 */
el14ja 4:7160c74529f7 71 InterruptIn left(PTC5); //PTC7
el14ja 3:8c5362716f4c 72 /**
el14ja 3:8c5362716f4c 73 @namespace right
el14ja 3:8c5362716f4c 74 @brief The interrupt input of the right button
el14ja 1:2b9ba34131ca 75 */
el14ja 3:8c5362716f4c 76 InterruptIn right(PTC9);
el14ja 1:2b9ba34131ca 77 /**
el14ja 3:8c5362716f4c 78 @namespace spin
el14ja 3:8c5362716f4c 79 @brief The interrupt input of the spin button
el14ja 1:2b9ba34131ca 80 */
el14ja 4:7160c74529f7 81 InterruptIn spin(PTC7);
el14ja 3:8c5362716f4c 82 /**
el14ja 3:8c5362716f4c 83 @namespace down
el14ja 3:8c5362716f4c 84 @brief The interrupt input of the down button
el14ja 3:8c5362716f4c 85 */
el14ja 4:7160c74529f7 86 InterruptIn down(PTC0); //PTC5
el14ja 4:7160c74529f7 87
el14ja 4:7160c74529f7 88 /**
el14ja 7:dfb290c7958c 89 Displays the splash Screen
el14ja 7:dfb290c7958c 90 */
el14ja 7:dfb290c7958c 91 void startScreen();
el14ja 7:dfb290c7958c 92
el14ja 7:dfb290c7958c 93 /**
el14ja 4:7160c74529f7 94 Sets up the mode and the rise settings of the button interrupts
el14ja 4:7160c74529f7 95 */
el14ja 6:9cb8171ca6df 96 void setupInterruptInputs();
el14ja 1:2b9ba34131ca 97
el14ja 1:2b9ba34131ca 98 /**
el14ja 3:8c5362716f4c 99 Creates the game menu and wait for user input
el14ja 1:2b9ba34131ca 100 */
el14ja 3:8c5362716f4c 101 void gameMenu();
el14ja 3:8c5362716f4c 102 /**
el14ja 4:7160c74529f7 103 The screen that appears when the game is over and tells the user their score
el14ja 4:7160c74529f7 104 */
el14ja 4:7160c74529f7 105 void gameOverScreen();
el14ja 4:7160c74529f7 106 /**
el14ja 3:8c5362716f4c 107 Checks if any of the flags are one
el14ja 3:8c5362716f4c 108 */
el14ja 3:8c5362716f4c 109 void checkGameFlags();
el14ja 1:2b9ba34131ca 110 /**
el14ja 3:8c5362716f4c 111 Displays the help screen text and waits for user input
el14ja 1:2b9ba34131ca 112 */
el14ja 3:8c5362716f4c 113 void helpScreen();
el14ja 1:2b9ba34131ca 114 /**
el14ja 3:8c5362716f4c 115 Sets the gamearea array to its initial state
el14ja 1:2b9ba34131ca 116 */
el14ja 3:8c5362716f4c 117 void setGameAreaArray();
el14ja 1:2b9ba34131ca 118 /**
el14ja 1:2b9ba34131ca 119 Randomly generates new bag of 7
el14ja 1:2b9ba34131ca 120 */
el14ja 1:2b9ba34131ca 121 void newShapeBag();
el14ja 3:8c5362716f4c 122 /**
el14ja 3:8c5362716f4c 123 Moves the piece in the game area
el14ja 3:8c5362716f4c 124 @param x - How far the piece is moved horizontally
el14ja 3:8c5362716f4c 125 @param y - How far the piece is moved vertically
el14ja 3:8c5362716f4c 126 @param spin - How far the piece is spun round
el14ja 3:8c5362716f4c 127 */
el14ja 2:61caca4dd3f0 128 void movePiece(int x, int y,int spin);
el14ja 3:8c5362716f4c 129 /**
el14ja 3:8c5362716f4c 130 Moves the piece right
el14ja 3:8c5362716f4c 131 */
el14ja 3:8c5362716f4c 132 void movePieceRight();
el14ja 3:8c5362716f4c 133 /**
el14ja 3:8c5362716f4c 134 Moves the piece left
el14ja 3:8c5362716f4c 135 */
el14ja 3:8c5362716f4c 136 void movePieceLeft();
el14ja 3:8c5362716f4c 137 /**
el14ja 3:8c5362716f4c 138 Spins the piece clockwise
el14ja 3:8c5362716f4c 139 */
el14ja 3:8c5362716f4c 140 void spinPiece();
el14ja 3:8c5362716f4c 141 /**
el14ja 3:8c5362716f4c 142 Moves the piece downwards
el14ja 3:8c5362716f4c 143 */
el14ja 3:8c5362716f4c 144 void movePieceDown();
el14ja 3:8c5362716f4c 145
el14ja 3:8c5362716f4c 146 /**
el14ja 3:8c5362716f4c 147 Returns turn if piece placement is valid
el14ja 3:8c5362716f4c 148 @param xpos - The horizontal position of where to piece is moving to
el14ja 3:8c5362716f4c 149 @param ypos - The vertical position of where to piece is moving to
el14ja 3:8c5362716f4c 150 @param orientation - The orientation the piece will be in when it moves
el14ja 3:8c5362716f4c 151 */
el14ja 3:8c5362716f4c 152 bool movePossible(int xpos, int ypos, int orientation); //returns turn if piece placement is valid
el14ja 3:8c5362716f4c 153
el14ja 3:8c5362716f4c 154 /**
el14ja 3:8c5362716f4c 155 Places the next piece in the game area
el14ja 3:8c5362716f4c 156 */
el14ja 3:8c5362716f4c 157 void newpiece();
el14ja 1:2b9ba34131ca 158
el14ja 1:2b9ba34131ca 159 /**
el14ja 3:8c5362716f4c 160 Adds the placed piece to the game area array
el14ja 1:2b9ba34131ca 161 */
el14ja 1:2b9ba34131ca 162 void pieceToGameArray();
el14ja 3:8c5362716f4c 163 /**
el14ja 3:8c5362716f4c 164 Checks each row of the game area to see if there are any rows complete
el14ja 3:8c5362716f4c 165 */
el14ja 1:2b9ba34131ca 166 void checkCompleteLine();
el14ja 3:8c5362716f4c 167 /**
el14ja 3:8c5362716f4c 168 Removes any completed rows
el14ja 3:8c5362716f4c 169 @param rowscomplete[4] - An array with of upto 4 row numbers
el14ja 3:8c5362716f4c 170 */
el14ja 1:2b9ba34131ca 171 void removeCompleteLines(int rowscomplete[4]);
el14ja 3:8c5362716f4c 172 /**
el14ja 3:8c5362716f4c 173 Sets the game area from the game area array
el14ja 3:8c5362716f4c 174 */
el14ja 3:8c5362716f4c 175 void setGameArea();
el14ja 3:8c5362716f4c 176 /**
el14ja 3:8c5362716f4c 177 Resets the Ticker to chance the speed at which a piece falls
el14ja 3:8c5362716f4c 178 */
el14ja 1:2b9ba34131ca 179 void setTicker(float time);
el14ja 3:8c5362716f4c 180 /**
el14ja 3:8c5362716f4c 181 Fucntions called by the ticker to set g_timer_flag = 1
el14ja 3:8c5362716f4c 182 */
el14ja 3:8c5362716f4c 183 void moveDownTicker();
el14ja 3:8c5362716f4c 184 /**
el14ja 3:8c5362716f4c 185 Fucntions called by the ticker to set g_left = 1
el14ja 3:8c5362716f4c 186 */
el14ja 3:8c5362716f4c 187 void left_isr();
el14ja 3:8c5362716f4c 188 /**
el14ja 3:8c5362716f4c 189 Fucntions called by the ticker to set g_right = 1
el14ja 3:8c5362716f4c 190 */
el14ja 3:8c5362716f4c 191 void right_isr();
el14ja 3:8c5362716f4c 192 /**
el14ja 3:8c5362716f4c 193 Fucntions called by the ticker to set g_spin = 1
el14ja 3:8c5362716f4c 194 */
el14ja 3:8c5362716f4c 195 void spin_isr();
el14ja 3:8c5362716f4c 196 /**
el14ja 3:8c5362716f4c 197 Fucntions called by the ticker to set g_down = 1
el14ja 3:8c5362716f4c 198 */
el14ja 3:8c5362716f4c 199 void down_isr();
el14ja 3:8c5362716f4c 200 /**
el14ja 3:8c5362716f4c 201 Fucntions called by the ticker to set g_music = 1
el14ja 3:8c5362716f4c 202 */
el14ja 3:8c5362716f4c 203 void music_isr();
el14ja 3:8c5362716f4c 204
el14ja 3:8c5362716f4c 205
el14ja 3:8c5362716f4c 206 /**
el14ja 3:8c5362716f4c 207 Fucntions called by the timeout to set g_lefttimeout = 1
el14ja 3:8c5362716f4c 208 */
el14ja 3:8c5362716f4c 209 void lefttimeout_isr();
el14ja 3:8c5362716f4c 210 /**
el14ja 3:8c5362716f4c 211 Fucntions called by the timeout to set g_righttimeout = 1
el14ja 3:8c5362716f4c 212 */
el14ja 3:8c5362716f4c 213 void righttimeout_isr();
el14ja 3:8c5362716f4c 214 /**
el14ja 3:8c5362716f4c 215 Fucntions called by the timeout to set g_spintimeout = 1
el14ja 3:8c5362716f4c 216 */
el14ja 3:8c5362716f4c 217 void spintimeout_isr();
el14ja 3:8c5362716f4c 218 /**
el14ja 3:8c5362716f4c 219 Fucntions called by the timeout to set g_downtimeout = 1
el14ja 3:8c5362716f4c 220 */
el14ja 3:8c5362716f4c 221 void downtimeout_isr();
el14ja 1:2b9ba34131ca 222
el14ja 4:7160c74529f7 223 /**
el14ja 4:7160c74529f7 224 Function called by the timeout to ser g_pausemenu = 1;
el14ja 4:7160c74529f7 225 */
el14ja 4:7160c74529f7 226 void pausemenu_isr();
el14ja 4:7160c74529f7 227
el14ja 1:2b9ba34131ca 228
el14ja 1:2b9ba34131ca 229 /**
el14ja 3:8c5362716f4c 230 True if the game is running
el14ja 3:8c5362716f4c 231 */
el14ja 3:8c5362716f4c 232 bool gameplaying = true;
el14ja 3:8c5362716f4c 233 /**
el14ja 3:8c5362716f4c 234 True if the game menu is being displayed
el14ja 3:8c5362716f4c 235 */
el14ja 3:8c5362716f4c 236 bool gamemenu = true;
el14ja 3:8c5362716f4c 237
el14ja 3:8c5362716f4c 238 /**
el14ja 3:8c5362716f4c 239 True if the game music is playing
el14ja 3:8c5362716f4c 240 */
el14ja 4:7160c74529f7 241 bool musicplaying = false;
el14ja 3:8c5362716f4c 242 /**
el14ja 3:8c5362716f4c 243 Counts through the notes in the array
el14ja 1:2b9ba34131ca 244 */
el14ja 3:8c5362716f4c 245 int musiccounter = 0;
el14ja 3:8c5362716f4c 246 /**
el14ja 3:8c5362716f4c 247 Stores the next 7 shapes to be used - 7 bag system
el14ja 3:8c5362716f4c 248 */
el14ja 3:8c5362716f4c 249 int nextshape[7];
el14ja 3:8c5362716f4c 250 /**
el14ja 3:8c5362716f4c 251 Stores the players current score
el14ja 3:8c5362716f4c 252 */
el14ja 3:8c5362716f4c 253 int score = 0;
el14ja 3:8c5362716f4c 254 /**
el14ja 3:8c5362716f4c 255 The current level the user is on
el14ja 3:8c5362716f4c 256 */
el14ja 3:8c5362716f4c 257 int level = 1;
el14ja 3:8c5362716f4c 258 /**
el14ja 3:8c5362716f4c 259 The number of lines the user has completed
el14ja 3:8c5362716f4c 260 */
el14ja 3:8c5362716f4c 261 int completedlines = 0;
el14ja 3:8c5362716f4c 262 /**
el14ja 3:8c5362716f4c 263 The time between each downward movement of the piece
el14ja 3:8c5362716f4c 264 */
el14ja 3:8c5362716f4c 265 double gamespeed = 0.8; //how often a block moves down
el14ja 1:2b9ba34131ca 266
el14ja 3:8c5362716f4c 267 /**
el14ja 3:8c5362716f4c 268 Stores the postion of the piece (x,y)
el14ja 3:8c5362716f4c 269 */
el14ja 3:8c5362716f4c 270 int pieceposition[2] = {4,0}; //set position of the current dropping piece ***
el14ja 3:8c5362716f4c 271 /**
el14ja 3:8c5362716f4c 272 The orientation of the piece
el14ja 3:8c5362716f4c 273 */
el14ja 3:8c5362716f4c 274 int orientation = 0;
el14ja 3:8c5362716f4c 275 /**
el14ja 3:8c5362716f4c 276 The current piece on the screen
el14ja 3:8c5362716f4c 277 */
el14ja 3:8c5362716f4c 278 int currentshape = 3;
el14ja 3:8c5362716f4c 279 /**
el14ja 3:8c5362716f4c 280 The next piece to be played on the screen
el14ja 3:8c5362716f4c 281 */
el14ja 3:8c5362716f4c 282 int nextpiece;
el14ja 3:8c5362716f4c 283 /**
el14ja 3:8c5362716f4c 284 Counts through the shape in the 7 system bag of shapes
el14ja 3:8c5362716f4c 285 */
el14ja 3:8c5362716f4c 286 int shapebagcounter = 0;
el14ja 1:2b9ba34131ca 287
el14ja 3:8c5362716f4c 288 /**
el14ja 3:8c5362716f4c 289 Stores all the shapes I,J,L,O,T,S,Z all stored as Hex values
el14ja 3:8c5362716f4c 290 */
el14ja 4:7160c74529f7 291 int shapes[7][4] = {{0x00F0,0x4444,0x0F00,0x2222},{0x0071,0x0226,0x0470,0x0322},{0x0074,0x0622,0x0170,0x0223},{0x0033,0x0033,0x0033,0x0033},
el14ja 4:7160c74529f7 292 {0x0072,0x0262,0x0270,0x0232},{0x0036,0x0462,0x0360,0x0231},{0x0063,0x0264,0x0630,0x0132}};
el14ja 3:8c5362716f4c 293 /**
el14ja 3:8c5362716f4c 294 Stores the current state of the game area. Used for checking collison
el14ja 3:8c5362716f4c 295 */
el14ja 3:8c5362716f4c 296 int gamearea[12][16];
el14ja 3:8c5362716f4c 297
el14ja 3:8c5362716f4c 298 /**
el14ja 3:8c5362716f4c 299 Stores the notes used to play the music
el14ja 3:8c5362716f4c 300 */
el14ja 3:8c5362716f4c 301 int musisnotes[64] = {E,E,E,GSharp,B,B,G,E,A,A,A,C,HE,HE,HD,C,B,B,B,C,HD,HD,HE,HE,C,C,A,A,A,A,A,A,
el14ja 3:8c5362716f4c 302 HF,HF,HF,HG,HA,HA,HG,HF,HE,HE,HE,HF,HE,HE,HD,C,B,B,B,C,HD,HD,HE,HE,C,C,A,A,A,A,A,A};
el14ja 3:8c5362716f4c 303
el14ja 3:8c5362716f4c 304
el14ja 3:8c5362716f4c 305 /**
el14ja 3:8c5362716f4c 306 Flag for the ticker
el14ja 3:8c5362716f4c 307 */
el14ja 1:2b9ba34131ca 308 volatile int g_timer_flag = 0;
el14ja 3:8c5362716f4c 309 /**
el14ja 3:8c5362716f4c 310 Flag for the left inperrupt
el14ja 3:8c5362716f4c 311 */
el14ja 1:2b9ba34131ca 312 volatile int g_left = 0;
el14ja 3:8c5362716f4c 313 /**
el14ja 3:8c5362716f4c 314 Flag for the right inperrupt
el14ja 3:8c5362716f4c 315 */
el14ja 1:2b9ba34131ca 316 volatile int g_right = 0;
el14ja 3:8c5362716f4c 317 /**
el14ja 3:8c5362716f4c 318 Flag for the spin inperrupt
el14ja 3:8c5362716f4c 319 */
el14ja 1:2b9ba34131ca 320 volatile int g_spin = 0;
el14ja 3:8c5362716f4c 321 /**
el14ja 3:8c5362716f4c 322 Flag for the down inperrupt
el14ja 3:8c5362716f4c 323 */
el14ja 1:2b9ba34131ca 324 volatile int g_down = 0;
el14ja 1:2b9ba34131ca 325
el14ja 3:8c5362716f4c 326 /**Flag for the music ticker
el14ja 3:8c5362716f4c 327 */
el14ja 1:2b9ba34131ca 328 volatile int g_music = 0;
el14ja 1:2b9ba34131ca 329
el14ja 3:8c5362716f4c 330 /**
el14ja 3:8c5362716f4c 331 Flag used to stop the left interrupt being called twice unintentionally
el14ja 3:8c5362716f4c 332 */
el14ja 1:2b9ba34131ca 333 volatile int g_lefttimeout = 1;
el14ja 3:8c5362716f4c 334 /**
el14ja 3:8c5362716f4c 335 Flag used to stop the right interrupt being called twice unintentionally
el14ja 3:8c5362716f4c 336 */
el14ja 1:2b9ba34131ca 337 volatile int g_righttimeout = 1;
el14ja 3:8c5362716f4c 338 /**
el14ja 3:8c5362716f4c 339 Flag used to stop the spin interrupt being called twice unintentionally
el14ja 3:8c5362716f4c 340 */
el14ja 1:2b9ba34131ca 341 volatile int g_spintimeout = 1;
el14ja 3:8c5362716f4c 342 /**
el14ja 3:8c5362716f4c 343 Flag used to stop the drop interrupt being called twice unintentionally
el14ja 3:8c5362716f4c 344 */
el14ja 1:2b9ba34131ca 345 volatile int g_downtimeout = 1;
el14ja 1:2b9ba34131ca 346
el14ja 3:8c5362716f4c 347 /**
el14ja 4:7160c74529f7 348 Flag used to reset the pause menu flags
el14ja 4:7160c74529f7 349 */
el14ja 4:7160c74529f7 350 volatile int g_pausemenu = 0;
el14ja 4:7160c74529f7 351
el14ja 4:7160c74529f7 352 /**
el14ja 3:8c5362716f4c 353 Flag used when a new peice needs placing at the top of the screen
el14ja 3:8c5362716f4c 354 */
el14ja 1:2b9ba34131ca 355 volatile int g_newpiece = 1;
el14ja 3:8c5362716f4c 356 /**
el14ja 3:8c5362716f4c 357 Flag used when a piece is being hard dropped
el14ja 3:8c5362716f4c 358 */
el14ja 2:61caca4dd3f0 359 volatile int harddropping = 0;
el14ja 1:2b9ba34131ca 360
el14ja 4:7160c74529f7 361 /**
el14ja 4:7160c74529f7 362 Flag used to start pause menu
el14ja 4:7160c74529f7 363 */
el14ja 4:7160c74529f7 364 volatile int pauseleft = 0;
el14ja 1:2b9ba34131ca 365
el14ja 4:7160c74529f7 366 /**
el14ja 4:7160c74529f7 367 Flag used to start pause menu
el14ja 4:7160c74529f7 368 */
el14ja 3:8c5362716f4c 369
el14ja 4:7160c74529f7 370 volatile int pauseright = 0;
el14ja 3:8c5362716f4c 371
el14ja 3:8c5362716f4c 372 #endif