corrección spi.

Dependencies:   mbed MPU6050

Revision:
6:973f374e8ea4
Parent:
5:616b5894b610
--- a/metrix.cpp	Wed Oct 17 18:36:09 2018 +0000
+++ b/metrix.cpp	Thu Nov 22 11:30:46 2018 +0000
@@ -1,23 +1,87 @@
 #include "mbed.h"
 #include "moggo.h" //agrega la libreria que contiene las figuras.
+#include "MPU6050.h"
 
-//SPI deviceM(PB_15, PB_14, PB_13);
-//DigitalOut ssel (PB_12);
-SPI deviceM(PB_5, PB_4, PB_3);    //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
-DigitalOut ssel (PB_9);           //Chip Select para el controlador.
 Serial command(USBTX,USBRX);        //habilitar la comunicacion serial a traves del puerto usb.
-Serial com_tar(PC_10,PC_11,9600);        //habilitar la comunicacion serial a traves del puerto usb.
-//Serial com_tar(PC_12,PD_2);       //F446RE PC_10 TX; PC_11 RX;PC_12 TX; PD_2 RX.
-//Serial com_tar(PC_6,PC_5);        //F446RE PC_6 TX; PC_5 RX;
-//Serial com_tar(PA_15,PB_7);        //F411R PA_15 TX; PB_7 RX;
+MPU6050 Wire(PB_9 , PB_8 ); 
+
+SPI deviceM(PB_15, PB_14, PB_13);
+DigitalOut ssel (PB_12);
+//SPI deviceM(PB_5, PB_4, PB_3);    //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
+//DigitalOut ssel (PB_9);           //Chip Select para el controlador.
+//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;
+DigitalIn pulsador(PH_1);
+AnalogIn in1(PC_2);
+AnalogIn in2(PC_3);
 
 #define  VEL 120                //Velocidad de actualizacion de dato en el controlador.
 
 uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0};    //Matriz para mostrar en pantalla.
 uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0};           //Matriz temporal para arrojar las fichas.
 uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0};
-uint8_t cont=0,ncolumna=1,grados=1,ficha=0;
+uint8_t cont=0,ncolumna=1,grados=1,ficha=0,GIRO=2,G_DIAG=2,correr=0,linea_spi=0;
 int fila=0;
+bool _boton=0;
+
+void boton();
+void sendSPI(uint8_t,uint8_t);
+void borrar();
+
+//--------------------------------------------------
+//-------------------CARA_FELIZ------------------------
+//--------------------------------------------------  
+void cara_feliz()
+{
+    borrar();
+    sendSPI(0x02,0b01100110);
+    wait(0.2);
+    sendSPI(0x03,0b01100110);
+    wait(0.2); 
+    sendSPI(0x06,0b01000010);   
+    wait(0.2);
+    sendSPI(0x07,0b00111100);
+    wait(0.2);
+        for(int i=0;i<4;i++)
+        {
+            sendSPI(0x06,0x00);
+            sendSPI(0x07,0b00111100); 
+            wait(0.2);   
+            sendSPI(0x07,0b00111100);   
+            wait(0.2);
+            sendSPI(0x06,0b01000010);
+            wait(0.2);
+        }   
+}
+
+//--------------------------------------------------
+//-------------------CARA_TRISTE------------------------
+//--------------------------------------------------  
+void cara_triste()
+{
+    borrar();
+    sendSPI(0x02,0b01100110);
+    wait(0.2);
+    sendSPI(0x03,0b01100110);
+    wait(0.2); 
+    sendSPI(0x06,0b00111100);   
+    wait(0.2);
+    sendSPI(0x07,0b01000010);
+    wait(0.2);
+        for(int i=0;i<4;i++)
+        {
+            sendSPI(0x07,0x00);
+            sendSPI(0x06,0b00111100); 
+            wait(0.2);   
+            sendSPI(0x06,0b00111100);   
+            wait(0.2);
+            sendSPI(0x07,0b01000010);
+            wait(0.2);
+        }        
+}
+
 
 void sendSPI(uint8_t d1, uint8_t d2)
 {
@@ -87,8 +151,8 @@
         borrar();
         fila=8;
     }
-    command.printf("\n buscar fila \n %d",fila);
-    command.printf("\n ******************************* \n ");
+    //command.printf("\n buscar fila \n %d",fila);
+    //command.printf("\n ******************************* \n ");
 };
 
 void guardar_mat(){
@@ -99,36 +163,39 @@
         };
         
 void correr_fig(){
-    
-    uint8_t correr;
-
-    char t=1;    
-    while (t==1 && command.readable()==0){ 
-        wait_ms(0.1);
-        t=0;
+        
+    //correr=command.getc();  
+    int y=0,x=0,acc[3];
+    //float gyro[3]; Wire.getGyro(gyro);
+    float _acc[3];     
+    //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]);
+       if(_boton==0) 
+        {
+            x=((int)(in1.read()*-650))+13;
+            y=((int)(in2.read()*-650))+13;
+            acc[0]=x;
+            acc[1]=y;
+            //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); 
         }
-    if (command.readable()){   
-        correr=command.getc();
-        com_tar.putc(correr);
-        wait_ms(100);
+        else
+        {
+            Wire.getAccelero(_acc);
+            acc[0]=(int)_acc[0];
+            acc[1]=(int)_acc[1];
+            //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]);     
         }
-    else
+        if(acc[0]>GIRO)         //eje x derecha
+            correr=1;          
+        else if(acc[0]<-GIRO)   //eje x izquierda
+            correr=0;          
+        else if(acc[1]>GIRO)    //eje y arriba
+            correr=2;
+        else if(acc[1]<-GIRO)   //eje y abajo
+            correr=3;
+        else
         correr=0;
     
-    //correr=command.getc();
-       
-    if(correr==30)  //Arriba, voltear figura.
-    correr=1;
-    
-    if(correr==29)  //izquierda, correr la figura.
-    correr=2;
-    
-    if(correr==28)  //Derecha correr la figura.
-    correr=3;
-    
-    if(correr==31)  //Bajar la figura.
-    correr=0;
-    
     
     switch (correr){
                 
@@ -228,21 +295,86 @@
     ncolumna=4; 
     }
     
-void buscar_linea(){
-    for(uint8_t i=0;i<9;i++){
-        if(mat_act[i]==255){
-            for(uint8_t a=i;a>=1;a--){
+void buscar_linea()
+{
+    for(uint8_t i=0;i<9;i++)
+    {
+        if(mat_act[i]==255)
+        {
+            linea_spi++;
+            for(uint8_t a=i;a>=1;a--)
+            {
                 sendSPI(a,mat_act[a-1]);
                 mat_act[a]=mat_act[a-1];
             }
         }
     }
+    if(linea_spi==2)
+    {
+        com_tar.putc(1);
+        cara_feliz();
+    }
 }
 
+//--------------------------------------------------
+//-------------------TICKER------------------------
+//--------------------------------------------------  
+void attime() 
+{
+    void correr_fig();
+    //blink();
+    boton();
+    if (command.readable())
+    {
+    int x=com_tar.getc();
+    if(x==1)
+    cara_triste();
+    //command.printf("Button pressed %b\n", _boton);
+    }
+}; 
+
+//--------------------------------------------------
+//-------------------BOTON------------------------
+//--------------------------------------------------  
+void boton()
+{
+    if (pulsador == 0) 
+        {
+            //command.printf("Button pressed %b\n", _boton);
+            _boton=!_boton;
+        }
+}  
+
+//--------------------------------------------------
+//-------------------VELOCIDAD-----------------------
+//-------------------------------------------------- 
+int velocidad(float _acc[3])
+{
+    int vel,x=1,y=2; //
+    int acc[3];
+        acc[0]=(int)_acc[0];
+        acc[1]=(int)_acc[1];
+       if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO))
+        {
+                if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x)))
+                    {
+                        vel=300; 
+                        if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y)))
+                            vel=100;
+                    }
+                else
+                    vel=500;   
+        }
+    return vel;
+}
+
+//--------------------------------------------------
+//-------------------MAIN------------------------
+//--------------------------------------------------   
 int main() {
+    timer.attach(&attime, 0.3);
     test();
     borrar();
-    
     while(1){
             fichas();
             buscar_fil();