![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
lighthouse
Dependencies: RobotArmController SerialHalfDuplex mbed
Fork of PR_RobotArm by
serial.cpp@2:55f39e7883a6, 2017-07-14 (annotated)
- Committer:
- JessicaGao
- Date:
- Fri Jul 14 12:32:18 2017 +0000
- Revision:
- 2:55f39e7883a6
lighthouse;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JessicaGao | 2:55f39e7883a6 | 1 | |
JessicaGao | 2:55f39e7883a6 | 2 | #include "serial.h" |
JessicaGao | 2:55f39e7883a6 | 3 | #include "mbed.h" |
JessicaGao | 2:55f39e7883a6 | 4 | #include "robotarm.h" |
JessicaGao | 2:55f39e7883a6 | 5 | |
JessicaGao | 2:55f39e7883a6 | 6 | #define ENABLE_BLUETOOTH 1 |
JessicaGao | 2:55f39e7883a6 | 7 | #define ENABLE_PC_SERIAL 1 |
JessicaGao | 2:55f39e7883a6 | 8 | #define COMMAND_MESSAGE_BYTE 0X1D |
JessicaGao | 2:55f39e7883a6 | 9 | |
JessicaGao | 2:55f39e7883a6 | 10 | #define PC_BAUD 115200 |
JessicaGao | 2:55f39e7883a6 | 11 | #define BLUETOOTH_BAUD 115200 |
JessicaGao | 2:55f39e7883a6 | 12 | |
JessicaGao | 2:55f39e7883a6 | 13 | Serial bt(p13, p14); |
JessicaGao | 2:55f39e7883a6 | 14 | |
JessicaGao | 2:55f39e7883a6 | 15 | int turning = 1; |
JessicaGao | 2:55f39e7883a6 | 16 | |
JessicaGao | 2:55f39e7883a6 | 17 | char pc_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 18 | char pc_command_message_byte = 0; |
JessicaGao | 2:55f39e7883a6 | 19 | char pc_command_message[3]; |
JessicaGao | 2:55f39e7883a6 | 20 | |
JessicaGao | 2:55f39e7883a6 | 21 | char bt_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 22 | char bt_command_message_byte = 0; |
JessicaGao | 2:55f39e7883a6 | 23 | char bt_command_message[3]; |
JessicaGao | 2:55f39e7883a6 | 24 | |
JessicaGao | 2:55f39e7883a6 | 25 | char file_transfer_mode = 0; |
JessicaGao | 2:55f39e7883a6 | 26 | Timeout pc_command_timeout; |
JessicaGao | 2:55f39e7883a6 | 27 | Timeout bt_command_timeout; |
JessicaGao | 2:55f39e7883a6 | 28 | |
JessicaGao | 2:55f39e7883a6 | 29 | void SerialControl::setup_serial_interfaces() |
JessicaGao | 2:55f39e7883a6 | 30 | { |
JessicaGao | 2:55f39e7883a6 | 31 | if(ENABLE_PC_SERIAL) { |
JessicaGao | 2:55f39e7883a6 | 32 | pc.baud(PC_BAUD); |
JessicaGao | 2:55f39e7883a6 | 33 | pc.attach(this,&SerialControl::IF_pc_rx_callback, Serial::RxIrq); |
JessicaGao | 2:55f39e7883a6 | 34 | } |
JessicaGao | 2:55f39e7883a6 | 35 | if(ENABLE_BLUETOOTH) { |
JessicaGao | 2:55f39e7883a6 | 36 | bt.baud(BLUETOOTH_BAUD); |
JessicaGao | 2:55f39e7883a6 | 37 | bt.attach(this,&SerialControl::IF_bt_rx_callback, Serial::RxIrq); |
JessicaGao | 2:55f39e7883a6 | 38 | } |
JessicaGao | 2:55f39e7883a6 | 39 | } |
JessicaGao | 2:55f39e7883a6 | 40 | |
JessicaGao | 2:55f39e7883a6 | 41 | void SerialControl::IF_handle_command_serial_message(char message[3], char interface){ |
JessicaGao | 2:55f39e7883a6 | 42 | if(message[0]==1){ |
JessicaGao | 2:55f39e7883a6 | 43 | turning = 1; |
JessicaGao | 2:55f39e7883a6 | 44 | pc.printf("start turn(beacon on)%d\n",turning); |
JessicaGao | 2:55f39e7883a6 | 45 | } |
JessicaGao | 2:55f39e7883a6 | 46 | else if(message[1]==1){ |
JessicaGao | 2:55f39e7883a6 | 47 | pc.printf("stop turn(beacon on)%d\n", turning); |
JessicaGao | 2:55f39e7883a6 | 48 | turning = 0; |
JessicaGao | 2:55f39e7883a6 | 49 | } |
JessicaGao | 2:55f39e7883a6 | 50 | else if(message[2]==1){ |
JessicaGao | 2:55f39e7883a6 | 51 | pc.printf("beacon off\n"); |
JessicaGao | 2:55f39e7883a6 | 52 | turning = 2; |
JessicaGao | 2:55f39e7883a6 | 53 | } |
JessicaGao | 2:55f39e7883a6 | 54 | else if(message[0] == 2){ |
JessicaGao | 2:55f39e7883a6 | 55 | bt.printf("%c%d",COMMAND_MESSAGE_BYTE, turning); |
JessicaGao | 2:55f39e7883a6 | 56 | pc.printf("%c%d", COMMAND_MESSAGE_BYTE, turning); |
JessicaGao | 2:55f39e7883a6 | 57 | } |
JessicaGao | 2:55f39e7883a6 | 58 | } |
JessicaGao | 2:55f39e7883a6 | 59 | |
JessicaGao | 2:55f39e7883a6 | 60 | |
JessicaGao | 2:55f39e7883a6 | 61 | Timeout bt_message_timeout; |
JessicaGao | 2:55f39e7883a6 | 62 | //static float bt_message_timeout_period = 0.001; // 1 millisecond |
JessicaGao | 2:55f39e7883a6 | 63 | char bt_buffer[255]; |
JessicaGao | 2:55f39e7883a6 | 64 | int bt_buffer_index = 0; |
JessicaGao | 2:55f39e7883a6 | 65 | |
JessicaGao | 2:55f39e7883a6 | 66 | void SerialControl::IF_bt_message_timeout() |
JessicaGao | 2:55f39e7883a6 | 67 | { |
JessicaGao | 2:55f39e7883a6 | 68 | char buffer[255]; |
JessicaGao | 2:55f39e7883a6 | 69 | |
JessicaGao | 2:55f39e7883a6 | 70 | sprintf(buffer, bt_buffer, bt_buffer_index); |
JessicaGao | 2:55f39e7883a6 | 71 | buffer[bt_buffer_index] = 0; |
JessicaGao | 2:55f39e7883a6 | 72 | if(file_transfer_mode == 1) { |
JessicaGao | 2:55f39e7883a6 | 73 | } else { |
JessicaGao | 2:55f39e7883a6 | 74 | // debug("BT message timeout: %s [%d chars]\n", buffer, bt_buffer_index); |
JessicaGao | 2:55f39e7883a6 | 75 | if(bt_buffer_index == 5 && buffer[0] == COMMAND_MESSAGE_BYTE && buffer[4] == COMMAND_MESSAGE_BYTE) { |
JessicaGao | 2:55f39e7883a6 | 76 | bt_command_message[0] = buffer[1]; |
JessicaGao | 2:55f39e7883a6 | 77 | bt_command_message[1] = buffer[2]; |
JessicaGao | 2:55f39e7883a6 | 78 | bt_command_message[2] = buffer[3]; |
JessicaGao | 2:55f39e7883a6 | 79 | IF_handle_command_serial_message(bt_command_message , 1); |
JessicaGao | 2:55f39e7883a6 | 80 | } |
JessicaGao | 2:55f39e7883a6 | 81 | } |
JessicaGao | 2:55f39e7883a6 | 82 | bt_buffer_index = 0; |
JessicaGao | 2:55f39e7883a6 | 83 | } |
JessicaGao | 2:55f39e7883a6 | 84 | |
JessicaGao | 2:55f39e7883a6 | 85 | |
JessicaGao | 2:55f39e7883a6 | 86 | void SerialControl::IF_bt_rx_callback() |
JessicaGao | 2:55f39e7883a6 | 87 | { |
JessicaGao | 2:55f39e7883a6 | 88 | int count = 0; |
JessicaGao | 2:55f39e7883a6 | 89 | char message_array[255]; |
JessicaGao | 2:55f39e7883a6 | 90 | |
JessicaGao | 2:55f39e7883a6 | 91 | wait_ms(500); // Wait 0.5ms to allow a complete message to arrive before atttempting to process it |
JessicaGao | 2:55f39e7883a6 | 92 | |
JessicaGao | 2:55f39e7883a6 | 93 | while(bt.readable()) { |
JessicaGao | 2:55f39e7883a6 | 94 | char tc = bt.getc(); |
JessicaGao | 2:55f39e7883a6 | 95 | message_array[count] = tc; |
JessicaGao | 2:55f39e7883a6 | 96 | count ++; |
JessicaGao | 2:55f39e7883a6 | 97 | if(bt_command_message_started == 1) { |
JessicaGao | 2:55f39e7883a6 | 98 | if(bt_command_message_byte == 3) { |
JessicaGao | 2:55f39e7883a6 | 99 | bt_command_timeout.detach(); |
JessicaGao | 2:55f39e7883a6 | 100 | if(tc == COMMAND_MESSAGE_BYTE) { |
JessicaGao | 2:55f39e7883a6 | 101 | // A complete command message succesfully received, call handler |
JessicaGao | 2:55f39e7883a6 | 102 | bt_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 103 | count = 0; |
JessicaGao | 2:55f39e7883a6 | 104 | IF_handle_command_serial_message(bt_command_message , 1); |
JessicaGao | 2:55f39e7883a6 | 105 | } else { |
JessicaGao | 2:55f39e7883a6 | 106 | // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message |
JessicaGao | 2:55f39e7883a6 | 107 | bt_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 108 | message_array[0] = COMMAND_MESSAGE_BYTE; |
JessicaGao | 2:55f39e7883a6 | 109 | message_array[1] = bt_command_message[0]; |
JessicaGao | 2:55f39e7883a6 | 110 | message_array[2] = bt_command_message[1]; |
JessicaGao | 2:55f39e7883a6 | 111 | message_array[3] = bt_command_message[2]; |
JessicaGao | 2:55f39e7883a6 | 112 | message_array[4] = tc; |
JessicaGao | 2:55f39e7883a6 | 113 | count = 5; |
JessicaGao | 2:55f39e7883a6 | 114 | } |
JessicaGao | 2:55f39e7883a6 | 115 | } else { |
JessicaGao | 2:55f39e7883a6 | 116 | |
JessicaGao | 2:55f39e7883a6 | 117 | bt_command_message[bt_command_message_byte] = tc; |
JessicaGao | 2:55f39e7883a6 | 118 | bt_command_message_byte ++; |
JessicaGao | 2:55f39e7883a6 | 119 | } |
JessicaGao | 2:55f39e7883a6 | 120 | } else { |
JessicaGao | 2:55f39e7883a6 | 121 | if(count == 1) { |
JessicaGao | 2:55f39e7883a6 | 122 | if(tc == COMMAND_MESSAGE_BYTE) { |
JessicaGao | 2:55f39e7883a6 | 123 | bt_command_message_started = 1; |
JessicaGao | 2:55f39e7883a6 | 124 | bt_command_message_byte = 0; |
JessicaGao | 2:55f39e7883a6 | 125 | |
JessicaGao | 2:55f39e7883a6 | 126 | } |
JessicaGao | 2:55f39e7883a6 | 127 | } |
JessicaGao | 2:55f39e7883a6 | 128 | } |
JessicaGao | 2:55f39e7883a6 | 129 | } |
JessicaGao | 2:55f39e7883a6 | 130 | } |
JessicaGao | 2:55f39e7883a6 | 131 | |
JessicaGao | 2:55f39e7883a6 | 132 | void SerialControl::IF_pc_rx_callback() |
JessicaGao | 2:55f39e7883a6 | 133 | { |
JessicaGao | 2:55f39e7883a6 | 134 | int count = 0; |
JessicaGao | 2:55f39e7883a6 | 135 | char message_array[255]; |
JessicaGao | 2:55f39e7883a6 | 136 | |
JessicaGao | 2:55f39e7883a6 | 137 | while(pc.readable()) { |
JessicaGao | 2:55f39e7883a6 | 138 | char tc = pc.getc(); |
JessicaGao | 2:55f39e7883a6 | 139 | message_array[count] = tc; |
JessicaGao | 2:55f39e7883a6 | 140 | count ++; |
JessicaGao | 2:55f39e7883a6 | 141 | if(pc_command_message_started == 1) { |
JessicaGao | 2:55f39e7883a6 | 142 | if(pc_command_message_byte == 3) { |
JessicaGao | 2:55f39e7883a6 | 143 | pc_command_timeout.detach(); |
JessicaGao | 2:55f39e7883a6 | 144 | if(tc == COMMAND_MESSAGE_BYTE) { |
JessicaGao | 2:55f39e7883a6 | 145 | // A complete command message succesfully received, call handler |
JessicaGao | 2:55f39e7883a6 | 146 | pc_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 147 | count = 0; |
JessicaGao | 2:55f39e7883a6 | 148 | IF_handle_command_serial_message(pc_command_message , 0); |
JessicaGao | 2:55f39e7883a6 | 149 | } else { |
JessicaGao | 2:55f39e7883a6 | 150 | // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message |
JessicaGao | 2:55f39e7883a6 | 151 | pc_command_message_started = 0; |
JessicaGao | 2:55f39e7883a6 | 152 | message_array[0] = COMMAND_MESSAGE_BYTE; |
JessicaGao | 2:55f39e7883a6 | 153 | message_array[1] = pc_command_message[0]; |
JessicaGao | 2:55f39e7883a6 | 154 | message_array[2] = pc_command_message[1]; |
JessicaGao | 2:55f39e7883a6 | 155 | message_array[3] = pc_command_message[2]; |
JessicaGao | 2:55f39e7883a6 | 156 | message_array[4] = tc; |
JessicaGao | 2:55f39e7883a6 | 157 | count = 5; |
JessicaGao | 2:55f39e7883a6 | 158 | } |
JessicaGao | 2:55f39e7883a6 | 159 | } else { |
JessicaGao | 2:55f39e7883a6 | 160 | pc_command_message[pc_command_message_byte] = tc; |
JessicaGao | 2:55f39e7883a6 | 161 | pc_command_message_byte ++; |
JessicaGao | 2:55f39e7883a6 | 162 | } |
JessicaGao | 2:55f39e7883a6 | 163 | } else { |
JessicaGao | 2:55f39e7883a6 | 164 | if(count == 1) { |
JessicaGao | 2:55f39e7883a6 | 165 | if(tc == COMMAND_MESSAGE_BYTE) { |
JessicaGao | 2:55f39e7883a6 | 166 | pc_command_message_started = 1; |
JessicaGao | 2:55f39e7883a6 | 167 | pc_command_message_byte = 0; |
JessicaGao | 2:55f39e7883a6 | 168 | |
JessicaGao | 2:55f39e7883a6 | 169 | } |
JessicaGao | 2:55f39e7883a6 | 170 | } |
JessicaGao | 2:55f39e7883a6 | 171 | } |
JessicaGao | 2:55f39e7883a6 | 172 | } |
JessicaGao | 2:55f39e7883a6 | 173 | } |