Piccolo
Dependencies: mbed
Fork of 02_LAB_serial_protocol by
main.cpp@6:cb6b868465c3, 2017-09-23 (annotated)
- Committer:
- fabeltranm
- Date:
- Sat Sep 23 13:49:55 2017 +0000
- Revision:
- 6:cb6b868465c3
- Parent:
- 5:bf9591a365a4
- Child:
- 7:fab201aa45b7
comandoled;
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 | 6:cb6b868465c3 | 11 | // | < | #C | a | b | c | d | > | |
fabeltranm | 0:55d11eeb0faf | 12 | // |
fabeltranm | 6:cb6b868465c3 | 13 | // #C -> indica el comando |
fabeltranm | 6:cb6b868465c3 | 14 | // a,b,c,d parametros del comando |
fabeltranm | 6:cb6b868465c3 | 15 | // <,> -> inicio, 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 | 6:cb6b868465c3 | 21 | #define COMM_N 0 |
fabeltranm | 6:cb6b868465c3 | 22 | #define INITPARAMETER 1 |
fabeltranm | 0:55d11eeb0faf | 23 | |
fabeltranm | 0:55d11eeb0faf | 24 | |
fabeltranm | 0:55d11eeb0faf | 25 | uint8_t buffer_command[BUFF_SIZE]={0,0,0,0}; |
fabeltranm | 0:55d11eeb0faf | 26 | |
fabeltranm | 1:0bcd96e56022 | 27 | |
fabeltranm | 1:0bcd96e56022 | 28 | void print_num(uint8_t val) |
fabeltranm | 1:0bcd96e56022 | 29 | |
fabeltranm | 1:0bcd96e56022 | 30 | { |
fabeltranm | 1:0bcd96e56022 | 31 | if (val <10) |
fabeltranm | 1:0bcd96e56022 | 32 | command.putc(val+0x30); |
fabeltranm | 1:0bcd96e56022 | 33 | else |
fabeltranm | 2:200a9507b696 | 34 | command.putc(val-9+0x40); |
fabeltranm | 1:0bcd96e56022 | 35 | |
fabeltranm | 1:0bcd96e56022 | 36 | } |
fabeltranm | 1:0bcd96e56022 | 37 | void print_bin2hex (uint8_t val) |
fabeltranm | 1:0bcd96e56022 | 38 | { |
fabeltranm | 1:0bcd96e56022 | 39 | command.printf(" 0x"); |
fabeltranm | 1:0bcd96e56022 | 40 | print_num(val>>4); |
fabeltranm | 1:0bcd96e56022 | 41 | print_num(val&0x0f); |
fabeltranm | 1:0bcd96e56022 | 42 | |
fabeltranm | 1:0bcd96e56022 | 43 | |
fabeltranm | 1:0bcd96e56022 | 44 | } |
fabeltranm | 1:0bcd96e56022 | 45 | |
fabeltranm | 0:55d11eeb0faf | 46 | // TODO : TIMEOUT UART SERIAL |
fabeltranm | 0:55d11eeb0faf | 47 | void Read_command() |
fabeltranm | 0:55d11eeb0faf | 48 | { |
fabeltranm | 0:55d11eeb0faf | 49 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
fabeltranm | 0:55d11eeb0faf | 50 | buffer_command[i]=command.getc(); |
fabeltranm | 0:55d11eeb0faf | 51 | |
fabeltranm | 0:55d11eeb0faf | 52 | } |
fabeltranm | 0:55d11eeb0faf | 53 | |
fabeltranm | 0:55d11eeb0faf | 54 | void echo_command() |
fabeltranm | 0:55d11eeb0faf | 55 | { |
fabeltranm | 0:55d11eeb0faf | 56 | for (uint8_t i=0; i<BUFF_SIZE;i++) |
fabeltranm | 1:0bcd96e56022 | 57 | print_bin2hex(buffer_command[i]); |
fabeltranm | 0:55d11eeb0faf | 58 | |
fabeltranm | 0:55d11eeb0faf | 59 | } |
fabeltranm | 0:55d11eeb0faf | 60 | |
fabeltranm | 0:55d11eeb0faf | 61 | |
fabeltranm | 2:200a9507b696 | 62 | uint8_t check_command() |
fabeltranm | 0:55d11eeb0faf | 63 | { |
fabeltranm | 6:cb6b868465c3 | 64 | if (buffer_command[BUFF_SIZE-1]== '>'){ |
fabeltranm | 0:55d11eeb0faf | 65 | |
fabeltranm | 2:200a9507b696 | 66 | #if DEBUG |
fabeltranm | 6:cb6b868465c3 | 67 | command.printf("\nComando:"); |
fabeltranm | 6:cb6b868465c3 | 68 | print_bin2hex(buffer_command[COMM_N]); |
fabeltranm | 2:200a9507b696 | 69 | command.printf(" -> "); |
fabeltranm | 2:200a9507b696 | 70 | #endif |
fabeltranm | 2:200a9507b696 | 71 | return 1; |
fabeltranm | 2:200a9507b696 | 72 | } |
fabeltranm | 0:55d11eeb0faf | 73 | #if DEBUG |
fabeltranm | 1:0bcd96e56022 | 74 | command.printf("\n ERROR COMANDO -> "); |
fabeltranm | 0:55d11eeb0faf | 75 | echo_command(); |
fabeltranm | 0:55d11eeb0faf | 76 | #endif |
fabeltranm | 2:200a9507b696 | 77 | return 0; |
fabeltranm | 0:55d11eeb0faf | 78 | |
fabeltranm | 0:55d11eeb0faf | 79 | |
fabeltranm | 0:55d11eeb0faf | 80 | } |
fabeltranm | 6:cb6b868465c3 | 81 | void command_led(int tm) |
fabeltranm | 5:bf9591a365a4 | 82 | { |
fabeltranm | 5:bf9591a365a4 | 83 | //EJEMPLO DE COMANDO |
fabeltranm | 5:bf9591a365a4 | 84 | led=1; |
fabeltranm | 6:cb6b868465c3 | 85 | wait(tm); |
fabeltranm | 5:bf9591a365a4 | 86 | led=0; |
fabeltranm | 5:bf9591a365a4 | 87 | |
fabeltranm | 5:bf9591a365a4 | 88 | } |
fabeltranm | 0:55d11eeb0faf | 89 | int main() { |
fabeltranm | 0:55d11eeb0faf | 90 | #if DEBUG |
fabeltranm | 0:55d11eeb0faf | 91 | command.printf("inicio con debug\n"); |
fabeltranm | 0:55d11eeb0faf | 92 | #else |
fabeltranm | 0:55d11eeb0faf | 93 | command.printf("inicio sin debug\n"); |
fabeltranm | 0:55d11eeb0faf | 94 | #endif |
fabeltranm | 0:55d11eeb0faf | 95 | uint8_t val; |
fabeltranm | 0:55d11eeb0faf | 96 | while(1){ |
fabeltranm | 0:55d11eeb0faf | 97 | val=command.getc(); |
fabeltranm | 0:55d11eeb0faf | 98 | if (val== '<'){ |
fabeltranm | 0:55d11eeb0faf | 99 | Read_command(); |
fabeltranm | 4:bcc2d1bebb95 | 100 | if (check_command()){ |
fabeltranm | 6:cb6b868465c3 | 101 | command_led(buffer_command[INITPARAMETER]); |
fabeltranm | 6:cb6b868465c3 | 102 | #if DEBUG |
fabeltranm | 6:cb6b868465c3 | 103 | echo_command(); |
fabeltranm | 6:cb6b868465c3 | 104 | #endif |
fabeltranm | 6:cb6b868465c3 | 105 | } |
fabeltranm | 3:7c26d338e372 | 106 | } |
fabeltranm | 3:7c26d338e372 | 107 | |
fabeltranm | 0:55d11eeb0faf | 108 | } |
fabeltranm | 0:55d11eeb0faf | 109 | } |
fabeltranm | 0:55d11eeb0faf | 110 |