Acercamiento a lo que pide el profe

Dependencies:   mbed

Fork of 01-01EjercicioFuncionXY by ferney alberto beltran molina

Tomando como base los ejemplos que el Ing. Ferney subió, realizamos este primer acercamiento al objetivo del primer corte.

en síntesis se generó el código de guardar y ejecutar.

Slds!

01_main.cpp

Committer:
Bethory
Date:
2018-03-14
Revision:
4:8a884a5dd8c8
Parent:
3:3665eb7346e2
Child:
5:8e100835b017

File content as of revision 4:8a884a5dd8c8:

#include "mbed.h"
#include "draw.h"
#define CM_EJECUTAR 0xFF
#define CM_GUARDAR 0xFE
#define CM_VERTEX2D 0xFD
#define CM_DRAW 0XFC
#define CM_NODRAW 0xFB
#define CM_STOP 0xFA
#define CM_ENTER 0xF0

#define MEM_SIZE 10
#define MEM_TYPE uint32_t //significa que de ahora en más donde diga MEM_TYPE será tipo uint32_t

uint8_t mem_head = 0;
uint8_t mem_tail = 0;
bool full = 0;

MEM_TYPE buffer[MEM_SIZE];
Serial command(USBTX, USBRX);

void mem_free(){ //Estaba como uint32_t no como void
    mem_head=0;
    full=0;
}

bool mem_put(MEM_TYPE data){ //Escribir
    if (full)
        return 1;
    buffer[mem_head] = data; //carga en dato en el buffer
    mem_head += 1;
    if (mem_head == MEM_SIZE)
        full =1;
    return 0;
}

bool mem_get(MEM_TYPE* data){ //Leer
    if (mem_head == 0)
        return 1; 
    if (mem_head == mem_tail)
        return 1; 
    *data = buffer[mem_tail];
    mem_tail += 1;
    return 0;
}

void ejecutar(){
    command.printf("se inicia el comando de ejecutar...\n");
    /*for(int i=1;i<=MEM_SIZE;i++){
        command.printf("%x\n",buffer[i]);
    }*/
}

void guardar(){
    command.printf("se inicia el comando de guardar...\n");
    uint32_t val; //significa que la variable "val" es del tipo uint32_t
    char dato1,dato2;
    
    dato1=command.getc();
    if(dato1==CM_STOP){
        dato2=command.getc();
        if(dato2==CM_ENTER){
            val=dato1;
            return;
        }
    }
    if(dato1==CM_NODRAW|dato1==CM_DRAW){
        dato2=command.getc();
        if(dato2==CM_ENTER){
            val=dato1;
        }
    }
    if(dato1==CM_VERTEX2D){
        dato2=command.getc();
        if(dato2<=0x32){
            val=CM_VERTEX2D;
            val=val<<8;
            val=val|dato2;
            dato2=command.getc();
            if(dato2<=0x32){
                val=val<<8;
                val=val|dato2;
                dato2=command.getc();
                if(dato2==CM_ENTER){
                    val=val<<8;
                    val=val|CM_ENTER;
                }
            }  
        }     
    }
}

int main(){
    init_servo();
    command.baud(9600);
    home(); //llama a no_draw y va a 0,0
   
    char read_cc;
    while(1){
        read_cc=command.getc();
        switch (read_cc){
            //case CM_STOP: command.printf("comando no válido en este momento");break; //FA
            //case CM_NODRAW: guardar(read_cc);break; //FB
            //case CM_DRAW: guardar(read_cc);break; //FC
            //case CM_VERTEX2D: command.printf("comando no válido en este momento");break; //FD
            case CM_GUARDAR: guardar(); break; //FE
            case CM_EJECUTAR: ejecutar(); break;  //FF
            default: command.printf("Error de comando \n");break;      
        }
    }  
}
/*
FE FB FD 00 00 F0
FC F0 FD 32 00 F0
FD 32 32 F0 FD 00
32 F0 FD 00 00 F0
FB F0 FA F0 FF

FE F0 FD 54 32 F0 FC F0 FD 32 00 F0 FD 32 32 F0 FD 00 32 F0 FD 00 00 F0 FB F0 FA F0 FF
FE 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 FA FF

#define CM_EJECUTAR 0xFF
#define CM_GUARDAR 0xFE
#define CM_VERTEX2D 0xFD
#define CM_DRAW 0XFC
#define CM_NODRAW 0xFB
#define CM_STOP 0xFA
#define CM_END 0xF0

    while(!full){
        for(int i=1;i<=4;i++){
            raw_data=command.getc();
            command.printf("%x",raw_data);
            if(raw_data==CM_STOP)
                return;
            if(i==1){
                val=raw_data;
            }else{
                
                val=val << 8;
                val=val|raw_data;
            }
            command.printf(" %d",i);
            command.printf(" %x\n",val);
        }
        mem_put(val);
    }
    //mem_get(&val); //&val es la dirección de la variable, no su contenido
    //command.putc(val); //manda por puerto serial el contenido de "val" npi para qué

*/