Piccolo

Dependencies:   mbed

Fork of 02_LAB_serial_protocol by ferney alberto beltran molina

Committer:
fabeltranm
Date:
Sat Sep 23 14:44:47 2017 +0000
Revision:
7:fab201aa45b7
Parent:
6:cb6b868465c3
Child:
8:38ae341e2a4f
case command

Who changed what in which revision?

UserRevisionLine numberNew 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 7:fab201aa45b7 11 // | < | #C | a | b | > |
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 7:fab201aa45b7 25 // COMANDOS
fabeltranm 7:fab201aa45b7 26 #define LED_NC 0
fabeltranm 7:fab201aa45b7 27 #define DOT_NC 1
fabeltranm 7:fab201aa45b7 28 #define LINE_NC 2
fabeltranm 7:fab201aa45b7 29 #define RECTANGLE_NC 3
fabeltranm 7:fab201aa45b7 30 #define CICLE_NC 4
fabeltranm 7:fab201aa45b7 31 #define HOME_NC 5
fabeltranm 7:fab201aa45b7 32
fabeltranm 7:fab201aa45b7 33
fabeltranm 7:fab201aa45b7 34
fabeltranm 0:55d11eeb0faf 35 uint8_t buffer_command[BUFF_SIZE]={0,0,0,0};
fabeltranm 0:55d11eeb0faf 36
fabeltranm 1:0bcd96e56022 37
fabeltranm 1:0bcd96e56022 38 void print_num(uint8_t val)
fabeltranm 1:0bcd96e56022 39
fabeltranm 1:0bcd96e56022 40 {
fabeltranm 1:0bcd96e56022 41 if (val <10)
fabeltranm 1:0bcd96e56022 42 command.putc(val+0x30);
fabeltranm 1:0bcd96e56022 43 else
fabeltranm 2:200a9507b696 44 command.putc(val-9+0x40);
fabeltranm 1:0bcd96e56022 45
fabeltranm 1:0bcd96e56022 46 }
fabeltranm 1:0bcd96e56022 47 void print_bin2hex (uint8_t val)
fabeltranm 1:0bcd96e56022 48 {
fabeltranm 1:0bcd96e56022 49 command.printf(" 0x");
fabeltranm 1:0bcd96e56022 50 print_num(val>>4);
fabeltranm 1:0bcd96e56022 51 print_num(val&0x0f);
fabeltranm 1:0bcd96e56022 52
fabeltranm 1:0bcd96e56022 53
fabeltranm 1:0bcd96e56022 54 }
fabeltranm 1:0bcd96e56022 55
fabeltranm 0:55d11eeb0faf 56 // TODO : TIMEOUT UART SERIAL
fabeltranm 0:55d11eeb0faf 57 void Read_command()
fabeltranm 0:55d11eeb0faf 58 {
fabeltranm 0:55d11eeb0faf 59 for (uint8_t i=0; i<BUFF_SIZE;i++)
fabeltranm 0:55d11eeb0faf 60 buffer_command[i]=command.getc();
fabeltranm 0:55d11eeb0faf 61
fabeltranm 0:55d11eeb0faf 62 }
fabeltranm 0:55d11eeb0faf 63
fabeltranm 0:55d11eeb0faf 64 void echo_command()
fabeltranm 0:55d11eeb0faf 65 {
fabeltranm 0:55d11eeb0faf 66 for (uint8_t i=0; i<BUFF_SIZE;i++)
fabeltranm 1:0bcd96e56022 67 print_bin2hex(buffer_command[i]);
fabeltranm 0:55d11eeb0faf 68
fabeltranm 0:55d11eeb0faf 69 }
fabeltranm 0:55d11eeb0faf 70
fabeltranm 0:55d11eeb0faf 71
fabeltranm 2:200a9507b696 72 uint8_t check_command()
fabeltranm 0:55d11eeb0faf 73 {
fabeltranm 6:cb6b868465c3 74 if (buffer_command[BUFF_SIZE-1]== '>'){
fabeltranm 0:55d11eeb0faf 75
fabeltranm 2:200a9507b696 76 #if DEBUG
fabeltranm 6:cb6b868465c3 77 command.printf("\nComando:");
fabeltranm 6:cb6b868465c3 78 print_bin2hex(buffer_command[COMM_N]);
fabeltranm 2:200a9507b696 79 command.printf(" -> ");
fabeltranm 2:200a9507b696 80 #endif
fabeltranm 2:200a9507b696 81 return 1;
fabeltranm 2:200a9507b696 82 }
fabeltranm 0:55d11eeb0faf 83 #if DEBUG
fabeltranm 1:0bcd96e56022 84 command.printf("\n ERROR COMANDO -> ");
fabeltranm 0:55d11eeb0faf 85 echo_command();
fabeltranm 0:55d11eeb0faf 86 #endif
fabeltranm 2:200a9507b696 87 return 0;
fabeltranm 0:55d11eeb0faf 88
fabeltranm 0:55d11eeb0faf 89
fabeltranm 0:55d11eeb0faf 90 }
fabeltranm 6:cb6b868465c3 91 void command_led(int tm)
fabeltranm 5:bf9591a365a4 92 {
fabeltranm 5:bf9591a365a4 93 //EJEMPLO DE COMANDO
fabeltranm 5:bf9591a365a4 94 led=1;
fabeltranm 6:cb6b868465c3 95 wait(tm);
fabeltranm 5:bf9591a365a4 96 led=0;
fabeltranm 5:bf9591a365a4 97
fabeltranm 5:bf9591a365a4 98 }
fabeltranm 7:fab201aa45b7 99 void command_exe()
fabeltranm 7:fab201aa45b7 100 {
fabeltranm 7:fab201aa45b7 101
fabeltranm 7:fab201aa45b7 102 switch (buffer_command[COMM_N]){
fabeltranm 7:fab201aa45b7 103
fabeltranm 7:fab201aa45b7 104 case (LED_NC): command_led(buffer_command[INITPARAMETER]);
fabeltranm 7:fab201aa45b7 105 break;
fabeltranm 7:fab201aa45b7 106 case (DOT_NC):
fabeltranm 7:fab201aa45b7 107 #if DEBUG
fabeltranm 7:fab201aa45b7 108 command.printf("draw dot\n");
fabeltranm 7:fab201aa45b7 109 #endif
fabeltranm 7:fab201aa45b7 110
fabeltranm 7:fab201aa45b7 111 break;
fabeltranm 7:fab201aa45b7 112
fabeltranm 7:fab201aa45b7 113 case LINE_NC:
fabeltranm 7:fab201aa45b7 114 #if DEBUG
fabeltranm 7:fab201aa45b7 115 command.printf("draw line\n");
fabeltranm 7:fab201aa45b7 116 #endif
fabeltranm 7:fab201aa45b7 117
fabeltranm 7:fab201aa45b7 118
fabeltranm 7:fab201aa45b7 119 break;
fabeltranm 7:fab201aa45b7 120
fabeltranm 7:fab201aa45b7 121 case RECTANGLE_NC:
fabeltranm 7:fab201aa45b7 122 #if DEBUG
fabeltranm 7:fab201aa45b7 123 command.printf("draw rectabgle\n");
fabeltranm 7:fab201aa45b7 124 #endif
fabeltranm 7:fab201aa45b7 125
fabeltranm 7:fab201aa45b7 126
fabeltranm 7:fab201aa45b7 127 break;
fabeltranm 7:fab201aa45b7 128
fabeltranm 7:fab201aa45b7 129 default:
fabeltranm 7:fab201aa45b7 130
fabeltranm 7:fab201aa45b7 131 #if DEBUG
fabeltranm 7:fab201aa45b7 132 command.printf("comando no encontrado\n");
fabeltranm 7:fab201aa45b7 133 #endif
fabeltranm 7:fab201aa45b7 134
fabeltranm 7:fab201aa45b7 135
fabeltranm 7:fab201aa45b7 136 }
fabeltranm 7:fab201aa45b7 137 }
fabeltranm 7:fab201aa45b7 138
fabeltranm 7:fab201aa45b7 139
fabeltranm 7:fab201aa45b7 140
fabeltranm 0:55d11eeb0faf 141 int main() {
fabeltranm 0:55d11eeb0faf 142 #if DEBUG
fabeltranm 0:55d11eeb0faf 143 command.printf("inicio con debug\n");
fabeltranm 0:55d11eeb0faf 144 #else
fabeltranm 0:55d11eeb0faf 145 command.printf("inicio sin debug\n");
fabeltranm 0:55d11eeb0faf 146 #endif
fabeltranm 0:55d11eeb0faf 147 uint8_t val;
fabeltranm 0:55d11eeb0faf 148 while(1){
fabeltranm 0:55d11eeb0faf 149 val=command.getc();
fabeltranm 0:55d11eeb0faf 150 if (val== '<'){
fabeltranm 0:55d11eeb0faf 151 Read_command();
fabeltranm 4:bcc2d1bebb95 152 if (check_command()){
fabeltranm 7:fab201aa45b7 153 command_exe();
fabeltranm 6:cb6b868465c3 154 #if DEBUG
fabeltranm 6:cb6b868465c3 155 echo_command();
fabeltranm 6:cb6b868465c3 156 #endif
fabeltranm 6:cb6b868465c3 157 }
fabeltranm 3:7c26d338e372 158 }
fabeltranm 3:7c26d338e372 159
fabeltranm 0:55d11eeb0faf 160 }
fabeltranm 0:55d11eeb0faf 161 }
fabeltranm 0:55d11eeb0faf 162