Version mas reciente 2.0. Contiene manejo de joystick, interrupciones y mejoras de algunos bugs. Game over
Tetrix.cpp@3:c9c3a56609f9, 2018-10-18 (annotated)
- Committer:
- acastiblancoc
- Date:
- Thu Oct 18 23:55:58 2018 +0000
- Revision:
- 3:c9c3a56609f9
- Child:
- 4:6f2a8091f2a0
codigo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acastiblancoc | 3:c9c3a56609f9 | 1 | #include "mbed.h" |
acastiblancoc | 3:c9c3a56609f9 | 2 | #include "figs.h" |
acastiblancoc | 3:c9c3a56609f9 | 3 | |
acastiblancoc | 3:c9c3a56609f9 | 4 | SPI deviceM(PB_15, PB_14, PB_13); |
acastiblancoc | 3:c9c3a56609f9 | 5 | DigitalOut ssel (PB_12); |
acastiblancoc | 3:c9c3a56609f9 | 6 | Serial command(USBTX,USBRX); |
acastiblancoc | 3:c9c3a56609f9 | 7 | |
acastiblancoc | 3:c9c3a56609f9 | 8 | #define VEL 100 |
acastiblancoc | 3:c9c3a56609f9 | 9 | |
acastiblancoc | 3:c9c3a56609f9 | 10 | uint8_t mat_act [11]={0,0,0,0,0,0,0,0,0,0,0}; |
acastiblancoc | 3:c9c3a56609f9 | 11 | uint8_t mat_tmp [8]={0,0,0,0,0,0,0,0}; |
acastiblancoc | 3:c9c3a56609f9 | 12 | uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0}; |
acastiblancoc | 3:c9c3a56609f9 | 13 | uint8_t fila=1,cont=0,ncolumna=1,grados=1,ficha=0; |
acastiblancoc | 3:c9c3a56609f9 | 14 | |
acastiblancoc | 3:c9c3a56609f9 | 15 | void sendSPI(uint8_t d1, uint8_t d2) |
acastiblancoc | 3:c9c3a56609f9 | 16 | { |
acastiblancoc | 3:c9c3a56609f9 | 17 | deviceM.unlock(); |
acastiblancoc | 3:c9c3a56609f9 | 18 | ssel=0; |
acastiblancoc | 3:c9c3a56609f9 | 19 | deviceM.write(d1); |
acastiblancoc | 3:c9c3a56609f9 | 20 | deviceM.write(d2); |
acastiblancoc | 3:c9c3a56609f9 | 21 | ssel=1; |
acastiblancoc | 3:c9c3a56609f9 | 22 | deviceM.lock(); |
acastiblancoc | 3:c9c3a56609f9 | 23 | } |
acastiblancoc | 3:c9c3a56609f9 | 24 | |
acastiblancoc | 3:c9c3a56609f9 | 25 | |
acastiblancoc | 3:c9c3a56609f9 | 26 | |
acastiblancoc | 3:c9c3a56609f9 | 27 | void test() |
acastiblancoc | 3:c9c3a56609f9 | 28 | { |
acastiblancoc | 3:c9c3a56609f9 | 29 | sendSPI(0x0c,1); |
acastiblancoc | 3:c9c3a56609f9 | 30 | sendSPI(0x0b,7); |
acastiblancoc | 3:c9c3a56609f9 | 31 | sendSPI(0x09,0); |
acastiblancoc | 3:c9c3a56609f9 | 32 | sendSPI(0x0A,0x0E); |
acastiblancoc | 3:c9c3a56609f9 | 33 | int j; |
acastiblancoc | 3:c9c3a56609f9 | 34 | for (j=0;j<4;j++){ |
acastiblancoc | 3:c9c3a56609f9 | 35 | sendSPI(0x0F,1); |
acastiblancoc | 3:c9c3a56609f9 | 36 | wait (0.2); |
acastiblancoc | 3:c9c3a56609f9 | 37 | sendSPI(0x0F,0); |
acastiblancoc | 3:c9c3a56609f9 | 38 | wait (0.2); |
acastiblancoc | 3:c9c3a56609f9 | 39 | } |
acastiblancoc | 3:c9c3a56609f9 | 40 | } |
acastiblancoc | 3:c9c3a56609f9 | 41 | |
acastiblancoc | 3:c9c3a56609f9 | 42 | void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) |
acastiblancoc | 3:c9c3a56609f9 | 43 | { |
acastiblancoc | 3:c9c3a56609f9 | 44 | if(((mat_act[cont-2]& *(fig+c)>>columna-1)==0)&&((mat_act[cont-1]& *(fig+b)>>columna-1)==0)&&((mat_act[cont]& *(fig+a)>>columna-1)==0)){ |
acastiblancoc | 3:c9c3a56609f9 | 45 | mat_tmp[0]= *(fig+5)>>columna-1; |
acastiblancoc | 3:c9c3a56609f9 | 46 | mat_tmp[1]= *(fig+a)>>columna-1; |
acastiblancoc | 3:c9c3a56609f9 | 47 | mat_tmp[2]= *(fig+b)>>columna-1; |
acastiblancoc | 3:c9c3a56609f9 | 48 | mat_tmp[3]= *(fig+c)>>columna-1; |
acastiblancoc | 3:c9c3a56609f9 | 49 | } |
acastiblancoc | 3:c9c3a56609f9 | 50 | } |
acastiblancoc | 3:c9c3a56609f9 | 51 | |
acastiblancoc | 3:c9c3a56609f9 | 52 | void borrar() |
acastiblancoc | 3:c9c3a56609f9 | 53 | { |
acastiblancoc | 3:c9c3a56609f9 | 54 | int i; |
acastiblancoc | 3:c9c3a56609f9 | 55 | for(i=0;i<=8;i++) |
acastiblancoc | 3:c9c3a56609f9 | 56 | { |
acastiblancoc | 3:c9c3a56609f9 | 57 | sendSPI(i,0); |
acastiblancoc | 3:c9c3a56609f9 | 58 | mat_act[i]=0; |
acastiblancoc | 3:c9c3a56609f9 | 59 | } |
acastiblancoc | 3:c9c3a56609f9 | 60 | }; |
acastiblancoc | 3:c9c3a56609f9 | 61 | |
acastiblancoc | 3:c9c3a56609f9 | 62 | void buscar_fil(){ |
acastiblancoc | 3:c9c3a56609f9 | 63 | for (uint8_t i=0;i<9;i++){ |
acastiblancoc | 3:c9c3a56609f9 | 64 | if((mat_act[i]& mat_tmp[3])==0){ |
acastiblancoc | 3:c9c3a56609f9 | 65 | fila=i; |
acastiblancoc | 3:c9c3a56609f9 | 66 | } |
acastiblancoc | 3:c9c3a56609f9 | 67 | if((mat_act[i]& mat_tmp[3])!=0){ |
acastiblancoc | 3:c9c3a56609f9 | 68 | fila=i-1; |
acastiblancoc | 3:c9c3a56609f9 | 69 | i=9; |
acastiblancoc | 3:c9c3a56609f9 | 70 | } |
acastiblancoc | 3:c9c3a56609f9 | 71 | if((mat_act[i]& mat_tmp[2])!=0){ |
acastiblancoc | 3:c9c3a56609f9 | 72 | fila=i; |
acastiblancoc | 3:c9c3a56609f9 | 73 | i=9; |
acastiblancoc | 3:c9c3a56609f9 | 74 | } |
acastiblancoc | 3:c9c3a56609f9 | 75 | if((mat_act[i]& mat_tmp[1])!=0){ |
acastiblancoc | 3:c9c3a56609f9 | 76 | fila=i+1; |
acastiblancoc | 3:c9c3a56609f9 | 77 | i=9; |
acastiblancoc | 3:c9c3a56609f9 | 78 | //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); |
acastiblancoc | 3:c9c3a56609f9 | 79 | } |
acastiblancoc | 3:c9c3a56609f9 | 80 | } |
acastiblancoc | 3:c9c3a56609f9 | 81 | if(fila>8) |
acastiblancoc | 3:c9c3a56609f9 | 82 | fila=8; |
acastiblancoc | 3:c9c3a56609f9 | 83 | if(fila<=1){ |
acastiblancoc | 3:c9c3a56609f9 | 84 | wait_ms(VEL); |
acastiblancoc | 3:c9c3a56609f9 | 85 | borrar(); |
acastiblancoc | 3:c9c3a56609f9 | 86 | fila=8; |
acastiblancoc | 3:c9c3a56609f9 | 87 | } |
acastiblancoc | 3:c9c3a56609f9 | 88 | }; |
acastiblancoc | 3:c9c3a56609f9 | 89 | |
acastiblancoc | 3:c9c3a56609f9 | 90 | void guardar_mat(){ |
acastiblancoc | 3:c9c3a56609f9 | 91 | mat_act[fila-3]=mat_tmp1[0]; |
acastiblancoc | 3:c9c3a56609f9 | 92 | mat_act[fila-2]=mat_tmp1[1]; |
acastiblancoc | 3:c9c3a56609f9 | 93 | mat_act[fila-1]=mat_tmp1[2]; |
acastiblancoc | 3:c9c3a56609f9 | 94 | mat_act[fila ]=mat_tmp1[3]; |
acastiblancoc | 3:c9c3a56609f9 | 95 | }; |
acastiblancoc | 3:c9c3a56609f9 | 96 | |
acastiblancoc | 3:c9c3a56609f9 | 97 | void correr_fig(){ |
acastiblancoc | 3:c9c3a56609f9 | 98 | |
acastiblancoc | 3:c9c3a56609f9 | 99 | uint8_t correr=command.getc(); |
acastiblancoc | 3:c9c3a56609f9 | 100 | |
acastiblancoc | 3:c9c3a56609f9 | 101 | if(correr==30) |
acastiblancoc | 3:c9c3a56609f9 | 102 | correr=1; |
acastiblancoc | 3:c9c3a56609f9 | 103 | |
acastiblancoc | 3:c9c3a56609f9 | 104 | if(correr==29) |
acastiblancoc | 3:c9c3a56609f9 | 105 | correr=2; |
acastiblancoc | 3:c9c3a56609f9 | 106 | |
acastiblancoc | 3:c9c3a56609f9 | 107 | if(correr==28) |
acastiblancoc | 3:c9c3a56609f9 | 108 | correr=3; |
acastiblancoc | 3:c9c3a56609f9 | 109 | |
acastiblancoc | 3:c9c3a56609f9 | 110 | if(correr==31) |
acastiblancoc | 3:c9c3a56609f9 | 111 | correr=0; |
acastiblancoc | 3:c9c3a56609f9 | 112 | |
acastiblancoc | 3:c9c3a56609f9 | 113 | switch (correr){ |
acastiblancoc | 3:c9c3a56609f9 | 114 | |
acastiblancoc | 3:c9c3a56609f9 | 115 | case 0: |
acastiblancoc | 3:c9c3a56609f9 | 116 | cont++; |
acastiblancoc | 3:c9c3a56609f9 | 117 | if(cont>8)cont=8; |
acastiblancoc | 3:c9c3a56609f9 | 118 | break; |
acastiblancoc | 3:c9c3a56609f9 | 119 | |
acastiblancoc | 3:c9c3a56609f9 | 120 | case 1: |
acastiblancoc | 3:c9c3a56609f9 | 121 | if( (mat_tmp[1]==2) || (mat_tmp[1]==1)|| (mat_tmp[1]==3)) |
acastiblancoc | 3:c9c3a56609f9 | 122 | ncolumna--; |
acastiblancoc | 3:c9c3a56609f9 | 123 | grados++; |
acastiblancoc | 3:c9c3a56609f9 | 124 | if(grados>4) |
acastiblancoc | 3:c9c3a56609f9 | 125 | grados=1; |
acastiblancoc | 3:c9c3a56609f9 | 126 | break; |
acastiblancoc | 3:c9c3a56609f9 | 127 | |
acastiblancoc | 3:c9c3a56609f9 | 128 | case 2: |
acastiblancoc | 3:c9c3a56609f9 | 129 | if((mat_tmp[2]==7 || mat_tmp[3]==7)||(mat_tmp[2]==3 && mat_tmp[3]==6)) { |
acastiblancoc | 3:c9c3a56609f9 | 130 | command.printf("a"); |
acastiblancoc | 3:c9c3a56609f9 | 131 | ncolumna=5; |
acastiblancoc | 3:c9c3a56609f9 | 132 | } |
acastiblancoc | 3:c9c3a56609f9 | 133 | if((mat_tmp[1]==2 && mat_tmp[2]==2 && mat_tmp[3]==2)){ |
acastiblancoc | 3:c9c3a56609f9 | 134 | command.printf("b"); |
acastiblancoc | 3:c9c3a56609f9 | 135 | ncolumna=7; |
acastiblancoc | 3:c9c3a56609f9 | 136 | } |
acastiblancoc | 3:c9c3a56609f9 | 137 | if (ncolumna<7){ |
acastiblancoc | 3:c9c3a56609f9 | 138 | command.printf("c"); |
acastiblancoc | 3:c9c3a56609f9 | 139 | ncolumna++; |
acastiblancoc | 3:c9c3a56609f9 | 140 | } |
acastiblancoc | 3:c9c3a56609f9 | 141 | break; |
acastiblancoc | 3:c9c3a56609f9 | 142 | /*if( ((mat_tmp[2]==3) && (mat_tmp[3]==6)) || (mat_tmp[2]==7) || (mat_tmp[3]==7)) { |
acastiblancoc | 3:c9c3a56609f9 | 143 | command.printf("c"); |
acastiblancoc | 3:c9c3a56609f9 | 144 | ncolumna=6; |
acastiblancoc | 3:c9c3a56609f9 | 145 | } |
acastiblancoc | 3:c9c3a56609f9 | 146 | |
acastiblancoc | 3:c9c3a56609f9 | 147 | if( (mat_tmp[1]==2) &&(mat_tmp[2]==2) && (mat_tmp[3]==2) ) { |
acastiblancoc | 3:c9c3a56609f9 | 148 | command.printf("d"); |
acastiblancoc | 3:c9c3a56609f9 | 149 | ncolumna=7; |
acastiblancoc | 3:c9c3a56609f9 | 150 | } |
acastiblancoc | 3:c9c3a56609f9 | 151 | */ |
acastiblancoc | 3:c9c3a56609f9 | 152 | |
acastiblancoc | 3:c9c3a56609f9 | 153 | /*if(ncolumna>7) |
acastiblancoc | 3:c9c3a56609f9 | 154 | {command.printf("d"); |
acastiblancoc | 3:c9c3a56609f9 | 155 | ncolumna=7;}*/ |
acastiblancoc | 3:c9c3a56609f9 | 156 | //correr a la Derecha; |
acastiblancoc | 3:c9c3a56609f9 | 157 | |
acastiblancoc | 3:c9c3a56609f9 | 158 | case 3: |
acastiblancoc | 3:c9c3a56609f9 | 159 | ncolumna--; |
acastiblancoc | 3:c9c3a56609f9 | 160 | if(ncolumna<1) |
acastiblancoc | 3:c9c3a56609f9 | 161 | ncolumna=1; |
acastiblancoc | 3:c9c3a56609f9 | 162 | break; |
acastiblancoc | 3:c9c3a56609f9 | 163 | } |
acastiblancoc | 3:c9c3a56609f9 | 164 | |
acastiblancoc | 3:c9c3a56609f9 | 165 | } |
acastiblancoc | 3:c9c3a56609f9 | 166 | |
acastiblancoc | 3:c9c3a56609f9 | 167 | |
acastiblancoc | 3:c9c3a56609f9 | 168 | void imp_mat(uint8_t *temp){ |
acastiblancoc | 3:c9c3a56609f9 | 169 | mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); |
acastiblancoc | 3:c9c3a56609f9 | 170 | mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); |
acastiblancoc | 3:c9c3a56609f9 | 171 | mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); |
acastiblancoc | 3:c9c3a56609f9 | 172 | mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); |
acastiblancoc | 3:c9c3a56609f9 | 173 | sendSPI(cont-3,mat_tmp1[0]); |
acastiblancoc | 3:c9c3a56609f9 | 174 | sendSPI(cont-2,mat_tmp1[1]); |
acastiblancoc | 3:c9c3a56609f9 | 175 | sendSPI(cont-1,mat_tmp1[2]); |
acastiblancoc | 3:c9c3a56609f9 | 176 | sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 |
acastiblancoc | 3:c9c3a56609f9 | 177 | wait_ms(VEL); |
acastiblancoc | 3:c9c3a56609f9 | 178 | } |
acastiblancoc | 3:c9c3a56609f9 | 179 | |
acastiblancoc | 3:c9c3a56609f9 | 180 | void dibujar(char type_fig,char grados,char columna) |
acastiblancoc | 3:c9c3a56609f9 | 181 | { |
acastiblancoc | 3:c9c3a56609f9 | 182 | switch (type_fig){ |
acastiblancoc | 3:c9c3a56609f9 | 183 | case 1: if(columna>7) |
acastiblancoc | 3:c9c3a56609f9 | 184 | columna=7; |
acastiblancoc | 3:c9c3a56609f9 | 185 | cop_mat(5,1,1,PLANT,columna); break; |
acastiblancoc | 3:c9c3a56609f9 | 186 | |
acastiblancoc | 3:c9c3a56609f9 | 187 | case 2: if(grados==1 || grados==3) |
acastiblancoc | 3:c9c3a56609f9 | 188 | cop_mat(0,0,0,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 189 | if(grados==2 || grados==4) |
acastiblancoc | 3:c9c3a56609f9 | 190 | cop_mat(5,5,2,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 191 | break; |
acastiblancoc | 3:c9c3a56609f9 | 192 | |
acastiblancoc | 3:c9c3a56609f9 | 193 | case 3: if(grados==1) |
acastiblancoc | 3:c9c3a56609f9 | 194 | cop_mat(0,0,1,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 195 | if(grados==2) |
acastiblancoc | 3:c9c3a56609f9 | 196 | cop_mat(5,2,0,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 197 | if(grados==3) |
acastiblancoc | 3:c9c3a56609f9 | 198 | cop_mat(1,3,3,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 199 | if(grados==4) |
acastiblancoc | 3:c9c3a56609f9 | 200 | cop_mat(5,6,2,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 201 | break; |
acastiblancoc | 3:c9c3a56609f9 | 202 | |
acastiblancoc | 3:c9c3a56609f9 | 203 | case 4: if(grados==1) |
acastiblancoc | 3:c9c3a56609f9 | 204 | cop_mat(5,3,2,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 205 | if(grados==2) |
acastiblancoc | 3:c9c3a56609f9 | 206 | cop_mat(0,1,0,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 207 | if(grados==3) |
acastiblancoc | 3:c9c3a56609f9 | 208 | cop_mat(5,2,3,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 209 | if(grados==4) |
acastiblancoc | 3:c9c3a56609f9 | 210 | cop_mat(3,1,3,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 211 | break; |
acastiblancoc | 3:c9c3a56609f9 | 212 | |
acastiblancoc | 3:c9c3a56609f9 | 213 | case 5: if(grados==1 || grados==3) |
acastiblancoc | 3:c9c3a56609f9 | 214 | cop_mat(5,4,1,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 215 | if(grados==2 || grados==4) |
acastiblancoc | 3:c9c3a56609f9 | 216 | cop_mat(0,1,3,PLANT,columna); |
acastiblancoc | 3:c9c3a56609f9 | 217 | break; |
acastiblancoc | 3:c9c3a56609f9 | 218 | } |
acastiblancoc | 3:c9c3a56609f9 | 219 | } |
acastiblancoc | 3:c9c3a56609f9 | 220 | |
acastiblancoc | 3:c9c3a56609f9 | 221 | void fichas(){ |
acastiblancoc | 3:c9c3a56609f9 | 222 | ficha= rand() % 5+1; |
acastiblancoc | 3:c9c3a56609f9 | 223 | grados= rand() % 4+1; |
acastiblancoc | 3:c9c3a56609f9 | 224 | ncolumna= 4; |
acastiblancoc | 3:c9c3a56609f9 | 225 | } |
acastiblancoc | 3:c9c3a56609f9 | 226 | |
acastiblancoc | 3:c9c3a56609f9 | 227 | void buscar_linea(){ |
acastiblancoc | 3:c9c3a56609f9 | 228 | for(uint8_t i=0;i<9;i++){ |
acastiblancoc | 3:c9c3a56609f9 | 229 | if(mat_act[i]==255){ |
acastiblancoc | 3:c9c3a56609f9 | 230 | for(uint8_t a=i;a>=1;a--){ |
acastiblancoc | 3:c9c3a56609f9 | 231 | sendSPI(a,mat_act[a-1]); |
acastiblancoc | 3:c9c3a56609f9 | 232 | } |
acastiblancoc | 3:c9c3a56609f9 | 233 | } |
acastiblancoc | 3:c9c3a56609f9 | 234 | } |
acastiblancoc | 3:c9c3a56609f9 | 235 | } |
acastiblancoc | 3:c9c3a56609f9 | 236 | |
acastiblancoc | 3:c9c3a56609f9 | 237 | int main() { |
acastiblancoc | 3:c9c3a56609f9 | 238 | test(); |
acastiblancoc | 3:c9c3a56609f9 | 239 | borrar(); |
acastiblancoc | 3:c9c3a56609f9 | 240 | while(1){ |
acastiblancoc | 3:c9c3a56609f9 | 241 | fichas(); |
acastiblancoc | 3:c9c3a56609f9 | 242 | buscar_fil(); |
acastiblancoc | 3:c9c3a56609f9 | 243 | cont=0; |
acastiblancoc | 3:c9c3a56609f9 | 244 | while (cont<fila){ |
acastiblancoc | 3:c9c3a56609f9 | 245 | correr_fig(); |
acastiblancoc | 3:c9c3a56609f9 | 246 | dibujar(ficha,grados,ncolumna); |
acastiblancoc | 3:c9c3a56609f9 | 247 | buscar_fil(); |
acastiblancoc | 3:c9c3a56609f9 | 248 | imp_mat(mat_tmp); |
acastiblancoc | 3:c9c3a56609f9 | 249 | } |
acastiblancoc | 3:c9c3a56609f9 | 250 | buscar_linea(); |
acastiblancoc | 3:c9c3a56609f9 | 251 | guardar_mat(); |
acastiblancoc | 3:c9c3a56609f9 | 252 | } |
acastiblancoc | 3:c9c3a56609f9 | 253 | } |