juego 2 presentación
Embed:
(wiki syntax)
Show/hide line numbers
recoge_manzanas.cpp
00001 // Read from I2C slave at address 0x62 00002 00003 #include "mbed.h" 00004 #include "Ticker.h" 00005 #include "MPU6050.h" 00006 00007 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. 00008 MPU6050 Wire(PB_9 , PB_8 ); 00009 00010 SPI deviceM(PB_15, PB_14, PB_13); 00011 DigitalOut ssel (PB_12); 00012 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi). 00013 //DigitalOut ssel (PB_9); //Chip Select para el controlador. 00014 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb. 00015 //Serial com_tar(PC_10,PC_11); //master f446R 00016 Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb. 00017 Ticker timer; 00018 DigitalIn pulsador(PH_1); 00019 AnalogIn in1(PC_2); 00020 AnalogIn in2(PC_3); 00021 00022 #define VEL 200 //Velocidad de actualizacion de dato en el controlador. 00023 #define MIN 1 00024 #define MAX 8 00025 #define MINC 128 00026 #define MAXC 1 00027 #define DEL 0.1 00028 #define GIRO 2 00029 #define G_DIAG 2 00030 00031 int columna=1,manzanas=0; 00032 int fila=1, fil=1, colum=1; 00033 bool _boton=0; 00034 void blink(); 00035 void boton(); 00036 void sendSPI(uint8_t,uint8_t); 00037 void borrar(); 00038 00039 00040 //-------------------------------------------------- 00041 //-------------------CARA_FELIZ------------------------ 00042 //-------------------------------------------------- 00043 void cara_feliz() 00044 { 00045 borrar(); 00046 sendSPI(0x02,0b01100110); 00047 wait(0.2); 00048 sendSPI(0x03,0b01100110); 00049 wait(0.2); 00050 sendSPI(0x06,0b01000010); 00051 wait(0.2); 00052 sendSPI(0x07,0b00111100); 00053 wait(0.2); 00054 for(int i=0;i<4;i++) 00055 { 00056 sendSPI(0x06,0x00); 00057 sendSPI(0x07,0b00111100); 00058 wait(0.2); 00059 sendSPI(0x07,0b00111100); 00060 wait(0.2); 00061 sendSPI(0x06,0b01000010); 00062 wait(0.2); 00063 } 00064 } 00065 00066 //-------------------------------------------------- 00067 //-------------------CARA_TRISTE------------------------ 00068 //-------------------------------------------------- 00069 void cara_triste() 00070 { 00071 borrar(); 00072 sendSPI(0x02,0b01100110); 00073 wait(0.2); 00074 sendSPI(0x03,0b01100110); 00075 wait(0.2); 00076 sendSPI(0x06,0b00111100); 00077 wait(0.2); 00078 sendSPI(0x07,0b01000010); 00079 wait(0.2); 00080 for(int i=0;i<4;i++) 00081 { 00082 sendSPI(0x07,0x00); 00083 sendSPI(0x06,0b00111100); 00084 wait(0.2); 00085 sendSPI(0x06,0b00111100); 00086 wait(0.2); 00087 sendSPI(0x07,0b01000010); 00088 wait(0.2); 00089 } 00090 } 00091 00092 //-------------------------------------------------- 00093 //-------------------TICKER------------------------ 00094 //-------------------------------------------------- 00095 void attime() 00096 { 00097 blink(); 00098 boton(); 00099 if (com_tar.readable()) 00100 { 00101 int x=com_tar.getc(); 00102 if(x==1) 00103 cara_triste(); 00104 //command.printf("Button pressed %b\n", _boton); 00105 } 00106 }; 00107 00108 //-------------------------------------------------- 00109 //-------------------BOTON------------------------ 00110 //-------------------------------------------------- 00111 void boton() 00112 { 00113 if (pulsador == 0) 00114 { 00115 //command.printf("Button pressed %b\n", _boton); 00116 _boton=!_boton; 00117 } 00118 } 00119 00120 //-------------------------------------------------- 00121 //-------------------SPI------------------------ 00122 //-------------------------------------------------- 00123 void sendSPI(uint8_t d1, uint8_t d2) 00124 { 00125 deviceM.unlock(); 00126 ssel=0; 00127 deviceM.write(d1); 00128 deviceM.write(d2); 00129 ssel=1; 00130 deviceM.lock(); 00131 }; 00132 00133 00134 //-------------------------------------------------- 00135 //-------------------TEST------------------------ 00136 //-------------------------------------------------- 00137 void test() //test 00138 { 00139 sendSPI(0x09,0); //no decodificacion 00140 sendSPI(0x0A,0x9); //intensidad 00141 sendSPI(0x0B,0x07); //usa 7 leds 00142 sendSPI(0x0C,1); //no apaga 00143 sendSPI(0x0F,0); //operacion normal 00144 } 00145 00146 00147 //-------------------------------------------------- 00148 //-------------------BORRAR------------------------ 00149 //-------------------------------------------------- 00150 void borrar() //borrar toda la matriz; 00151 { 00152 int i; 00153 for(i=0;i<=8;i++) 00154 { 00155 sendSPI(i,0); 00156 } 00157 } 00158 00159 00160 //-------------------------------------------------- 00161 //-------------------GENERAR_PUNTO------------------------ 00162 //-------------------------------------------------- 00163 void generar_punto() 00164 { 00165 int col[8]={1,2,4,8,16,32,64,128}; 00166 //int fil=0; 00167 borrar(); 00168 int _fil=fil; 00169 fil= rand() % 7+1; 00170 int y= rand() % 7+1; 00171 //command.printf("\n\n Fila: %d",fil); 00172 //command.printf("\n Columna:%d",col[y]); 00173 //int time=0; 00174 //while(time<4) 00175 // { 00176 if(_fil==fil) 00177 colum=colum+col[y]; 00178 colum=col[y]; 00179 if(fil==fila){ 00180 int guarda_linea=columna+colum; 00181 sendSPI(fila,guarda_linea); 00182 } 00183 else 00184 sendSPI(fil,col[y]); 00185 00186 // wait_ms(VEL); 00187 // sendSPI(fil,0); 00188 // wait_ms(VEL); 00189 // time++; 00190 // } 00191 00192 } 00193 00194 //-------------------------------------------------- 00195 //-------------------BLINK------------------------ 00196 //-------------------------------------------------- 00197 void blink(){ 00198 sendSPI(fil,0); 00199 if(fil==fila) 00200 sendSPI(fila,columna); 00201 wait(0.05); 00202 } 00203 00204 //-------------------------------------------------- 00205 //-------------------REVISAR_MOV------------------------ 00206 //-------------------------------------------------- 00207 void revisar_mov(uint8_t correr){ 00208 switch (correr){ 00209 00210 case 2: 00211 columna=columna<<1; 00212 fila--; 00213 if (columna>MINC) 00214 columna=MAXC; 00215 if (fila<MIN){ 00216 fila=MAX; 00217 sendSPI(MIN,0); 00218 } 00219 sendSPI(fila+1,0); 00220 break; 00221 00222 case 4: 00223 columna=columna<<1; 00224 fila++; 00225 if (fila>MAX){ 00226 fila=MIN; 00227 sendSPI(MAX,0); 00228 } 00229 if (columna>MINC) 00230 columna=MAXC; 00231 sendSPI(fila-1,0); 00232 break; 00233 00234 case 1: 00235 columna=columna>>1; 00236 fila--; 00237 if (columna<MAXC) 00238 columna=MINC; 00239 if (fila<MIN){ 00240 fila=MAX; 00241 sendSPI(MIN,0); 00242 } 00243 sendSPI(fila+1,0); 00244 //command.printf("\n case1 Col:%d",columna); 00245 //command.printf("\n FIL:%d",fila); 00246 break; 00247 00248 case 3: 00249 columna=columna>>1; 00250 fila++; 00251 if (columna<MAXC) 00252 columna=MINC; 00253 if (fila>MAX){ 00254 fila=MIN; 00255 sendSPI(MAX,0); 00256 } 00257 sendSPI(fila-1,0); 00258 //command.printf("\n case3 Col:%d",columna); 00259 //command.printf("\n FIL:%d",fila); 00260 break; 00261 00262 case 28: 00263 columna=columna<<1; 00264 if (columna>MINC) 00265 columna=MAXC; 00266 break; 00267 00268 case 29: 00269 columna=columna>>1; 00270 if (columna<MAXC) 00271 columna=MINC; 00272 break; 00273 00274 case 30: 00275 fila--; 00276 if (fila<MIN){ 00277 fila=MAX; 00278 sendSPI(MIN,0); 00279 } 00280 sendSPI(fila+1,0); 00281 break; 00282 00283 case 31: 00284 fila++; 00285 if (fila>MAX){ 00286 fila=MIN; 00287 sendSPI(MAX,0); 00288 } 00289 sendSPI(fila-1,0); 00290 break; 00291 00292 case 0: 00293 break; 00294 } 00295 } 00296 00297 //-------------------------------------------------- 00298 //-------------------VELOCIDAD----------------------- 00299 //-------------------------------------------------- 00300 int velocidad(float _acc[3]) 00301 { 00302 int vel,x=1,y=2; // 00303 int acc[3]; 00304 acc[0]=(int)_acc[0]; 00305 acc[1]=(int)_acc[1]; 00306 if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO)) 00307 { 00308 if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x))) 00309 { 00310 vel=200; 00311 if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y))) 00312 vel=100; 00313 } 00314 else 00315 vel=300; 00316 } 00317 return vel; 00318 } 00319 00320 //-------------------------------------------------- 00321 //-------------------BUSCADOR------------------------ 00322 //-------------------------------------------------- 00323 void buscador() 00324 { 00325 uint8_t correr; 00326 int y=0,x=0,acc[3],v; 00327 //float gyro[3]; Wire.getGyro(gyro); 00328 float _acc[3]; 00329 //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]); 00330 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]); 00331 if(_boton==1) 00332 { 00333 x=((int)(in1.read()*-650))+13; 00334 y=((int)(in2.read()*-650))+13; 00335 acc[0]=x; 00336 acc[1]=y; 00337 //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); 00338 } 00339 else 00340 { 00341 Wire.getAccelero(_acc); 00342 acc[0]=(int)_acc[0]; 00343 acc[1]=(int)_acc[1]; 00344 //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]); 00345 } 00346 00347 //command.printf("Velocidad %d \n", v); 00348 if(acc[0]>GIRO) //eje x derecha 00349 { 00350 if(acc[1]>G_DIAG) //eje y arriba 00351 correr=1; 00352 else if(acc[1]<-G_DIAG)//eje y abajo 00353 correr=2; 00354 else 00355 correr=30; 00356 } 00357 else if(acc[0]<-GIRO) //eje x izquierda 00358 { 00359 if(acc[1]>G_DIAG) //eje y arriba 00360 correr=3; 00361 else if(acc[1]<-G_DIAG)//eje y abajo 00362 correr=4; 00363 else 00364 correr=31; 00365 } 00366 else if(acc[1]>GIRO) //eje y arriba 00367 correr=29; 00368 else if(acc[1]<-GIRO) //eje y abajo 00369 correr=28; 00370 else 00371 correr=0; // 00372 revisar_mov(correr); 00373 v=velocidad(_acc); 00374 wait_ms(v); 00375 //wait (DEL); 00376 //command.printf("\n correr:\n %d",correr); 00377 } 00378 00379 00380 //-------------------------------------------------- 00381 //-------------------COMER------------------------ 00382 //-------------------------------------------------- 00383 int comer () 00384 { 00385 int x=0; 00386 if((fil==fila)&&((colum & columna) != 0)) 00387 { 00388 manzanas++; 00389 sendSPI(fila,columna); 00390 int guarda_linea=columna+colum; 00391 sendSPI(fila,guarda_linea); 00392 generar_punto(); 00393 sendSPI(fil,colum); 00394 if(manzanas==4) 00395 { 00396 com_tar.putc(1); 00397 //command.printf("Enviado"); 00398 cara_feliz(); 00399 } 00400 x=1; 00401 } 00402 return x; 00403 } 00404 00405 00406 //-------------------------------------------------- 00407 //-------------------VALIDAR_CONTRINCANTE------------------------ 00408 //-------------------------------------------------- 00409 int validar_contrincante(){ 00410 int x=com_tar.getc(); 00411 return x; 00412 } 00413 00414 00415 //-------------------------------------------------- 00416 //-------------------MAIN------------------------ 00417 //-------------------------------------------------- 00418 int main () 00419 { 00420 int guarda_linea,x=0; 00421 timer.attach(&attime, 0.1); 00422 test(); 00423 borrar(); 00424 sendSPI(fila,columna); 00425 generar_punto(); 00426 while(1) 00427 { 00428 buscador(); 00429 //validar_contrincante(); 00430 x=comer(); 00431 //command.printf("\n comer:\n %d",x); 00432 if (x==0) 00433 { 00434 if(fil==fila){ 00435 guarda_linea=columna+colum; 00436 sendSPI(fila,guarda_linea); 00437 } 00438 else 00439 { 00440 guarda_linea=columna; 00441 sendSPI(fila,guarda_linea); 00442 sendSPI(fil,colum); 00443 } 00444 } 00445 else{ 00446 sendSPI(fila,columna); 00447 } 00448 //generar_punto(); 00449 } 00450 }
Generated on Wed Aug 3 2022 19:31:36 by
1.7.2