Library for the PsiSwarm Robot - Version 0.7

Dependents:   PsiSwarm_V7_Blank

Fork of PsiSwarmLibrary by James Hilder

Committer:
jah128
Date:
Thu Feb 04 21:48:54 2016 +0000
Revision:
0:d6269d17c8cf
Child:
1:060690a934a9
PsiSwarm Library Version 0.4 - Initial Commit

Who changed what in which revision?

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