cristhian david santos oggonei
/
METRIX_presentacion
Entrega fnal tetris
metrix.cpp@5:616b5894b610, 2018-10-17 (annotated)
- Committer:
- jiuk
- Date:
- Wed Oct 17 18:36:09 2018 +0000
- Revision:
- 5:616b5894b610
- Parent:
- 4:78194ed9ff6b
- Child:
- 6:973f374e8ea4
Entrega segundo corte
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jiuk | 0:2b55edb1057b | 1 | #include "mbed.h" |
jiuk | 0:2b55edb1057b | 2 | #include "moggo.h" //agrega la libreria que contiene las figuras. |
jiuk | 0:2b55edb1057b | 3 | |
jiuk | 5:616b5894b610 | 4 | //SPI deviceM(PB_15, PB_14, PB_13); |
jiuk | 5:616b5894b610 | 5 | //DigitalOut ssel (PB_12); |
jiuk | 5:616b5894b610 | 6 | SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi). |
jiuk | 5:616b5894b610 | 7 | DigitalOut ssel (PB_9); //Chip Select para el controlador. |
jiuk | 5:616b5894b610 | 8 | Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. |
jiuk | 5:616b5894b610 | 9 | Serial com_tar(PC_10,PC_11,9600); //habilitar la comunicacion serial a traves del puerto usb. |
jiuk | 5:616b5894b610 | 10 | //Serial com_tar(PC_12,PD_2); //F446RE PC_10 TX; PC_11 RX;PC_12 TX; PD_2 RX. |
jiuk | 5:616b5894b610 | 11 | //Serial com_tar(PC_6,PC_5); //F446RE PC_6 TX; PC_5 RX; |
jiuk | 5:616b5894b610 | 12 | //Serial com_tar(PA_15,PB_7); //F411R PA_15 TX; PB_7 RX; |
jiuk | 0:2b55edb1057b | 13 | |
jiuk | 5:616b5894b610 | 14 | #define VEL 120 //Velocidad de actualizacion de dato en el controlador. |
jiuk | 0:2b55edb1057b | 15 | |
jiuk | 0:2b55edb1057b | 16 | uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla. |
jiuk | 5:616b5894b610 | 17 | uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas. |
jiuk | 0:2b55edb1057b | 18 | uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0}; |
jiuk | 3:7ae3dd76ba0a | 19 | uint8_t cont=0,ncolumna=1,grados=1,ficha=0; |
jiuk | 3:7ae3dd76ba0a | 20 | int fila=0; |
jiuk | 5:616b5894b610 | 21 | |
jiuk | 0:2b55edb1057b | 22 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:2b55edb1057b | 23 | { |
jiuk | 0:2b55edb1057b | 24 | deviceM.unlock(); |
jiuk | 0:2b55edb1057b | 25 | ssel=0; |
jiuk | 0:2b55edb1057b | 26 | deviceM.write(d1); |
jiuk | 0:2b55edb1057b | 27 | deviceM.write(d2); |
jiuk | 0:2b55edb1057b | 28 | ssel=1; |
jiuk | 0:2b55edb1057b | 29 | deviceM.lock(); |
jiuk | 0:2b55edb1057b | 30 | } |
jiuk | 0:2b55edb1057b | 31 | |
jiuk | 5:616b5894b610 | 32 | void test() //test |
jiuk | 0:2b55edb1057b | 33 | { |
jiuk | 0:2b55edb1057b | 34 | sendSPI(0x09,0); //no decodificacion |
jiuk | 5:616b5894b610 | 35 | sendSPI(0x0A,0x00); //intensidad |
jiuk | 0:2b55edb1057b | 36 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:2b55edb1057b | 37 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:2b55edb1057b | 38 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:2b55edb1057b | 39 | } |
jiuk | 0:2b55edb1057b | 40 | |
jiuk | 0:2b55edb1057b | 41 | void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) |
jiuk | 0:2b55edb1057b | 42 | { |
jiuk | 3:7ae3dd76ba0a | 43 | 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)){ |
jiuk | 2:fc251f89bd1b | 44 | mat_tmp[0]= *(fig+5)>>columna-1; |
jiuk | 2:fc251f89bd1b | 45 | mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila. |
jiuk | 2:fc251f89bd1b | 46 | mat_tmp[2]= *(fig+b)>>columna-1; |
jiuk | 2:fc251f89bd1b | 47 | mat_tmp[3]= *(fig+c)>>columna-1; |
jiuk | 3:7ae3dd76ba0a | 48 | } |
jiuk | 0:2b55edb1057b | 49 | } |
jiuk | 0:2b55edb1057b | 50 | |
jiuk | 2:fc251f89bd1b | 51 | void borrar() //borrar toda la matriz; |
jiuk | 0:2b55edb1057b | 52 | { |
jiuk | 0:2b55edb1057b | 53 | int i; |
jiuk | 0:2b55edb1057b | 54 | for(i=0;i<=8;i++) |
jiuk | 0:2b55edb1057b | 55 | { |
jiuk | 0:2b55edb1057b | 56 | sendSPI(i,0); |
jiuk | 0:2b55edb1057b | 57 | mat_act[i]=0; //Matriz para mostrar en pantalla. |
jiuk | 0:2b55edb1057b | 58 | } |
jiuk | 0:2b55edb1057b | 59 | }; |
jiuk | 0:2b55edb1057b | 60 | |
jiuk | 0:2b55edb1057b | 61 | void buscar_fil(){ |
jiuk | 0:2b55edb1057b | 62 | for (uint8_t i=0;i<9;i++){ |
jiuk | 0:2b55edb1057b | 63 | if((mat_act[i]& mat_tmp[3])==0){ |
jiuk | 0:2b55edb1057b | 64 | fila=i; |
jiuk | 0:2b55edb1057b | 65 | //command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 66 | } |
jiuk | 0:2b55edb1057b | 67 | if((mat_act[i]& mat_tmp[3])!=0){ |
jiuk | 0:2b55edb1057b | 68 | fila=i-1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 69 | i=9; |
jiuk | 0:2b55edb1057b | 70 | //command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 71 | } |
jiuk | 0:2b55edb1057b | 72 | if((mat_act[i]& mat_tmp[2])!=0){ //se condicionan las salidas de la busqueda de lanueva fila |
jiuk | 0:2b55edb1057b | 73 | fila=i; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 74 | i=9; |
jiuk | 0:2b55edb1057b | 75 | //command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]); |
jiuk | 0:2b55edb1057b | 76 | } |
jiuk | 0:2b55edb1057b | 77 | if((mat_act[i]& mat_tmp[1])!=0){ |
jiuk | 0:2b55edb1057b | 78 | fila=i+1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 79 | i=9; |
jiuk | 0:2b55edb1057b | 80 | //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); |
jiuk | 0:2b55edb1057b | 81 | } |
jiuk | 0:2b55edb1057b | 82 | } |
jiuk | 0:2b55edb1057b | 83 | if(fila>8) |
jiuk | 2:fc251f89bd1b | 84 | fila=8; |
jiuk | 3:7ae3dd76ba0a | 85 | if(fila<=0){ |
jiuk | 2:fc251f89bd1b | 86 | wait_ms(VEL); |
jiuk | 2:fc251f89bd1b | 87 | borrar(); |
jiuk | 2:fc251f89bd1b | 88 | fila=8; |
jiuk | 0:2b55edb1057b | 89 | } |
jiuk | 0:2b55edb1057b | 90 | command.printf("\n buscar fila \n %d",fila); |
jiuk | 0:2b55edb1057b | 91 | command.printf("\n ******************************* \n "); |
jiuk | 0:2b55edb1057b | 92 | }; |
jiuk | 0:2b55edb1057b | 93 | |
jiuk | 0:2b55edb1057b | 94 | void guardar_mat(){ |
jiuk | 0:2b55edb1057b | 95 | mat_act[fila-3]=mat_tmp1[0]; |
jiuk | 0:2b55edb1057b | 96 | mat_act[fila-2]=mat_tmp1[1]; |
jiuk | 2:fc251f89bd1b | 97 | mat_act[fila-1]=mat_tmp1[2]; |
jiuk | 2:fc251f89bd1b | 98 | mat_act[fila ]=mat_tmp1[3]; |
jiuk | 2:fc251f89bd1b | 99 | }; |
jiuk | 4:78194ed9ff6b | 100 | |
jiuk | 0:2b55edb1057b | 101 | void correr_fig(){ |
jiuk | 2:fc251f89bd1b | 102 | |
jiuk | 4:78194ed9ff6b | 103 | uint8_t correr; |
jiuk | 5:616b5894b610 | 104 | |
jiuk | 4:78194ed9ff6b | 105 | char t=1; |
jiuk | 3:7ae3dd76ba0a | 106 | while (t==1 && command.readable()==0){ |
jiuk | 4:78194ed9ff6b | 107 | wait_ms(0.1); |
jiuk | 3:7ae3dd76ba0a | 108 | t=0; |
jiuk | 3:7ae3dd76ba0a | 109 | } |
jiuk | 5:616b5894b610 | 110 | if (command.readable()){ |
jiuk | 3:7ae3dd76ba0a | 111 | correr=command.getc(); |
jiuk | 5:616b5894b610 | 112 | com_tar.putc(correr); |
jiuk | 5:616b5894b610 | 113 | wait_ms(100); |
jiuk | 5:616b5894b610 | 114 | } |
jiuk | 3:7ae3dd76ba0a | 115 | else |
jiuk | 5:616b5894b610 | 116 | correr=0; |
jiuk | 4:78194ed9ff6b | 117 | |
jiuk | 5:616b5894b610 | 118 | //correr=command.getc(); |
jiuk | 4:78194ed9ff6b | 119 | |
jiuk | 0:2b55edb1057b | 120 | if(correr==30) //Arriba, voltear figura. |
jiuk | 0:2b55edb1057b | 121 | correr=1; |
jiuk | 2:fc251f89bd1b | 122 | |
jiuk | 0:2b55edb1057b | 123 | if(correr==29) //izquierda, correr la figura. |
jiuk | 0:2b55edb1057b | 124 | correr=2; |
jiuk | 2:fc251f89bd1b | 125 | |
jiuk | 0:2b55edb1057b | 126 | if(correr==28) //Derecha correr la figura. |
jiuk | 0:2b55edb1057b | 127 | correr=3; |
jiuk | 2:fc251f89bd1b | 128 | |
jiuk | 3:7ae3dd76ba0a | 129 | if(correr==31) //Bajar la figura. |
jiuk | 4:78194ed9ff6b | 130 | correr=0; |
jiuk | 3:7ae3dd76ba0a | 131 | |
jiuk | 2:fc251f89bd1b | 132 | |
jiuk | 0:2b55edb1057b | 133 | switch (correr){ |
jiuk | 2:fc251f89bd1b | 134 | |
jiuk | 0:2b55edb1057b | 135 | case 0: |
jiuk | 2:fc251f89bd1b | 136 | cont++; |
jiuk | 2:fc251f89bd1b | 137 | if(cont>8)cont=8; |
jiuk | 2:fc251f89bd1b | 138 | break; //cae la figura; |
jiuk | 2:fc251f89bd1b | 139 | |
jiuk | 0:2b55edb1057b | 140 | case 1: |
jiuk | 3:7ae3dd76ba0a | 141 | if((mat_tmp[1]==2) || (mat_tmp[1]==1) || (mat_tmp[1]==3)) //faltan condiciones para evitar desplazamiento innecesario. |
jiuk | 2:fc251f89bd1b | 142 | ncolumna--; |
jiuk | 0:2b55edb1057b | 143 | grados++; |
jiuk | 0:2b55edb1057b | 144 | if(grados>4) |
jiuk | 0:2b55edb1057b | 145 | grados=1; |
jiuk | 2:fc251f89bd1b | 146 | break; //Girar la figura; |
jiuk | 2:fc251f89bd1b | 147 | |
jiuk | 0:2b55edb1057b | 148 | case 2: |
jiuk | 4:78194ed9ff6b | 149 | if((mat_tmp[1]!=1) && (mat_tmp[2]!=1) && (mat_tmp[3]!=1)){ |
jiuk | 4:78194ed9ff6b | 150 | if((mat_tmp[1]!=3) && (mat_tmp[2]!=3) && (mat_tmp[3]!=3)){ |
jiuk | 4:78194ed9ff6b | 151 | if((mat_tmp[1]!=7) && (mat_tmp[2]!=7) && (mat_tmp[3]!=7)) |
jiuk | 4:78194ed9ff6b | 152 | ncolumna++; |
jiuk | 4:78194ed9ff6b | 153 | } |
jiuk | 1:bd308ee8776e | 154 | } |
jiuk | 4:78194ed9ff6b | 155 | |
jiuk | 2:fc251f89bd1b | 156 | break; //correr a la Derecha; |
jiuk | 2:fc251f89bd1b | 157 | |
jiuk | 0:2b55edb1057b | 158 | case 3: |
jiuk | 0:2b55edb1057b | 159 | ncolumna--; |
jiuk | 0:2b55edb1057b | 160 | if(ncolumna<1) |
jiuk | 0:2b55edb1057b | 161 | ncolumna=1; |
jiuk | 2:fc251f89bd1b | 162 | break; //correr a la izquierda; |
jiuk | 4:78194ed9ff6b | 163 | /* |
jiuk | 3:7ae3dd76ba0a | 164 | case 4: |
jiuk | 3:7ae3dd76ba0a | 165 | cont++; |
jiuk | 3:7ae3dd76ba0a | 166 | if(cont>8)cont=8; |
jiuk | 3:7ae3dd76ba0a | 167 | break; |
jiuk | 4:78194ed9ff6b | 168 | */ |
jiuk | 0:2b55edb1057b | 169 | } |
jiuk | 0:2b55edb1057b | 170 | } |
jiuk | 0:2b55edb1057b | 171 | |
jiuk | 1:bd308ee8776e | 172 | void imp_mat(uint8_t *temp){ //se genera la variable global cont. |
jiuk | 0:2b55edb1057b | 173 | mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); |
jiuk | 0:2b55edb1057b | 174 | mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); |
jiuk | 0:2b55edb1057b | 175 | mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); |
jiuk | 0:2b55edb1057b | 176 | mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart. |
jiuk | 0:2b55edb1057b | 177 | sendSPI(cont-3,mat_tmp1[0]); |
jiuk | 0:2b55edb1057b | 178 | sendSPI(cont-2,mat_tmp1[1]); |
jiuk | 0:2b55edb1057b | 179 | sendSPI(cont-1,mat_tmp1[2]); |
jiuk | 1:bd308ee8776e | 180 | sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 |
jiuk | 0:2b55edb1057b | 181 | wait_ms(VEL); |
jiuk | 0:2b55edb1057b | 182 | } |
jiuk | 0:2b55edb1057b | 183 | |
jiuk | 0:2b55edb1057b | 184 | void dibujar(char type_fig,char grados,char columna) |
jiuk | 0:2b55edb1057b | 185 | { |
jiuk | 5:616b5894b610 | 186 | switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 0:2b55edb1057b | 187 | case 1: if(columna>7) |
jiuk | 0:2b55edb1057b | 188 | columna=7; |
jiuk | 0:2b55edb1057b | 189 | cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro; |
jiuk | 0:2b55edb1057b | 190 | |
jiuk | 0:2b55edb1057b | 191 | case 2: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 192 | cop_mat(0,0,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 193 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 194 | cop_mat(5,5,2,FIG_ALL,columna); //2: I; |
jiuk | 0:2b55edb1057b | 195 | break; |
jiuk | 0:2b55edb1057b | 196 | |
jiuk | 0:2b55edb1057b | 197 | case 3: if(grados==1) |
jiuk | 0:2b55edb1057b | 198 | cop_mat(0,0,1,FIG_ALL,columna); //3: L; |
jiuk | 0:2b55edb1057b | 199 | if(grados==2) |
jiuk | 0:2b55edb1057b | 200 | cop_mat(5,2,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 201 | if(grados==3) |
jiuk | 0:2b55edb1057b | 202 | cop_mat(1,3,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 203 | if(grados==4) |
jiuk | 0:2b55edb1057b | 204 | cop_mat(5,6,2,FIG_ALL,columna); |
jiuk | 1:bd308ee8776e | 205 | break; |
jiuk | 0:2b55edb1057b | 206 | |
jiuk | 0:2b55edb1057b | 207 | case 4: if(grados==1) |
jiuk | 0:2b55edb1057b | 208 | cop_mat(5,3,2,FIG_ALL,columna); //4: T; |
jiuk | 0:2b55edb1057b | 209 | if(grados==2) |
jiuk | 0:2b55edb1057b | 210 | cop_mat(0,1,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 211 | if(grados==3) |
jiuk | 0:2b55edb1057b | 212 | cop_mat(5,2,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 213 | if(grados==4) |
jiuk | 0:2b55edb1057b | 214 | cop_mat(3,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 215 | break; |
jiuk | 0:2b55edb1057b | 216 | |
jiuk | 0:2b55edb1057b | 217 | case 5: if(grados==1 || grados==3) |
jiuk | 5:616b5894b610 | 218 | cop_mat(5,4,1,FIG_ALL,columna); //5: S; |
jiuk | 0:2b55edb1057b | 219 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 220 | cop_mat(0,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 221 | break; |
jiuk | 0:2b55edb1057b | 222 | } |
jiuk | 0:2b55edb1057b | 223 | } |
jiuk | 0:2b55edb1057b | 224 | |
jiuk | 0:2b55edb1057b | 225 | void fichas(){ |
jiuk | 0:2b55edb1057b | 226 | ficha= rand() % 5+1; |
jiuk | 4:78194ed9ff6b | 227 | //grados= rand() % 4+1; |
jiuk | 2:fc251f89bd1b | 228 | ncolumna=4; |
jiuk | 0:2b55edb1057b | 229 | } |
jiuk | 2:fc251f89bd1b | 230 | |
jiuk | 2:fc251f89bd1b | 231 | void buscar_linea(){ |
jiuk | 2:fc251f89bd1b | 232 | for(uint8_t i=0;i<9;i++){ |
jiuk | 2:fc251f89bd1b | 233 | if(mat_act[i]==255){ |
jiuk | 2:fc251f89bd1b | 234 | for(uint8_t a=i;a>=1;a--){ |
jiuk | 2:fc251f89bd1b | 235 | sendSPI(a,mat_act[a-1]); |
jiuk | 3:7ae3dd76ba0a | 236 | mat_act[a]=mat_act[a-1]; |
jiuk | 2:fc251f89bd1b | 237 | } |
jiuk | 2:fc251f89bd1b | 238 | } |
jiuk | 2:fc251f89bd1b | 239 | } |
jiuk | 2:fc251f89bd1b | 240 | } |
jiuk | 0:2b55edb1057b | 241 | |
jiuk | 0:2b55edb1057b | 242 | int main() { |
jiuk | 0:2b55edb1057b | 243 | test(); |
jiuk | 0:2b55edb1057b | 244 | borrar(); |
jiuk | 5:616b5894b610 | 245 | |
jiuk | 0:2b55edb1057b | 246 | while(1){ |
jiuk | 1:bd308ee8776e | 247 | fichas(); |
jiuk | 0:2b55edb1057b | 248 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 249 | cont=0; |
jiuk | 5:616b5894b610 | 250 | |
jiuk | 2:fc251f89bd1b | 251 | while (cont<fila){ |
jiuk | 1:bd308ee8776e | 252 | correr_fig(); |
jiuk | 1:bd308ee8776e | 253 | dibujar(ficha,grados,ncolumna); |
jiuk | 1:bd308ee8776e | 254 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 255 | imp_mat(mat_tmp); |
jiuk | 3:7ae3dd76ba0a | 256 | wait_ms(VEL*2); |
jiuk | 3:7ae3dd76ba0a | 257 | } |
jiuk | 2:fc251f89bd1b | 258 | guardar_mat(); |
jiuk | 3:7ae3dd76ba0a | 259 | buscar_linea(); |
jiuk | 0:2b55edb1057b | 260 | } |
jiuk | 0:2b55edb1057b | 261 | } |