mbedbidos
/
METRIX_presentacion
corrección spi.
metrix.cpp@1:bd308ee8776e, 2018-10-02 (annotated)
- Committer:
- jiuk
- Date:
- Tue Oct 02 17:51:50 2018 +0000
- Revision:
- 1:bd308ee8776e
- Parent:
- 0:2b55edb1057b
- Child:
- 2:fc251f89bd1b
versi?n probadora de bugs no baja en tiempo sino por comandos.
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 | 1:bd308ee8776e | 113 | if(ncolumna>6){ |
jiuk | 1:bd308ee8776e | 114 | if(((mat_tmp[2]==3) && (mat_tmp[3]==6)) || (mat_tmp[2]==7) || (mat_tmp[3]==7)) |
jiuk | 1:bd308ee8776e | 115 | ncolumna=6; |
jiuk | 1:bd308ee8776e | 116 | } |
jiuk | 0:2b55edb1057b | 117 | if(ncolumna>7) |
jiuk | 0:2b55edb1057b | 118 | ncolumna=7; |
jiuk | 0:2b55edb1057b | 119 | break; //correr a la Derecha; |
jiuk | 0:2b55edb1057b | 120 | case 3: |
jiuk | 0:2b55edb1057b | 121 | ncolumna--; |
jiuk | 0:2b55edb1057b | 122 | if(ncolumna<1) |
jiuk | 0:2b55edb1057b | 123 | ncolumna=1; |
jiuk | 0:2b55edb1057b | 124 | break; //correr a la izquierda; |
jiuk | 0:2b55edb1057b | 125 | } |
jiuk | 0:2b55edb1057b | 126 | |
jiuk | 0:2b55edb1057b | 127 | } |
jiuk | 0:2b55edb1057b | 128 | |
jiuk | 0:2b55edb1057b | 129 | |
jiuk | 1:bd308ee8776e | 130 | void imp_mat(uint8_t *temp){ //se genera la variable global cont. |
jiuk | 0:2b55edb1057b | 131 | mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); |
jiuk | 0:2b55edb1057b | 132 | mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); |
jiuk | 0:2b55edb1057b | 133 | mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); |
jiuk | 0:2b55edb1057b | 134 | mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart. |
jiuk | 0:2b55edb1057b | 135 | sendSPI(cont-3,mat_tmp1[0]); |
jiuk | 0:2b55edb1057b | 136 | sendSPI(cont-2,mat_tmp1[1]); |
jiuk | 0:2b55edb1057b | 137 | sendSPI(cont-1,mat_tmp1[2]); |
jiuk | 1:bd308ee8776e | 138 | sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 |
jiuk | 0:2b55edb1057b | 139 | wait_ms(VEL); |
jiuk | 0:2b55edb1057b | 140 | } |
jiuk | 0:2b55edb1057b | 141 | |
jiuk | 0:2b55edb1057b | 142 | void dibujar(char type_fig,char grados,char columna) |
jiuk | 0:2b55edb1057b | 143 | { |
jiuk | 0:2b55edb1057b | 144 | switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. |
jiuk | 0:2b55edb1057b | 145 | case 1: if(columna>7) |
jiuk | 0:2b55edb1057b | 146 | columna=7; |
jiuk | 0:2b55edb1057b | 147 | cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro; |
jiuk | 0:2b55edb1057b | 148 | |
jiuk | 0:2b55edb1057b | 149 | case 2: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 150 | cop_mat(0,0,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 151 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 152 | cop_mat(5,5,2,FIG_ALL,columna); //2: I; |
jiuk | 0:2b55edb1057b | 153 | break; |
jiuk | 0:2b55edb1057b | 154 | |
jiuk | 0:2b55edb1057b | 155 | case 3: if(grados==1) |
jiuk | 0:2b55edb1057b | 156 | cop_mat(0,0,1,FIG_ALL,columna); //3: L; |
jiuk | 0:2b55edb1057b | 157 | if(grados==2) |
jiuk | 0:2b55edb1057b | 158 | cop_mat(5,2,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 159 | if(grados==3) |
jiuk | 0:2b55edb1057b | 160 | cop_mat(1,3,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 161 | if(grados==4) |
jiuk | 0:2b55edb1057b | 162 | cop_mat(5,6,2,FIG_ALL,columna); |
jiuk | 1:bd308ee8776e | 163 | break; |
jiuk | 0:2b55edb1057b | 164 | |
jiuk | 0:2b55edb1057b | 165 | case 4: if(grados==1) |
jiuk | 0:2b55edb1057b | 166 | cop_mat(5,3,2,FIG_ALL,columna); //4: T; |
jiuk | 0:2b55edb1057b | 167 | if(grados==2) |
jiuk | 0:2b55edb1057b | 168 | cop_mat(0,1,0,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 169 | if(grados==3) |
jiuk | 0:2b55edb1057b | 170 | cop_mat(5,2,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 171 | if(grados==4) |
jiuk | 0:2b55edb1057b | 172 | cop_mat(3,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 173 | break; |
jiuk | 0:2b55edb1057b | 174 | |
jiuk | 0:2b55edb1057b | 175 | case 5: if(grados==1 || grados==3) |
jiuk | 0:2b55edb1057b | 176 | cop_mat(5,4,1,FIG_ALL,columna); //5: S; |
jiuk | 0:2b55edb1057b | 177 | if(grados==2 || grados==4) |
jiuk | 0:2b55edb1057b | 178 | cop_mat(0,1,3,FIG_ALL,columna); |
jiuk | 0:2b55edb1057b | 179 | break; |
jiuk | 0:2b55edb1057b | 180 | } |
jiuk | 0:2b55edb1057b | 181 | } |
jiuk | 0:2b55edb1057b | 182 | |
jiuk | 0:2b55edb1057b | 183 | void fichas(){ |
jiuk | 0:2b55edb1057b | 184 | ficha= rand() % 5+1; |
jiuk | 0:2b55edb1057b | 185 | grados= rand() % 4+1; |
jiuk | 0:2b55edb1057b | 186 | ncolumna=3; |
jiuk | 0:2b55edb1057b | 187 | } |
jiuk | 0:2b55edb1057b | 188 | |
jiuk | 0:2b55edb1057b | 189 | int main() { |
jiuk | 0:2b55edb1057b | 190 | test(); |
jiuk | 0:2b55edb1057b | 191 | borrar(); |
jiuk | 0:2b55edb1057b | 192 | while(1){ |
jiuk | 1:bd308ee8776e | 193 | fichas(); |
jiuk | 0:2b55edb1057b | 194 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 195 | cont=0; |
jiuk | 1:bd308ee8776e | 196 | while (cont<=fila){ |
jiuk | 1:bd308ee8776e | 197 | correr_fig(); |
jiuk | 1:bd308ee8776e | 198 | dibujar(ficha,grados,ncolumna); |
jiuk | 1:bd308ee8776e | 199 | buscar_fil(); |
jiuk | 1:bd308ee8776e | 200 | imp_mat(mat_tmp); |
jiuk | 1:bd308ee8776e | 201 | //probador bugs |
jiuk | 1:bd308ee8776e | 202 | uint8_t correr=command.getc(); |
jiuk | 1:bd308ee8776e | 203 | if(correr==31) //Arriba, voltear figura. |
jiuk | 1:bd308ee8776e | 204 | cont++; |
jiuk | 1:bd308ee8776e | 205 | command.printf("\n4- Cont: %d",cont); |
jiuk | 1:bd308ee8776e | 206 | } |
jiuk | 0:2b55edb1057b | 207 | guardar_mat(); |
jiuk | 0:2b55edb1057b | 208 | } |
jiuk | 0:2b55edb1057b | 209 | } |