cualquier cosa

Dependencies:   mbed

Fork of 00_LAB_matrix8x8_SPI by ferney alberto beltran molina

main.cpp

Committer:
oggonei
Date:
2018-09-06
Revision:
5:377b23953ba1
Parent:
4:b38a9b0501dc
Child:
7:315c0b5f9425

File content as of revision 5:377b23953ba1:

#include "mbed.h"

SPI deviceM(PB_5, PB_4, PB_3);
DigitalOut ssel (PB_9);
Serial command(USBTX,USBRX);

#define FIG_C 0x01
#define FIG_I 0x02
#define FIG_L 0x03
#define FIG_T 0x04
#define FIG_S 0x05

//#define  uint8_t unsigned char
//uint8_t vcol2[8]={0,0,0,0,0,0,0,0};

int enviarficha(int x,int y)
{
    int k=y;
    
    if(x==1) // cuadrado
        {
        return k; 
        }
    else if (x==2) //linea
        {
        return k;
        }
    else if (x==3) //ele
        {
        return k;
        }
    else if (x==4) //te
        {
        return k;
        }
    else if (x==5) //ese
        {
        return k;
        }         
    else
    return 1;
}
 
void sendSPI(uint8_t d1, uint8_t d2)
{
    deviceM.unlock();
    ssel=0;
    deviceM.write(d1);
    deviceM.write(d2);
    ssel=1;
    deviceM.lock();
};

void test()  //test
{
    sendSPI(0x0F,1);
    wait(0.5);
    sendSPI(0x0F,0);
    wait(0.5);    
};

void cuadro(int x,int y)
{
    int j,r;

    if (x==1)
    r=0xC0;
    else if(x==2)
    r=0x60;
    else if(x==3)
    r=0x30;
    else if(x==4)
    r=0x18;
    else if(x==5)
    r=0xC;
    else if(x==6)
    r=0x06;
    else if(x==7)
    r=0x03;
    else if(x==8)
    r=0x01;
    {
         for(j=0;j<=7;j++)
             {
                sendSPI(0x0+j,r);
                sendSPI(0x0+(j+1),r);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                sendSPI(0x0+(j+1),0x00);
                wait_us(10); 
            } 
                sendSPI(0x07,r);
                sendSPI(0x08,r);  
    }                
};

void linea(int x,int y)
{
    int j,r=y;
    int lin0[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
    int lin90[8]={0xe0,0x70,0x38,0x1c,0x56,0x6f,0x6f,0x6f};
    {
    switch (r){
        case 1:  for(j=0;j<=6;j++)
             {
                sendSPI(0x0+j,lin0[x]);
                sendSPI(0x0+(j+1),lin0[x]);
                sendSPI(0x0+(j+2),lin0[x]);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                sendSPI(0x0+(j+1),0x00);
                sendSPI(0x0+(j+2),0x00);
                wait_us(10); 
            } 
                sendSPI(0x06,lin0[x]);
                sendSPI(0x07,lin0[x]);
                sendSPI(0x08,lin0[x]);  
                break;
        case 2:
                for(j=0;j<=6;j++)
             {
                sendSPI(0x0+j,lin90[x]);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                wait_us(10); 
            } 
                sendSPI(0x06,lin90[x]);
                sendSPI(0x07,lin90[x]);
                sendSPI(0x08,lin90[x]);  
                break;           
        //case 3: ele(columna,grados); break;
        //case 4: te(columna,grados); break;    
    }
        
 }                
};

void ele(int x,int y)
{
    int j,r,r2;
    if (x==1)
    {
    r=0xC0;
    r2=0x80;
    }
    else if(x==2)
    {
    r=0x60;
    r2=0x40;
    }
    else if(x==3)
    {
    r=0x30;
    r2=0x20;
    }
    else if(x==4)
    {
    r=0x18;
    r2=0x10;
    }
    else if(x==5)
    {
    r=0xC;
    r2=0x08;
    }
    else if(x==6)
    {
    r=0x06;
    r2=0x04;
    }
    else if(x==7)
    {
    r=0x03;
    r2=0x02;
    }
    else if(x==8)
    {
    r=0x01;
    r2=0x01;
    }
    {
         for(j=0;j<=6;j++)
             {
                sendSPI(0x0+j,r2);
                sendSPI(0x0+(j+1),r2);
                sendSPI(0x0+(j+2),r);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                sendSPI(0x0+(j+1),0x00);
                sendSPI(0x0+(j+2),0x00);
                wait_us(10); 
            } 
                sendSPI(0x06,r2);
                sendSPI(0x07,r2);
                sendSPI(0x08,r);  
    }                
};

void te(int x,int y)
{
    int j,r,r2;

    if (x==1)
    {
    r=0xE0;
    r2=0x40;
    }
    else if(x==2)
    {
    r=0x70;
    r2=0x20;
    }
    else if(x==3)
    {
    r=0x38;
    r2=0x10;
    }
    else if(x==4)
    {
    r=0x1C;
    r2=0x8;
    }
    else if(x==5)
    {
    r=0xE;
    r2=0x4;
    }
    else if(x==6)
    {
    r=0x7;
    r2=0x2;
    }
    else if(x==7)
    {
    r=0x03;
    r2=0x1;
    }
    else if(x==8)
    r=0x01;
    {
         for(j=0;j<=7;j++)
             {
                sendSPI(0x0+j,r2);
                sendSPI(0x0+(j+1),r);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                sendSPI(0x0+(j+1),0x00);
                wait_us(10); 
            } 
                sendSPI(0x07,r2);
                sendSPI(0x08,r);  
    }                
};

void ese(int x,int y)
{
    int j,r,r2;

    if (x==1)
    {
    r=0xC0;
    r2=0x60;
    }
    else if(x==2)
    {
    r=0x60;
    r2=0x30;
    }
    else if(x==3)
    {
    r=0x30;
    r2=0x18;
    }
    else if(x==4)
    {
    r=0x18;
    r2=0xC;
    }
    else if(x==5)
    {
    r=0xC;
    r2=0x6;
    }
    else if(x==6)
    {
    r=0x6;
    r2=0x3;
    }
    else if(x==7)
    {
    r=0x03;
    r2=0x1;
    }
    else if(x==8)
    r=0x01;
    {
         for(j=0;j<=7;j++)
             {
                sendSPI(0x0+j,r2);
                sendSPI(0x0+(j+1),r);
                wait_ms(200);
                sendSPI(0x0+j,0x00);
                sendSPI(0x0+(j+1),0x00);
                wait_us(10); 
            } 
                sendSPI(0x07,r2);
                sendSPI(0x08,r);  
    }                
};

void borrar()
{
    int i;
    for(i=0;i<=8;i++)
    {
        sendSPI(0x0+i,0x00);
    }
};

void dibujar(char type_fig,char grados, char columna)
{
    //borrar();
    
    switch (type_fig){
        case FIG_C: cuadro(columna,grados); break;
        case FIG_I: linea(columna,grados);  break;
        case FIG_L: ele(columna,grados); break;
        case FIG_T: te(columna,grados); break;
        case FIG_S: ese(columna,grados); break;
    
    };
    
}

int main() {
    sendSPI(0x09,0);        //no decodificacion
    sendSPI(0x0A,0x0);     //intensidad
    sendSPI(0x0B,0x07);     //usa 7 leds                     
    sendSPI(0x0C,1);        //no apaga
    sendSPI(0x0F,0);        //operacion normal  
 
    int tipo_fig=1,ncolumna=1,grados=1,lee1=0x00,lee2=0x00;  // enviar ficha y columna
    
    while(1){
        lee1=command.getc();
        tipo_fig=command.getc();
        grados=command.getc();
        ncolumna=command.getc();       
        lee2=command.getc();
        
            if(lee1==0x3c && lee2==0x3e)
            {
            dibujar(tipo_fig,grados,ncolumna);            
            }        
        }
    }