falta verificar cambio de joystick a accel

Dependencies:   mbed MPU6050

Revision:
2:484928b9a5b5
Parent:
1:efcead3fb5eb
Child:
3:81dc55b0a8d7
diff -r efcead3fb5eb -r 484928b9a5b5 recoge_manzanas.cpp
--- a/recoge_manzanas.cpp	Wed Nov 14 22:10:24 2018 +0000
+++ b/recoge_manzanas.cpp	Sun Nov 18 03:37:38 2018 +0000
@@ -14,6 +14,7 @@
 //Serial command(USBTX,USBRX);        //habilitar la comunicacion serial a traves del puerto usb.
 Serial com_tar(PC_10,PC_11);        //master f446R
 //Serial com_tar(PA_15,PB_7);       //slave f411R        //habilitar la comunicacion serial a traves del puerto usb.
+Ticker timer;
 
 #define  VEL 200                     //Velocidad de actualizacion de dato en el controlador.
 #define  MIN 1
@@ -22,9 +23,20 @@
 #define  MAXC 1
 #define  DEL 0.1
 
-int columna=1;
+int columna=1,manzanas=0;
 int fila=1, fil=1, colum=1;
+void blink();
 
+//--------------------------------------------------
+//-------------------TICKER------------------------
+//--------------------------------------------------  
+void attime() {
+    blink();
+    }; 
+    
+//--------------------------------------------------
+//-------------------SPI------------------------
+//--------------------------------------------------   
 void sendSPI(uint8_t d1, uint8_t d2)
 {
     deviceM.unlock();
@@ -33,8 +45,12 @@
     deviceM.write(d2);
     ssel=1;
     deviceM.lock();
-}
+};
+
 
+//--------------------------------------------------
+//-------------------TEST------------------------
+//--------------------------------------------------   
 void test()                 //test
 {
     sendSPI(0x09,0);        //no decodificacion
@@ -44,6 +60,10 @@
     sendSPI(0x0F,0);        //operacion normal     
 }
 
+
+//--------------------------------------------------
+//-------------------BORRAR------------------------
+//--------------------------------------------------   
 void borrar()            //borrar toda la matriz;
 {
     int i;
@@ -53,25 +73,33 @@
     }
 }
 
+
+//--------------------------------------------------
+//-------------------GENERAR_PUNTO------------------------
+//--------------------------------------------------   
 void generar_punto()
 {
-    int col[8]={0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};
+    int col[8]={1,2,4,8,16,32,64,128};
     //int fil=0;
     borrar();
-    fil= rand() % 7+1; 
+    int _fil=fil;
+    fil= rand() % 7+1;
     int y= rand() % 7+1;
     command.printf("\n\n Fila: %d",fil); 
     command.printf("\n Columna:%d",col[y]);
     //int time=0;
     //while(time<4)
     //    {
+    if(_fil==fil)
+    colum=colum+col[y];
+    colum=col[y];
     if(fil==fila){
                     int guarda_linea=columna+colum;
                     sendSPI(fila,guarda_linea);
                     }
     else
     sendSPI(fil,col[y]);
-    colum=col[y];
+    
     //    wait_ms(VEL);
     //    sendSPI(fil,0);
     //    wait_ms(VEL);
@@ -80,6 +108,18 @@
     
 }
 
+//--------------------------------------------------
+//-------------------BLINK------------------------
+//--------------------------------------------------  
+void blink(){
+    sendSPI(fil,0);
+        if(fil==fila)
+                sendSPI(fila,columna);
+    }
+
+//--------------------------------------------------
+//-------------------REVISAR_MOV------------------------
+//--------------------------------------------------   
 void revisar_mov(uint8_t correr){
     switch (correr){
         case 28:
@@ -111,53 +151,68 @@
         break;  
         
         case 0:
-        fila;
         break; 
         }
     }
-
+    
+    
+//--------------------------------------------------
+//-------------------BUSCADOR------------------------
+//--------------------------------------------------   
 void buscador()
 {
-
     uint8_t correr;
     //float gyro[3]; Wire.getGyro(gyro);
     float acc[3]; Wire.getAccelero(acc);     
-    command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
+  //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
   //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
  
     wait(DEL);
 
     int giro=3;
         if(acc[0]>giro)         //eje x derecha
-            correr=29;          
+            correr=30;          
         else if(acc[0]<-giro)   //eje x izquierda
-            correr=28;          
+            correr=31;          
         else if(acc[1]>giro)    //eje y arriba
-            correr=30;
+            correr=29;
         else if(acc[1]<-giro)   //eje y abajo
-            correr=31;
+            correr=28;
         else
         correr=0; //
         revisar_mov(correr);
         //wait (DEL);
-    command.printf("\n correr:\n %d",correr);    
+    //command.printf("\n correr:\n %d",correr);    
 }
 
+
+//--------------------------------------------------
+//-------------------COMER------------------------
+//--------------------------------------------------   
 int comer ()
 {
             int x=0;
-            if(fil==fila){
-                if((colum & columna) != 0)
-                    {
-                        generar_punto();
+            if((fil==fila)&&((colum & columna) != 0))
+            {
+            manzanas++;
+            sendSPI(fila,columna);
+                        for(int i=0;i<manzanas;i++)
+                        {
+                            int guarda_linea=columna+colum;
+                            sendSPI(fila,guarda_linea);
+                            generar_punto();
+                            sendSPI(fil,colum);
+                            //com_tar.putc(x);
+                        }
                         x=1;
-                        sendSPI(fil,colum);
-                        //com_tar.putc(x);
-                        }
-                }
+            }
                 return x;
-    }
+}
+
 
+//--------------------------------------------------
+//-------------------VALIDAR_CONTRINCANTE------------------------
+//--------------------------------------------------   
 int validar_contrincante(){
     int x=com_tar.getc();
     return x;
@@ -166,6 +221,7 @@
 int main ()
 {
     int guarda_linea,x=0;
+    timer.attach(&attime, 0.3);
     test();
     borrar();
     sendSPI(fila,columna);
@@ -175,7 +231,7 @@
         buscador();
         //validar_contrincante();
             x=comer();
-            command.printf("\n comer:\n %d",x);
+            //command.printf("\n comer:\n %d",x);
             if (x==0)
                 {
                 if(fil==fila){