mbedbidos
/
METRIX_presentacion
corrección spi.
metrix.cpp@0:2b55edb1057b, 2018-10-01 (annotated)
- Committer:
- jiuk
- Date:
- Mon Oct 01 15:34:36 2018 +0000
- Revision:
- 0:2b55edb1057b
- Child:
- 1:bd308ee8776e
Tetris Para matriz 8x8, SPI comunicaci?n.
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 | 0:2b55edb1057b | 15 | uint8_t fila=1,cont=0,ncolumna=1,grados=1,ficha=0; |
jiuk | 0:2b55edb1057b | 16 | |
jiuk | 0:2b55edb1057b | 17 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:2b55edb1057b | 18 | { |
jiuk | 0:2b55edb1057b | 19 | deviceM.unlock(); |
jiuk | 0:2b55edb1057b | 20 | ssel=0; |
jiuk | 0:2b55edb1057b | 21 | deviceM.write(d1); |
jiuk | 0:2b55edb1057b | 22 | deviceM.write(d2); |
jiuk | 0:2b55edb1057b | 23 | ssel=1; |
jiuk | 0:2b55edb1057b | 24 | deviceM.lock(); |
jiuk | 0:2b55edb1057b | 25 | } |
jiuk | 0:2b55edb1057b | 26 | |
jiuk | 0:2b55edb1057b | 27 | void test() //test |
jiuk | 0:2b55edb1057b | 28 | { |
jiuk | 0:2b55edb1057b | 29 | sendSPI(0x09,0); //no decodificacion |
jiuk | 0:2b55edb1057b | 30 | sendSPI(0x0A,0x00); //intensidad |
jiuk | 0:2b55edb1057b | 31 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:2b55edb1057b | 32 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:2b55edb1057b | 33 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:2b55edb1057b | 34 | } |
jiuk | 0:2b55edb1057b | 35 | |
jiuk | 0:2b55edb1057b | 36 | void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) |
jiuk | 0:2b55edb1057b | 37 | { |
jiuk | 0:2b55edb1057b | 38 | mat_tmp[0]= *(fig+5)>>columna-1; |
jiuk | 0:2b55edb1057b | 39 | mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila. |
jiuk | 0:2b55edb1057b | 40 | mat_tmp[2]= *(fig+b)>>columna-1; |
jiuk | 0:2b55edb1057b | 41 | mat_tmp[3]= *(fig+c)>>columna-1; |
jiuk | 0:2b55edb1057b | 42 | } |
jiuk | 0:2b55edb1057b | 43 | |
jiuk | 0:2b55edb1057b | 44 | void borrar() //borrar toda la matriz; |
jiuk | 0:2b55edb1057b | 45 | { |
jiuk | 0:2b55edb1057b | 46 | int i; |
jiuk | 0:2b55edb1057b | 47 | for(i=0;i<=8;i++) |
jiuk | 0:2b55edb1057b | 48 | { |
jiuk | 0:2b55edb1057b | 49 | sendSPI(i,0); |
jiuk | 0:2b55edb1057b | 50 | mat_act[i]=0; //Matriz para mostrar en pantalla. |
jiuk | 0:2b55edb1057b | 51 | } |
jiuk | 0:2b55edb1057b | 52 | }; |
jiuk | 0:2b55edb1057b | 53 | |
jiuk | 0:2b55edb1057b | 54 | void buscar_fil(){ |
jiuk | 0:2b55edb1057b | 55 | for (uint8_t i=0;i<9;i++){ |
jiuk | 0:2b55edb1057b | 56 | if((mat_act[i]& mat_tmp[3])==0){ |
jiuk | 0:2b55edb1057b | 57 | fila=i; |
jiuk | 0:2b55edb1057b | 58 | //command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 59 | } |
jiuk | 0:2b55edb1057b | 60 | if((mat_act[i]& mat_tmp[3])!=0){ |
jiuk | 0:2b55edb1057b | 61 | fila=i-1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 62 | i=9; |
jiuk | 0:2b55edb1057b | 63 | //command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]); |
jiuk | 0:2b55edb1057b | 64 | } |
jiuk | 0:2b55edb1057b | 65 | if((mat_act[i]& mat_tmp[2])!=0){ //se condicionan las salidas de la busqueda de lanueva fila |
jiuk | 0:2b55edb1057b | 66 | fila=i; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 67 | i=9; |
jiuk | 0:2b55edb1057b | 68 | //command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]); |
jiuk | 0:2b55edb1057b | 69 | } |
jiuk | 0:2b55edb1057b | 70 | if((mat_act[i]& mat_tmp[1])!=0){ |
jiuk | 0:2b55edb1057b | 71 | fila=i+1; //cambio de 2 a 1 |
jiuk | 0:2b55edb1057b | 72 | i=9; |
jiuk | 0:2b55edb1057b | 73 | //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); |
jiuk | 0:2b55edb1057b | 74 | } |
jiuk | 0:2b55edb1057b | 75 | } |
jiuk | 0:2b55edb1057b | 76 | if(fila>8) |
jiuk | 0:2b55edb1057b | 77 | fila=8; |
jiuk | 0:2b55edb1057b | 78 | if(fila==0){ |
jiuk | 0:2b55edb1057b | 79 | borrar(); |
jiuk | 0:2b55edb1057b | 80 | fila=8; |
jiuk | 0:2b55edb1057b | 81 | } |
jiuk | 0:2b55edb1057b | 82 | command.printf("\n buscar fila \n %d",fila); |
jiuk | 0:2b55edb1057b | 83 | command.printf("\n ******************************* \n "); |
jiuk | 0:2b55edb1057b | 84 | }; |
jiuk | 0:2b55edb1057b | 85 | |
jiuk | 0:2b55edb1057b | 86 | void guardar_mat(){ |
jiuk | 0:2b55edb1057b | 87 | mat_act[fila-3]=mat_tmp1[0]; |
jiuk | 0:2b55edb1057b | 88 | mat_act[fila-2]=mat_tmp1[1]; |
jiuk | 0:2b55edb1057b | 89 | mat_act[fila-1]=mat_tmp1[2]; //pasa de 0 a -1 |
jiuk | 0:2b55edb1057b | 90 | mat_act[fila]=mat_tmp1[3]; //pasa de +1 a 0 |
jiuk | 0:2b55edb1057b | 91 | }; |
jiuk | 0:2b55edb1057b | 92 | |
jiuk | 0:2b55edb1057b | 93 | void correr_fig(){ |
jiuk | 0:2b55edb1057b | 94 | uint8_t correr=0; |
jiuk | 0:2b55edb1057b | 95 | correr=command.getc(); |
jiuk | 0:2b55edb1057b | 96 | command.printf("\n4- Correr: %d",correr); |
jiuk | 0:2b55edb1057b | 97 | if(correr==30) //Arriba, voltear figura. |
jiuk | 0:2b55edb1057b | 98 | correr=1; |
jiuk | 0:2b55edb1057b | 99 | if(correr==29) //izquierda, correr la figura. |
jiuk | 0:2b55edb1057b | 100 | correr=2; |
jiuk | 0:2b55edb1057b | 101 | if(correr==28) //Derecha correr la figura. |
jiuk | 0:2b55edb1057b | 102 | correr=3; |
jiuk | 0:2b55edb1057b | 103 | switch (correr){ |
jiuk | 0:2b55edb1057b | 104 | case 0: |
jiuk | 0:2b55edb1057b | 105 | break; //cae la figura; |
jiuk | 0:2b55edb1057b | 106 | case 1: |
jiuk | 0:2b55edb1057b | 107 | grados++; |
jiuk | 0:2b55edb1057b | 108 | if(grados>4) |
jiuk | 0:2b55edb1057b | 109 | grados=1; |
jiuk | 0:2b55edb1057b | 110 | break; //Girar la figura; |
jiuk | 0:2b55edb1057b | 111 | case 2: |
jiuk | 0:2b55edb1057b | 112 | ncolumna++; |
jiuk | 0:2b55edb1057b | 113 | if(ncolumna>7) |
jiuk | 0:2b55edb1057b | 114 | ncolumna=7; |
jiuk | 0:2b55edb1057b | 115 | break; //correr a la Derecha; |
jiuk | 0:2b55edb1057b | 116 | case 3: |
jiuk | 0:2b55edb1057b | 117 | ncolumna--; |
jiuk | 0:2b55edb1057b | 118 | if(ncolumna<1) |
jiuk | 0:2b55edb1057b | 119 | ncolumna=1; |
jiuk | 0:2b55edb1057b | 120 | break; //correr a la izquierda; |
jiuk | 0:2b55edb1057b | 121 | } |
jiuk | 0:2b55edb1057b | 122 | |
jiuk | 0:2b55edb1057b | 123 | } |
jiuk | 0:2b55edb1057b | 124 | |
jiuk | 0:2b55edb1057b | 125 | |
jiuk | 0:2b55edb1057b | 126 | void imp_mat(uint8_t *temp){ //se genera la variable global cont. |
jiuk | 0:2b55edb1057b | 127 | mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); |
jiuk | 0:2b55edb1057b | 128 | mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); |
jiuk | 0:2b55edb1057b | 129 | mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); |
jiuk | 0:2b55edb1057b | 130 | mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart. |
jiuk | 0:2b55edb1057b | 131 | sendSPI(cont-3,mat_tmp1[0]); |
jiuk | 0:2b55edb1057b | 132 | sendSPI(cont-2,mat_tmp1[1]); |
jiuk | 0:2b55edb1057b | 133 | sendSPI(cont-1,mat_tmp1[2]); |
jiuk | 0:2b55edb1057b | 134 | sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 |
jiuk | 0:2b55edb1057b | 135 | wait_ms(VEL); |
jiuk | 0:2b55edb1057b | 136 | } |
jiuk | 0:2b55edb1057b | 137 | |
jiuk | 0:2b55edb1057b | 138 | void dibujar(char type_fig,char grados,char columna) |
jiuk | 0:2b55edb1057b | 139 | { |
jiuk | 0:2b55edb1057b | 140 | switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 0:2b55edb1057b | 141 | case 1: if(columna>7) |
jiuk | 0:2b55edb1057b | 142 | columna=7; |
jiuk | 0:2b55edb1057b | 143 | cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro; |
jiuk | 0:2b55edb1057b | 144 | |
jiuk | 0:2b55edb1057b | 145 | case 2: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 146 | cop_mat(0,0,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 147 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 148 | cop_mat(5,5,2,FIG_ALL,columna); //2: I; |
jiuk | 0:2b55edb1057b | 149 | break; |
jiuk | 0:2b55edb1057b | 150 | |
jiuk | 0:2b55edb1057b | 151 | case 3: if(grados==1) |
jiuk | 0:2b55edb1057b | 152 | cop_mat(0,0,1,FIG_ALL,columna); //3: L; |
jiuk | 0:2b55edb1057b | 153 | if(grados==2) |
jiuk | 0:2b55edb1057b | 154 | cop_mat(5,2,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 155 | if(grados==3) |
jiuk | 0:2b55edb1057b | 156 | cop_mat(1,3,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 157 | if(grados==4) |
jiuk | 0:2b55edb1057b | 158 | cop_mat(5,6,2,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 159 | break; //2: I; |
jiuk | 0:2b55edb1057b | 160 | |
jiuk | 0:2b55edb1057b | 161 | case 4: if(grados==1) |
jiuk | 0:2b55edb1057b | 162 | cop_mat(5,3,2,FIG_ALL,columna); //4: T; |
jiuk | 0:2b55edb1057b | 163 | if(grados==2) |
jiuk | 0:2b55edb1057b | 164 | cop_mat(0,1,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 165 | if(grados==3) |
jiuk | 0:2b55edb1057b | 166 | cop_mat(5,2,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 167 | if(grados==4) |
jiuk | 0:2b55edb1057b | 168 | cop_mat(3,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 169 | break; |
jiuk | 0:2b55edb1057b | 170 | |
jiuk | 0:2b55edb1057b | 171 | case 5: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 172 | cop_mat(5,4,1,FIG_ALL,columna); //5: S; |
jiuk | 0:2b55edb1057b | 173 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 174 | cop_mat(0,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 175 | break; |
jiuk | 0:2b55edb1057b | 176 | } |
jiuk | 0:2b55edb1057b | 177 | } |
jiuk | 0:2b55edb1057b | 178 | |
jiuk | 0:2b55edb1057b | 179 | void fichas(){ |
jiuk | 0:2b55edb1057b | 180 | ficha= rand() % 5+1; |
jiuk | 0:2b55edb1057b | 181 | grados= rand() % 4+1; |
jiuk | 0:2b55edb1057b | 182 | ncolumna=3; |
jiuk | 0:2b55edb1057b | 183 | } |
jiuk | 0:2b55edb1057b | 184 | |
jiuk | 0:2b55edb1057b | 185 | int main() { |
jiuk | 0:2b55edb1057b | 186 | test(); |
jiuk | 0:2b55edb1057b | 187 | borrar(); |
jiuk | 0:2b55edb1057b | 188 | while(1){ |
jiuk | 0:2b55edb1057b | 189 | fichas(); |
jiuk | 0:2b55edb1057b | 190 | buscar_fil(); |
jiuk | 0:2b55edb1057b | 191 | for (cont=0;cont<=fila;cont++){ |
jiuk | 0:2b55edb1057b | 192 | correr_fig(); |
jiuk | 0:2b55edb1057b | 193 | dibujar(ficha,grados,ncolumna); |
jiuk | 0:2b55edb1057b | 194 | buscar_fil(); |
jiuk | 0:2b55edb1057b | 195 | imp_mat(mat_tmp); |
jiuk | 0:2b55edb1057b | 196 | } |
jiuk | 0:2b55edb1057b | 197 | guardar_mat(); |
jiuk | 0:2b55edb1057b | 198 | } |
jiuk | 0:2b55edb1057b | 199 | } |