Bluetooth communication for flocking.
Fork of BeautifulMemeProject by
PsiSwarm/serial.cpp@20:e08376c0b4ea, 2016-01-15 (annotated)
- Committer:
- alanmillard
- Date:
- Fri Jan 15 14:42:42 2016 +0000
- Revision:
- 20:e08376c0b4ea
- Parent:
- 18:5921c1853e8a
- Child:
- 21:e5ab8c56a769
Bluetooth communication working reliably with two robots.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:8a5497a2e366 | 1 | /* University of York Robotics Laboratory PsiSwarm Library: Serial Control Source File |
jah128 | 0:8a5497a2e366 | 2 | * |
jah128 | 0:8a5497a2e366 | 3 | * File: serial.cpp |
jah128 | 0:8a5497a2e366 | 4 | * |
jah128 | 6:ff3c66f7372b | 5 | * (C) Dept. Electronics & Computer Science, University of York |
jah128 | 6:ff3c66f7372b | 6 | * James Hilder, Alan Millard, Homero Elizondo, Jon Timmis |
jah128 | 0:8a5497a2e366 | 7 | * |
jah128 | 6:ff3c66f7372b | 8 | * PsiSwarm Library Version: 0.3 |
jah128 | 0:8a5497a2e366 | 9 | * |
jah128 | 6:ff3c66f7372b | 10 | * October 2015 |
jah128 | 0:8a5497a2e366 | 11 | * |
jah128 | 0:8a5497a2e366 | 12 | */ |
jah128 | 0:8a5497a2e366 | 13 | |
jah128 | 0:8a5497a2e366 | 14 | #include "psiswarm.h" |
jah128 | 0:8a5497a2e366 | 15 | |
jah128 | 0:8a5497a2e366 | 16 | static float command_timeout_period = 0.1f; //If a complete command message is not received in 0.1s then consider it a user message |
jah128 | 0:8a5497a2e366 | 17 | char pc_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 18 | char pc_command_message_byte = 0; |
jah128 | 0:8a5497a2e366 | 19 | char pc_command_message[3]; |
jah128 | 0:8a5497a2e366 | 20 | char bt_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 21 | char bt_command_message_byte = 0; |
jah128 | 0:8a5497a2e366 | 22 | char bt_command_message[3]; |
jah128 | 0:8a5497a2e366 | 23 | |
jah128 | 0:8a5497a2e366 | 24 | char allow_commands = 1; |
jah128 | 0:8a5497a2e366 | 25 | char allow_requests = 1; |
jah128 | 0:8a5497a2e366 | 26 | |
jah128 | 0:8a5497a2e366 | 27 | Timeout pc_command_timeout; |
jah128 | 0:8a5497a2e366 | 28 | Timeout bt_command_timeout; |
jah128 | 0:8a5497a2e366 | 29 | |
jah128 | 0:8a5497a2e366 | 30 | // A predefined message structure for command messages is as follows: |
jah128 | 0:8a5497a2e366 | 31 | // [Byte 0][Byte 1][Byte 2][Byte 3][Byte 4] |
jah128 | 0:8a5497a2e366 | 32 | // Byte 0 and Byte 4 must be equal to COMMAND_MESSAGE_BYTE [in psiswarm.h] or message is treated as a user message |
jah128 | 0:8a5497a2e366 | 33 | |
jah128 | 0:8a5497a2e366 | 34 | void handle_user_serial_message(char * message, char length, char interface) |
jah128 | 0:8a5497a2e366 | 35 | { |
alanmillard | 18:5921c1853e8a | 36 | // This is where user code for handling a (non-system) serial message should go |
jah128 | 0:8a5497a2e366 | 37 | // |
alanmillard | 18:5921c1853e8a | 38 | // message = pointer to message char array |
alanmillard | 18:5921c1853e8a | 39 | // length = length of message |
alanmillard | 18:5921c1853e8a | 40 | // interface = 0 for PC serial connection, 1 for Bluetooth |
alanmillard | 17:da524989b637 | 41 | |
alanmillard | 18:5921c1853e8a | 42 | if(interface) |
alanmillard | 18:5921c1853e8a | 43 | { |
alanmillard | 18:5921c1853e8a | 44 | if(length == 8) |
alanmillard | 18:5921c1853e8a | 45 | { |
alanmillard | 18:5921c1853e8a | 46 | // Convert 360 to 255 values! |
alanmillard | 18:5921c1853e8a | 47 | for(int i = 0; i < length; i++) |
alanmillard | 18:5921c1853e8a | 48 | { |
alanmillard | 18:5921c1853e8a | 49 | flocking_headings[i] = message[i]; |
alanmillard | 20:e08376c0b4ea | 50 | // debug("%d, ", message[i]); |
alanmillard | 18:5921c1853e8a | 51 | } |
alanmillard | 18:5921c1853e8a | 52 | |
alanmillard | 20:e08376c0b4ea | 53 | // debug("\n"); |
alanmillard | 18:5921c1853e8a | 54 | } |
alanmillard | 18:5921c1853e8a | 55 | } |
jah128 | 0:8a5497a2e366 | 56 | } |
jah128 | 0:8a5497a2e366 | 57 | |
jah128 | 0:8a5497a2e366 | 58 | void IF_handle_user_serial_message(char * message, char length, char interface) |
jah128 | 0:8a5497a2e366 | 59 | { |
alanmillard | 18:5921c1853e8a | 60 | char buffer[255]; |
alanmillard | 18:5921c1853e8a | 61 | sprintf(buffer,message,length); |
alanmillard | 18:5921c1853e8a | 62 | for(int i=0; i<length; i++) { |
alanmillard | 18:5921c1853e8a | 63 | buffer[i]=message[i]; |
jah128 | 0:8a5497a2e366 | 64 | } |
alanmillard | 18:5921c1853e8a | 65 | buffer[length]=0; |
alanmillard | 20:e08376c0b4ea | 66 | // if(interface) debug("Received BT message:%s [%d chars]\n",buffer,length); |
alanmillard | 20:e08376c0b4ea | 67 | // else debug("Received USB message:%s [%d chars]\n",buffer,length); |
alanmillard | 18:5921c1853e8a | 68 | handle_user_serial_message(message,length,interface); |
jah128 | 0:8a5497a2e366 | 69 | } |
jah128 | 0:8a5497a2e366 | 70 | |
jah128 | 0:8a5497a2e366 | 71 | void IF_handle_command_serial_message(char message[3], char interface) |
jah128 | 0:8a5497a2e366 | 72 | { |
jah128 | 0:8a5497a2e366 | 73 | char iface [4]; |
jah128 | 0:8a5497a2e366 | 74 | if(interface) strcpy(iface,"BT"); |
jah128 | 0:8a5497a2e366 | 75 | else strcpy(iface,"USB"); |
jah128 | 0:8a5497a2e366 | 76 | char command [26]; |
jah128 | 0:8a5497a2e366 | 77 | char subcommand[30]; |
jah128 | 0:8a5497a2e366 | 78 | float dec; |
jah128 | 0:8a5497a2e366 | 79 | float l_dec; |
jah128 | 0:8a5497a2e366 | 80 | float r_dec; |
jah128 | 0:8a5497a2e366 | 81 | int irp_delay; |
jah128 | 0:8a5497a2e366 | 82 | char colour_string[7]; |
jah128 | 0:8a5497a2e366 | 83 | char ret_message[50]; |
jah128 | 0:8a5497a2e366 | 84 | char send_message = 0; |
jah128 | 0:8a5497a2e366 | 85 | char command_status = 0; |
jah128 | 0:8a5497a2e366 | 86 | // command_status values: |
jah128 | 0:8a5497a2e366 | 87 | // 0 - unrecognised command |
jah128 | 0:8a5497a2e366 | 88 | // 1 - command actioned |
jah128 | 0:8a5497a2e366 | 89 | // 2 - command blocked |
jah128 | 0:8a5497a2e366 | 90 | // 3 - invalid parameters |
jah128 | 0:8a5497a2e366 | 91 | |
jah128 | 0:8a5497a2e366 | 92 | subcommand[0]=0; |
jah128 | 0:8a5497a2e366 | 93 | command[0]=0; |
jah128 | 0:8a5497a2e366 | 94 | switch(message[0]) { |
jah128 | 0:8a5497a2e366 | 95 | |
jah128 | 0:8a5497a2e366 | 96 | // MOTOR COMMANDS |
jah128 | 0:8a5497a2e366 | 97 | |
jah128 | 0:8a5497a2e366 | 98 | case 1: |
jah128 | 0:8a5497a2e366 | 99 | strcpy(command,"SET LEFT MOTOR"); |
jah128 | 0:8a5497a2e366 | 100 | dec = IF_decode_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 101 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 102 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 103 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 104 | set_left_motor_speed(dec); |
jah128 | 0:8a5497a2e366 | 105 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 106 | break; |
jah128 | 0:8a5497a2e366 | 107 | case 2: |
jah128 | 0:8a5497a2e366 | 108 | strcpy(command,"SET RIGHT MOTOR"); |
jah128 | 0:8a5497a2e366 | 109 | dec = IF_decode_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 110 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 111 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 112 | set_right_motor_speed(dec); |
jah128 | 0:8a5497a2e366 | 113 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 114 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 115 | break; |
jah128 | 0:8a5497a2e366 | 116 | case 3: |
jah128 | 0:8a5497a2e366 | 117 | strcpy(command,"SET BOTH MOTORS"); |
jah128 | 0:8a5497a2e366 | 118 | dec = IF_decode_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 119 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 120 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 121 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 122 | forward(dec); |
jah128 | 0:8a5497a2e366 | 123 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 124 | break; |
jah128 | 0:8a5497a2e366 | 125 | case 4: |
jah128 | 0:8a5497a2e366 | 126 | strcpy(command,"BRAKE LEFT MOTOR"); |
jah128 | 0:8a5497a2e366 | 127 | sprintf(subcommand,""); |
jah128 | 0:8a5497a2e366 | 128 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 129 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 130 | brake_left_motor(); |
jah128 | 0:8a5497a2e366 | 131 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 132 | break; |
jah128 | 0:8a5497a2e366 | 133 | case 5: |
jah128 | 0:8a5497a2e366 | 134 | strcpy(command,"BRAKE RIGHT MOTOR"); |
jah128 | 0:8a5497a2e366 | 135 | sprintf(subcommand,""); |
jah128 | 0:8a5497a2e366 | 136 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 137 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 138 | brake_right_motor(); |
jah128 | 0:8a5497a2e366 | 139 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 140 | break; |
jah128 | 0:8a5497a2e366 | 141 | case 6: |
jah128 | 0:8a5497a2e366 | 142 | strcpy(command,"BRAKE BOTH MOTORS"); |
jah128 | 0:8a5497a2e366 | 143 | sprintf(subcommand,""); |
jah128 | 0:8a5497a2e366 | 144 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 145 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 146 | brake(); |
jah128 | 0:8a5497a2e366 | 147 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 148 | break; |
jah128 | 0:8a5497a2e366 | 149 | case 7: |
jah128 | 0:8a5497a2e366 | 150 | strcpy(command,"STOP BOTH MOTORS"); |
jah128 | 0:8a5497a2e366 | 151 | sprintf(subcommand,""); |
jah128 | 0:8a5497a2e366 | 152 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 153 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 154 | stop(); |
jah128 | 0:8a5497a2e366 | 155 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 156 | break; |
jah128 | 0:8a5497a2e366 | 157 | case 8: |
jah128 | 0:8a5497a2e366 | 158 | strcpy(command,"TURN ON SPOT"); |
jah128 | 0:8a5497a2e366 | 159 | dec = IF_decode_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 160 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 161 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 162 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 163 | turn(dec); |
jah128 | 0:8a5497a2e366 | 164 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 165 | break; |
jah128 | 0:8a5497a2e366 | 166 | case 9: |
jah128 | 0:8a5497a2e366 | 167 | strcpy(command,"SET EACH MOTOR"); |
jah128 | 0:8a5497a2e366 | 168 | l_dec = IF_decode_float(message[1]); |
jah128 | 0:8a5497a2e366 | 169 | r_dec = IF_decode_float(message[2]); |
jah128 | 0:8a5497a2e366 | 170 | sprintf(subcommand,"L=%1.3f R=%1.3f",l_dec,r_dec); |
jah128 | 0:8a5497a2e366 | 171 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 172 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 173 | |
jah128 | 0:8a5497a2e366 | 174 | set_left_motor_speed(l_dec); |
jah128 | 0:8a5497a2e366 | 175 | set_right_motor_speed(r_dec); |
jah128 | 0:8a5497a2e366 | 176 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 177 | break; |
jah128 | 0:8a5497a2e366 | 178 | // LED COMMANDS |
jah128 | 0:8a5497a2e366 | 179 | |
jah128 | 0:8a5497a2e366 | 180 | case 10: |
jah128 | 0:8a5497a2e366 | 181 | strcpy(command,"SET LED STATES"); |
jah128 | 0:8a5497a2e366 | 182 | sprintf(subcommand,"G:%s R:%s",IF_char_to_binary_char(message[1]), IF_char_to_binary_char(message[2])); |
jah128 | 0:8a5497a2e366 | 183 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 184 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 185 | set_leds(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 186 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 187 | break; |
jah128 | 0:8a5497a2e366 | 188 | case 11: |
jah128 | 0:8a5497a2e366 | 189 | strcpy(command,"SET RED LED STATES"); |
jah128 | 0:8a5497a2e366 | 190 | sprintf(subcommand,"%s",IF_char_to_binary_char(message[1])); |
jah128 | 0:8a5497a2e366 | 191 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 192 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 193 | set_red_leds(message[1]); |
jah128 | 0:8a5497a2e366 | 194 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 195 | break; |
jah128 | 0:8a5497a2e366 | 196 | case 12: |
jah128 | 0:8a5497a2e366 | 197 | strcpy(command,"SET GREEN LED STATES"); |
jah128 | 0:8a5497a2e366 | 198 | sprintf(subcommand,"%s",IF_char_to_binary_char(message[1])); |
jah128 | 0:8a5497a2e366 | 199 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 200 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 201 | set_green_leds(message[1]); |
jah128 | 0:8a5497a2e366 | 202 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 203 | break; |
jah128 | 0:8a5497a2e366 | 204 | case 13: |
jah128 | 0:8a5497a2e366 | 205 | strcpy(command,"SET LED"); |
jah128 | 0:8a5497a2e366 | 206 | switch(message[2]) { |
jah128 | 0:8a5497a2e366 | 207 | case 1: |
jah128 | 0:8a5497a2e366 | 208 | strcpy(colour_string,"RED"); |
jah128 | 0:8a5497a2e366 | 209 | break; |
jah128 | 0:8a5497a2e366 | 210 | case 2: |
jah128 | 0:8a5497a2e366 | 211 | strcpy(colour_string,"GREEN"); |
jah128 | 0:8a5497a2e366 | 212 | break; |
jah128 | 0:8a5497a2e366 | 213 | case 3: |
jah128 | 0:8a5497a2e366 | 214 | strcpy(colour_string,"BOTH"); |
jah128 | 0:8a5497a2e366 | 215 | break; |
jah128 | 0:8a5497a2e366 | 216 | case 0: |
jah128 | 0:8a5497a2e366 | 217 | strcpy(colour_string,"OFF"); |
jah128 | 0:8a5497a2e366 | 218 | break; |
jah128 | 0:8a5497a2e366 | 219 | } |
jah128 | 0:8a5497a2e366 | 220 | if(message[1] < 8 && message[2] < 4) { |
jah128 | 0:8a5497a2e366 | 221 | sprintf(subcommand,"%d %s",message[1],colour_string); |
jah128 | 0:8a5497a2e366 | 222 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 223 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 224 | set_led(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 225 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 226 | } else { |
jah128 | 0:8a5497a2e366 | 227 | sprintf(subcommand,"[INVALID CODE]"); |
jah128 | 0:8a5497a2e366 | 228 | command_status = 3; |
jah128 | 0:8a5497a2e366 | 229 | } |
jah128 | 0:8a5497a2e366 | 230 | break; |
jah128 | 0:8a5497a2e366 | 231 | case 14: |
jah128 | 0:8a5497a2e366 | 232 | strcpy(command,"SET CENTER LED STATE"); |
jah128 | 0:8a5497a2e366 | 233 | switch(message[1]) { |
jah128 | 0:8a5497a2e366 | 234 | case 1: |
jah128 | 0:8a5497a2e366 | 235 | strcpy(colour_string,"RED"); |
jah128 | 0:8a5497a2e366 | 236 | break; |
jah128 | 0:8a5497a2e366 | 237 | case 2: |
jah128 | 0:8a5497a2e366 | 238 | strcpy(colour_string,"GREEN"); |
jah128 | 0:8a5497a2e366 | 239 | break; |
jah128 | 0:8a5497a2e366 | 240 | case 3: |
jah128 | 0:8a5497a2e366 | 241 | strcpy(colour_string,"BOTH"); |
jah128 | 0:8a5497a2e366 | 242 | break; |
jah128 | 0:8a5497a2e366 | 243 | case 0: |
jah128 | 0:8a5497a2e366 | 244 | strcpy(colour_string,"OFF"); |
jah128 | 0:8a5497a2e366 | 245 | break; |
jah128 | 0:8a5497a2e366 | 246 | } |
jah128 | 0:8a5497a2e366 | 247 | if(message[1] < 4) { |
jah128 | 0:8a5497a2e366 | 248 | sprintf(subcommand,"%s",colour_string); |
jah128 | 0:8a5497a2e366 | 249 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 250 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 251 | set_center_led(message[1]); |
jah128 | 0:8a5497a2e366 | 252 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 253 | } else { |
jah128 | 0:8a5497a2e366 | 254 | sprintf(subcommand,"[INVALID CODE]"); |
jah128 | 0:8a5497a2e366 | 255 | command_status = 3; |
jah128 | 0:8a5497a2e366 | 256 | } |
jah128 | 0:8a5497a2e366 | 257 | break; |
jah128 | 0:8a5497a2e366 | 258 | case 15: |
jah128 | 0:8a5497a2e366 | 259 | strcpy(command,"SET C.LED BRIGHTNESS"); |
jah128 | 0:8a5497a2e366 | 260 | dec = IF_decode_unsigned_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 261 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 262 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 263 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 264 | set_center_led_brightness(dec); |
jah128 | 0:8a5497a2e366 | 265 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 266 | break; |
jah128 | 0:8a5497a2e366 | 267 | case 16: |
jah128 | 0:8a5497a2e366 | 268 | strcpy(command,"SET MBED LEDS"); |
jah128 | 0:8a5497a2e366 | 269 | sprintf(subcommand,"%s",IF_nibble_to_binary_char(message[1])); |
jah128 | 0:8a5497a2e366 | 270 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 271 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 272 | mbed_led1 = (message[1] & 128) >> 7; |
jah128 | 0:8a5497a2e366 | 273 | mbed_led2 = (message[1] & 64) >> 6; |
jah128 | 0:8a5497a2e366 | 274 | mbed_led3 = (message[1] & 32) >> 5; |
jah128 | 0:8a5497a2e366 | 275 | mbed_led4 = (message[1] & 16) >> 4; |
jah128 | 0:8a5497a2e366 | 276 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 277 | break; |
jah128 | 0:8a5497a2e366 | 278 | case 17: |
jah128 | 0:8a5497a2e366 | 279 | strcpy(command,"BLINK OUTER LEDS"); |
jah128 | 0:8a5497a2e366 | 280 | dec = IF_decode_unsigned_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 281 | sprintf(subcommand,"FOR %1.5fS",dec); |
jah128 | 0:8a5497a2e366 | 282 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 283 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 284 | blink_leds(dec); |
jah128 | 0:8a5497a2e366 | 285 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 286 | break; |
jah128 | 0:8a5497a2e366 | 287 | case 18: |
jah128 | 0:8a5497a2e366 | 288 | strcpy(command,"SET BASE LED STATE"); |
jah128 | 0:8a5497a2e366 | 289 | switch(message[1]) { |
jah128 | 0:8a5497a2e366 | 290 | case 1: |
jah128 | 0:8a5497a2e366 | 291 | strcpy(subcommand,"ON"); |
jah128 | 0:8a5497a2e366 | 292 | break; |
jah128 | 0:8a5497a2e366 | 293 | case 0: |
jah128 | 0:8a5497a2e366 | 294 | strcpy(subcommand,"OFF"); |
jah128 | 0:8a5497a2e366 | 295 | break; |
jah128 | 0:8a5497a2e366 | 296 | } |
jah128 | 0:8a5497a2e366 | 297 | //Function not yet implemented |
jah128 | 0:8a5497a2e366 | 298 | break; |
jah128 | 0:8a5497a2e366 | 299 | case 19: |
jah128 | 0:8a5497a2e366 | 300 | strcpy(command,"SET CENTER LED "); |
jah128 | 0:8a5497a2e366 | 301 | switch(message[1]) { |
jah128 | 0:8a5497a2e366 | 302 | case 1: |
jah128 | 0:8a5497a2e366 | 303 | strcpy(colour_string,"RED"); |
jah128 | 0:8a5497a2e366 | 304 | break; |
jah128 | 0:8a5497a2e366 | 305 | case 2: |
jah128 | 0:8a5497a2e366 | 306 | strcpy(colour_string,"GREEN"); |
jah128 | 0:8a5497a2e366 | 307 | break; |
jah128 | 0:8a5497a2e366 | 308 | case 3: |
jah128 | 0:8a5497a2e366 | 309 | strcpy(colour_string,"BOTH"); |
jah128 | 0:8a5497a2e366 | 310 | break; |
jah128 | 0:8a5497a2e366 | 311 | case 0: |
jah128 | 0:8a5497a2e366 | 312 | strcpy(colour_string,"OFF"); |
jah128 | 0:8a5497a2e366 | 313 | break; |
jah128 | 0:8a5497a2e366 | 314 | } |
jah128 | 0:8a5497a2e366 | 315 | dec = IF_decode_unsigned_float(message[2]); |
jah128 | 0:8a5497a2e366 | 316 | sprintf(subcommand,"%s @ %1.5f brightness",colour_string,dec); |
jah128 | 0:8a5497a2e366 | 317 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 318 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 319 | set_center_led(message[1],dec); |
jah128 | 0:8a5497a2e366 | 320 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 321 | break; |
jah128 | 0:8a5497a2e366 | 322 | |
jah128 | 0:8a5497a2e366 | 323 | // DISPLAY COMMANDS |
jah128 | 0:8a5497a2e366 | 324 | |
jah128 | 0:8a5497a2e366 | 325 | case 20: |
jah128 | 0:8a5497a2e366 | 326 | strcpy(command,"SET DISPLAY "); |
jah128 | 0:8a5497a2e366 | 327 | switch(message[1]) { |
jah128 | 0:8a5497a2e366 | 328 | case 0: |
jah128 | 0:8a5497a2e366 | 329 | strcpy(subcommand,"CLEAR"); |
jah128 | 0:8a5497a2e366 | 330 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 331 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 332 | display.clear_display(); |
jah128 | 0:8a5497a2e366 | 333 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 334 | break; |
jah128 | 0:8a5497a2e366 | 335 | case 1: |
jah128 | 0:8a5497a2e366 | 336 | strcpy(subcommand,"MESSAGE 1"); |
jah128 | 0:8a5497a2e366 | 337 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 338 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 339 | display.clear_display(); |
jah128 | 0:8a5497a2e366 | 340 | display.home(); |
jah128 | 0:8a5497a2e366 | 341 | display.write_string("PC CONNECTION"); |
jah128 | 0:8a5497a2e366 | 342 | display.set_position(1,0); |
jah128 | 0:8a5497a2e366 | 343 | display.write_string("STARTED"); |
jah128 | 0:8a5497a2e366 | 344 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 345 | break; |
jah128 | 0:8a5497a2e366 | 346 | case 2: |
jah128 | 0:8a5497a2e366 | 347 | strcpy(subcommand,"MESSAGE 2"); |
jah128 | 0:8a5497a2e366 | 348 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 349 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 350 | display.clear_display(); |
jah128 | 0:8a5497a2e366 | 351 | display.home(); |
jah128 | 0:8a5497a2e366 | 352 | display.write_string("PC CONNECTION"); |
jah128 | 0:8a5497a2e366 | 353 | display.set_position(1,0); |
jah128 | 0:8a5497a2e366 | 354 | display.write_string("TERMINATED"); |
jah128 | 0:8a5497a2e366 | 355 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 356 | break; |
jah128 | 0:8a5497a2e366 | 357 | case 3: |
jah128 | 0:8a5497a2e366 | 358 | strcpy(subcommand,"MESSAGE 3"); |
jah128 | 0:8a5497a2e366 | 359 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 360 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 361 | display.clear_display(); |
jah128 | 0:8a5497a2e366 | 362 | display.home(); |
jah128 | 0:8a5497a2e366 | 363 | display.write_string("ANDROID DEVICE"); |
jah128 | 0:8a5497a2e366 | 364 | display.set_position(1,0); |
jah128 | 0:8a5497a2e366 | 365 | display.write_string("CONNECTED"); |
jah128 | 0:8a5497a2e366 | 366 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 367 | break; |
jah128 | 0:8a5497a2e366 | 368 | case 4: |
jah128 | 0:8a5497a2e366 | 369 | strcpy(subcommand,"MESSAGE 4"); |
jah128 | 0:8a5497a2e366 | 370 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 371 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 372 | display.clear_display(); |
jah128 | 0:8a5497a2e366 | 373 | display.home(); |
jah128 | 0:8a5497a2e366 | 374 | display.write_string("ANDROID DEVICE"); |
jah128 | 0:8a5497a2e366 | 375 | display.set_position(1,0); |
jah128 | 0:8a5497a2e366 | 376 | display.write_string("DISCONNECTED"); |
jah128 | 0:8a5497a2e366 | 377 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 378 | break; |
jah128 | 0:8a5497a2e366 | 379 | } |
jah128 | 0:8a5497a2e366 | 380 | break; |
jah128 | 0:8a5497a2e366 | 381 | case 21: |
jah128 | 0:8a5497a2e366 | 382 | strcpy(command,"SET CURSOR "); |
jah128 | 0:8a5497a2e366 | 383 | if(message[1] < 2 && message[2] < 16) { |
jah128 | 0:8a5497a2e366 | 384 | sprintf(subcommand,"[%d,%d]",message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 385 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 386 | display.set_position(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 387 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 388 | } else { |
jah128 | 0:8a5497a2e366 | 389 | sprintf(subcommand,"[INVALID]"); |
jah128 | 0:8a5497a2e366 | 390 | command_status = 3; |
jah128 | 0:8a5497a2e366 | 391 | } |
jah128 | 0:8a5497a2e366 | 392 | break; |
jah128 | 0:8a5497a2e366 | 393 | case 22: |
jah128 | 0:8a5497a2e366 | 394 | strcpy(command,"PRINT CHARACTERS "); |
jah128 | 0:8a5497a2e366 | 395 | char print_message[2]; |
jah128 | 0:8a5497a2e366 | 396 | print_message[0]=message[1]; |
jah128 | 0:8a5497a2e366 | 397 | print_message[1]=message[2]; |
jah128 | 0:8a5497a2e366 | 398 | sprintf(subcommand,"[%c,%c]",message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 399 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 400 | display.write_string(print_message,2); |
jah128 | 0:8a5497a2e366 | 401 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 402 | break; |
jah128 | 0:8a5497a2e366 | 403 | case 23: |
jah128 | 0:8a5497a2e366 | 404 | strcpy(command,"SET DISPLAY B.NESS"); |
jah128 | 0:8a5497a2e366 | 405 | dec = IF_decode_unsigned_float(message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 406 | sprintf(subcommand,"%1.5f",dec); |
jah128 | 0:8a5497a2e366 | 407 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 408 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 409 | display.set_backlight_brightness(dec); |
jah128 | 0:8a5497a2e366 | 410 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 411 | break; |
jah128 | 0:8a5497a2e366 | 412 | |
jah128 | 0:8a5497a2e366 | 413 | case 30: |
jah128 | 0:8a5497a2e366 | 414 | strcpy(command,"SET DEBUG MODE"); |
jah128 | 0:8a5497a2e366 | 415 | switch(message[1]) { |
jah128 | 0:8a5497a2e366 | 416 | case 1: |
jah128 | 0:8a5497a2e366 | 417 | strcpy(subcommand,"ON"); |
jah128 | 0:8a5497a2e366 | 418 | break; |
jah128 | 0:8a5497a2e366 | 419 | case 0: |
jah128 | 0:8a5497a2e366 | 420 | strcpy(subcommand,"OFF"); |
jah128 | 0:8a5497a2e366 | 421 | break; |
jah128 | 0:8a5497a2e366 | 422 | } |
jah128 | 0:8a5497a2e366 | 423 | if(message[2] & 1) strcat (subcommand,"-PC"); |
jah128 | 0:8a5497a2e366 | 424 | if(message[2] & 2) strcat (subcommand,"-BT"); |
jah128 | 0:8a5497a2e366 | 425 | if(message[2] & 4) strcat (subcommand,"-DISP"); |
jah128 | 0:8a5497a2e366 | 426 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 427 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 428 | debug_mode = message[1]; |
jah128 | 0:8a5497a2e366 | 429 | debug_output = message[2]; |
jah128 | 0:8a5497a2e366 | 430 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 431 | break; |
jah128 | 0:8a5497a2e366 | 432 | case 31: |
jah128 | 0:8a5497a2e366 | 433 | strcpy(command,"SET DEMO MODE"); |
jah128 | 0:8a5497a2e366 | 434 | switch(message[1] % 2) { |
jah128 | 0:8a5497a2e366 | 435 | case 1: |
jah128 | 0:8a5497a2e366 | 436 | strcpy(subcommand,"ON"); |
jah128 | 0:8a5497a2e366 | 437 | break; |
jah128 | 0:8a5497a2e366 | 438 | case 0: |
jah128 | 0:8a5497a2e366 | 439 | strcpy(subcommand,"OFF"); |
jah128 | 0:8a5497a2e366 | 440 | break; |
jah128 | 0:8a5497a2e366 | 441 | } |
jah128 | 0:8a5497a2e366 | 442 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 443 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 444 | demo_on = message[1] % 2; |
jah128 | 0:8a5497a2e366 | 445 | if(demo_on == 1) { |
jah128 | 0:8a5497a2e366 | 446 | user_code_restore_mode = user_code_running; |
jah128 | 0:8a5497a2e366 | 447 | user_code_running = 0; |
jah128 | 0:8a5497a2e366 | 448 | } else { |
jah128 | 0:8a5497a2e366 | 449 | user_code_running = user_code_restore_mode; |
jah128 | 0:8a5497a2e366 | 450 | } |
jah128 | 0:8a5497a2e366 | 451 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 452 | break; |
jah128 | 0:8a5497a2e366 | 453 | case 32: |
jah128 | 0:8a5497a2e366 | 454 | strcpy(command,"SET USER CODE"); |
jah128 | 0:8a5497a2e366 | 455 | switch(message[1] % 2) { |
jah128 | 0:8a5497a2e366 | 456 | case 1: |
jah128 | 0:8a5497a2e366 | 457 | strcpy(subcommand,"ON"); |
jah128 | 0:8a5497a2e366 | 458 | break; |
jah128 | 0:8a5497a2e366 | 459 | case 0: |
jah128 | 0:8a5497a2e366 | 460 | strcpy(subcommand,"OFF"); |
jah128 | 0:8a5497a2e366 | 461 | break; |
jah128 | 0:8a5497a2e366 | 462 | } |
jah128 | 0:8a5497a2e366 | 463 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 464 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 465 | user_code_running = message[1] % 2; |
jah128 | 0:8a5497a2e366 | 466 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 467 | break; |
jah128 | 0:8a5497a2e366 | 468 | case 33: |
jah128 | 0:8a5497a2e366 | 469 | strcpy(command,"PAUSE USER CODE"); |
jah128 | 0:8a5497a2e366 | 470 | dec = IF_decode_unsigned_float(message[1],message[2]) * 10; |
jah128 | 0:8a5497a2e366 | 471 | sprintf(subcommand,"FOR %2.3fS",dec); |
jah128 | 0:8a5497a2e366 | 472 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 473 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 474 | pause_user_code(dec); |
jah128 | 0:8a5497a2e366 | 475 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 476 | break; |
jah128 | 0:8a5497a2e366 | 477 | |
jah128 | 0:8a5497a2e366 | 478 | case 34: |
jah128 | 0:8a5497a2e366 | 479 | strcpy(command,"RESET ENCODERS"); |
jah128 | 0:8a5497a2e366 | 480 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 481 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 482 | reset_encoders(); |
jah128 | 0:8a5497a2e366 | 483 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 484 | break; |
jah128 | 0:8a5497a2e366 | 485 | |
jah128 | 0:8a5497a2e366 | 486 | case 35: |
jah128 | 0:8a5497a2e366 | 487 | strcpy(command,"SET ALLOW COMMANDS"); |
jah128 | 0:8a5497a2e366 | 488 | switch(message[1] % 2) { |
jah128 | 0:8a5497a2e366 | 489 | case 1: |
jah128 | 0:8a5497a2e366 | 490 | strcpy(subcommand,"ON"); |
jah128 | 0:8a5497a2e366 | 491 | break; |
jah128 | 0:8a5497a2e366 | 492 | case 0: |
jah128 | 0:8a5497a2e366 | 493 | strcpy(subcommand,"OFF"); |
jah128 | 0:8a5497a2e366 | 494 | break; |
jah128 | 0:8a5497a2e366 | 495 | } |
jah128 | 0:8a5497a2e366 | 496 | allow_commands = message[1] % 2; |
jah128 | 0:8a5497a2e366 | 497 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 498 | break; |
jah128 | 0:8a5497a2e366 | 499 | |
jah128 | 0:8a5497a2e366 | 500 | case 36: |
jah128 | 0:8a5497a2e366 | 501 | irp_delay = (message[1] << 8) + message[2]; |
jah128 | 0:8a5497a2e366 | 502 | sprintf(command,"SET IR PULSE DELAY %d MS",irp_delay); |
jah128 | 0:8a5497a2e366 | 503 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 504 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 505 | ir_pulse_delay = irp_delay; |
jah128 | 0:8a5497a2e366 | 506 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 507 | break; |
jah128 | 0:8a5497a2e366 | 508 | case 37: |
jah128 | 0:8a5497a2e366 | 509 | irp_delay = (message[1] << 8) + message[2]; |
jah128 | 0:8a5497a2e366 | 510 | sprintf(command,"SET BASE IR PULSE DELAY %d MS",irp_delay); |
jah128 | 0:8a5497a2e366 | 511 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 512 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 513 | base_ir_pulse_delay = irp_delay; |
jah128 | 0:8a5497a2e366 | 514 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 515 | break; |
jah128 | 0:8a5497a2e366 | 516 | |
jah128 | 0:8a5497a2e366 | 517 | // MOTOR REQUESTS |
jah128 | 0:8a5497a2e366 | 518 | case 40: |
jah128 | 0:8a5497a2e366 | 519 | strcpy(command,"GET LEFT MOTOR SPEED"); |
jah128 | 0:8a5497a2e366 | 520 | sprintf(ret_message,"%1.5f",motor_left_speed); |
jah128 | 0:8a5497a2e366 | 521 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 522 | break; |
jah128 | 0:8a5497a2e366 | 523 | |
jah128 | 0:8a5497a2e366 | 524 | case 41: |
jah128 | 0:8a5497a2e366 | 525 | strcpy(command,"GET RIGHT MOTOR SPEED"); |
jah128 | 0:8a5497a2e366 | 526 | sprintf(ret_message,"%1.5f",motor_right_speed); |
jah128 | 0:8a5497a2e366 | 527 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 528 | break; |
jah128 | 0:8a5497a2e366 | 529 | case 42: |
jah128 | 0:8a5497a2e366 | 530 | strcpy(command,"GET BRAKE STATES"); |
jah128 | 0:8a5497a2e366 | 531 | sprintf(ret_message,"%d,%d",motor_left_brake,motor_right_brake); |
jah128 | 0:8a5497a2e366 | 532 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 533 | break; |
jah128 | 0:8a5497a2e366 | 534 | case 43: |
jah128 | 0:8a5497a2e366 | 535 | strcpy(command,"GET MOTOR STATES"); |
jah128 | 0:8a5497a2e366 | 536 | //sprintf(ret_message,"%d,%d",motor_left_brake,motor_right_brake); |
jah128 | 0:8a5497a2e366 | 537 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 538 | break; |
jah128 | 0:8a5497a2e366 | 539 | case 44: |
jah128 | 0:8a5497a2e366 | 540 | strcpy(command,"GET ENCODERS"); |
jah128 | 0:8a5497a2e366 | 541 | sprintf(ret_message,"%d,%d",left_encoder,right_encoder); |
jah128 | 0:8a5497a2e366 | 542 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 543 | break; |
jah128 | 0:8a5497a2e366 | 544 | |
jah128 | 0:8a5497a2e366 | 545 | // LED REQUESTS |
jah128 | 0:8a5497a2e366 | 546 | case 50: |
jah128 | 0:8a5497a2e366 | 547 | strcpy(command,"GET LED STATES"); |
jah128 | 0:8a5497a2e366 | 548 | sprintf(ret_message,"%04x",get_led_states()); |
jah128 | 0:8a5497a2e366 | 549 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 550 | break; |
jah128 | 0:8a5497a2e366 | 551 | |
jah128 | 0:8a5497a2e366 | 552 | // GENERAL REQUESTS |
jah128 | 0:8a5497a2e366 | 553 | case 60: |
jah128 | 0:8a5497a2e366 | 554 | strcpy(command,"GET SOFTWARE VERSION"); |
jah128 | 0:8a5497a2e366 | 555 | sprintf(ret_message,"%1.2f",SOFTWARE_VERSION_CODE); |
jah128 | 0:8a5497a2e366 | 556 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 557 | break; |
jah128 | 0:8a5497a2e366 | 558 | |
jah128 | 0:8a5497a2e366 | 559 | case 61: |
jah128 | 0:8a5497a2e366 | 560 | strcpy(command,"GET UPTIME"); |
jah128 | 0:8a5497a2e366 | 561 | sprintf(ret_message,"%6.2f",get_uptime()); |
jah128 | 0:8a5497a2e366 | 562 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 563 | break; |
jah128 | 0:8a5497a2e366 | 564 | |
jah128 | 0:8a5497a2e366 | 565 | case 62: |
jah128 | 0:8a5497a2e366 | 566 | strcpy(command,"GET ID"); |
jah128 | 0:8a5497a2e366 | 567 | sprintf(ret_message,"%d",robot_id); |
jah128 | 0:8a5497a2e366 | 568 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 569 | break; |
jah128 | 0:8a5497a2e366 | 570 | |
jah128 | 0:8a5497a2e366 | 571 | case 63: |
jah128 | 0:8a5497a2e366 | 572 | strcpy(command,"GET SWITCH BYTE"); |
jah128 | 0:8a5497a2e366 | 573 | sprintf(ret_message,"%02x",switch_byte); |
jah128 | 0:8a5497a2e366 | 574 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 575 | break; |
jah128 | 0:8a5497a2e366 | 576 | case 64: |
jah128 | 0:8a5497a2e366 | 577 | strcpy(command,"GET USER CODE"); |
jah128 | 0:8a5497a2e366 | 578 | sprintf(ret_message,"%d",user_code_running); |
jah128 | 0:8a5497a2e366 | 579 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 580 | break; |
jah128 | 0:8a5497a2e366 | 581 | case 65: |
jah128 | 0:8a5497a2e366 | 582 | strcpy(command,"GET RESPONSE STRING"); |
jah128 | 0:8a5497a2e366 | 583 | sprintf(ret_message,"PSI"); |
jah128 | 0:8a5497a2e366 | 584 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 585 | break; |
jah128 | 0:8a5497a2e366 | 586 | case 66: |
jah128 | 0:8a5497a2e366 | 587 | strcpy(command,"GET PROGRAM NAME"); |
jah128 | 0:8a5497a2e366 | 588 | sprintf(ret_message,"%s",program_name); |
jah128 | 0:8a5497a2e366 | 589 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 590 | break; |
jah128 | 0:8a5497a2e366 | 591 | case 67: |
jah128 | 0:8a5497a2e366 | 592 | strcpy(command,"GET AUTHOR NAME"); |
jah128 | 0:8a5497a2e366 | 593 | sprintf(ret_message,"%s",author_name); |
jah128 | 0:8a5497a2e366 | 594 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 595 | break; |
jah128 | 0:8a5497a2e366 | 596 | case 68: |
jah128 | 0:8a5497a2e366 | 597 | strcpy(command,"GET DEBUG MODE"); |
jah128 | 0:8a5497a2e366 | 598 | sprintf(ret_message,"%1d%1d",debug_mode,debug_output); |
jah128 | 0:8a5497a2e366 | 599 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 600 | break; |
jah128 | 0:8a5497a2e366 | 601 | case 69: |
jah128 | 0:8a5497a2e366 | 602 | strcpy(command,"GET SYSTEM WARNINGS"); |
jah128 | 0:8a5497a2e366 | 603 | sprintf(ret_message,"%d",system_warnings); |
jah128 | 0:8a5497a2e366 | 604 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 605 | break; |
jah128 | 0:8a5497a2e366 | 606 | |
jah128 | 0:8a5497a2e366 | 607 | |
jah128 | 0:8a5497a2e366 | 608 | // Sensors |
jah128 | 0:8a5497a2e366 | 609 | case 80: |
jah128 | 0:8a5497a2e366 | 610 | strcpy(command,"STORE BG. IR VALUES"); |
jah128 | 0:8a5497a2e366 | 611 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 612 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 613 | store_background_raw_ir_values(); |
jah128 | 0:8a5497a2e366 | 614 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 615 | break; |
jah128 | 0:8a5497a2e366 | 616 | case 81: |
jah128 | 0:8a5497a2e366 | 617 | strcpy(command,"STORE IL. IR VALUES"); |
jah128 | 0:8a5497a2e366 | 618 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 619 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 620 | store_illuminated_raw_ir_values(); |
jah128 | 0:8a5497a2e366 | 621 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 622 | break; |
jah128 | 0:8a5497a2e366 | 623 | case 82: |
jah128 | 0:8a5497a2e366 | 624 | strcpy(command,"STORE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 625 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 626 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 627 | store_ir_values(); |
jah128 | 0:8a5497a2e366 | 628 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 629 | break; |
jah128 | 0:8a5497a2e366 | 630 | case 83: |
jah128 | 0:8a5497a2e366 | 631 | strcpy(command,"STORE BG BASE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 632 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 633 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 634 | store_background_base_ir_values(); |
jah128 | 0:8a5497a2e366 | 635 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 636 | break; |
jah128 | 0:8a5497a2e366 | 637 | case 84: |
jah128 | 0:8a5497a2e366 | 638 | strcpy(command,"STORE IL. BASE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 639 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 640 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 641 | store_illuminated_base_ir_values(); |
jah128 | 0:8a5497a2e366 | 642 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 643 | break; |
jah128 | 0:8a5497a2e366 | 644 | case 85: |
jah128 | 0:8a5497a2e366 | 645 | strcpy(command,"STORE BASE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 646 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 647 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 648 | store_base_ir_values(); |
jah128 | 0:8a5497a2e366 | 649 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 650 | break; |
jah128 | 0:8a5497a2e366 | 651 | case 86: |
jah128 | 0:8a5497a2e366 | 652 | strcpy(command,"STORE ALL IR VALUES"); |
jah128 | 0:8a5497a2e366 | 653 | if(allow_commands) { |
jah128 | 0:8a5497a2e366 | 654 | command_status = 1; |
jah128 | 0:8a5497a2e366 | 655 | store_ir_values(); |
jah128 | 0:8a5497a2e366 | 656 | store_base_ir_values(); |
jah128 | 0:8a5497a2e366 | 657 | } else command_status = 2; |
jah128 | 0:8a5497a2e366 | 658 | break; |
jah128 | 0:8a5497a2e366 | 659 | case 90: |
jah128 | 0:8a5497a2e366 | 660 | sprintf(command,"%s %d","GET BG IR VALUE",message[1]); |
jah128 | 0:8a5497a2e366 | 661 | sprintf(ret_message,"%d",get_background_raw_ir_value(message[1])); |
jah128 | 0:8a5497a2e366 | 662 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 663 | break; |
jah128 | 0:8a5497a2e366 | 664 | case 91: |
jah128 | 0:8a5497a2e366 | 665 | sprintf(command,"%s %d","GET IL IR VALUE",message[1]); |
jah128 | 0:8a5497a2e366 | 666 | sprintf(ret_message,"%d",get_illuminated_raw_ir_value(message[1])); |
jah128 | 0:8a5497a2e366 | 667 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 668 | break; |
jah128 | 0:8a5497a2e366 | 669 | case 92: |
jah128 | 0:8a5497a2e366 | 670 | strcpy(command,"GET BG IR VALUES"); |
jah128 | 0:8a5497a2e366 | 671 | sprintf(ret_message,"%03X%03X%03X%03X%03X%03X%03X%03X",get_background_raw_ir_value(0),get_background_raw_ir_value(1),get_background_raw_ir_value(2),get_background_raw_ir_value(3),get_background_raw_ir_value(4),get_background_raw_ir_value(5),get_background_raw_ir_value(6),get_background_raw_ir_value(7)); |
jah128 | 0:8a5497a2e366 | 672 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 673 | break; |
jah128 | 0:8a5497a2e366 | 674 | case 93: |
jah128 | 0:8a5497a2e366 | 675 | strcpy(command,"GET ILLUMINATED IR VALUES"); |
jah128 | 0:8a5497a2e366 | 676 | sprintf(ret_message,"%03X%03X%03X%03X%03X%03X%03X%03X",get_illuminated_raw_ir_value(0),get_illuminated_raw_ir_value(1),get_illuminated_raw_ir_value(2),get_illuminated_raw_ir_value(3),get_illuminated_raw_ir_value(4),get_illuminated_raw_ir_value(5),get_illuminated_raw_ir_value(6),get_illuminated_raw_ir_value(7)); |
jah128 | 0:8a5497a2e366 | 677 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 678 | break; |
jah128 | 0:8a5497a2e366 | 679 | case 94: |
jah128 | 0:8a5497a2e366 | 680 | sprintf(command,"%s %d","GET BG BASE IR VALUE",message[1]); |
jah128 | 0:8a5497a2e366 | 681 | sprintf(ret_message,"%d",get_background_base_ir_value(message[1])); |
jah128 | 0:8a5497a2e366 | 682 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 683 | break; |
jah128 | 0:8a5497a2e366 | 684 | case 95: |
jah128 | 0:8a5497a2e366 | 685 | sprintf(command,"%s %d","GET IL BASE IR VALUE",message[1]); |
jah128 | 0:8a5497a2e366 | 686 | sprintf(ret_message,"%d",get_illuminated_base_ir_value(message[1])); |
jah128 | 0:8a5497a2e366 | 687 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 688 | break; |
jah128 | 0:8a5497a2e366 | 689 | case 96: |
jah128 | 0:8a5497a2e366 | 690 | strcpy(command,"GET BG BASE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 691 | sprintf(ret_message,"%03X%03X%03X%03X%03X",get_background_base_ir_value(0),get_background_base_ir_value(1),get_background_base_ir_value(2),get_background_base_ir_value(3),get_background_base_ir_value(4)); |
jah128 | 0:8a5497a2e366 | 692 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 693 | break; |
jah128 | 0:8a5497a2e366 | 694 | case 97: |
jah128 | 0:8a5497a2e366 | 695 | strcpy(command,"GET IL BASE IR VALUES"); |
jah128 | 0:8a5497a2e366 | 696 | sprintf(ret_message,"%03X%03X%03X%03X%03X",get_illuminated_base_ir_value(0),get_illuminated_base_ir_value(1),get_illuminated_base_ir_value(2),get_illuminated_base_ir_value(3),get_illuminated_base_ir_value(4)); |
jah128 | 0:8a5497a2e366 | 697 | send_message = 1; |
jah128 | 0:8a5497a2e366 | 698 | break; |
jah128 | 0:8a5497a2e366 | 699 | } |
jah128 | 0:8a5497a2e366 | 700 | |
jah128 | 0:8a5497a2e366 | 701 | |
jah128 | 0:8a5497a2e366 | 702 | if(send_message) { |
jah128 | 0:8a5497a2e366 | 703 | char message_length = strlen(ret_message); |
jah128 | 0:8a5497a2e366 | 704 | switch(interface) { |
jah128 | 0:8a5497a2e366 | 705 | case 0: |
jah128 | 0:8a5497a2e366 | 706 | pc.printf("%c%c%s",RESPONSE_MESSAGE_BYTE,message_length,ret_message); |
jah128 | 0:8a5497a2e366 | 707 | break; |
jah128 | 0:8a5497a2e366 | 708 | case 1: |
jah128 | 0:8a5497a2e366 | 709 | bt.printf("%c%c%s",RESPONSE_MESSAGE_BYTE,message_length,ret_message); |
jah128 | 0:8a5497a2e366 | 710 | break; |
jah128 | 0:8a5497a2e366 | 711 | } |
jah128 | 0:8a5497a2e366 | 712 | debug("Received %s request message: %s %s [%02x%02x%02x]\nReply: %s [%d ch]\n",iface, command, subcommand,message[0],message[1],message[2],ret_message,message_length); |
jah128 | 0:8a5497a2e366 | 713 | } else { |
jah128 | 0:8a5497a2e366 | 714 | switch(interface) { |
jah128 | 0:8a5497a2e366 | 715 | case 0: |
jah128 | 0:8a5497a2e366 | 716 | pc.printf("%c%c",ACKNOWLEDGE_MESSAGE_BYTE,command_status); |
jah128 | 0:8a5497a2e366 | 717 | break; |
jah128 | 0:8a5497a2e366 | 718 | case 1: |
jah128 | 0:8a5497a2e366 | 719 | bt.printf("%c%c",ACKNOWLEDGE_MESSAGE_BYTE,command_status); |
jah128 | 0:8a5497a2e366 | 720 | break; |
jah128 | 0:8a5497a2e366 | 721 | } |
jah128 | 0:8a5497a2e366 | 722 | switch(command_status) { |
jah128 | 0:8a5497a2e366 | 723 | case 0: |
jah128 | 0:8a5497a2e366 | 724 | debug("Unrecognised %s command message [%02x%02x%02x]\n",iface,message[0],message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 725 | break; |
jah128 | 0:8a5497a2e366 | 726 | case 1: |
jah128 | 0:8a5497a2e366 | 727 | debug("Actioned %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 728 | break; |
jah128 | 0:8a5497a2e366 | 729 | case 2: |
jah128 | 0:8a5497a2e366 | 730 | debug("Blocked %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 731 | break; |
jah128 | 0:8a5497a2e366 | 732 | case 3: |
jah128 | 0:8a5497a2e366 | 733 | debug("Invalid %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]); |
jah128 | 0:8a5497a2e366 | 734 | break; |
jah128 | 0:8a5497a2e366 | 735 | } |
jah128 | 0:8a5497a2e366 | 736 | } |
jah128 | 0:8a5497a2e366 | 737 | } |
jah128 | 0:8a5497a2e366 | 738 | |
jah128 | 0:8a5497a2e366 | 739 | char * IF_nibble_to_binary_char(char in) |
jah128 | 0:8a5497a2e366 | 740 | { |
jah128 | 0:8a5497a2e366 | 741 | char * ret = (char*)malloc(sizeof(char)*5); |
jah128 | 0:8a5497a2e366 | 742 | for(int i=0; i<4; i++) { |
jah128 | 0:8a5497a2e366 | 743 | if(in & (128 >> i)) ret[i]='1'; |
jah128 | 0:8a5497a2e366 | 744 | else ret[i]='0'; |
jah128 | 0:8a5497a2e366 | 745 | } |
jah128 | 0:8a5497a2e366 | 746 | ret[4]=0; |
jah128 | 0:8a5497a2e366 | 747 | return ret; |
jah128 | 0:8a5497a2e366 | 748 | } |
jah128 | 0:8a5497a2e366 | 749 | |
jah128 | 0:8a5497a2e366 | 750 | char * IF_char_to_binary_char(char in) |
jah128 | 0:8a5497a2e366 | 751 | { |
jah128 | 0:8a5497a2e366 | 752 | char * ret = (char*)malloc(sizeof(char)*9); |
jah128 | 0:8a5497a2e366 | 753 | for(int i=0; i<8; i++) { |
jah128 | 0:8a5497a2e366 | 754 | if(in & (128 >> i)) ret[i]='1'; |
jah128 | 0:8a5497a2e366 | 755 | else ret[i]='0'; |
jah128 | 0:8a5497a2e366 | 756 | } |
jah128 | 0:8a5497a2e366 | 757 | ret[8]=0; |
jah128 | 0:8a5497a2e366 | 758 | return ret; |
jah128 | 0:8a5497a2e366 | 759 | } |
jah128 | 0:8a5497a2e366 | 760 | |
jah128 | 0:8a5497a2e366 | 761 | float IF_decode_unsigned_float(char byte0, char byte1) |
jah128 | 0:8a5497a2e366 | 762 | { |
jah128 | 0:8a5497a2e366 | 763 | unsigned short sval = (byte0) << 8; |
jah128 | 0:8a5497a2e366 | 764 | sval += byte1; |
jah128 | 0:8a5497a2e366 | 765 | float scaled = sval / 65535.0f; |
jah128 | 0:8a5497a2e366 | 766 | return scaled; |
jah128 | 0:8a5497a2e366 | 767 | } |
jah128 | 0:8a5497a2e366 | 768 | |
jah128 | 0:8a5497a2e366 | 769 | float IF_decode_float(char byte0, char byte1) |
jah128 | 0:8a5497a2e366 | 770 | { |
jah128 | 0:8a5497a2e366 | 771 | // MSB is byte 0 is sign, rest is linear spread between 0 and 1 |
jah128 | 0:8a5497a2e366 | 772 | char sign = byte0 / 128; |
jah128 | 0:8a5497a2e366 | 773 | short sval = (byte0 % 128) << 8; |
jah128 | 0:8a5497a2e366 | 774 | sval += byte1; |
jah128 | 0:8a5497a2e366 | 775 | float scaled = sval / 32767.0f; |
jah128 | 0:8a5497a2e366 | 776 | if(sign == 0) scaled = 0-scaled; |
jah128 | 0:8a5497a2e366 | 777 | return scaled; |
jah128 | 0:8a5497a2e366 | 778 | } |
jah128 | 0:8a5497a2e366 | 779 | |
jah128 | 0:8a5497a2e366 | 780 | float IF_decode_unsigned_float(char byte0) |
jah128 | 0:8a5497a2e366 | 781 | { |
jah128 | 0:8a5497a2e366 | 782 | unsigned short sval = (byte0); |
jah128 | 0:8a5497a2e366 | 783 | float scaled = sval / 255.0f; |
jah128 | 0:8a5497a2e366 | 784 | return scaled; |
jah128 | 0:8a5497a2e366 | 785 | } |
jah128 | 0:8a5497a2e366 | 786 | |
jah128 | 0:8a5497a2e366 | 787 | float IF_decode_float(char byte0) |
jah128 | 0:8a5497a2e366 | 788 | { |
jah128 | 0:8a5497a2e366 | 789 | // MSB is byte 0 is sign, rest is linear spread between 0 and 1 |
jah128 | 0:8a5497a2e366 | 790 | char sign = byte0 / 128; |
jah128 | 0:8a5497a2e366 | 791 | short sval = (byte0 % 128); |
jah128 | 0:8a5497a2e366 | 792 | float scaled = sval / 127.0f; |
jah128 | 0:8a5497a2e366 | 793 | if(sign == 0) scaled = 0-scaled; |
jah128 | 0:8a5497a2e366 | 794 | return scaled; |
jah128 | 0:8a5497a2e366 | 795 | } |
jah128 | 0:8a5497a2e366 | 796 | |
jah128 | 0:8a5497a2e366 | 797 | void IF_setup_serial_interfaces() |
jah128 | 0:8a5497a2e366 | 798 | { |
jah128 | 0:8a5497a2e366 | 799 | if(ENABLE_PC_SERIAL) { |
jah128 | 0:8a5497a2e366 | 800 | pc.baud(PC_BAUD); |
jah128 | 0:8a5497a2e366 | 801 | pc.attach(&IF_pc_rx_callback, Serial::RxIrq); |
jah128 | 0:8a5497a2e366 | 802 | } |
jah128 | 0:8a5497a2e366 | 803 | if(ENABLE_BLUETOOTH) { |
jah128 | 0:8a5497a2e366 | 804 | bt.baud(BLUETOOTH_BAUD); |
jah128 | 0:8a5497a2e366 | 805 | bt.attach(&IF_bt_rx_callback, Serial::RxIrq); |
jah128 | 0:8a5497a2e366 | 806 | } |
jah128 | 0:8a5497a2e366 | 807 | } |
jah128 | 0:8a5497a2e366 | 808 | |
jah128 | 0:8a5497a2e366 | 809 | void IF_pc_rx_command_timeout() |
jah128 | 0:8a5497a2e366 | 810 | { |
jah128 | 0:8a5497a2e366 | 811 | char message_array[6]; |
jah128 | 0:8a5497a2e366 | 812 | char length = 1 + pc_command_message_byte; |
jah128 | 0:8a5497a2e366 | 813 | pc_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 814 | message_array[0] = COMMAND_MESSAGE_BYTE; |
jah128 | 0:8a5497a2e366 | 815 | for(int k=0; k<pc_command_message_byte; k++) { |
jah128 | 0:8a5497a2e366 | 816 | message_array[k+1] = pc_command_message[k]; |
jah128 | 0:8a5497a2e366 | 817 | } |
jah128 | 0:8a5497a2e366 | 818 | IF_handle_user_serial_message(message_array, length, 0); |
jah128 | 0:8a5497a2e366 | 819 | } |
jah128 | 0:8a5497a2e366 | 820 | |
jah128 | 0:8a5497a2e366 | 821 | void IF_bt_rx_command_timeout() |
jah128 | 0:8a5497a2e366 | 822 | { |
jah128 | 0:8a5497a2e366 | 823 | char message_array[6]; |
jah128 | 0:8a5497a2e366 | 824 | char length = 1 + bt_command_message_byte; |
jah128 | 0:8a5497a2e366 | 825 | bt_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 826 | message_array[0] = COMMAND_MESSAGE_BYTE; |
jah128 | 0:8a5497a2e366 | 827 | for(int k=0; k<bt_command_message_byte; k++) { |
jah128 | 0:8a5497a2e366 | 828 | message_array[k+1] = bt_command_message[k]; |
jah128 | 0:8a5497a2e366 | 829 | } |
jah128 | 0:8a5497a2e366 | 830 | IF_handle_user_serial_message(message_array, length, 1); |
jah128 | 0:8a5497a2e366 | 831 | } |
jah128 | 0:8a5497a2e366 | 832 | |
jah128 | 0:8a5497a2e366 | 833 | void IF_pc_rx_callback() |
jah128 | 0:8a5497a2e366 | 834 | { |
jah128 | 0:8a5497a2e366 | 835 | int count = 0; |
jah128 | 0:8a5497a2e366 | 836 | char message_array[255]; |
jah128 | 0:8a5497a2e366 | 837 | |
jah128 | 0:8a5497a2e366 | 838 | while(pc.readable()) { |
jah128 | 0:8a5497a2e366 | 839 | char tc = pc.getc(); |
jah128 | 0:8a5497a2e366 | 840 | message_array[count] = tc; |
jah128 | 0:8a5497a2e366 | 841 | count ++; |
jah128 | 0:8a5497a2e366 | 842 | if(pc_command_message_started == 1) { |
jah128 | 0:8a5497a2e366 | 843 | if(pc_command_message_byte == 3) { |
jah128 | 0:8a5497a2e366 | 844 | pc_command_timeout.detach(); |
jah128 | 0:8a5497a2e366 | 845 | if(tc == COMMAND_MESSAGE_BYTE) { |
jah128 | 0:8a5497a2e366 | 846 | // A complete command message succesfully received, call handler |
jah128 | 0:8a5497a2e366 | 847 | pc_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 848 | count = 0; |
jah128 | 0:8a5497a2e366 | 849 | IF_handle_command_serial_message(pc_command_message , 0); |
jah128 | 0:8a5497a2e366 | 850 | } else { |
jah128 | 0:8a5497a2e366 | 851 | // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message |
jah128 | 0:8a5497a2e366 | 852 | pc_command_message_started = 0; |
jah128 | 0:8a5497a2e366 | 853 | message_array[0] = COMMAND_MESSAGE_BYTE; |
jah128 | 0:8a5497a2e366 | 854 | message_array[1] = pc_command_message[0]; |
jah128 | 0:8a5497a2e366 | 855 | message_array[2] = pc_command_message[1]; |
jah128 | 0:8a5497a2e366 | 856 | message_array[3] = pc_command_message[2]; |
jah128 | 0:8a5497a2e366 | 857 | message_array[4] = tc; |
jah128 | 0:8a5497a2e366 | 858 | count = 5; |
jah128 | 0:8a5497a2e366 | 859 | } |
jah128 | 0:8a5497a2e366 | 860 | } else { |
jah128 | 0:8a5497a2e366 | 861 | pc_command_message[pc_command_message_byte] = tc; |
jah128 | 0:8a5497a2e366 | 862 | pc_command_message_byte ++; |
jah128 | 0:8a5497a2e366 | 863 | } |
jah128 | 0:8a5497a2e366 | 864 | } else { |
jah128 | 0:8a5497a2e366 | 865 | if(count == 1) { |
jah128 | 0:8a5497a2e366 | 866 | if(tc == COMMAND_MESSAGE_BYTE) { |
jah128 | 0:8a5497a2e366 | 867 | pc_command_timeout.attach(&IF_pc_rx_command_timeout,command_timeout_period); |
jah128 | 0:8a5497a2e366 | 868 | pc_command_message_started = 1; |
jah128 | 0:8a5497a2e366 | 869 | pc_command_message_byte = 0; |
jah128 | 0:8a5497a2e366 | 870 | |
jah128 | 0:8a5497a2e366 | 871 | } |
jah128 | 0:8a5497a2e366 | 872 | } |
jah128 | 0:8a5497a2e366 | 873 | } |
jah128 | 0:8a5497a2e366 | 874 | } |
jah128 | 0:8a5497a2e366 | 875 | if(!pc_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 0); |
jah128 | 0:8a5497a2e366 | 876 | } |
alanmillard | 20:e08376c0b4ea | 877 | |
alanmillard | 20:e08376c0b4ea | 878 | Timeout bt_message_timeout; |
alanmillard | 20:e08376c0b4ea | 879 | static float bt_message_timeout_period = 0.001; // 1 millisecond |
alanmillard | 20:e08376c0b4ea | 880 | char bt_buffer[255]; |
alanmillard | 20:e08376c0b4ea | 881 | int bt_buffer_index = 0; |
alanmillard | 20:e08376c0b4ea | 882 | |
alanmillard | 20:e08376c0b4ea | 883 | void IF_bt_message_timeout() |
alanmillard | 20:e08376c0b4ea | 884 | { |
alanmillard | 20:e08376c0b4ea | 885 | char buffer[255]; |
alanmillard | 20:e08376c0b4ea | 886 | |
alanmillard | 20:e08376c0b4ea | 887 | sprintf(buffer, bt_buffer, bt_buffer_index); |
alanmillard | 20:e08376c0b4ea | 888 | buffer[bt_buffer_index] = 0; |
alanmillard | 20:e08376c0b4ea | 889 | |
alanmillard | 20:e08376c0b4ea | 890 | // debug("BT message timeout: %s [%d chars]\n", buffer, bt_buffer_index); |
alanmillard | 20:e08376c0b4ea | 891 | |
alanmillard | 20:e08376c0b4ea | 892 | IF_handle_user_serial_message(bt_buffer, bt_buffer_index, 1); |
alanmillard | 20:e08376c0b4ea | 893 | |
alanmillard | 20:e08376c0b4ea | 894 | bt_buffer_index = 0; |
alanmillard | 20:e08376c0b4ea | 895 | } |
alanmillard | 20:e08376c0b4ea | 896 | |
jah128 | 0:8a5497a2e366 | 897 | void IF_bt_rx_callback() |
jah128 | 0:8a5497a2e366 | 898 | { |
alanmillard | 20:e08376c0b4ea | 899 | while(bt.readable()) |
alanmillard | 20:e08376c0b4ea | 900 | { |
alanmillard | 20:e08376c0b4ea | 901 | char byte = bt.getc(); |
alanmillard | 20:e08376c0b4ea | 902 | |
alanmillard | 20:e08376c0b4ea | 903 | bt_buffer[bt_buffer_index] = byte; |
alanmillard | 20:e08376c0b4ea | 904 | bt_buffer_index++; |
alanmillard | 20:e08376c0b4ea | 905 | } |
alanmillard | 20:e08376c0b4ea | 906 | |
alanmillard | 20:e08376c0b4ea | 907 | bt_message_timeout.attach(&IF_bt_message_timeout, bt_message_timeout_period); |
alanmillard | 20:e08376c0b4ea | 908 | } |
alanmillard | 20:e08376c0b4ea | 909 | |
alanmillard | 20:e08376c0b4ea | 910 | //void IF_bt_rx_callback() |
alanmillard | 20:e08376c0b4ea | 911 | //{ |
alanmillard | 18:5921c1853e8a | 912 | // int count = 0; |
alanmillard | 18:5921c1853e8a | 913 | // char message_array[255]; |
alanmillard | 18:5921c1853e8a | 914 | // |
alanmillard | 18:5921c1853e8a | 915 | // wait_ms(500); // Wait 0.5ms to allow a complete message to arrive before atttempting to process it |
alanmillard | 18:5921c1853e8a | 916 | // |
alanmillard | 18:5921c1853e8a | 917 | // while(bt.readable()) { |
alanmillard | 18:5921c1853e8a | 918 | // char tc = bt.getc(); |
alanmillard | 18:5921c1853e8a | 919 | // message_array[count] = tc; |
alanmillard | 18:5921c1853e8a | 920 | // count ++; |
alanmillard | 18:5921c1853e8a | 921 | // if(bt_command_message_started == 1) { |
alanmillard | 18:5921c1853e8a | 922 | // if(bt_command_message_byte == 3) { |
alanmillard | 18:5921c1853e8a | 923 | // bt_command_timeout.detach(); |
alanmillard | 18:5921c1853e8a | 924 | // if(tc == COMMAND_MESSAGE_BYTE) { |
alanmillard | 18:5921c1853e8a | 925 | // // A complete command message succesfully received, call handler |
alanmillard | 18:5921c1853e8a | 926 | // bt_command_message_started = 0; |
alanmillard | 18:5921c1853e8a | 927 | // count = 0; |
alanmillard | 18:5921c1853e8a | 928 | // IF_handle_command_serial_message(bt_command_message , 1); |
alanmillard | 18:5921c1853e8a | 929 | // } else { |
alanmillard | 18:5921c1853e8a | 930 | // // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message |
alanmillard | 18:5921c1853e8a | 931 | // bt_command_message_started = 0; |
alanmillard | 18:5921c1853e8a | 932 | // message_array[0] = COMMAND_MESSAGE_BYTE; |
alanmillard | 18:5921c1853e8a | 933 | // message_array[1] = bt_command_message[0]; |
alanmillard | 18:5921c1853e8a | 934 | // message_array[2] = bt_command_message[1]; |
alanmillard | 18:5921c1853e8a | 935 | // message_array[3] = bt_command_message[2]; |
alanmillard | 18:5921c1853e8a | 936 | // message_array[4] = tc; |
alanmillard | 18:5921c1853e8a | 937 | // count = 5; |
alanmillard | 18:5921c1853e8a | 938 | // } |
alanmillard | 18:5921c1853e8a | 939 | // } else { |
alanmillard | 18:5921c1853e8a | 940 | // bt_command_timeout.attach(&IF_bt_rx_command_timeout,command_timeout_period); |
alanmillard | 18:5921c1853e8a | 941 | // bt_command_message[bt_command_message_byte] = tc; |
alanmillard | 18:5921c1853e8a | 942 | // bt_command_message_byte ++; |
alanmillard | 17:da524989b637 | 943 | // } |
alanmillard | 18:5921c1853e8a | 944 | // } else { |
alanmillard | 18:5921c1853e8a | 945 | // if(count == 1) { |
alanmillard | 18:5921c1853e8a | 946 | // if(tc == COMMAND_MESSAGE_BYTE) { |
alanmillard | 18:5921c1853e8a | 947 | // bt_command_message_started = 1; |
alanmillard | 18:5921c1853e8a | 948 | // bt_command_message_byte = 0; |
alanmillard | 18:5921c1853e8a | 949 | // |
alanmillard | 18:5921c1853e8a | 950 | // } |
alanmillard | 18:5921c1853e8a | 951 | // } |
alanmillard | 17:da524989b637 | 952 | // } |
alanmillard | 17:da524989b637 | 953 | // } |
alanmillard | 18:5921c1853e8a | 954 | // if(!bt_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 1); |
alanmillard | 20:e08376c0b4ea | 955 | //} |