This project was school project with special shield with LED matrix. So sorry but comments are in my language because I think this program is unusable for others when they haven't this special shield. But functions are simple for understanding ;-).

Dependencies:   MMA8451Q mbed

Committer:
lamaking
Date:
Sat Jan 17 19:57:58 2015 +0000
Revision:
0:a837349b0219
This projekt was school projekt for special shield with LED matrix, so comments are in my language because I think this program is unusable for others.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lamaking 0:a837349b0219 1 #include "mbed.h"
lamaking 0:a837349b0219 2 #include "MMA8451Q.h" //kniznica akcelerometra
lamaking 0:a837349b0219 3
lamaking 0:a837349b0219 4 #define MMA8451_I2C_ADDRESS (0x1d<<1) //definicia pre akcelerometer
lamaking 0:a837349b0219 5
lamaking 0:a837349b0219 6 Ticker disptick; //definovanie casoveho prerusenia pre volanie funkcie vysvietenia
lamaking 0:a837349b0219 7 AnalogIn randAdc(A3);
lamaking 0:a837349b0219 8 // definovanie pinov pre zelenu farbu
lamaking 0:a837349b0219 9 DigitalOut gr0(PTC7);
lamaking 0:a837349b0219 10 DigitalOut gr1(PTC6);
lamaking 0:a837349b0219 11 DigitalOut gr2(PTC5);
lamaking 0:a837349b0219 12 DigitalOut gr3(PTC4);
lamaking 0:a837349b0219 13 DigitalOut gr4(PTC3);
lamaking 0:a837349b0219 14 DigitalOut gr5(PTC2);
lamaking 0:a837349b0219 15 DigitalOut gr6(PTC1);
lamaking 0:a837349b0219 16 DigitalOut gr7(PTC0);
lamaking 0:a837349b0219 17
lamaking 0:a837349b0219 18 //definovanie pinov pre cervenu farbu
lamaking 0:a837349b0219 19 DigitalOut rd0(PTC17);
lamaking 0:a837349b0219 20 DigitalOut rd1(PTC16);
lamaking 0:a837349b0219 21 DigitalOut rd2(PTC13);
lamaking 0:a837349b0219 22 DigitalOut rd3(PTC12);
lamaking 0:a837349b0219 23 DigitalOut rd4(PTC11);
lamaking 0:a837349b0219 24 DigitalOut rd5(PTC10);
lamaking 0:a837349b0219 25 DigitalOut rd6(PTC9);
lamaking 0:a837349b0219 26 DigitalOut rd7(PTC8);
lamaking 0:a837349b0219 27
lamaking 0:a837349b0219 28 //definovanie pinov pre adresaciu riadkov
lamaking 0:a837349b0219 29 DigitalOut ln0(PTB0);
lamaking 0:a837349b0219 30 DigitalOut ln1(PTB1);
lamaking 0:a837349b0219 31 DigitalOut ln2(PTB2);
lamaking 0:a837349b0219 32
lamaking 0:a837349b0219 33 // prototypy funkcii
lamaking 0:a837349b0219 34 short RandSegment(void); //funkcia nahodne vybera novy segment pomocou citania A/D vstupu
lamaking 0:a837349b0219 35 void SegmentInit(void); //prida jednotlivym segmentom informacie o ich vlastnostiach - pocet riadkov, stlpcov, rotovany segment, priradi pole s vlastnostou farby
lamaking 0:a837349b0219 36 void SegmentCopy(struct segments *psgm, int segmentIn[2][4]); //v SegmentInit skopiruje vytvorene pole segmentu do pola v strukture (struct segments)
lamaking 0:a837349b0219 37 void MoveDown(int array[8][16], int line); //posun pola smerom dole po odstranení zaplneneho riadku
lamaking 0:a837349b0219 38 void LineCheck(void); //funkcia kontroluje zaplneny riadok
lamaking 0:a837349b0219 39 void SegmentMove(struct segments *psgm); //funkcia sa stara o pohyby segmentu
lamaking 0:a837349b0219 40 void WriteClrSeg(int wrtEnab, struct segments *psgm); //zmazanie alebo zapis segmentu pri pohybe maticou
lamaking 0:a837349b0219 41 int DecLvl(void); //obsluha akcelerometra (preco prave Dec som zabudol)
lamaking 0:a837349b0219 42 void LineAdd(int line); //adresacia riadkov
lamaking 0:a837349b0219 43 void Display(void); //rozsvecovanie farieb v riadkoch podla hlavnej matice
lamaking 0:a837349b0219 44 void OutPortInit(void); //nastavenie portov do pociatocneho stavu - vypnutie/zhasnutie
lamaking 0:a837349b0219 45
lamaking 0:a837349b0219 46 //_____________________________________________________
lamaking 0:a837349b0219 47 int collision = 0; //globalna premenna zaznamenania kolizie pricom dany segment ostane ulozeny na danom mieste a vyberie sa novy
lamaking 0:a837349b0219 48 short choice = 0; //globálna premenna vybranej struktury segmentu (pr. seg[choice])
lamaking 0:a837349b0219 49
lamaking 0:a837349b0219 50 //nasledujuce segmenty si nesu o sebe vlastnost farby(nulovy a parny stlpec v 1 - svieti zelena, neparny - cervena, obe - oranzova), neskor su priradene do struktury a su pridane dalsie vlastnosti
lamaking 0:a837349b0219 51 //____________________________________________________kocka
lamaking 0:a837349b0219 52 int segment0[2][4] = {{1,1, 1,1}, //oranzova
lamaking 0:a837349b0219 53 {1,1, 1,1},};
lamaking 0:a837349b0219 54 int segment1[2][4] = {{1,0, 1,0}, //zelena
lamaking 0:a837349b0219 55 {1,0, 1,0},};
lamaking 0:a837349b0219 56 int segment2[2][4] = {{0,1, 0,1}, //cervena
lamaking 0:a837349b0219 57 {0,1, 0,1},};
lamaking 0:a837349b0219 58 //_____________________________________________________bodka
lamaking 0:a837349b0219 59
lamaking 0:a837349b0219 60 int segment3[2][4] = {{1,1, 0,0}, //oranzova
lamaking 0:a837349b0219 61 {0,0, 0,0},};
lamaking 0:a837349b0219 62 int segment4[2][4] = {{1,0, 0,0}, //zelena
lamaking 0:a837349b0219 63 {0,0, 0,0},};
lamaking 0:a837349b0219 64 int segment5[2][4] = {{0,1, 0,0}, //cervena
lamaking 0:a837349b0219 65 {0,0, 0,0},};
lamaking 0:a837349b0219 66 //____________________________________________________I
lamaking 0:a837349b0219 67
lamaking 0:a837349b0219 68 int segment6[2][4] = {{1,1, 1,1}, //oranzova
lamaking 0:a837349b0219 69 {0,0, 0,0},};
lamaking 0:a837349b0219 70 int segment61[2][4] = {{1,1, 0,0}, //oranzova rot1
lamaking 0:a837349b0219 71 {1,1, 0,0},};
lamaking 0:a837349b0219 72
lamaking 0:a837349b0219 73 int segment7[2][4] = {{1,0, 1,0}, //zelena
lamaking 0:a837349b0219 74 {0,0, 0,0},};
lamaking 0:a837349b0219 75 int segment71[2][4] = {{1,0, 0,0}, //zelena rot1
lamaking 0:a837349b0219 76 {1,0, 0,0},};
lamaking 0:a837349b0219 77
lamaking 0:a837349b0219 78 int segment8[2][4] = {{0,1, 0,1}, //cervena
lamaking 0:a837349b0219 79 {0,0, 0,0},};
lamaking 0:a837349b0219 80 int segment81[2][4] = {{0,1, 0,0}, //cervena rot1
lamaking 0:a837349b0219 81 {0,1, 0,0},};
lamaking 0:a837349b0219 82 //______________________________________________________L
lamaking 0:a837349b0219 83
lamaking 0:a837349b0219 84 int segment9[2][4] = {{1,1, 1,1}, //oranzova
lamaking 0:a837349b0219 85 {1,1, 0,0},};
lamaking 0:a837349b0219 86 int segment91[2][4] = {{1,1, 1,1}, //oranzova rot1
lamaking 0:a837349b0219 87 {0,0, 1,1},};
lamaking 0:a837349b0219 88 int segment92[2][4] = {{0,0, 1,1}, //oranzova rot2
lamaking 0:a837349b0219 89 {1,1, 1,1},};
lamaking 0:a837349b0219 90 int segment93[2][4] = {{1,1, 0,0}, //oranzova rot3
lamaking 0:a837349b0219 91 {1,1, 1,1},};
lamaking 0:a837349b0219 92
lamaking 0:a837349b0219 93 int segment10[2][4] = {{1,0, 1,0}, //zelena
lamaking 0:a837349b0219 94 {1,0, 0,0},};
lamaking 0:a837349b0219 95 int segment101[2][4] = {{1,0, 1,0}, //zelena rot1
lamaking 0:a837349b0219 96 {0,0, 1,0},};
lamaking 0:a837349b0219 97 int segment102[2][4] = {{0,0, 1,0}, //zelena rot2
lamaking 0:a837349b0219 98 {1,0, 1,0},};
lamaking 0:a837349b0219 99 int segment103[2][4] = {{1,0, 0,0}, //zelena rot3
lamaking 0:a837349b0219 100 {1,0, 1,0},};
lamaking 0:a837349b0219 101
lamaking 0:a837349b0219 102 int segment11[2][4] = {{0,1, 0,1}, //cervena
lamaking 0:a837349b0219 103 {0,1, 0,0},};
lamaking 0:a837349b0219 104 int segment111[2][4] = {{0,1, 0,1}, //cervena rot1
lamaking 0:a837349b0219 105 {0,0, 0,1},};
lamaking 0:a837349b0219 106 int segment112[2][4] = {{0,0, 0,1}, //cervena rot2
lamaking 0:a837349b0219 107 {0,1, 0,1},};
lamaking 0:a837349b0219 108 int segment113[2][4] = {{0,1, 0,0}, //cervena rot3
lamaking 0:a837349b0219 109 {0,1, 0,1},};
lamaking 0:a837349b0219 110 //________________________________________________________________
lamaking 0:a837349b0219 111 struct segments { //struktura segmentov
lamaking 0:a837349b0219 112 char line; //pocet riadkov
lamaking 0:a837349b0219 113 char colum; //pocet stlpcov
lamaking 0:a837349b0219 114 char nextRot; //cislo noveho rotovaneho segmentu
lamaking 0:a837349b0219 115 int segmentx[2][4]; //pole pre segmnet
lamaking 0:a837349b0219 116 }seg[30];
lamaking 0:a837349b0219 117 //_________________________________________________________________
lamaking 0:a837349b0219 118 // | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
lamaking 0:a837349b0219 119 int dispArray[8][16] = { //pracovne pole, v ktorom sa vsetko odohrava, nulovy a parny stlpec v 1 - svieti zelena, neparny - cervena, obe - oranzova
lamaking 0:a837349b0219 120 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //1
lamaking 0:a837349b0219 121 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //2
lamaking 0:a837349b0219 122 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //3
lamaking 0:a837349b0219 123 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //4
lamaking 0:a837349b0219 124 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //5
lamaking 0:a837349b0219 125 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //6
lamaking 0:a837349b0219 126 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, //7
lamaking 0:a837349b0219 127 {0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0}, };//8
lamaking 0:a837349b0219 128
lamaking 0:a837349b0219 129 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 130 /*+++ MAIN +++*/
lamaking 0:a837349b0219 131 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 132
lamaking 0:a837349b0219 133 int main() {
lamaking 0:a837349b0219 134 SegmentInit(); //nahranie informacii do struktur segmentov (struct segments)
lamaking 0:a837349b0219 135 OutPortInit(); //porty vypnute - zhasnuta matica
lamaking 0:a837349b0219 136 disptick.attach(&Display, 0.002); //nastavenie prerusenia pre vysvecovanie displeja
lamaking 0:a837349b0219 137 while(1) {
lamaking 0:a837349b0219 138 choice = RandSegment(); //nahodny vyber segmentu
lamaking 0:a837349b0219 139 while(collision != 1){ //pokial nenastane kolizia spodnej hrany segmentu
lamaking 0:a837349b0219 140 SegmentMove(&seg[choice]); //pohybuj segmentov - v pravo, v lavo, dole, rotacia
lamaking 0:a837349b0219 141 }
lamaking 0:a837349b0219 142 LineCheck(); //skontroluje riadoky ci nie su zaplnene ak hej posunie sa vrchna cast dole
lamaking 0:a837349b0219 143 collision = 0;
lamaking 0:a837349b0219 144 }
lamaking 0:a837349b0219 145 }
lamaking 0:a837349b0219 146 /*--------------------------------------------------------*/
lamaking 0:a837349b0219 147 /*--- MAIN ---*/
lamaking 0:a837349b0219 148 /*--------------------------------------------------------*/
lamaking 0:a837349b0219 149
lamaking 0:a837349b0219 150 /*___________________________________________________________________________________________*/
lamaking 0:a837349b0219 151
lamaking 0:a837349b0219 152
lamaking 0:a837349b0219 153 /*++++++++++++++++++++++RandSegment+++++++++++++++++++++++*/
lamaking 0:a837349b0219 154 //funkcia nahodne vybera novy segment pomocou citania A/D vstupu
lamaking 0:a837349b0219 155 short RandSegment(void){
lamaking 0:a837349b0219 156 static short num = 0;
lamaking 0:a837349b0219 157 num = randAdc.read_u16()%12; //precitanie A/D s %12 - zvysok od 0 do 11 <-> zakladne segmenty od 0 do 11
lamaking 0:a837349b0219 158 return(num);
lamaking 0:a837349b0219 159 }
lamaking 0:a837349b0219 160 /*---------------------RandSegment-----------------------*/
lamaking 0:a837349b0219 161
lamaking 0:a837349b0219 162 /*++++++++++++++++++++++SegmentInit+++++++++++++++++++++++*/
lamaking 0:a837349b0219 163 //prida jednotlivym segmentom informacie o ich vlastnostiach - pocet riadkov, stlpcov, rotovany segment, priradi pole s vlastnostou farby
lamaking 0:a837349b0219 164 void SegmentInit(void){
lamaking 0:a837349b0219 165 seg[0].line = 2;
lamaking 0:a837349b0219 166 seg[0].colum = 4;
lamaking 0:a837349b0219 167 seg[0].nextRot = 0;
lamaking 0:a837349b0219 168 SegmentCopy(&seg[0], segment0); //oranzova kocka 2x2
lamaking 0:a837349b0219 169 seg[1].line = 2;
lamaking 0:a837349b0219 170 seg[1].colum = 4;
lamaking 0:a837349b0219 171 seg[1].nextRot = 1;
lamaking 0:a837349b0219 172 SegmentCopy(&seg[1], segment1); //zelena kocka 2x2
lamaking 0:a837349b0219 173 seg[2].line = 2;
lamaking 0:a837349b0219 174 seg[2].colum = 4;
lamaking 0:a837349b0219 175 seg[2].nextRot = 2;
lamaking 0:a837349b0219 176 SegmentCopy(&seg[2], segment2); //cervena kocka 2x2
lamaking 0:a837349b0219 177 //_________________________________________________________________
lamaking 0:a837349b0219 178 seg[3].line = 1;
lamaking 0:a837349b0219 179 seg[3].colum = 2;
lamaking 0:a837349b0219 180 seg[3].nextRot = 3;
lamaking 0:a837349b0219 181 SegmentCopy(&seg[3], segment3); //oranzova bodka
lamaking 0:a837349b0219 182 seg[4].line = 1;
lamaking 0:a837349b0219 183 seg[4].colum = 2;
lamaking 0:a837349b0219 184 seg[4].nextRot = 4;
lamaking 0:a837349b0219 185 SegmentCopy(&seg[4], segment4); //zelena bodka
lamaking 0:a837349b0219 186 seg[5].line = 1;
lamaking 0:a837349b0219 187 seg[5].colum = 2;
lamaking 0:a837349b0219 188 seg[5].nextRot = 5;
lamaking 0:a837349b0219 189 SegmentCopy(&seg[5], segment5); //cervena bodka
lamaking 0:a837349b0219 190 //__________________________________________________________________
lamaking 0:a837349b0219 191 seg[6].line = 1;
lamaking 0:a837349b0219 192 seg[6].colum = 4;
lamaking 0:a837349b0219 193 seg[6].nextRot = 12;
lamaking 0:a837349b0219 194 SegmentCopy(&seg[6], segment6); //oranzova ciara
lamaking 0:a837349b0219 195 seg[12].line = 2;
lamaking 0:a837349b0219 196 seg[12].colum = 2;
lamaking 0:a837349b0219 197 seg[12].nextRot = 6;
lamaking 0:a837349b0219 198 SegmentCopy(&seg[12], segment61); //oranzova ciara rot
lamaking 0:a837349b0219 199
lamaking 0:a837349b0219 200 seg[7].line = 1;
lamaking 0:a837349b0219 201 seg[7].colum = 4;
lamaking 0:a837349b0219 202 seg[7].nextRot = 13;
lamaking 0:a837349b0219 203 SegmentCopy(&seg[7], segment7); //zelena ciara
lamaking 0:a837349b0219 204 seg[13].line = 2;
lamaking 0:a837349b0219 205 seg[13].colum = 2;
lamaking 0:a837349b0219 206 seg[13].nextRot = 7;
lamaking 0:a837349b0219 207 SegmentCopy(&seg[13], segment71); //zelena ciara rot
lamaking 0:a837349b0219 208
lamaking 0:a837349b0219 209 seg[8].line = 1;
lamaking 0:a837349b0219 210 seg[8].colum = 4;
lamaking 0:a837349b0219 211 seg[8].nextRot = 14;
lamaking 0:a837349b0219 212 SegmentCopy(&seg[8], segment8); //cervena ciara
lamaking 0:a837349b0219 213 seg[14].line = 2;
lamaking 0:a837349b0219 214 seg[14].colum = 2;
lamaking 0:a837349b0219 215 seg[14].nextRot = 8;
lamaking 0:a837349b0219 216 SegmentCopy(&seg[14], segment81); //cervena ciara rot
lamaking 0:a837349b0219 217 //__________________________________________________________________
lamaking 0:a837349b0219 218 seg[9].line = 2;
lamaking 0:a837349b0219 219 seg[9].colum = 4;
lamaking 0:a837349b0219 220 seg[9].nextRot = 15;
lamaking 0:a837349b0219 221 SegmentCopy(&seg[9], segment9); //oranzova L
lamaking 0:a837349b0219 222 seg[15].line = 2;
lamaking 0:a837349b0219 223 seg[15].colum = 4;
lamaking 0:a837349b0219 224 seg[15].nextRot = 16;
lamaking 0:a837349b0219 225 SegmentCopy(&seg[15], segment91); //oranzova L rot1
lamaking 0:a837349b0219 226 seg[16].line = 2;
lamaking 0:a837349b0219 227 seg[16].colum = 4;
lamaking 0:a837349b0219 228 seg[16].nextRot = 17;
lamaking 0:a837349b0219 229 SegmentCopy(&seg[16], segment92); //oranzova L rot2
lamaking 0:a837349b0219 230 seg[17].line = 2;
lamaking 0:a837349b0219 231 seg[17].colum = 4;
lamaking 0:a837349b0219 232 seg[17].nextRot = 9;
lamaking 0:a837349b0219 233 SegmentCopy(&seg[17], segment93); //oranzova L rot3
lamaking 0:a837349b0219 234
lamaking 0:a837349b0219 235 seg[10].line = 2;
lamaking 0:a837349b0219 236 seg[10].colum = 4;
lamaking 0:a837349b0219 237 seg[10].nextRot = 18;
lamaking 0:a837349b0219 238 SegmentCopy(&seg[10], segment10); //zelena L
lamaking 0:a837349b0219 239 seg[18].line = 2;
lamaking 0:a837349b0219 240 seg[18].colum = 4;
lamaking 0:a837349b0219 241 seg[18].nextRot = 19;
lamaking 0:a837349b0219 242 SegmentCopy(&seg[18], segment101); //zelena L rot1
lamaking 0:a837349b0219 243 seg[19].line = 2;
lamaking 0:a837349b0219 244 seg[19].colum = 4;
lamaking 0:a837349b0219 245 seg[19].nextRot = 20;
lamaking 0:a837349b0219 246 SegmentCopy(&seg[19], segment102); //zelena L rot2
lamaking 0:a837349b0219 247 seg[20].line = 2;
lamaking 0:a837349b0219 248 seg[20].colum = 4;
lamaking 0:a837349b0219 249 seg[20].nextRot = 10;
lamaking 0:a837349b0219 250 SegmentCopy(&seg[20], segment103); //zelena L rot3
lamaking 0:a837349b0219 251
lamaking 0:a837349b0219 252 seg[11].line = 2;
lamaking 0:a837349b0219 253 seg[11].colum = 4;
lamaking 0:a837349b0219 254 seg[11].nextRot = 21;
lamaking 0:a837349b0219 255 SegmentCopy(&seg[11], segment11); //cervena L
lamaking 0:a837349b0219 256 seg[21].line = 2;
lamaking 0:a837349b0219 257 seg[21].colum = 4;
lamaking 0:a837349b0219 258 seg[21].nextRot = 22;
lamaking 0:a837349b0219 259 SegmentCopy(&seg[21], segment111); //cervena L rot1
lamaking 0:a837349b0219 260 seg[22].line = 2;
lamaking 0:a837349b0219 261 seg[22].colum = 4;
lamaking 0:a837349b0219 262 seg[22].nextRot = 23;
lamaking 0:a837349b0219 263 SegmentCopy(&seg[22], segment112); //cervena L rot2
lamaking 0:a837349b0219 264 seg[23].line = 2;
lamaking 0:a837349b0219 265 seg[23].colum = 4;
lamaking 0:a837349b0219 266 seg[23].nextRot = 11;
lamaking 0:a837349b0219 267 SegmentCopy(&seg[23], segment113); //cervena L rot3
lamaking 0:a837349b0219 268
lamaking 0:a837349b0219 269 }
lamaking 0:a837349b0219 270 /*----------------------SegmentInit-----------------------*/
lamaking 0:a837349b0219 271
lamaking 0:a837349b0219 272 /*++++++++++++++++++++++SegmentCopy+++++++++++++++++++++++*/
lamaking 0:a837349b0219 273 //v SegmentInit skopiruje vytvorene pole segmentu do pola v strukture (struct segments)
lamaking 0:a837349b0219 274 void SegmentCopy(struct segments *psgm, int segmentIn[2][4]){
lamaking 0:a837349b0219 275 static int i,j;
lamaking 0:a837349b0219 276 for(i = 0 ; i < (psgm->line); i++){ //riadok
lamaking 0:a837349b0219 277 for(j = 0 ; j < (psgm->colum); j++ ){ //slpec
lamaking 0:a837349b0219 278 psgm->segmentx[i][j] = segmentIn[i][j];
lamaking 0:a837349b0219 279 }
lamaking 0:a837349b0219 280 }
lamaking 0:a837349b0219 281 }
lamaking 0:a837349b0219 282 /*--------------------SegmentCopy------------------------*/
lamaking 0:a837349b0219 283
lamaking 0:a837349b0219 284 /*++++++++++++++++++++++LineCheck+++++++++++++++++++++++*/
lamaking 0:a837349b0219 285 //funkcia kontroluje zaplneny riadok
lamaking 0:a837349b0219 286 void LineCheck(void){
lamaking 0:a837349b0219 287 static int ch=0;
lamaking 0:a837349b0219 288 static int lnCh, colCh;
lamaking 0:a837349b0219 289
lamaking 0:a837349b0219 290 for(lnCh = 0; lnCh < 8; lnCh++){
lamaking 0:a837349b0219 291 ch = 0;
lamaking 0:a837349b0219 292 for(colCh=0; colCh < 15; colCh+=2){
lamaking 0:a837349b0219 293 if((dispArray[lnCh][colCh]==1) || (dispArray[lnCh][colCh+1]==1)){ //ak je bod vysvieteny
lamaking 0:a837349b0219 294 ch++;} //pripocita k premennej
lamaking 0:a837349b0219 295 else {
lamaking 0:a837349b0219 296 colCh = 15; //ak bod v riadku nie je vysvieteny nema cenu ho cely kontrolovat a skoci sa tak na dalsi riadok
lamaking 0:a837349b0219 297 ch = 0;
lamaking 0:a837349b0219 298 }
lamaking 0:a837349b0219 299 }
lamaking 0:a837349b0219 300 if((ch == 8)){ //ak je cely riadok vysvieteny
lamaking 0:a837349b0219 301 for(colCh=0; colCh < 16; colCh++){ //nuluj riadok
lamaking 0:a837349b0219 302 dispArray[lnCh][colCh] = 0;
lamaking 0:a837349b0219 303 }
lamaking 0:a837349b0219 304 MoveDown(dispArray, lnCh); //posunie zvysok pola dole a kontroluje sa zvysok a teda proces sa opakuje kym sa neskontroluje cele pole
lamaking 0:a837349b0219 305 }
lamaking 0:a837349b0219 306 }
lamaking 0:a837349b0219 307 }
lamaking 0:a837349b0219 308
lamaking 0:a837349b0219 309 /*-------------------------LineCheck----------------------------*/
lamaking 0:a837349b0219 310
lamaking 0:a837349b0219 311 /*++++++++++++++++++++++++MoveDown++++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 312 //posun pola smerom dole po odstranení zaplneneho riadku
lamaking 0:a837349b0219 313 void MoveDown(int dispArray[8][16], int line){
lamaking 0:a837349b0219 314 static int ln, col;
lamaking 0:a837349b0219 315 for(ln = line; ln>=0; ln--){
lamaking 0:a837349b0219 316 for(col=0; col < 16; col++){
lamaking 0:a837349b0219 317 if(ln>=1){
lamaking 0:a837349b0219 318 dispArray[ln][col] = dispArray[ln-1][col];
lamaking 0:a837349b0219 319 }
lamaking 0:a837349b0219 320 if(ln<1){
lamaking 0:a837349b0219 321 dispArray[ln][col] = 0;
lamaking 0:a837349b0219 322 }
lamaking 0:a837349b0219 323 }
lamaking 0:a837349b0219 324 }
lamaking 0:a837349b0219 325 }
lamaking 0:a837349b0219 326
lamaking 0:a837349b0219 327 /*-----------------------MoveDown-------------------------------*/
lamaking 0:a837349b0219 328
lamaking 0:a837349b0219 329 /*+++++++++++++++++++++SegmentMove++++++++++++++++++++++++*/
lamaking 0:a837349b0219 330 //funkcia sa stara o pohyby segmentu
lamaking 0:a837349b0219 331 static int x=6, y=0; //suradnice segmentu pricom hodnotu na suradnici x,y je vzdy lavy horny roh segmentu
lamaking 0:a837349b0219 332
lamaking 0:a837349b0219 333 void SegmentMove(struct segments *psgm){
lamaking 0:a837349b0219 334 static long int counter=0; //vytvara zdrzanie pri pohybe
lamaking 0:a837349b0219 335 static int col, ln, dm; //col, ln - premenne v cykloch pri kontrole kolizii, dm detekcia kolizie, zakaze pohyb danym smerom
lamaking 0:a837349b0219 336
lamaking 0:a837349b0219 337 if( ((x == 6) && (y == 0)) && counter == 0){ //kontrola pri novom segmente ci nedojde ku kolizii
lamaking 0:a837349b0219 338 for(ln = 0; ln < psgm->line; ln++){ //skontroluje ci v priestore nedojde ku kolizii
lamaking 0:a837349b0219 339 for(col=0; col < psgm->colum; col+=2){
lamaking 0:a837349b0219 340 if((dispArray[y+ln][x+col] | dispArray[y+ln][x+col+1]) & (psgm->segmentx[ln][col] | psgm->segmentx[ln][col+1]) == 1){ //zistuje ci nie je v danom bode kolizia so segmentom
lamaking 0:a837349b0219 341 ln = psgm->line; //ak je kolizia nema cenu kontrolovat zvysok
lamaking 0:a837349b0219 342 col = psgm->colum; //
lamaking 0:a837349b0219 343 static int lnclr, colclr; //vynulovanie celeho pola
lamaking 0:a837349b0219 344 for(lnclr = 0; lnclr < 8; lnclr++){ //
lamaking 0:a837349b0219 345 for(colclr=0; colclr < 16; colclr++){ //
lamaking 0:a837349b0219 346 dispArray[lnclr][colclr] = 0; //
lamaking 0:a837349b0219 347 }
lamaking 0:a837349b0219 348 }
lamaking 0:a837349b0219 349 }
lamaking 0:a837349b0219 350 }
lamaking 0:a837349b0219 351 }
lamaking 0:a837349b0219 352 }
lamaking 0:a837349b0219 353
lamaking 0:a837349b0219 354 WriteClrSeg(1, &seg[choice]); //zapise segment do pola
lamaking 0:a837349b0219 355 counter++; //counter zacina pocitat
lamaking 0:a837349b0219 356 if((counter%4000 == 0)){ //ak je counter nasobkom 4000 je umozneny pohyb pomocou akcelerometra
lamaking 0:a837349b0219 357 if(DecLvl()==3) { //podmienka pre pohyb v pravo
lamaking 0:a837349b0219 358 WriteClrSeg(0, &seg[choice]); //zmaze segment z pola, inak by za sebou nechaval zapisane hodnoty
lamaking 0:a837349b0219 359 for(ln = 0; ln < psgm->line; ln++){ //skontroluje ci v priestore pohybu nedojde ku kolizii
lamaking 0:a837349b0219 360 for(col=0; col < psgm->colum; col+=2){
lamaking 0:a837349b0219 361 if((dispArray[y+ln][x+col+2] | dispArray[y+ln][x+col+1+2]) & (psgm->segmentx[ln][col] | psgm->segmentx[ln][col+1]) == 1){
lamaking 0:a837349b0219 362 ln = psgm->line;
lamaking 0:a837349b0219 363 col = psgm->colum;
lamaking 0:a837349b0219 364 dm = 1; //doslo ku kolizii
lamaking 0:a837349b0219 365 }
lamaking 0:a837349b0219 366 }
lamaking 0:a837349b0219 367 }
lamaking 0:a837349b0219 368 if (dm == 0){ //nedoslo ku kolizii, segment sa moze pohnut
lamaking 0:a837349b0219 369 x+=2;
lamaking 0:a837349b0219 370 if (x>16-(psgm->colum)){
lamaking 0:a837349b0219 371 x=16-(psgm->colum);
lamaking 0:a837349b0219 372 }
lamaking 0:a837349b0219 373 }
lamaking 0:a837349b0219 374 dm = 0; //po kolizii sa premenna vynuluje
lamaking 0:a837349b0219 375 WriteClrSeg(1, &seg[choice]); //opatovne zapisanie segmentu do pola
lamaking 0:a837349b0219 376 }
lamaking 0:a837349b0219 377 if((DecLvl()==4) ){ //opodmienka pre pohohyb v lavo - analogia s pohybom v pravo
lamaking 0:a837349b0219 378 WriteClrSeg(0, &seg[choice]);
lamaking 0:a837349b0219 379 for(ln = 0; ln < psgm->line; ln++){ //skontroluje ci v priestore pohybu nedojde ku kolizii
lamaking 0:a837349b0219 380 for(col=0; col < psgm->colum; col+=2){
lamaking 0:a837349b0219 381 if((dispArray[y+ln][x+col-2] | dispArray[y+ln][x+col+1-2]) & (psgm->segmentx[ln][col] | psgm->segmentx[ln][col+1]) == 1){
lamaking 0:a837349b0219 382 ln = psgm->line;
lamaking 0:a837349b0219 383 col = psgm->colum;
lamaking 0:a837349b0219 384 dm = 1;
lamaking 0:a837349b0219 385 }
lamaking 0:a837349b0219 386 }
lamaking 0:a837349b0219 387 }
lamaking 0:a837349b0219 388 if (dm == 0){
lamaking 0:a837349b0219 389 x-=2;
lamaking 0:a837349b0219 390 if (x<0){
lamaking 0:a837349b0219 391 x=0;
lamaking 0:a837349b0219 392 }
lamaking 0:a837349b0219 393 }
lamaking 0:a837349b0219 394 dm = 0;
lamaking 0:a837349b0219 395 WriteClrSeg(1, &seg[choice]);
lamaking 0:a837349b0219 396 }
lamaking 0:a837349b0219 397 if(DecLvl()==1){ //pohyb k sebe - segment pada zrychlene dole
lamaking 0:a837349b0219 398 counter = 40001;
lamaking 0:a837349b0219 399 }
lamaking 0:a837349b0219 400 if(DecLvl()==2){ //pohyb od seba - rotacia segmentu
lamaking 0:a837349b0219 401 WriteClrSeg(0, &seg[choice]); //zmaze segment z pola
lamaking 0:a837349b0219 402 for(ln = 0; ln < seg[psgm->nextRot].line; ln++){ //skontroluje ci v priestore rotovaneho segmentu nedojde ku kolizii
lamaking 0:a837349b0219 403 for(col=0; col < seg[psgm->nextRot].colum; col+=2){
lamaking 0:a837349b0219 404 if(((dispArray[y+ln][x+col] | dispArray[y+ln][x+col+1]) & (seg[psgm->nextRot].segmentx[ln][col] | seg[psgm->nextRot].segmentx[ln][col+1]) == 1) || ((seg[psgm->nextRot].line-1+y) > 7) || ((seg[psgm->nextRot].colum-1+x) > 15)){
lamaking 0:a837349b0219 405 ln = seg[psgm->nextRot].line; //parametre orotovaneho segmentu
lamaking 0:a837349b0219 406 col = seg[psgm->nextRot].colum; //
lamaking 0:a837349b0219 407 dm = 1; //premenna naznacujuca koliziu
lamaking 0:a837349b0219 408 }
lamaking 0:a837349b0219 409 }
lamaking 0:a837349b0219 410 }
lamaking 0:a837349b0219 411 if (dm == 0){
lamaking 0:a837349b0219 412 choice = psgm->nextRot; //nahradi segment za rotovany
lamaking 0:a837349b0219 413 }
lamaking 0:a837349b0219 414 dm = 0;
lamaking 0:a837349b0219 415 WriteClrSeg(1, &seg[choice]); //zapise segment do pola
lamaking 0:a837349b0219 416 }
lamaking 0:a837349b0219 417 }
lamaking 0:a837349b0219 418
lamaking 0:a837349b0219 419 if(counter >= 40001){ //zdrzanie
lamaking 0:a837349b0219 420 WriteClrSeg(0, &seg[choice]); //zmaze segment z pola
lamaking 0:a837349b0219 421 counter = 0; //vynuluje counter
lamaking 0:a837349b0219 422 y++; //segment sa posunie dole ak splni podmnienku nizsie
lamaking 0:a837349b0219 423 for(ln = 0; ln < psgm->line; ln++){ //skontroluje ci v priestore pohybu nedojde ku kolizii
lamaking 0:a837349b0219 424 for(col=0; col < psgm->colum; col+=2){
lamaking 0:a837349b0219 425 if(((dispArray[y+ln][x+col] | dispArray[y+ln][x+col+1]) & (psgm->segmentx[ln][col] | psgm->segmentx[ln][col+1]) == 1) || ((y+psgm->line-1) > 7) ){
lamaking 0:a837349b0219 426 y--; //segment sa dostal mimo pola alebo sa dostal do kolizie, suradnica sa vracia
lamaking 0:a837349b0219 427 ln = psgm->line; //aby sa nemusel kontrolovat pripadne zvysok
lamaking 0:a837349b0219 428 col = psgm->colum; //
lamaking 0:a837349b0219 429 dm = 1;
lamaking 0:a837349b0219 430 collision = 1; //detekuje sa kolizia a vyberie sa novy segment
lamaking 0:a837349b0219 431 }
lamaking 0:a837349b0219 432 }
lamaking 0:a837349b0219 433 }
lamaking 0:a837349b0219 434 WriteClrSeg(1, &seg[choice]);
lamaking 0:a837349b0219 435 if (dm == 1){ //po detekcii kolizie sa nastavia pociatocne suradnice
lamaking 0:a837349b0219 436 y = 0;
lamaking 0:a837349b0219 437 x = 6;
lamaking 0:a837349b0219 438 dm = 0;
lamaking 0:a837349b0219 439 }
lamaking 0:a837349b0219 440 }
lamaking 0:a837349b0219 441 }
lamaking 0:a837349b0219 442 /*---------------------------SegmentMove---------------------------*/
lamaking 0:a837349b0219 443
lamaking 0:a837349b0219 444 /*+++++++++++++++++++++++++++WriteClrSeg+++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 445 //zmazanie alebo zapis segmentu pri pohybe maticou
lamaking 0:a837349b0219 446 void WriteClrSeg(int wrtEnab, struct segments *psgm){ //ak je wrtEnab 0 zmaze sa ak 1 zapise sa dany segment do pola, segment sa cita zo struktury vybranej randomom
lamaking 0:a837349b0219 447 static int ln, col;
lamaking 0:a837349b0219 448
lamaking 0:a837349b0219 449 for(ln = 0; ln < psgm->line; ln++){
lamaking 0:a837349b0219 450 for(col=0; col < psgm->colum; col++){
lamaking 0:a837349b0219 451 if(wrtEnab == 1){
lamaking 0:a837349b0219 452 dispArray[ln+y][col+x] |= psgm->segmentx[ln][col];}
lamaking 0:a837349b0219 453 else{
lamaking 0:a837349b0219 454 dispArray[ln+y][col+x] = (psgm->segmentx[ln][col] == 1 ? 0:dispArray[ln+y][col+x] );}
lamaking 0:a837349b0219 455 }
lamaking 0:a837349b0219 456 }
lamaking 0:a837349b0219 457 }
lamaking 0:a837349b0219 458 /*-------------------------WriteClrSeg-----------------------------*/
lamaking 0:a837349b0219 459
lamaking 0:a837349b0219 460 /*++++++++++++++++++++++++++++++DecLvl+++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 461 //obsluha akcelerometra
lamaking 0:a837349b0219 462 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
lamaking 0:a837349b0219 463
lamaking 0:a837349b0219 464 int DecLvl(void){
lamaking 0:a837349b0219 465
lamaking 0:a837349b0219 466 static float ax, ay;
lamaking 0:a837349b0219 467 static float point = 0.34; //nastavenie uhla...cca 30° +- nieco
lamaking 0:a837349b0219 468
lamaking 0:a837349b0219 469 ax = float(acc.getAccX()); //citanie hodnoty v x-ovej osi akcelerometra
lamaking 0:a837349b0219 470 ay = float(acc.getAccY()); //citanie hodnoty v y-ovej osi akcelerometra
lamaking 0:a837349b0219 471
lamaking 0:a837349b0219 472 if((ax > point)){ //k sebe
lamaking 0:a837349b0219 473 return(1);
lamaking 0:a837349b0219 474 }
lamaking 0:a837349b0219 475 else if((ax < -point)){ //od seba
lamaking 0:a837349b0219 476 return(2);
lamaking 0:a837349b0219 477 }
lamaking 0:a837349b0219 478 else if((ay > point)){ //naklon v pravo
lamaking 0:a837349b0219 479 return(3);
lamaking 0:a837349b0219 480 }
lamaking 0:a837349b0219 481 else if((ay < -point)){ //naklon v lavo
lamaking 0:a837349b0219 482 return(4);
lamaking 0:a837349b0219 483 }
lamaking 0:a837349b0219 484 return 0;
lamaking 0:a837349b0219 485 }
lamaking 0:a837349b0219 486 /*--------------------------------DecLvl-------------------------------*/
lamaking 0:a837349b0219 487
lamaking 0:a837349b0219 488 /*++++++++++++++++++++++++++++++Display+++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 489 //rozsvecovanie farieb v riadkoch podla hlavnej matice
lamaking 0:a837349b0219 490 void Display(void){
lamaking 0:a837349b0219 491 static int line = 0;
lamaking 0:a837349b0219 492
lamaking 0:a837349b0219 493 LineAdd(line); //adresacia riadku pomocou funkcie,
lamaking 0:a837349b0219 494 gr0 = !dispArray[line][0]; //parny a nulovy stlpec znaci zelenu farbu (dany bod svieti po pripojeni na zem, ale zapis svietecej casti v poli je pomocou 1)
lamaking 0:a837349b0219 495 gr1 = !dispArray[line][2];
lamaking 0:a837349b0219 496 gr2 = !dispArray[line][4];
lamaking 0:a837349b0219 497 gr3 = !dispArray[line][6];
lamaking 0:a837349b0219 498 gr4 = !dispArray[line][8];
lamaking 0:a837349b0219 499 gr5 = !dispArray[line][10];
lamaking 0:a837349b0219 500 gr6 = !dispArray[line][12];
lamaking 0:a837349b0219 501 gr7 = !dispArray[line][14];
lamaking 0:a837349b0219 502
lamaking 0:a837349b0219 503 rd0 = !dispArray[line][1]; //neparny riadok znaci cervenu farbu
lamaking 0:a837349b0219 504 rd1 = !dispArray[line][3];
lamaking 0:a837349b0219 505 rd2 = !dispArray[line][5];
lamaking 0:a837349b0219 506 rd3 = !dispArray[line][7];
lamaking 0:a837349b0219 507 rd4 = !dispArray[line][9];
lamaking 0:a837349b0219 508 rd5 = !dispArray[line][11];
lamaking 0:a837349b0219 509 rd6 = !dispArray[line][13];
lamaking 0:a837349b0219 510 rd7 = !dispArray[line][15];
lamaking 0:a837349b0219 511 //ak su vysvietene obe, svieti oranzova
lamaking 0:a837349b0219 512 wait(0.001);
lamaking 0:a837349b0219 513 line++; //nasledujuci riadok
lamaking 0:a837349b0219 514 if(line == 8){
lamaking 0:a837349b0219 515 line = 0;
lamaking 0:a837349b0219 516 }
lamaking 0:a837349b0219 517 }
lamaking 0:a837349b0219 518 /*--------------------------------Display-------------------------------*/
lamaking 0:a837349b0219 519
lamaking 0:a837349b0219 520 /*++++++++++++++++++++++++++++++++LineAdd++++++++++++++++++++++++++++++++++*/
lamaking 0:a837349b0219 521 //adresacia riadkov
lamaking 0:a837349b0219 522 void LineAdd(int line){
lamaking 0:a837349b0219 523 switch(line){
lamaking 0:a837349b0219 524 case 0: ln0 = 0;
lamaking 0:a837349b0219 525 ln1 = 0;
lamaking 0:a837349b0219 526 ln2 = 0;
lamaking 0:a837349b0219 527 break;
lamaking 0:a837349b0219 528 case 1: ln0 = 1;
lamaking 0:a837349b0219 529 ln1 = 0;
lamaking 0:a837349b0219 530 ln2 = 0;
lamaking 0:a837349b0219 531 break;
lamaking 0:a837349b0219 532 case 2: ln0 = 0;
lamaking 0:a837349b0219 533 ln1 = 1;
lamaking 0:a837349b0219 534 ln2 = 0;
lamaking 0:a837349b0219 535 break;
lamaking 0:a837349b0219 536 case 3: ln0 = 1;
lamaking 0:a837349b0219 537 ln1 = 1;
lamaking 0:a837349b0219 538 ln2 = 0;
lamaking 0:a837349b0219 539 break;
lamaking 0:a837349b0219 540 case 4: ln0 = 0;
lamaking 0:a837349b0219 541 ln1 = 0;
lamaking 0:a837349b0219 542 ln2 = 1;
lamaking 0:a837349b0219 543 break;
lamaking 0:a837349b0219 544 case 5: ln0 = 1;
lamaking 0:a837349b0219 545 ln1 = 0;
lamaking 0:a837349b0219 546 ln2 = 1;
lamaking 0:a837349b0219 547 break;
lamaking 0:a837349b0219 548 case 6: ln0 = 0;
lamaking 0:a837349b0219 549 ln1 = 1;
lamaking 0:a837349b0219 550 ln2 = 1;
lamaking 0:a837349b0219 551 break;
lamaking 0:a837349b0219 552 case 7: ln0 = 1;
lamaking 0:a837349b0219 553 ln1 = 1;
lamaking 0:a837349b0219 554 ln2 = 1;
lamaking 0:a837349b0219 555 break;
lamaking 0:a837349b0219 556 default: break;
lamaking 0:a837349b0219 557 }
lamaking 0:a837349b0219 558 }
lamaking 0:a837349b0219 559 /*-----------------------LineAdd------------------------*/
lamaking 0:a837349b0219 560
lamaking 0:a837349b0219 561 /*+++++++++++++++++++++++OutPortInit++++++++++++++++++++*/
lamaking 0:a837349b0219 562 //nastavenie portov do pociatocneho stavu - vypnutie/zhasnutie
lamaking 0:a837349b0219 563 void OutPortInit(void){
lamaking 0:a837349b0219 564 rd0 = 1;
lamaking 0:a837349b0219 565 rd1 = 1;
lamaking 0:a837349b0219 566 rd2 = 1;
lamaking 0:a837349b0219 567 rd3 = 1;
lamaking 0:a837349b0219 568 rd4 = 1;
lamaking 0:a837349b0219 569 rd5 = 1;
lamaking 0:a837349b0219 570 rd6 = 1;
lamaking 0:a837349b0219 571 rd7 = 1;
lamaking 0:a837349b0219 572 gr0 = 1;
lamaking 0:a837349b0219 573 gr1 = 1;
lamaking 0:a837349b0219 574 gr2 = 1;
lamaking 0:a837349b0219 575 gr3 = 1;
lamaking 0:a837349b0219 576 gr4 = 1;
lamaking 0:a837349b0219 577 gr5 = 1;
lamaking 0:a837349b0219 578 gr6 = 1;
lamaking 0:a837349b0219 579 gr7 = 1;
lamaking 0:a837349b0219 580 }
lamaking 0:a837349b0219 581 /*---------------------OutPortInit--------------------*/