Piccolo
Dependencies: mbed
Fork of 02_LAB_serial_protocol by
main.cpp@2:200a9507b696, 2017-04-26 (annotated)
- Committer:
- fabeltranm
- Date:
- Wed Apr 26 18:31:32 2017 +0000
- Revision:
- 2:200a9507b696
- Parent:
- 1:0bcd96e56022
- Child:
- 3:7c26d338e372
return check_command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabeltranm | 0:55d11eeb0faf | 1 | #include "mbed.h" |
fabeltranm | 0:55d11eeb0faf | 2 | #include "mbed.h" |
fabeltranm | 0:55d11eeb0faf | 3 | |
fabeltranm | 0:55d11eeb0faf | 4 | Serial command(USBTX, USBRX); |
fabeltranm | 2:200a9507b696 | 5 | DigitalOut led(LED1); |
fabeltranm | 0:55d11eeb0faf | 6 | #define DEBUG 1 |
fabeltranm | 0:55d11eeb0faf | 7 | |
fabeltranm | 0:55d11eeb0faf | 8 | //***************************************************************************** |
fabeltranm | 0:55d11eeb0faf | 9 | // COMANDO MOVER MOTOR |
fabeltranm | 0:55d11eeb0faf | 10 | // |POS 1|POS 2|POS 3|POS 4| POS 5| |
fabeltranm | 0:55d11eeb0faf | 11 | // | < | #M | , | #°G | > | |
fabeltranm | 0:55d11eeb0faf | 12 | // |
fabeltranm | 0:55d11eeb0faf | 13 | // #M -> indica el motor que se va a mover |
fabeltranm | 0:55d11eeb0faf | 14 | // #°G -> indica los grados a mover del servomotor |
fabeltranm | 0:55d11eeb0faf | 15 | // <,> -> inicio, separdor y fin de comando |
fabeltranm | 0:55d11eeb0faf | 16 | // el inicio de comando no se almacena en el buffer |
fabeltranm | 0:55d11eeb0faf | 17 | //***************************************************************************** |
fabeltranm | 0:55d11eeb0faf | 18 | |
fabeltranm | 0:55d11eeb0faf | 19 | // VARIABLES PARA DEFINIR EL COMMANDO |
fabeltranm | 0:55d11eeb0faf | 20 | #define BUFF_SIZE 4 |
fabeltranm | 0:55d11eeb0faf | 21 | #define COMM_NUM_MOTOR 0 |
fabeltranm | 0:55d11eeb0faf | 22 | #define COMM_SEPARADOR 1 |
fabeltranm | 0:55d11eeb0faf | 23 | #define COMM_GRADOS_MOTOR 2 |
fabeltranm | 0:55d11eeb0faf | 24 | |
fabeltranm | 0:55d11eeb0faf | 25 | |
fabeltranm | 0:55d11eeb0faf | 26 | uint8_t buffer_command[BUFF_SIZE]={0,0,0,0}; |
fabeltranm | 0:55d11eeb0faf | 27 | |
fabeltranm | 1:0bcd96e56022 | 28 | |
fabeltranm | 1:0bcd96e56022 | 29 | void print_num(uint8_t val) |
fabeltranm | 1:0bcd96e56022 | 30 | |
fabeltranm | 1:0bcd96e56022 | 31 | { |
fabeltranm | 1:0bcd96e56022 | 32 | if (val <10) |
fabeltranm | 1:0bcd96e56022 | 33 | command.putc(val+0x30); |
fabeltranm | 1:0bcd96e56022 | 34 | else |
fabeltranm | 2:200a9507b696 | 35 | command.putc(val-9+0x40); |
fabeltranm | 1:0bcd96e56022 | 36 | |
fabeltranm | 1:0bcd96e56022 | 37 | } |
fabeltranm | 1:0bcd96e56022 | 38 | void print_bin2hex (uint8_t val) |
fabeltranm | 1:0bcd96e56022 | 39 | { |
fabeltranm | 1:0bcd96e56022 | 40 | command.printf(" 0x"); |
fabeltranm | 1:0bcd96e56022 | 41 | print_num(val>>4); |
fabeltranm | 1:0bcd96e56022 | 42 | print_num(val&0x0f); |
fabeltranm | 1:0bcd96e56022 | 43 | |
fabeltranm | 1:0bcd96e56022 | 44 | |
fabeltranm | 1:0bcd96e56022 | 45 | } |
fabeltranm | 1:0bcd96e56022 | 46 | |
fabeltranm | 0:55d11eeb0faf | 47 | // TODO : TIMEOUT UART SERIAL |
fabeltranm | 0:55d11eeb0faf | 48 | void Read_command() |
fabeltranm | 0:55d11eeb0faf | 49 | { |
fabeltranm | 0:55d11eeb0faf | 50 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
fabeltranm | 0:55d11eeb0faf | 51 | buffer_command[i]=command.getc(); |
fabeltranm | 0:55d11eeb0faf | 52 | |
fabeltranm | 0:55d11eeb0faf | 53 | } |
fabeltranm | 0:55d11eeb0faf | 54 | |
fabeltranm | 0:55d11eeb0faf | 55 | void echo_command() |
fabeltranm | 0:55d11eeb0faf | 56 | { |
fabeltranm | 0:55d11eeb0faf | 57 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
fabeltranm | 1:0bcd96e56022 | 58 | print_bin2hex(buffer_command[i]); |
fabeltranm | 0:55d11eeb0faf | 59 | |
fabeltranm | 0:55d11eeb0faf | 60 | } |
fabeltranm | 0:55d11eeb0faf | 61 | |
fabeltranm | 0:55d11eeb0faf | 62 | |
fabeltranm | 2:200a9507b696 | 63 | uint8_t check_command() |
fabeltranm | 0:55d11eeb0faf | 64 | { |
fabeltranm | 0:55d11eeb0faf | 65 | if (buffer_command[BUFF_SIZE-1]== '>' && buffer_command[COMM_SEPARADOR]==','){ |
fabeltranm | 0:55d11eeb0faf | 66 | |
fabeltranm | 2:200a9507b696 | 67 | #if DEBUG |
fabeltranm | 2:200a9507b696 | 68 | command.printf("\nMover Motor:"); |
fabeltranm | 2:200a9507b696 | 69 | print_bin2hex(buffer_command[COMM_NUM_MOTOR]); |
fabeltranm | 2:200a9507b696 | 70 | command.printf(" -> "); |
fabeltranm | 2:200a9507b696 | 71 | print_bin2hex(buffer_command[COMM_GRADOS_MOTOR]); |
fabeltranm | 2:200a9507b696 | 72 | command.printf(" grados \n"); |
fabeltranm | 2:200a9507b696 | 73 | #endif |
fabeltranm | 2:200a9507b696 | 74 | return 1; |
fabeltranm | 2:200a9507b696 | 75 | } |
fabeltranm | 0:55d11eeb0faf | 76 | #if DEBUG |
fabeltranm | 1:0bcd96e56022 | 77 | command.printf("\n ERROR COMANDO -> "); |
fabeltranm | 0:55d11eeb0faf | 78 | echo_command(); |
fabeltranm | 0:55d11eeb0faf | 79 | #endif |
fabeltranm | 2:200a9507b696 | 80 | return 0; |
fabeltranm | 0:55d11eeb0faf | 81 | |
fabeltranm | 0:55d11eeb0faf | 82 | |
fabeltranm | 0:55d11eeb0faf | 83 | } |
fabeltranm | 0:55d11eeb0faf | 84 | int main() { |
fabeltranm | 0:55d11eeb0faf | 85 | #if DEBUG |
fabeltranm | 0:55d11eeb0faf | 86 | command.printf("inicio con debug\n"); |
fabeltranm | 0:55d11eeb0faf | 87 | #else |
fabeltranm | 0:55d11eeb0faf | 88 | command.printf("inicio sin debug\n"); |
fabeltranm | 0:55d11eeb0faf | 89 | #endif |
fabeltranm | 0:55d11eeb0faf | 90 | uint8_t val; |
fabeltranm | 0:55d11eeb0faf | 91 | while(1){ |
fabeltranm | 0:55d11eeb0faf | 92 | val=command.getc(); |
fabeltranm | 0:55d11eeb0faf | 93 | if (val== '<'){ |
fabeltranm | 0:55d11eeb0faf | 94 | Read_command(); |
fabeltranm | 2:200a9507b696 | 95 | if (check_command()) |
fabeltranm | 2:200a9507b696 | 96 | led=1; |
fabeltranm | 2:200a9507b696 | 97 | else |
fabeltranm | 2:200a9507b696 | 98 | led=0; |
fabeltranm | 0:55d11eeb0faf | 99 | } |
fabeltranm | 0:55d11eeb0faf | 100 | } |
fabeltranm | 0:55d11eeb0faf | 101 | } |
fabeltranm | 0:55d11eeb0faf | 102 |