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