corrección spi.

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Thu Oct 11 03:33:57 2018 +0000
Revision:
3:7ae3dd76ba0a
Parent:
2:fc251f89bd1b
Child:
4:78194ed9ff6b
version para revison versiones juegos.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:2b55edb1057b 96 void correr_fig(){
jiuk 2:fc251f89bd1b 97
jiuk 3:7ae3dd76ba0a 98 char t=1;
jiuk 3:7ae3dd76ba0a 99 uint8_t correr;
jiuk 3:7ae3dd76ba0a 100 while (t==1 && command.readable()==0){
jiuk 3:7ae3dd76ba0a 101 wait(0.1);
jiuk 3:7ae3dd76ba0a 102 t=0;
jiuk 3:7ae3dd76ba0a 103 }
jiuk 3:7ae3dd76ba0a 104 if (command.readable())
jiuk 3:7ae3dd76ba0a 105 correr=command.getc();
jiuk 3:7ae3dd76ba0a 106 else
jiuk 3:7ae3dd76ba0a 107 correr=0;
jiuk 0:2b55edb1057b 108 if(correr==30) //Arriba, voltear figura.
jiuk 0:2b55edb1057b 109 correr=1;
jiuk 2:fc251f89bd1b 110
jiuk 0:2b55edb1057b 111 if(correr==29) //izquierda, correr la figura.
jiuk 0:2b55edb1057b 112 correr=2;
jiuk 2:fc251f89bd1b 113
jiuk 0:2b55edb1057b 114 if(correr==28) //Derecha correr la figura.
jiuk 0:2b55edb1057b 115 correr=3;
jiuk 2:fc251f89bd1b 116
jiuk 3:7ae3dd76ba0a 117 if(correr==31) //Bajar la figura.
jiuk 3:7ae3dd76ba0a 118 correr=4;
jiuk 3:7ae3dd76ba0a 119
jiuk 2:fc251f89bd1b 120
jiuk 0:2b55edb1057b 121 switch (correr){
jiuk 2:fc251f89bd1b 122
jiuk 0:2b55edb1057b 123 case 0:
jiuk 2:fc251f89bd1b 124 cont++;
jiuk 2:fc251f89bd1b 125 if(cont>8)cont=8;
jiuk 2:fc251f89bd1b 126 break; //cae la figura;
jiuk 2:fc251f89bd1b 127
jiuk 0:2b55edb1057b 128 case 1:
jiuk 3:7ae3dd76ba0a 129 if((mat_tmp[1]==2) || (mat_tmp[1]==1) || (mat_tmp[1]==3)) //faltan condiciones para evitar desplazamiento innecesario.
jiuk 2:fc251f89bd1b 130 ncolumna--;
jiuk 0:2b55edb1057b 131 grados++;
jiuk 0:2b55edb1057b 132 if(grados>4)
jiuk 0:2b55edb1057b 133 grados=1;
jiuk 2:fc251f89bd1b 134 break; //Girar la figura;
jiuk 2:fc251f89bd1b 135
jiuk 0:2b55edb1057b 136 case 2:
jiuk 0:2b55edb1057b 137 ncolumna++;
jiuk 1:bd308ee8776e 138 if(ncolumna>6){
jiuk 2:fc251f89bd1b 139 if(((mat_tmp[2]==3) && (mat_tmp[3]==6)) || (mat_tmp[2]==7) || (mat_tmp[3]==7))
jiuk 1:bd308ee8776e 140 ncolumna=6;
jiuk 3:7ae3dd76ba0a 141 //if((mat_tmp[1]==2) &&(mat_tmp[2]==2) && (mat_tmp[3]==2))
jiuk 3:7ae3dd76ba0a 142 //ncolumna=7;
jiuk 1:bd308ee8776e 143 }
jiuk 3:7ae3dd76ba0a 144 if(ncolumna>7)
jiuk 3:7ae3dd76ba0a 145 ncolumna=7;
jiuk 2:fc251f89bd1b 146 break; //correr a la Derecha;
jiuk 2:fc251f89bd1b 147
jiuk 0:2b55edb1057b 148 case 3:
jiuk 0:2b55edb1057b 149 ncolumna--;
jiuk 0:2b55edb1057b 150 if(ncolumna<1)
jiuk 0:2b55edb1057b 151 ncolumna=1;
jiuk 2:fc251f89bd1b 152 break; //correr a la izquierda;
jiuk 3:7ae3dd76ba0a 153
jiuk 3:7ae3dd76ba0a 154 case 4:
jiuk 3:7ae3dd76ba0a 155 cont++;
jiuk 3:7ae3dd76ba0a 156 if(cont>8)cont=8;
jiuk 3:7ae3dd76ba0a 157 break;
jiuk 0:2b55edb1057b 158 }
jiuk 0:2b55edb1057b 159 }
jiuk 0:2b55edb1057b 160
jiuk 1:bd308ee8776e 161 void imp_mat(uint8_t *temp){ //se genera la variable global cont.
jiuk 0:2b55edb1057b 162 mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]);
jiuk 0:2b55edb1057b 163 mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]);
jiuk 0:2b55edb1057b 164 mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]);
jiuk 0:2b55edb1057b 165 mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart.
jiuk 0:2b55edb1057b 166 sendSPI(cont-3,mat_tmp1[0]);
jiuk 0:2b55edb1057b 167 sendSPI(cont-2,mat_tmp1[1]);
jiuk 0:2b55edb1057b 168 sendSPI(cont-1,mat_tmp1[2]);
jiuk 1:bd308ee8776e 169 sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1
jiuk 0:2b55edb1057b 170 wait_ms(VEL);
jiuk 0:2b55edb1057b 171 }
jiuk 0:2b55edb1057b 172
jiuk 0:2b55edb1057b 173 void dibujar(char type_fig,char grados,char columna)
jiuk 0:2b55edb1057b 174 {
jiuk 0:2b55edb1057b 175 switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz.
jiuk 0:2b55edb1057b 176 case 1: if(columna>7)
jiuk 0:2b55edb1057b 177 columna=7;
jiuk 0:2b55edb1057b 178 cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro;
jiuk 0:2b55edb1057b 179
jiuk 0:2b55edb1057b 180 case 2: if(grados==1 || grados==3)
jiuk 0:2b55edb1057b 181 cop_mat(0,0,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 182 if(grados==2 || grados==4)
jiuk 0:2b55edb1057b 183 cop_mat(5,5,2,FIG_ALL,columna); //2: I;
jiuk 0:2b55edb1057b 184 break;
jiuk 0:2b55edb1057b 185
jiuk 0:2b55edb1057b 186 case 3: if(grados==1)
jiuk 0:2b55edb1057b 187 cop_mat(0,0,1,FIG_ALL,columna); //3: L;
jiuk 0:2b55edb1057b 188 if(grados==2)
jiuk 0:2b55edb1057b 189 cop_mat(5,2,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 190 if(grados==3)
jiuk 0:2b55edb1057b 191 cop_mat(1,3,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 192 if(grados==4)
jiuk 0:2b55edb1057b 193 cop_mat(5,6,2,FIG_ALL,columna);
jiuk 1:bd308ee8776e 194 break;
jiuk 0:2b55edb1057b 195
jiuk 0:2b55edb1057b 196 case 4: if(grados==1)
jiuk 0:2b55edb1057b 197 cop_mat(5,3,2,FIG_ALL,columna); //4: T;
jiuk 0:2b55edb1057b 198 if(grados==2)
jiuk 0:2b55edb1057b 199 cop_mat(0,1,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 200 if(grados==3)
jiuk 0:2b55edb1057b 201 cop_mat(5,2,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 202 if(grados==4)
jiuk 0:2b55edb1057b 203 cop_mat(3,1,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 204 break;
jiuk 0:2b55edb1057b 205
jiuk 0:2b55edb1057b 206 case 5: if(grados==1 || grados==3)
jiuk 0:2b55edb1057b 207 cop_mat(5,4,1,FIG_ALL,columna); //5: S;
jiuk 0:2b55edb1057b 208 if(grados==2 || grados==4)
jiuk 0:2b55edb1057b 209 cop_mat(0,1,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 210 break;
jiuk 0:2b55edb1057b 211 }
jiuk 0:2b55edb1057b 212 }
jiuk 0:2b55edb1057b 213
jiuk 0:2b55edb1057b 214 void fichas(){
jiuk 0:2b55edb1057b 215 ficha= rand() % 5+1;
jiuk 0:2b55edb1057b 216 grados= rand() % 4+1;
jiuk 2:fc251f89bd1b 217 ncolumna=4;
jiuk 0:2b55edb1057b 218 }
jiuk 2:fc251f89bd1b 219
jiuk 2:fc251f89bd1b 220 void buscar_linea(){
jiuk 2:fc251f89bd1b 221 for(uint8_t i=0;i<9;i++){
jiuk 2:fc251f89bd1b 222 if(mat_act[i]==255){
jiuk 2:fc251f89bd1b 223 for(uint8_t a=i;a>=1;a--){
jiuk 2:fc251f89bd1b 224 sendSPI(a,mat_act[a-1]);
jiuk 3:7ae3dd76ba0a 225 mat_act[a]=mat_act[a-1];
jiuk 2:fc251f89bd1b 226 }
jiuk 2:fc251f89bd1b 227 }
jiuk 2:fc251f89bd1b 228 }
jiuk 2:fc251f89bd1b 229 }
jiuk 0:2b55edb1057b 230
jiuk 0:2b55edb1057b 231 int main() {
jiuk 0:2b55edb1057b 232 test();
jiuk 0:2b55edb1057b 233 borrar();
jiuk 0:2b55edb1057b 234 while(1){
jiuk 1:bd308ee8776e 235 fichas();
jiuk 0:2b55edb1057b 236 buscar_fil();
jiuk 1:bd308ee8776e 237 cont=0;
jiuk 2:fc251f89bd1b 238 while (cont<fila){
jiuk 1:bd308ee8776e 239 correr_fig();
jiuk 1:bd308ee8776e 240 dibujar(ficha,grados,ncolumna);
jiuk 1:bd308ee8776e 241 buscar_fil();
jiuk 1:bd308ee8776e 242 imp_mat(mat_tmp);
jiuk 3:7ae3dd76ba0a 243 wait_ms(VEL*2);
jiuk 3:7ae3dd76ba0a 244 }
jiuk 2:fc251f89bd1b 245 guardar_mat();
jiuk 3:7ae3dd76ba0a 246 buscar_linea();
jiuk 0:2b55edb1057b 247 }
jiuk 0:2b55edb1057b 248 }