Erick Barrera / Mbed 2 deprecated second_game

Dependencies:   mbed MPU6050

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MPU6050.h"
00003 
00004 Ticker Game;
00005 Serial pc(USBTX,USBRX);
00006 SPI device (PB_15,PB_14,PB_13);
00007 DigitalOut ssel(PB_12);
00008 MPU6050 mpu1(PB_9,PB_8);
00009 MPU6050 mpu2(PB_3,PB_10);
00010 
00011 // variables
00012 int band=0;
00013 int move1=0,move2=0,col =0,fil =0;
00014 int status =0;
00015 float x[3],y[3];
00016 float aux1=0,aux2 =0;
00017 uint8_t pos[6]={0b11100000,0b01110000,0b00111000,0b00011100,0b00001110,0b00000111};
00018 uint8_t player1[1],player2[1];
00019 uint8_t ball[8]={0b10000000,0b01000000,0b00100000,0b00010000,0b00001000,0b00000100,0b00000010,0b00000001};
00020 uint8_t aux[1],scan[8];
00021 
00022 ////////////////////////////////////////////////////////////////////////////////
00023 void sendSPI(uint8_t d1, uint8_t d2)
00024 {
00025     device.unlock();
00026     ssel=0;
00027     device.write(d1); 
00028     device.write(d2);  
00029     ssel=1;
00030     device.lock();
00031 };
00032 ////////////////////////////////////////////////////////////////////////////////
00033 void clr_vcol()
00034     {
00035     for (int i=0; i<8;i++){
00036             sendSPI(i,0);
00037     }
00038     }
00039 ////////////////////////////////////////////////////////////////////////////////
00040 void inicializar(){
00041      
00042     sendSPI(0x0c,1);
00043     sendSPI(0x0b,7);
00044     sendSPI(0x0F,1);
00045     wait (0.9);
00046     sendSPI(0x0f,0);
00047     }
00048 ////////////////////////////////////////////////////////////////////////////////
00049 void act_status(void){
00050     status += 1;
00051     }
00052  ///////////////////////////////////////////////////////////////////////////////
00053 int main(){
00054        
00055        inicializar();
00056        Game.attach(&act_status,0.2);
00057        while (1){
00058         
00059         if(status==1){
00060            mpu1.getAccelero(x);
00061            //pc.printf("accelero 1: \t X = %f, \t Y = %f, \t Z = %f \n",x[0],x[1],x[2]);
00062            aux1 = x[0];
00063            mpu2.getAccelero(y);
00064            //pc.printf("accelero 2: \t X = %f, \t Y = %f, \t Z = %f \n",y[0],y[1],y[2]);
00065            aux2 = y[0];
00066            status = 2;
00067            }
00068         if(status ==2){
00069            
00070            if(aux1<-8){move1 =0;}                                             
00071            else if(aux1>-8 && aux1<-4){move1=1;}   
00072            else if(aux1>-4 && aux1<-1){move1 =2;}
00073            else if(aux1>1 && aux1<4){move1=3;}    
00074            else if(aux1>4 && aux1<8){move1=4;}
00075            else if(aux1>8){move1=5;}
00076                
00077            pc.printf("posicion 1: \t %d \n",move1);
00078            
00079            if(aux2<-8){move2 =0;}
00080            else if(aux2>-8 && aux2<-4){move2=1;}   
00081            else if(aux2>-4 && aux2<-1){move2 =2;}
00082            else if(aux2>1 && aux2<4){move2=3;}    
00083            else if(aux2>4 && aux2<8){move2=4;}
00084            else if(aux2>8){move2=5;}
00085                
00086            pc.printf("posicion 2: \t %d \n",move2);
00087            status =3;           
00088            }
00089         
00090         if(status==3){
00091            
00092             switch(move1){
00093                 case 0: player1[0] = pos[0];break;                              //se designa la posicion que va a tener el jugador 1
00094                 case 1: player1[0] = pos[1];break;
00095                 case 2: player1[0] = pos[2];break;
00096                 case 3: player1[0] = pos[3];break;
00097                 case 4: player1[0] = pos[4];break;
00098                 case 5: player1[0] = pos[5];break;
00099                 default:break;
00100                 }
00101                 
00102            switch(move2){
00103                 case 0: player2[0] = pos[5];break;                              //se designa la posicion que va a tener el jugador 1
00104                 case 1: player2[0] = pos[4];break;
00105                 case 2: player2[0] = pos[3];break;
00106                 case 3: player2[0] = pos[2];break;
00107                 case 4: player2[0] = pos[1];break;
00108                 case 5: player2[0] = pos[0];break;
00109                 default:break;
00110                 }
00111                 
00112            status=4;
00113            }             
00114       if(status==4){
00115             switch(band){
00116           case 0: 
00117                  switch(fil){
00118                      case 7:
00119                         switch(col){
00120                             case 0:
00121                                   if(move1==0){band =2;}break;
00122                             case 1:
00123                                   if(move1==0){band = 2;}
00124                                   else if(move1==1){band =2;}break;
00125                             case 2:
00126                                   if(move1==0){band =1;}
00127                                   else if(move1==1){band =2;}
00128                                   else if(move1==2){band =2;}break;
00129                             case 3:
00130                                   if(move1==1){band=1;}
00131                                   else if(move1==2){band=2;}
00132                                   else if(move1==3){band=2;}break;
00133                             case 4:
00134                                   if(move1==2){band=1;}
00135                                   else if(move1==3){band=2;}
00136                                   else if(move1==4){band=2;}break;
00137                             case 5:
00138                                   if(move1==3){band=1;}
00139                                   else if(move1==4){band=2;}
00140                                   else if(move1==5){band=2;}break; 
00141                             case 6:
00142                                   if(move1==4){band=1;}
00143                                   else if(move1==5){band=2;}break;
00144                             case 7:
00145                                   if(move1==5){band=2;}break;
00146                                 
00147                         }
00148                      //band = 1;break;
00149                      default: fil++; col++;break;
00150                      }break;
00151           case 1:
00152                  switch(col){
00153                      case 6:band = 2;break;
00154                      default: fil--; col++;break;
00155                      }break;
00156           case 2:
00157                  switch(fil){
00158                      case 3:
00159                            switch(col){
00160                             case 7:
00161                                   if(move2==0){band =0;}break;
00162                             case 6:
00163                                   if(move2==0){band = 0;}
00164                                   else if(move2==1){band =3;}break;
00165                             case 5:
00166                                   if(move2==0){band =0;}
00167                                   else if(move2==1){band =3;}
00168                                   else if(move2==2){band =3;}break;
00169                             case 4:
00170                                   if(move2==1){band=0;}
00171                                   else if(move2==2){band=3;}
00172                                   else if(move2==3){band=3;}break;
00173                             case 3:
00174                                   if(move2==2){band=0;}
00175                                   else if(move2==3){band=3;}
00176                                   else if(move2==4){band=3;}break;
00177                             case 2:
00178                                   if(move2==3){band=0;}
00179                                   else if(move2==4){band=3;}
00180                                   else if(move2==5){band=3;}break; 
00181                             case 1:
00182                                   if(move2==4){band=0;}
00183                                   else if(move2==5){band=3;}break;
00184                             case 0:
00185                                   if(move2==5){band=3;}break;
00186                                 
00187                                 }
00188                      //band = 3;break;
00189                      default: fil--;col--;break;
00190                      }break;
00191           case 3: 
00192                  switch(col){
00193                      case 0:band = 0;break;
00194                      default: fil++;col--;break;
00195                     }break;
00196           }
00197         status=5;
00198         }
00199         if(status==5){
00200             clr_vcol();
00201             sendSPI(8,player1[0]);
00202             sendSPI(1,player2[0]);
00203             sendSPI(fil,ball[col]);
00204             wait(0.2);
00205             status =0;
00206             }   
00207         }
00208 
00209  
00210 }