pausa

Dependencies:   mbed

Fork of primercorte by edson antonio vargas villarreal

main.cpp

Committer:
nicolasrojas
Date:
2018-06-05
Revision:
2:c457572a9bb2
Parent:
1:6ed951d975cc
Child:
4:244a242e0428

File content as of revision 2:c457572a9bb2:

#include "mbed.h" 
#include "main.h" 
#include "stepmotor.h" 


  
Serial command(USBTX, USBRX);
stepmotor smotor1(D9,D10,D11,D12);
stepmotor smotor2(D2,D3,D14,D15);

void Rx_interrupt();
 
int main()  {

    init_servo();
    init_serial();
    draw();
    Rx_interrupt ();
    nodraw();
    home();
    debug_m("inicio \n");
    uint32_t read_cc;
    while(1)
    {
        read_cc=read_command();
        switch (read_cc) {
            case  CM_DRAWING: drawing(); break;
            case  CM_SAVING: saving(); break;
            case CM_PAUSA:  Rx_interrupt(); break;
            
            default: debug_m("error de comando. \nSe espera  0xFEF0 o 0xfff0   \n");break ;      
        }
    }
}
 
 
 
uint32_t read_command()
{
  
   
    uint32_t val=0;
    uint8_t  cnt=0;
      
    char endc=command.getc();
    
    while(endc != CM_END && cnt <4) {
    if(endc!=CM_END)   
        val=((val<<8) +endc);
     endc=command.getc();
     cnt++;
    }   
    if(endc==CM_END)   
        return val;
    return 0;   
}
void init_serial(){

    command.baud(9600);  
    
}


 
 
void drawing(){

    debug_m("se esta ejecutando el dibujo... \n");       
     
    uint8_t error=0;
    MEM_TYPE dato;
 
    tail_reset();
 
    while(error==0){
        error = mem_get(&dato);
        
        
        if (error==0) {
            
            switch (dato) {   
          
            case  CM_DRAW: 
                    debug_m("-> Baja Z\n");
                    draw();
                    break ;     
            case  CM_NODRAW: 
                    debug_m("-> Sube Z\n");
                    nodraw();
                    break ;     
            
              
                
             default:  
                int y  = (uint8_t)(dato);
                int x  = (uint8_t)(dato>>8);
                char ncomm  = (uint8_t)(dato>>16);
                
                if (ncomm == CM_PAUSA){
                    debug_m("-pausa");
                command.attach(&Rx_interrupt, Serial::RxIrq); 
                    }               
                else if (ncomm == CM_VERTEX2D) {
                    debug_m("-> Mover piccolo x a %d y y a %d \n",x,x, y);
                    vertex2d(x,y);
                    
                }else if (ncomm == CM_MOTOR){
                    debug_m("-> mover motor paso a paso en x \n");
                    
                    smotor1.step(x*4096,(bool) y);
                    }
                    
                    debug_m("-> ERROR DE COMMANDO: %d %d %d \n " ,ncomm,x,y,y);
                break;   
             }
         }        
    }  
    
    debug_m("fin del comando dibujar..\n");    
    
}
 
 
void saving(){
    debug_m("se inicia el comando guardar..\n");    
    
    MEM_TYPE dato=0;
    mem_free();
    while(dato !=CM_STOP){
        dato = read_command();
        if (dato !=CM_STOP)
               mem_put(dato);
    }  
    debug_m("fin del comado guardar..\n");    
    
}
void Rx_interrupt() {
       debug_m(" dibujo en pausa ... \n"); 
 
     uint8_t error=0;
    MEM_TYPE dato;
    tail_reset();
       while(error==0){
        error = mem_get(&dato);
         if (error==0) {
            
            switch (dato) {   
          
            case  CM_PAUSA: 
                     debug_m("-> pausa \n");
                      Rx_interrupt();
                        break ;  
                
                
                          
               
    }  
    command.attach(&Rx_interrupt, Serial::RxIrq); 
    
}
    }
 }     
 
void debug_m(char *s , ... ){
    #if DEBUG
    command.printf(s);
    #endif  
}