Entrega fnal tetris
Embed:
(wiki syntax)
Show/hide line numbers
metrix.cpp
00001 #include "mbed.h" 00002 #include "moggo.h" //agrega la libreria que contiene las figuras. 00003 #include "MPU6050.h" 00004 00005 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. 00006 MPU6050 Wire(PB_9 , PB_8 ); 00007 00008 SPI deviceM(PB_15, PB_14, PB_13); 00009 DigitalOut ssel (PB_12); 00010 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi). 00011 //DigitalOut ssel (PB_9); //Chip Select para el controlador. 00012 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. 00013 //Serial com_tar(PC_10,PC_11); //master f446R 00014 Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb. 00015 Ticker timer; 00016 DigitalIn pulsador(PH_1); 00017 AnalogIn in1(PC_2); 00018 AnalogIn in2(PC_3); 00019 00020 #define VEL 120 //Velocidad de actualizacion de dato en el controlador. 00021 00022 uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla. 00023 uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas. 00024 uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0}; 00025 uint8_t cont=0,ncolumna=1,grados=1,ficha=0,GIRO=2,G_DIAG=2,correr=0,linea_spi=0; 00026 int fila=0; 00027 bool _boton=0; 00028 00029 void boton(); 00030 void sendSPI(uint8_t,uint8_t); 00031 void borrar(); 00032 00033 //-------------------------------------------------- 00034 //-------------------CARA_FELIZ------------------------ 00035 //-------------------------------------------------- 00036 void cara_feliz() 00037 { 00038 borrar(); 00039 sendSPI(0x02,0b01100110); 00040 wait(0.2); 00041 sendSPI(0x03,0b01100110); 00042 wait(0.2); 00043 sendSPI(0x06,0b01000010); 00044 wait(0.2); 00045 sendSPI(0x07,0b00111100); 00046 wait(0.2); 00047 for(int i=0;i<4;i++) 00048 { 00049 sendSPI(0x06,0x00); 00050 sendSPI(0x07,0b00111100); 00051 wait(0.2); 00052 sendSPI(0x07,0b00111100); 00053 wait(0.2); 00054 sendSPI(0x06,0b01000010); 00055 wait(0.2); 00056 } 00057 } 00058 00059 //-------------------------------------------------- 00060 //-------------------CARA_TRISTE------------------------ 00061 //-------------------------------------------------- 00062 void cara_triste() 00063 { 00064 borrar(); 00065 sendSPI(0x02,0b01100110); 00066 wait(0.2); 00067 sendSPI(0x03,0b01100110); 00068 wait(0.2); 00069 sendSPI(0x06,0b00111100); 00070 wait(0.2); 00071 sendSPI(0x07,0b01000010); 00072 wait(0.2); 00073 for(int i=0;i<4;i++) 00074 { 00075 sendSPI(0x07,0x00); 00076 sendSPI(0x06,0b00111100); 00077 wait(0.2); 00078 sendSPI(0x06,0b00111100); 00079 wait(0.2); 00080 sendSPI(0x07,0b01000010); 00081 wait(0.2); 00082 } 00083 } 00084 00085 00086 void sendSPI(uint8_t d1, uint8_t d2) 00087 { 00088 deviceM.unlock(); 00089 ssel=0; 00090 deviceM.write(d1); 00091 deviceM.write(d2); 00092 ssel=1; 00093 deviceM.lock(); 00094 } 00095 00096 void test() //test 00097 { 00098 sendSPI(0x09,0); //no decodificacion 00099 sendSPI(0x0A,0x00); //intensidad 00100 sendSPI(0x0B,0x07); //usa 7 leds 00101 sendSPI(0x0C,1); //no apaga 00102 sendSPI(0x0F,0); //operacion normal 00103 } 00104 00105 void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna) 00106 { 00107 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)){ 00108 mat_tmp[0]= *(fig+5)>>columna-1; 00109 mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila. 00110 mat_tmp[2]= *(fig+b)>>columna-1; 00111 mat_tmp[3]= *(fig+c)>>columna-1; 00112 } 00113 } 00114 00115 void borrar() //borrar toda la matriz; 00116 { 00117 int i; 00118 for(i=0;i<=8;i++) 00119 { 00120 sendSPI(i,0); 00121 mat_act[i]=0; //Matriz para mostrar en pantalla. 00122 } 00123 }; 00124 00125 void buscar_fil(){ 00126 for (uint8_t i=0;i<9;i++){ 00127 if((mat_act[i]& mat_tmp[3])==0){ 00128 fila=i; 00129 //command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]); 00130 } 00131 if((mat_act[i]& mat_tmp[3])!=0){ 00132 fila=i-1; //cambio de 2 a 1 00133 i=9; 00134 //command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]); 00135 } 00136 if((mat_act[i]& mat_tmp[2])!=0){ //se condicionan las salidas de la busqueda de lanueva fila 00137 fila=i; //cambio de 2 a 1 00138 i=9; 00139 //command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]); 00140 } 00141 if((mat_act[i]& mat_tmp[1])!=0){ 00142 fila=i+1; //cambio de 2 a 1 00143 i=9; 00144 //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]); 00145 } 00146 } 00147 if(fila>8) 00148 fila=8; 00149 if(fila<=0){ 00150 wait_ms(VEL); 00151 borrar(); 00152 fila=8; 00153 } 00154 //command.printf("\n buscar fila \n %d",fila); 00155 //command.printf("\n ******************************* \n "); 00156 }; 00157 00158 void guardar_mat(){ 00159 mat_act[fila-3]=mat_tmp1[0]; 00160 mat_act[fila-2]=mat_tmp1[1]; 00161 mat_act[fila-1]=mat_tmp1[2]; 00162 mat_act[fila ]=mat_tmp1[3]; 00163 }; 00164 00165 void correr_fig(){ 00166 00167 //correr=command.getc(); 00168 int y=0,x=0,acc[3]; 00169 //float gyro[3]; Wire.getGyro(gyro); 00170 float _acc[3]; 00171 //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]); 00172 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]); 00173 if(_boton==0) 00174 { 00175 x=((int)(in1.read()*-650))+13; 00176 y=((int)(in2.read()*-650))+13; 00177 acc[0]=x; 00178 acc[1]=y; 00179 //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); 00180 } 00181 else 00182 { 00183 Wire.getAccelero(_acc); 00184 acc[0]=(int)_acc[0]; 00185 acc[1]=(int)_acc[1]; 00186 //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]); 00187 } 00188 if(acc[0]>GIRO) //eje x derecha 00189 correr=0; 00190 else if(acc[0]<-GIRO) //eje x izquierda 00191 correr=1; 00192 else if(acc[1]>GIRO) //eje y arriba 00193 correr=3; 00194 else if(acc[1]<-GIRO) //eje y abajo 00195 correr=2; 00196 else 00197 correr=0; 00198 00199 00200 switch (correr){ 00201 00202 case 0: 00203 cont++; 00204 if(cont>8)cont=8; 00205 break; //cae la figura; 00206 00207 case 1: 00208 if((mat_tmp[1]==2) || (mat_tmp[1]==1) || (mat_tmp[1]==3)) //faltan condiciones para evitar desplazamiento innecesario. 00209 ncolumna--; 00210 grados++; 00211 if(grados>4) 00212 grados=1; 00213 break; //Girar la figura; 00214 00215 case 2: 00216 if((mat_tmp[1]!=1) && (mat_tmp[2]!=1) && (mat_tmp[3]!=1)){ 00217 if((mat_tmp[1]!=3) && (mat_tmp[2]!=3) && (mat_tmp[3]!=3)){ 00218 if((mat_tmp[1]!=7) && (mat_tmp[2]!=7) && (mat_tmp[3]!=7)) 00219 ncolumna++; 00220 } 00221 } 00222 00223 break; //correr a la Derecha; 00224 00225 case 3: 00226 ncolumna--; 00227 if(ncolumna<1) 00228 ncolumna=1; 00229 break; //correr a la izquierda; 00230 /* 00231 case 4: 00232 cont++; 00233 if(cont>8)cont=8; 00234 break; 00235 */ 00236 } 00237 } 00238 00239 void imp_mat(uint8_t *temp){ //se genera la variable global cont. 00240 mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]); 00241 mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]); 00242 mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]); 00243 mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart. 00244 sendSPI(cont-3,mat_tmp1[0]); 00245 sendSPI(cont-2,mat_tmp1[1]); 00246 sendSPI(cont-1,mat_tmp1[2]); 00247 sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1 00248 wait_ms(VEL); 00249 } 00250 00251 void dibujar(char type_fig,char grados,char columna) 00252 { 00253 switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz. 00254 case 1: if(columna>7) 00255 columna=7; 00256 cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro; 00257 00258 case 2: if(grados==1 || grados==3) 00259 cop_mat(0,0,0,FIG_ALL,columna); 00260 if(grados==2 || grados==4) 00261 cop_mat(5,5,2,FIG_ALL,columna); //2: I; 00262 break; 00263 00264 case 3: if(grados==1) 00265 cop_mat(0,0,1,FIG_ALL,columna); //3: L; 00266 if(grados==2) 00267 cop_mat(5,2,0,FIG_ALL,columna); 00268 if(grados==3) 00269 cop_mat(1,3,3,FIG_ALL,columna); 00270 if(grados==4) 00271 cop_mat(5,6,2,FIG_ALL,columna); 00272 break; 00273 00274 case 4: if(grados==1) 00275 cop_mat(5,3,2,FIG_ALL,columna); //4: T; 00276 if(grados==2) 00277 cop_mat(0,1,0,FIG_ALL,columna); 00278 if(grados==3) 00279 cop_mat(5,2,3,FIG_ALL,columna); 00280 if(grados==4) 00281 cop_mat(3,1,3,FIG_ALL,columna); 00282 break; 00283 00284 case 5: if(grados==1 || grados==3) 00285 cop_mat(5,4,1,FIG_ALL,columna); //5: S; 00286 if(grados==2 || grados==4) 00287 cop_mat(0,1,3,FIG_ALL,columna); 00288 break; 00289 } 00290 } 00291 00292 void fichas(){ 00293 ficha= rand() % 5+1; 00294 //grados= rand() % 4+1; 00295 ncolumna=4; 00296 } 00297 00298 void buscar_linea() 00299 { 00300 for(uint8_t i=0;i<9;i++) 00301 { 00302 if(mat_act[i]==255) 00303 { 00304 linea_spi++; 00305 for(uint8_t a=i;a>=1;a--) 00306 { 00307 sendSPI(a,mat_act[a-1]); 00308 mat_act[a]=mat_act[a-1]; 00309 } 00310 } 00311 } 00312 if(linea_spi==2) 00313 { 00314 com_tar.putc(1); 00315 cara_feliz(); 00316 } 00317 } 00318 00319 //-------------------------------------------------- 00320 //-------------------TICKER------------------------ 00321 //-------------------------------------------------- 00322 void attime() 00323 { 00324 void correr_fig(); 00325 //blink(); 00326 boton(); 00327 if (com_tar.readable()) 00328 { 00329 cara_triste(); 00330 //command.printf("Button pressed %b\n", _boton); 00331 } 00332 }; 00333 00334 //-------------------------------------------------- 00335 //-------------------BOTON------------------------ 00336 //-------------------------------------------------- 00337 void boton() 00338 { 00339 if (pulsador == 0) 00340 { 00341 //command.printf("Button pressed %b\n", _boton); 00342 _boton=!_boton; 00343 } 00344 } 00345 00346 //-------------------------------------------------- 00347 //-------------------VELOCIDAD----------------------- 00348 //-------------------------------------------------- 00349 int velocidad(float _acc[3]) 00350 { 00351 int vel,x=1,y=2; // 00352 int acc[3]; 00353 acc[0]=(int)_acc[0]; 00354 acc[1]=(int)_acc[1]; 00355 if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO)) 00356 { 00357 if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x))) 00358 { 00359 vel=300; 00360 if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y))) 00361 vel=100; 00362 } 00363 else 00364 vel=500; 00365 } 00366 return vel; 00367 } 00368 00369 //-------------------------------------------------- 00370 //-------------------MAIN------------------------ 00371 //-------------------------------------------------- 00372 int main() { 00373 timer.attach(&attime, 0.3); 00374 test(); 00375 borrar(); 00376 while(1){ 00377 fichas(); 00378 buscar_fil(); 00379 cont=0; 00380 00381 while (cont<fila){ 00382 correr_fig(); 00383 dibujar(ficha,grados,ncolumna); 00384 buscar_fil(); 00385 imp_mat(mat_tmp); 00386 wait_ms(VEL*2); 00387 } 00388 guardar_mat(); 00389 buscar_linea(); 00390 } 00391 }
Generated on Sun Aug 7 2022 15:04:03 by
1.7.2