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