julian alvarez / Mbed 2 deprecated moggo_1

Dependencies:   mbed mbed

Fork of moggo 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 250 //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 fila=0;
00015  
00016 void sendSPI(uint8_t d1, uint8_t d2)
00017 {
00018     deviceM.unlock();
00019     ssel=0;
00020     deviceM.write(d1); 
00021     deviceM.write(d2);
00022     ssel=1;
00023     deviceM.lock();
00024 }
00025 
00026 void test()  //test
00027 {
00028     sendSPI(0x09,0);        //no decodificacion
00029     sendSPI(0x0A,0x0);      //intensidad
00030     sendSPI(0x0B,0x07);     //usa 7 leds                     
00031     sendSPI(0x0C,1);        //no apaga
00032     sendSPI(0x0F,0);        //operacion normal     
00033 }
00034 
00035 void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
00036 {                    
00037         mat_tmp[0]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila.
00038         mat_tmp[1]= *(fig+b)>>columna-1;
00039         mat_tmp[2]= *(fig+c)>>columna-1;       
00040 }
00041 
00042 void borrar() //borrar toda la matriz;
00043 {
00044     int i;
00045     for(i=0;i<=8;i++)
00046     {
00047         sendSPI(0x0+i,0x00);
00048     }
00049 };
00050 
00051 void buscar_fil(){
00052     for (uint8_t i=0;i<8;i++){                      //Realiza la impresion de la matriz.
00053         if(mat_act[i]==0)
00054         fila=i;
00055         if(mat_act[i]!=0){
00056         fila=i-1;                                   //cambio de 2 a 1
00057         i=8;
00058         }
00059     }
00060     command.printf("\n buscar fila \n %d",fila);
00061     command.printf("\n ******************************* \n ");
00062 };
00063 
00064 void buscar_col(){
00065     uint8_t y;
00066         y= mat_act[fila] & mat_tmp[2];
00067         if(y != 0){
00068             fila=0;
00069             }
00070         else{
00071             fila++;
00072         }
00073         command.printf("\n fila cambiada en columna: y=%d",y);        
00074 };
00075 
00076 void guardar_mat(){
00077         //borrar();
00078         wait_ms(VEL);
00079         mat_act[fila-2]=mat_tmp[0];
00080         mat_act[fila-1]=mat_tmp[1];     //pasa de 0 a -1
00081         mat_act[fila]=mat_tmp[2];       //pasa de +1 a 0
00082     };
00083 
00084 
00085 void imp_mat(uint8_t *temp){
00086     uint8_t i=0;
00087     buscar_fil();
00088     for (i=0;i<=fila;i++){                      
00089     
00090         sendSPI(i-1,0);
00091         sendSPI(i-1,*(temp+0));
00092         sendSPI(i,*(temp+1));
00093         sendSPI(i+1,*(temp+2));                     //pasa de i+2 a i+1
00094         wait_ms(VEL);
00095     }
00096 }
00097 
00098 void dibujar(char type_fig,char grados,char columna)
00099 {
00100     //borrar();    
00101     switch (type_fig){                  //Se envia el vector que contiene la figura a la funcion copiar matriz.
00102         case 1: cop_mat(5,1,1,FIG_ALL,columna); break;  //1: cuadro;
00103         
00104         case 2: if(grados==1 || grados==3)              
00105                 cop_mat(0,0,0,FIG_ALL,columna);
00106                 if(grados==2 || grados==4)
00107                 cop_mat(5,5,2,FIG_ALL,columna);         //2: I;    
00108                 break;
00109                   
00110         case 3: if(grados==1)
00111                 cop_mat(0,0,1,FIG_ALL,columna);         //3: L; 
00112                 if(grados==2)
00113                 cop_mat(5,2,0,FIG_ALL,columna);
00114                 if(grados==3)
00115                 cop_mat(1,0,0,FIG_ALL,columna);
00116                 if(grados==4)
00117                 cop_mat(5,0,2,FIG_ALL,columna);
00118                 break;  //2: I;
00119         
00120         case 4: if(grados==1)
00121                 cop_mat(5,2,0,FIG_ALL,columna);         //4: T;
00122                 if(grados==2)
00123                 cop_mat(3,1,3,FIG_ALL,columna);
00124                 if(grados==3)
00125                 cop_mat(5,3,2,FIG_ALL,columna);
00126                 if(grados==4)
00127                 cop_mat(0,1,0,FIG_ALL,columna);
00128                 break;
00129         
00130         case 5: if(grados==1 || grados==3)
00131                 cop_mat(5,4,1,FIG_ALL,columna);   //5: S; 
00132                 if(grados==2 || grados==4)
00133                 cop_mat(0,1,3,FIG_ALL,columna);
00134                 break; 
00135     }  
00136 }
00137 
00138 int main() {
00139     char tipo_fig=1,ncolumna=1,grados=1,lee1=0,lee2=0;  // enviar ficha y columna
00140     test();
00141     borrar();
00142     while(1){
00143         lee1=command.getc();                //recive '<' para iniciar la linea de comandos.
00144         tipo_fig=command.getc();
00145         grados=command.getc();
00146         ncolumna=command.getc();       
00147         lee2=command.getc();                //recive '>' para terminar la linea de comandos.
00148             if(lee1==0x3c && lee2==0x3e)    //solo imprime una figura si viene bajo el parametro '<(t_fig) (Grados) (Col)>'.
00149             {
00150             dibujar(tipo_fig,grados,ncolumna);
00151             imp_mat(mat_tmp);
00152             guardar_mat();
00153             }     
00154         }
00155     }