julian alvarez / Mbed 2 deprecated moggo_3

Dependencies:   mbed mbed

Fork of moggo_2 by julian alvarez

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers moggo.cpp Source File

moggo.cpp

00001 #include "mbed.h"
00002 #include "moggo.h" //agrega la libreria que contiene las figuras.
00003 
00004 SPI deviceM(PB_15, PB_14, PB_13);
00005 DigitalOut ssel (PB_12);
00006 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
00007 //DigitalOut ssel (PB_9);         //Chip Select para el controlador.
00008 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
00009 
00010 #define  VEL 125 //Velocidad de actualizacion de dato en el controlador.
00011 
00012 uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0};    //Matriz para mostrar en pantalla.
00013 uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0};    //Matriz temporal para arrojar las fichas.
00014 uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0};
00015 uint8_t fila=0,cont=0;
00016  
00017 void sendSPI(uint8_t d1, uint8_t d2)
00018 {
00019     deviceM.unlock();
00020     ssel=0;
00021     deviceM.write(d1); 
00022     deviceM.write(d2);
00023     ssel=1;
00024     deviceM.lock();
00025 }
00026 
00027 void test()  //test
00028 {
00029     sendSPI(0x09,0);        //no decodificacion
00030     sendSPI(0x0A,0x0);      //intensidad
00031     sendSPI(0x0B,0x07);     //usa 7 leds                     
00032     sendSPI(0x0C,1);        //no apaga
00033     sendSPI(0x0F,0);        //operacion normal     
00034 }
00035 
00036 void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
00037 {       
00038         mat_tmp[0]= *(fig+5)>>columna-1;             
00039         mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila.
00040         mat_tmp[2]= *(fig+b)>>columna-1;
00041         mat_tmp[3]= *(fig+c)>>columna-1;       
00042 }
00043 
00044 void borrar() //borrar toda la matriz;
00045 {
00046     int i;
00047     for(i=0;i<=8;i++)
00048     {
00049         sendSPI(0x0+i,0x00);
00050     }
00051 };
00052 
00053 void buscar_fil(){
00054     for (uint8_t i=0;i<9;i++){                     
00055         if(((mat_act[i]& mat_tmp[3])==0) && ((mat_act[i]& mat_tmp[1])==0) && ((mat_act[i]& mat_tmp[0])==0)){
00056         fila=i;
00057         command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]);
00058         }
00059         if((mat_act[i]& mat_tmp[3])!=0){
00060         fila=i-1;                                   //cambio de 2 a 1
00061         i=9;
00062         command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]);
00063         }
00064         if((mat_act[i]& mat_tmp[2])!=0){            //se condicionan las salidas de la busqueda de lanueva fila
00065         fila=i;                                   //cambio de 2 a 1
00066         i=9;
00067         command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]);
00068         }
00069         if((mat_act[i]& mat_tmp[1])!=0){
00070         fila=i+1;                                   //cambio de 2 a 1
00071         i=9;
00072         command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]);
00073         }
00074     }
00075     if(fila>8)
00076     fila=8;
00077     command.printf("\n buscar fila \n %d",fila);
00078     command.printf("\n ******************************* \n ");
00079 };
00080 
00081 void guardar_mat(){
00082         mat_act[fila-3]=mat_tmp1[0];
00083         mat_act[fila-2]=mat_tmp1[1];
00084         mat_act[fila-1]=mat_tmp1[2];     //pasa de 0 a -1
00085         mat_act[fila]=mat_tmp1[3];       //pasa de +1 a 0
00086     };
00087 
00088 
00089 void imp_mat(uint8_t *temp){                         //se genera la variable global cont.
00090     buscar_fil();
00091     for (cont=0;cont<=fila;cont++){
00092         mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]);
00093         mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]);
00094         mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]);
00095         mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]);                          //condicionador de funcion, se descarta por una funcion smart.                     
00096         sendSPI(cont-3,mat_tmp1[0]);
00097         sendSPI(cont-2,mat_tmp1[1]);
00098         sendSPI(cont-1,mat_tmp1[2]);
00099         sendSPI(cont  ,mat_tmp1[3]);                    //pasa de i+2 a i+1
00100         wait_ms(VEL);
00101         }
00102 }
00103 
00104 void dibujar(char type_fig,char grados,char columna)
00105 {    
00106     switch (type_fig){                  //Se envia el vector que contiene la figura a la funcion copiar matriz.
00107         case 1: cop_mat(5,1,1,FIG_ALL,columna); break;  //1: cuadro;
00108         
00109         case 2: if(grados==1 || grados==3)              
00110                 cop_mat(0,0,0,FIG_ALL,columna);
00111                 if(grados==2 || grados==4)
00112                 cop_mat(5,5,2,FIG_ALL,columna);         //2: I;    
00113                 break;
00114                   
00115         case 3: if(grados==1)
00116                 cop_mat(0,0,1,FIG_ALL,columna);         //3: L; 
00117                 if(grados==2)
00118                 cop_mat(5,2,0,FIG_ALL,columna);
00119                 if(grados==3)
00120                 cop_mat(1,0,0,FIG_ALL,columna);
00121                 if(grados==4)
00122                 cop_mat(5,0,2,FIG_ALL,columna);
00123                 break;  //2: I;
00124         
00125         case 4: if(grados==1)
00126                 cop_mat(5,2,0,FIG_ALL,columna);         //4: T;
00127                 if(grados==2)
00128                 cop_mat(3,1,3,FIG_ALL,columna);
00129                 if(grados==3)
00130                 cop_mat(5,3,2,FIG_ALL,columna);
00131                 if(grados==4)
00132                 cop_mat(0,1,0,FIG_ALL,columna);
00133                 break;
00134         
00135         case 5: if(grados==1 || grados==3)
00136                 cop_mat(5,4,1,FIG_ALL,columna);   //5: S; 
00137                 if(grados==2 || grados==4)
00138                 cop_mat(0,1,3,FIG_ALL,columna);
00139                 break; 
00140     }  
00141 }
00142 
00143 int main() {
00144     int cant_fichas=10;              // cantidad de fichas que se enviaran en la linea de comando.
00145     int info[50];                   // almacenara todos los caracteres que se escriban inicialmente maximo 4 figuras.
00146     test();
00147     borrar();
00148         int r=cant_fichas*5;        //  r toma el valor de las fichas por 5 porque "<xyz>" son 5 caracteres.              
00149         for(int i=0;i<=r;i+=5)      // almacenallena el vector que recibe los caracteres de las fichas.
00150         {
00151             for(int j=0;j<5;j++ )
00152             {
00153             info[i+j]=command.getc();
00154             }
00155         }        
00156      //while(1){      
00157         for (int k=0;k<r;k+=5)      //imprime el vector con las fichas
00158         {
00159             if(info[k]==0x3c && info[k+4]==0x3e)
00160             {
00161                 if(info[k+1]<6 && info[k+2]<5 && info[k+3]<9)
00162                 {
00163                 dibujar(info[k+1],info[k+2],info[k+3]);
00164                 imp_mat(mat_tmp);
00165                 guardar_mat(); 
00166                 }
00167                 else
00168                 goto error;
00169             }
00170             else
00171             {
00172                 error:
00173                 while(1)  // error infinito en caso que no se cumplan las condiciones
00174                 {
00175                     borrar();
00176                     sendSPI(0x04,0b00011000);
00177                     sendSPI(0x05,0b00011000);
00178                     wait(0.5);
00179                     sendSPI(0x04,0b00000000);
00180                     sendSPI(0x05,0b00000000);
00181                     wait(0.1);
00182                 }
00183             }
00184             wait(1);  // tiempo entre caida de cada pieza puede dejarlo pequeño apra optimizar tiempo
00185     //    }
00186     }
00187 }