Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 02_LAB_serial_protocol by
Diff: main.cpp
- Revision:
- 0:55d11eeb0faf
- Child:
- 1:0bcd96e56022
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 26 18:04:57 2017 +0000 @@ -0,0 +1,83 @@ +#include "mbed.h" +#include "mbed.h" + +Serial command(USBTX, USBRX); + +#define DEBUG 1 + +//***************************************************************************** +// COMANDO MOVER MOTOR +// |POS 1|POS 2|POS 3|POS 4| POS 5| +// | < | #M | , | #°G | > | +// +// #M -> indica el motor que se va a mover +// #°G -> indica los grados a mover del servomotor +// <,> -> inicio, separdor y fin de comando +// el inicio de comando no se almacena en el buffer +//***************************************************************************** + +// VARIABLES PARA DEFINIR EL COMMANDO +#define BUFF_SIZE 4 +#define COMM_NUM_MOTOR 0 +#define COMM_SEPARADOR 1 +#define COMM_GRADOS_MOTOR 2 + + +uint8_t buffer_command[BUFF_SIZE]={0,0,0,0}; + +// TODO : TIMEOUT UART SERIAL +void Read_command() +{ + for (uint8_t i=0; i<BUFF_SIZE;i++) + buffer_command[i]=command.getc(); + +} + +void echo_command() +{ + for (uint8_t i=0; i<BUFF_SIZE;i++) + command.putc(buffer_command[i]); + +} + + +void check_command() +{ + if (buffer_command[BUFF_SIZE-1]== '>' && buffer_command[COMM_SEPARADOR]==','){ + + #if DEBUG + command.printf("Mover Motor:"); + command.putc(buffer_command[COMM_NUM_MOTOR]+0x30); + command.printf("->"); + command.putc(buffer_command[COMM_GRADOS_MOTOR]); + command.printf(" grados \n"); + #endif + }else{ + #if DEBUG + command.printf("ERROR COMANDO -> "); + echo_command(); + #endif + + } + +} +int main() { + #if DEBUG + command.printf("inicio con debug\n"); + #else + command.printf("inicio sin debug\n"); + #endif + uint8_t val; + while(1){ + val=command.getc(); + if (val== '<'){ + Read_command(); + #if DEBUG + echo_command(); + #endif + check_command(); + + } + } +} +