julian alvarez / Mbed 2 deprecated moggo_2

Dependencies:   mbed mbed

Fork of moggo_1 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+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila.
00039         mat_tmp[1]= *(fig+b)>>columna-1;
00040         mat_tmp[2]= *(fig+c)>>columna-1;       
00041 }
00042 
00043 void borrar() //borrar toda la matriz;
00044 {
00045     int i;
00046     for(i=0;i<=8;i++)
00047     {
00048         sendSPI(0x0+i,0x00);
00049     }
00050 };
00051 
00052 void buscar_fil(){
00053     for (uint8_t i=0;i<9;i++){                     
00054         if((mat_act[i]& mat_tmp[2])==0){ //&& mat_act[i]& mat_tmp[1]==0 && mat_act[i]& mat_tmp[0]==0)
00055         fila=i;
00056         command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]);
00057         }
00058         if((mat_act[i]& mat_tmp[2])!=0){
00059         fila=i-1;                                   //cambio de 2 a 1
00060         i=9;
00061         command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]);
00062         }
00063         if((mat_act[i]& mat_tmp[1])!=0){            //se condicionan las salidas de la busqueda de lanueva fila
00064         fila=i;                                   //cambio de 2 a 1
00065         i=9;
00066         command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]);
00067         }
00068         if((mat_act[i]& mat_tmp[0])!=0){
00069         fila=i+1;                                   //cambio de 2 a 1
00070         i=9;
00071         command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]);
00072         }
00073     }
00074     command.printf("\n buscar fila \n %d",fila);
00075     command.printf("\n ******************************* \n ");
00076 };
00077 
00078 void guardar_mat(){
00079         mat_act[fila-2]=mat_tmp1[0];
00080         mat_act[fila-1]=mat_tmp1[1];     //pasa de 0 a -1
00081         mat_act[fila]=mat_tmp1[2];       //pasa de +1 a 0
00082     };
00083 
00084 
00085 void imp_mat(uint8_t *temp){                         //se genera la variable global cont.
00086     buscar_fil();
00087     for (cont=0;cont<=fila;cont++){
00088         //mat_tmp1[-1]=(mat_tmp[0]+ mat_act[cont-2]);                             //se debe tener una copia y un cero encima de la ficha.
00089         mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-2]);
00090         mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-1]);
00091         mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont]);                               //condicionador de funcion, se descarta por una funcion smart.                     
00092         sendSPI(cont-3,0);
00093         sendSPI(cont-2,mat_tmp1[0]);
00094         sendSPI(cont-1,mat_tmp1[1]);
00095         sendSPI(cont,mat_tmp1[2]);                    //pasa de i+2 a i+1
00096         //mat_act[cont-2]=(mat_tmp[0]+ mat_act[cont-2]);
00097         //mat_act[cont-1]=(mat_tmp[1]+ mat_act[cont-1]);
00098         //mat_act[cont]=(mat_tmp[2]+ mat_act[cont]);
00099         wait_ms(VEL);
00100         }
00101 }
00102 
00103 void dibujar(char type_fig,char grados,char columna)
00104 {
00105     //borrar();    
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     //char tipo_fig=1,ncolumna=1,grados=1,lee1=0,lee2=0,lee3=0;  // enviar ficha y columna
00145     int info[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};  // almacenara todos los caracteres que se escriban inicialmente maximo 4 figuras
00146     int cant_fichas=3; // cantidad de fichas que se enviaran en la linea de comando
00147     test();
00148     borrar();
00149         int r=cant_fichas*5;     //  r toma el valor de las fichas por 5 porque "<xyz>" son 5 caracteres               
00150         for(int i=0;i<=r;i+=5)    // almacenallenael vector que recibe los caracteres de las fichas
00151         {
00152             for(int j=0;j<5;j++ )
00153             {
00154             info[i+j]=command.getc();
00155             }
00156         }        
00157      while(1){      
00158         for (int k=0;k<=r;k+=5)   //imprime el vector con las fichas
00159         {
00160             if(info[k]==0x3c && info[k+4]==0x3e)
00161             {
00162                 dibujar(info[k+1],info[k+2],info[k+3]);
00163                 imp_mat(mat_tmp);
00164                 guardar_mat(); 
00165             }
00166             else
00167             {
00168                 while(1)  // error infinito en caso que no se cumplan las condiciones
00169                 {
00170                     borrar();
00171                     sendSPI(0x04,0b00011000);
00172                     wait(0.5);
00173                     sendSPI(0x04,0b00000000);
00174                     wait(0.5);
00175                 }
00176             }
00177             wait(2);  // tiempo entre caida de cada pieza puede dejarlo pequeño apra optimizar tiempo
00178         }
00179     }
00180 }