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