ELEC2645 Joseph Allison 200860349
Dependencies: N5110 Tetris mbed
main.h@1:2b9ba34131ca, 2016-04-07 (annotated)
- Committer:
- el14ja
- Date:
- Thu Apr 07 10:38:32 2016 +0000
- Revision:
- 1:2b9ba34131ca
- Child:
- 2:61caca4dd3f0
Still creating
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14ja | 1:2b9ba34131ca | 1 | /** |
el14ja | 1:2b9ba34131ca | 2 | @file main.h |
el14ja | 1:2b9ba34131ca | 3 | @breif |
el14ja | 1:2b9ba34131ca | 4 | @author Joseph Allison |
el14ja | 1:2b9ba34131ca | 5 | @date 27/3/2016 |
el14ja | 1:2b9ba34131ca | 6 | */ |
el14ja | 1:2b9ba34131ca | 7 | |
el14ja | 1:2b9ba34131ca | 8 | #include "mbed.h" |
el14ja | 1:2b9ba34131ca | 9 | #include "N5110.h" |
el14ja | 1:2b9ba34131ca | 10 | #include "time.h" |
el14ja | 1:2b9ba34131ca | 11 | #include "Tetris.h" |
el14ja | 1:2b9ba34131ca | 12 | |
el14ja | 1:2b9ba34131ca | 13 | #define E 3034 |
el14ja | 1:2b9ba34131ca | 14 | #define GSharp 2408 |
el14ja | 1:2b9ba34131ca | 15 | #define G 2551 |
el14ja | 1:2b9ba34131ca | 16 | #define A 2273 |
el14ja | 1:2b9ba34131ca | 17 | #define B 2025 |
el14ja | 1:2b9ba34131ca | 18 | #define C 1911 |
el14ja | 1:2b9ba34131ca | 19 | #define HD 1703 |
el14ja | 1:2b9ba34131ca | 20 | #define HE 1517 |
el14ja | 1:2b9ba34131ca | 21 | #define HF 1431 |
el14ja | 1:2b9ba34131ca | 22 | #define HG 1276 |
el14ja | 1:2b9ba34131ca | 23 | #define HA 1136 |
el14ja | 1:2b9ba34131ca | 24 | |
el14ja | 1:2b9ba34131ca | 25 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
el14ja | 1:2b9ba34131ca | 26 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
el14ja | 1:2b9ba34131ca | 27 | Serial pc(USBTX,USBRX); |
el14ja | 1:2b9ba34131ca | 28 | |
el14ja | 1:2b9ba34131ca | 29 | /** Creates Tertis instance*/ |
el14ja | 1:2b9ba34131ca | 30 | Tetris tetris(); |
el14ja | 1:2b9ba34131ca | 31 | |
el14ja | 1:2b9ba34131ca | 32 | /** Creates Ticker instance*/ |
el14ja | 1:2b9ba34131ca | 33 | Ticker ticker; |
el14ja | 1:2b9ba34131ca | 34 | |
el14ja | 1:2b9ba34131ca | 35 | /** Creates music ticker instace*/ |
el14ja | 1:2b9ba34131ca | 36 | Ticker musicticker; |
el14ja | 1:2b9ba34131ca | 37 | |
el14ja | 1:2b9ba34131ca | 38 | /** Creates Timeout instances that are used to stop button double clicks */ |
el14ja | 1:2b9ba34131ca | 39 | Timeout lefttimeout; |
el14ja | 1:2b9ba34131ca | 40 | Timeout righttimeout; |
el14ja | 1:2b9ba34131ca | 41 | Timeout spintimeout; |
el14ja | 1:2b9ba34131ca | 42 | Timeout downtimeout; |
el14ja | 1:2b9ba34131ca | 43 | |
el14ja | 1:2b9ba34131ca | 44 | |
el14ja | 1:2b9ba34131ca | 45 | /** |
el14ja | 1:2b9ba34131ca | 46 | @namespace ain |
el14ja | 1:2b9ba34131ca | 47 | @breif Analog input that is used for the seed for the randomiser |
el14ja | 1:2b9ba34131ca | 48 | */ |
el14ja | 1:2b9ba34131ca | 49 | AnalogIn ain(A0); |
el14ja | 1:2b9ba34131ca | 50 | |
el14ja | 1:2b9ba34131ca | 51 | /** |
el14ja | 1:2b9ba34131ca | 52 | @namespace buzzer |
el14ja | 1:2b9ba34131ca | 53 | @brief Buzzer output to play tetris music |
el14ja | 1:2b9ba34131ca | 54 | */ |
el14ja | 1:2b9ba34131ca | 55 | PwmOut buzzer(PTA1); |
el14ja | 1:2b9ba34131ca | 56 | |
el14ja | 1:2b9ba34131ca | 57 | InterruptIn left(PTC0); |
el14ja | 1:2b9ba34131ca | 58 | InterruptIn right(PTC9); |
el14ja | 1:2b9ba34131ca | 59 | InterruptIn spin(PTC5); |
el14ja | 1:2b9ba34131ca | 60 | InterruptIn down(PTC7); |
el14ja | 1:2b9ba34131ca | 61 | |
el14ja | 1:2b9ba34131ca | 62 | int musiccounter = 0; |
el14ja | 1:2b9ba34131ca | 63 | |
el14ja | 1:2b9ba34131ca | 64 | int score = 0; /*!< */ |
el14ja | 1:2b9ba34131ca | 65 | int completedlines = 0; |
el14ja | 1:2b9ba34131ca | 66 | int level = 1; |
el14ja | 1:2b9ba34131ca | 67 | float gamespeed = 0.8; //how often a block moves down |
el14ja | 1:2b9ba34131ca | 68 | |
el14ja | 1:2b9ba34131ca | 69 | //**** |
el14ja | 1:2b9ba34131ca | 70 | int pieceposition[2] = {4,0}; //set position of the current dropping piece *** |
el14ja | 1:2b9ba34131ca | 71 | int orientation = 0; |
el14ja | 1:2b9ba34131ca | 72 | int currentshape = 3; |
el14ja | 1:2b9ba34131ca | 73 | int nextpiece; |
el14ja | 1:2b9ba34131ca | 74 | int shapebagcounter = 0; |
el14ja | 1:2b9ba34131ca | 75 | |
el14ja | 1:2b9ba34131ca | 76 | int nextshape[7];//stores the next 7 shapes to be used - 7 bag system |
el14ja | 1:2b9ba34131ca | 77 | |
el14ja | 1:2b9ba34131ca | 78 | /** |
el14ja | 1:2b9ba34131ca | 79 | Performs instuctions necessary to start the game |
el14ja | 1:2b9ba34131ca | 80 | */ |
el14ja | 1:2b9ba34131ca | 81 | void gameSetup(); |
el14ja | 1:2b9ba34131ca | 82 | |
el14ja | 1:2b9ba34131ca | 83 | /** |
el14ja | 1:2b9ba34131ca | 84 | Sets a 3x3 sets a pixel cell in the game area at position (x,y) |
el14ja | 1:2b9ba34131ca | 85 | @param x - cells horizontal position |
el14ja | 1:2b9ba34131ca | 86 | @param y - cells vertical position |
el14ja | 1:2b9ba34131ca | 87 | */ |
el14ja | 1:2b9ba34131ca | 88 | void gamePixel(int x,int y); |
el14ja | 1:2b9ba34131ca | 89 | |
el14ja | 1:2b9ba34131ca | 90 | /** |
el14ja | 1:2b9ba34131ca | 91 | Sets a 3x3 clears a pixel cell in the game area at position (x,y) |
el14ja | 1:2b9ba34131ca | 92 | @param x - cells horizontal position |
el14ja | 1:2b9ba34131ca | 93 | @param y - cells vertical position |
el14ja | 1:2b9ba34131ca | 94 | */ |
el14ja | 1:2b9ba34131ca | 95 | void clearGamePixel(int x, int y); |
el14ja | 1:2b9ba34131ca | 96 | |
el14ja | 1:2b9ba34131ca | 97 | /** |
el14ja | 1:2b9ba34131ca | 98 | Clears the game area |
el14ja | 1:2b9ba34131ca | 99 | */ |
el14ja | 1:2b9ba34131ca | 100 | void clearGame(); |
el14ja | 1:2b9ba34131ca | 101 | |
el14ja | 1:2b9ba34131ca | 102 | /** |
el14ja | 1:2b9ba34131ca | 103 | Sets the information for the user on the left side of the screen |
el14ja | 1:2b9ba34131ca | 104 | @param score |
el14ja | 1:2b9ba34131ca | 105 | @param level |
el14ja | 1:2b9ba34131ca | 106 | @param next |
el14ja | 1:2b9ba34131ca | 107 | */ |
el14ja | 1:2b9ba34131ca | 108 | void gameInfo(int score,int level,int next); |
el14ja | 1:2b9ba34131ca | 109 | |
el14ja | 1:2b9ba34131ca | 110 | /** |
el14ja | 1:2b9ba34131ca | 111 | Places a game piece on the screen |
el14ja | 1:2b9ba34131ca | 112 | @param x - cells horizontal position |
el14ja | 1:2b9ba34131ca | 113 | @param y - cells vertical position |
el14ja | 1:2b9ba34131ca | 114 | @param shape - the shape that is being placed |
el14ja | 1:2b9ba34131ca | 115 | */ |
el14ja | 1:2b9ba34131ca | 116 | void piecePlace(int x,int y,int shape); //places a piece |
el14ja | 1:2b9ba34131ca | 117 | |
el14ja | 1:2b9ba34131ca | 118 | /** |
el14ja | 1:2b9ba34131ca | 119 | Clears a game piece on the screen |
el14ja | 1:2b9ba34131ca | 120 | @param x - cells horizontal position |
el14ja | 1:2b9ba34131ca | 121 | @param y - cells vertical position |
el14ja | 1:2b9ba34131ca | 122 | @param shape - the shape that is being cleared |
el14ja | 1:2b9ba34131ca | 123 | */ |
el14ja | 1:2b9ba34131ca | 124 | void pieceClear(int x,int y,int shape); //clears a spesific shape |
el14ja | 1:2b9ba34131ca | 125 | |
el14ja | 1:2b9ba34131ca | 126 | /** |
el14ja | 1:2b9ba34131ca | 127 | Randomly generates new bag of 7 |
el14ja | 1:2b9ba34131ca | 128 | */ |
el14ja | 1:2b9ba34131ca | 129 | void newShapeBag(); |
el14ja | 1:2b9ba34131ca | 130 | |
el14ja | 1:2b9ba34131ca | 131 | |
el14ja | 1:2b9ba34131ca | 132 | void movePieceRight();//moves the piece right |
el14ja | 1:2b9ba34131ca | 133 | void movePieceLeft();//moves the piece left |
el14ja | 1:2b9ba34131ca | 134 | void spinPiece(); //spins the piece clockwise |
el14ja | 1:2b9ba34131ca | 135 | void movePieceDown(); //movespiece |
el14ja | 1:2b9ba34131ca | 136 | |
el14ja | 1:2b9ba34131ca | 137 | /** |
el14ja | 1:2b9ba34131ca | 138 | Returns turn if piece placement is valid ********** |
el14ja | 1:2b9ba34131ca | 139 | */ |
el14ja | 1:2b9ba34131ca | 140 | bool movePossible(int xpos, int ypos, int orientation); //returns turn if piece placement is valid |
el14ja | 1:2b9ba34131ca | 141 | void newpiece(); |
el14ja | 1:2b9ba34131ca | 142 | void pieceToGameArray(); |
el14ja | 1:2b9ba34131ca | 143 | void checkCompleteLine(); |
el14ja | 1:2b9ba34131ca | 144 | void setGameArea(); //sets the game area from the game area array |
el14ja | 1:2b9ba34131ca | 145 | void removeCompleteLines(int rowscomplete[4]); |
el14ja | 1:2b9ba34131ca | 146 | void setTicker(float time); |
el14ja | 1:2b9ba34131ca | 147 | |
el14ja | 1:2b9ba34131ca | 148 | |
el14ja | 1:2b9ba34131ca | 149 | /** |
el14ja | 1:2b9ba34131ca | 150 | Fucntions called by the button interupts to set the flags ****** |
el14ja | 1:2b9ba34131ca | 151 | */ |
el14ja | 1:2b9ba34131ca | 152 | void moveDownTicker(); |
el14ja | 1:2b9ba34131ca | 153 | void left_isr(); |
el14ja | 1:2b9ba34131ca | 154 | void right_isr(); |
el14ja | 1:2b9ba34131ca | 155 | void spin_isr(); |
el14ja | 1:2b9ba34131ca | 156 | void down_isr(); |
el14ja | 1:2b9ba34131ca | 157 | |
el14ja | 1:2b9ba34131ca | 158 | void music_isr(); |
el14ja | 1:2b9ba34131ca | 159 | |
el14ja | 1:2b9ba34131ca | 160 | void lefttimeout_isr(); |
el14ja | 1:2b9ba34131ca | 161 | void righttimeout_isr(); |
el14ja | 1:2b9ba34131ca | 162 | void spintimeout_isr(); |
el14ja | 1:2b9ba34131ca | 163 | void downtimeout_isr(); |
el14ja | 1:2b9ba34131ca | 164 | |
el14ja | 1:2b9ba34131ca | 165 | //interupt and ticker flags |
el14ja | 1:2b9ba34131ca | 166 | volatile int g_timer_flag = 0; |
el14ja | 1:2b9ba34131ca | 167 | volatile int g_left = 0; |
el14ja | 1:2b9ba34131ca | 168 | volatile int g_right = 0; |
el14ja | 1:2b9ba34131ca | 169 | volatile int g_spin = 0; |
el14ja | 1:2b9ba34131ca | 170 | volatile int g_down = 0; |
el14ja | 1:2b9ba34131ca | 171 | |
el14ja | 1:2b9ba34131ca | 172 | volatile int g_music = 0; |
el14ja | 1:2b9ba34131ca | 173 | |
el14ja | 1:2b9ba34131ca | 174 | volatile int g_lefttimeout = 1; |
el14ja | 1:2b9ba34131ca | 175 | volatile int g_righttimeout = 1; |
el14ja | 1:2b9ba34131ca | 176 | volatile int g_spintimeout = 1; |
el14ja | 1:2b9ba34131ca | 177 | volatile int g_downtimeout = 1; |
el14ja | 1:2b9ba34131ca | 178 | |
el14ja | 1:2b9ba34131ca | 179 | //misc game flags |
el14ja | 1:2b9ba34131ca | 180 | volatile int g_newpiece = 1; |
el14ja | 1:2b9ba34131ca | 181 | int harddropping = 0; |
el14ja | 1:2b9ba34131ca | 182 | |
el14ja | 1:2b9ba34131ca | 183 | |
el14ja | 1:2b9ba34131ca | 184 | |
el14ja | 1:2b9ba34131ca | 185 | //shapes I,J,L,O,T,S,Z all stored as Hex values |
el14ja | 1:2b9ba34131ca | 186 | int shapes[7][4] = {{0x00F0,0x4444,0x0F00,0x2222},{0x0071,0x0226,0x0470,0x0322},{0x0074,0x0622,0x0170,0x0223},{0x0033,0x0033,0x0033,0x0033},{0x0072,0x0262,0x0270,0x0232},{0x0036,0x0462,0x0360,0x0231},{0x0063,0x0264,0x0630,0x0132}}; |
el14ja | 1:2b9ba34131ca | 187 | int gamearea[12][16]; //stores the current states of the game area and is used to check for collisions ***add sides |
el14ja | 1:2b9ba34131ca | 188 | |
el14ja | 1:2b9ba34131ca | 189 | 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 | 1:2b9ba34131ca | 190 | 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}; |