25 static float command_timeout_period = 0.1f;
26 char pc_command_message_started = 0;
27 char pc_command_message_byte = 0;
28 char pc_command_message[3];
29 char bt_command_message_started = 0;
30 char bt_command_message_byte = 0;
31 char bt_command_message[3];
33 char allow_commands = 1;
34 char allow_requests = 1;
35 char file_transfer_state = 0;
46 Timeout pc_command_timeout;
47 Timeout bt_command_timeout;
56 if(ENABLE_PC_SERIAL) {
58 pc.attach(
this,&SerialControl::IF_pc_rx_callback, Serial::RxIrq);
61 bt.baud(BLUETOOTH_BAUD);
62 bt.attach(
this,&SerialControl::IF_bt_rx_callback, Serial::RxIrq);
67 void SerialControl::IF_start_file_transfer_mode()
75 file_transfer_mode = 1;
76 file_transfer_state = 0;
78 user_code_restore_mode = user_code_running;
79 user_code_running = 0;
80 ft_timeout.attach(
this,&SerialControl::IF_file_transfer_timeout,2.0);
84 void SerialControl::IF_invalid_transfer(
void)
86 psi.
debug(
"File transfer failed\n");
87 if(data_written == 1) {
88 psi.
debug(
"Deleting corrupted file\n");
95 IF_end_file_transfer_mode();
98 void SerialControl::IF_file_transfer_timeout(
void)
100 psi.
debug(
"File transfer failed: timeout\n");
105 IF_end_file_transfer_mode();
108 void SerialControl::IF_end_file_transfer_mode(
void)
111 file_transfer_mode = 0;
112 user_code_running = user_code_restore_mode;
116 void SerialControl::IF_handle_file_transfer_serial_message(
char * message,
char length,
char interface)
124 if(file_transfer_state < 2)psi.
debug(
"FTM Message:%.*s [%d]\n",length,message,length);
125 else psi.
debug(
"FTM data block received (%d bytes)\n",length);
128 if(message[0] != 33) {
129 IF_invalid_transfer();
131 switch(file_transfer_state) {
134 if(length == 1 || length > 13) IF_invalid_transfer();
136 strcpy(filename,
"/local/");
137 strncat(filename, message + 1, length - 1);
138 psi.
debug(
"Target filename:%s\n",filename);
141 ft_timeout.attach(
this,&SerialControl::IF_file_transfer_timeout,2.0);
142 bt.printf(
"%c%c%s",RESPONSE_MESSAGE_BYTE,2,
"FN");
143 file_transfer_state = 1;
148 if(length != 4) IF_invalid_transfer();
150 file_length = (message[1]) * 256;
151 file_length += (message[2]);
153 file_length += message[3];
154 file_transfer_state = 2;
156 char display_message[17];
157 sprintf(display_message,
"F:%s",filename);
161 sprintf(display_message,
"S:%d b",file_length);
167 final_block = file_length / block_size;
168 if(file_length % block_size != 0) final_block ++;
172 psi.
debug(
"File size %d bytes (%d blocks of %d bytes)\n",file_length,final_block,block_size);
174 ft_timeout.attach(
this,&SerialControl::IF_file_transfer_timeout,1.0);
176 bt.printf(
"%c%c%c%c%c",RESPONSE_MESSAGE_BYTE,3,message[1],message[2],message[3]);
185 char details_string[17];
186 sprintf(details_string,
"BLOCK %d OF %d",block_index,final_block);
188 expected_size = block_size;
189 if(block_index == final_block) expected_size = file_length % block_size;
190 if(expected_size == 0) expected_size = block_size;
191 if(length!=expected_size + 1) {
193 psi.
debug(
"File data unexpected length in packet %d (%d bytes received, %d bytes expected)\n",block_index,length-1,expected_size);
195 char transfer_mode[2]= {
'a'};
196 if(block_index == 1) {
197 transfer_mode[0]=
'w';
199 FILE *fp = fopen(filename,transfer_mode);
204 bytes_written = fwrite(message+1,expected_size,1,fp);
206 if(data_written ==
false && bytes_written > 0) data_written =
true;
207 psi.
debug(
"Bytes written: %d\n",expected_size * bytes_written);
208 if(block_index < final_block) {
209 psi.
debug(
"Message packet %d received and written\n",block_index);
212 ft_timeout.attach(
this,&SerialControl::IF_file_transfer_timeout,1.0);
213 bt.printf(
"%c%c%s",RESPONSE_MESSAGE_BYTE,1,
"D");
218 bt.printf(
"%c%c%s",RESPONSE_MESSAGE_BYTE,1,
"F");
220 psi.
debug(
"File transfer completed successfully\n");
223 IF_calculateCRC16(file_length);
230 psi.
debug(
"File transfer mode ended\n");
231 IF_end_file_transfer_mode();
240 void SerialControl::IF_handle_user_serial_message(
char * message,
char length,
char interface)
243 sprintf(buffer,message,length);
244 for(
int i=0; i<length; i++) {
245 buffer[i]=message[i];
250 handle_user_serial_message(message,length,interface);
253 void SerialControl::IF_handle_command_serial_message(
char message[3],
char interface)
256 if(interface) strcpy(iface,
"BT");
257 else strcpy(iface,
"USB");
264 char colour_string[7];
265 char ret_message[50];
266 char send_message = 0;
267 char command_status = 0;
281 strcpy(command,
"SET LEFT MOTOR");
282 dec = IF_decode_float(message[1],message[2]);
283 sprintf(subcommand,
"%1.5f",dec);
287 }
else command_status = 2;
290 strcpy(command,
"SET RIGHT MOTOR");
291 dec = IF_decode_float(message[1],message[2]);
292 sprintf(subcommand,
"%1.5f",dec);
296 }
else command_status = 2;
299 strcpy(command,
"SET BOTH MOTORS");
300 dec = IF_decode_float(message[1],message[2]);
301 sprintf(subcommand,
"%1.5f",dec);
305 }
else command_status = 2;
308 strcpy(command,
"BRAKE LEFT MOTOR");
309 sprintf(subcommand,
"");
313 }
else command_status = 2;
316 strcpy(command,
"BRAKE RIGHT MOTOR");
317 sprintf(subcommand,
"");
321 }
else command_status = 2;
324 strcpy(command,
"BRAKE BOTH MOTORS");
325 sprintf(subcommand,
"");
329 }
else command_status = 2;
332 strcpy(command,
"STOP BOTH MOTORS");
333 sprintf(subcommand,
"");
337 }
else command_status = 2;
340 strcpy(command,
"TURN ON SPOT");
341 dec = IF_decode_float(message[1],message[2]);
342 sprintf(subcommand,
"%1.5f",dec);
346 }
else command_status = 2;
349 strcpy(command,
"SET EACH MOTOR");
350 l_dec = IF_decode_float(message[1]);
351 r_dec = IF_decode_float(message[2]);
352 sprintf(subcommand,
"L=%1.3f R=%1.3f",l_dec,r_dec);
357 }
else command_status = 2;
362 strcpy(command,
"SET LED STATES");
363 sprintf(subcommand,
"G:%s R:%s",IF_char_to_binary_char(message[1]), IF_char_to_binary_char(message[2]));
366 led.
set_leds(message[1],message[2]);
367 }
else command_status = 2;
370 strcpy(command,
"SET RED LED STATES");
371 sprintf(subcommand,
"%s",IF_char_to_binary_char(message[1]));
375 }
else command_status = 2;
378 strcpy(command,
"SET GREEN LED STATES");
379 sprintf(subcommand,
"%s",IF_char_to_binary_char(message[1]));
383 }
else command_status = 2;
386 strcpy(command,
"SET LED");
389 strcpy(colour_string,
"RED");
392 strcpy(colour_string,
"GREEN");
395 strcpy(colour_string,
"BOTH");
398 strcpy(colour_string,
"OFF");
401 if(message[1] < 8 && message[2] < 4) {
402 sprintf(subcommand,
"%d %s",message[1],colour_string);
405 led.
set_led(message[1],message[2]);
406 }
else command_status = 2;
408 sprintf(subcommand,
"[INVALID CODE]");
413 strcpy(command,
"SET CENTER LED STATE");
416 strcpy(colour_string,
"RED");
419 strcpy(colour_string,
"GREEN");
422 strcpy(colour_string,
"BOTH");
425 strcpy(colour_string,
"OFF");
429 sprintf(subcommand,
"%s",colour_string);
433 }
else command_status = 2;
435 sprintf(subcommand,
"[INVALID CODE]");
440 strcpy(command,
"SET C.LED BRIGHTNESS");
441 dec = IF_decode_unsigned_float(message[1],message[2]);
442 sprintf(subcommand,
"%1.5f",dec);
446 }
else command_status = 2;
449 strcpy(command,
"SET MBED LEDS");
450 sprintf(subcommand,
"%s",IF_nibble_to_binary_char(message[1]));
453 mbed_led1 = (message[1] & 128) >> 7;
454 mbed_led2 = (message[1] & 64) >> 6;
455 mbed_led3 = (message[1] & 32) >> 5;
456 mbed_led4 = (message[1] & 16) >> 4;
457 }
else command_status = 2;
460 strcpy(command,
"BLINK OUTER LEDS");
461 dec = IF_decode_unsigned_float(message[1],message[2]);
462 sprintf(subcommand,
"FOR %1.5fS",dec);
466 }
else command_status = 2;
469 strcpy(command,
"SET BASE LED STATE");
472 strcpy(subcommand,
"ON");
475 strcpy(subcommand,
"OFF");
481 }
else command_status = 2;
484 strcpy(command,
"SET CENTER LED ");
487 strcpy(colour_string,
"RED");
490 strcpy(colour_string,
"GREEN");
493 strcpy(colour_string,
"BOTH");
496 strcpy(colour_string,
"OFF");
499 dec = IF_decode_unsigned_float(message[2]);
500 sprintf(subcommand,
"%s @ %1.5f brightness",colour_string,dec);
504 }
else command_status = 2;
510 strcpy(command,
"SET DISPLAY ");
513 strcpy(subcommand,
"CLEAR");
517 }
else command_status = 2;
520 strcpy(subcommand,
"MESSAGE 1");
528 }
else command_status = 2;
531 strcpy(subcommand,
"MESSAGE 2");
539 }
else command_status = 2;
542 strcpy(subcommand,
"MESSAGE 3");
550 }
else command_status = 2;
553 strcpy(subcommand,
"MESSAGE 4");
561 }
else command_status = 2;
564 strcpy(subcommand,
"MESSAGE 5");
572 }
else command_status = 2;
575 strcpy(subcommand,
"MESSAGE 6");
583 }
else command_status = 2;
588 strcpy(command,
"SET CURSOR ");
589 if(message[1] < 2 && message[2] < 16) {
590 sprintf(subcommand,
"[%d,%d]",message[1],message[2]);
593 }
else command_status = 2;
595 sprintf(subcommand,
"[INVALID]");
600 strcpy(command,
"PRINT CHARACTERS ");
601 char print_message[2];
602 print_message[0]=message[1];
603 print_message[1]=message[2];
604 sprintf(subcommand,
"[%c,%c]",message[1],message[2]);
607 }
else command_status = 2;
611 strcpy(command,
"SET DISPLAY B.NESS");
612 dec = IF_decode_unsigned_float(message[1],message[2]);
613 sprintf(subcommand,
"%1.5f",dec);
617 }
else command_status = 2;
621 strcpy(command,
"SET DEBUG MODE");
624 strcpy(subcommand,
"ON");
627 strcpy(subcommand,
"OFF");
630 if(message[2] & 1) strcat (subcommand,
"-PC");
631 if(message[2] & 2) strcat (subcommand,
"-BT");
632 if(message[2] & 4) strcat (subcommand,
"-DISP");
635 debug_mode = message[1];
636 debug_output = message[2];
637 }
else command_status = 2;
640 strcpy(command,
"SET DEMO MODE");
641 switch(message[1] % 2) {
643 strcpy(subcommand,
"ON");
646 strcpy(subcommand,
"OFF");
651 demo_on = message[1] % 2;
653 user_code_restore_mode = user_code_running;
654 user_code_running = 0;
656 user_code_running = user_code_restore_mode;
658 }
else command_status = 2;
661 strcpy(command,
"SET USER CODE");
662 switch(message[1] % 2) {
664 strcpy(subcommand,
"ON");
667 strcpy(subcommand,
"OFF");
672 user_code_running = message[1] % 2;
673 }
else command_status = 2;
676 strcpy(command,
"PAUSE USER CODE");
677 dec = IF_decode_unsigned_float(message[1],message[2]) * 10;
678 sprintf(subcommand,
"FOR %2.3fS",dec);
682 }
else command_status = 2;
686 strcpy(command,
"RESET ENCODERS");
690 }
else command_status = 2;
694 strcpy(command,
"SET ALLOW COMMANDS");
695 switch(message[1] % 2) {
697 strcpy(subcommand,
"ON");
700 strcpy(subcommand,
"OFF");
703 allow_commands = message[1] % 2;
708 irp_delay = (message[1] << 8) + message[2];
709 sprintf(command,
"SET IR PULSE DELAY %d MS",irp_delay);
712 ir_pulse_delay = irp_delay;
713 }
else command_status = 2;
716 irp_delay = (message[1] << 8) + message[2];
717 sprintf(command,
"SET BASE IR PULSE DELAY %d MS",irp_delay);
720 base_ir_pulse_delay = irp_delay;
721 }
else command_status = 2;
726 strcpy(command,
"GET LEFT MOTOR SPEED");
727 sprintf(ret_message,
"%1.5f",motor_left_speed);
732 strcpy(command,
"GET RIGHT MOTOR SPEED");
733 sprintf(ret_message,
"%1.5f",motor_right_speed);
737 strcpy(command,
"GET BRAKE STATES");
738 sprintf(ret_message,
"%d,%d",motor_left_brake,motor_right_brake);
742 strcpy(command,
"GET MOTOR STATES");
747 strcpy(command,
"GET ENCODERS");
748 sprintf(ret_message,
"%d,%d",left_encoder,right_encoder);
754 strcpy(command,
"GET LED STATES");
761 strcpy(command,
"GET SOFTWARE VERSION");
762 sprintf(ret_message,
"%1.2f",SOFTWARE_VERSION_CODE);
767 strcpy(command,
"GET UPTIME");
768 sprintf(ret_message,
"%6.2f",psi.
get_uptime());
773 strcpy(command,
"GET ID");
774 sprintf(ret_message,
"%d",robot_id);
779 strcpy(command,
"GET SWITCH BYTE");
780 sprintf(ret_message,
"%02x",switch_byte);
784 strcpy(command,
"GET USER CODE");
785 sprintf(ret_message,
"%d",user_code_running);
789 strcpy(command,
"GET RESPONSE STRING");
790 sprintf(ret_message,
"PSI");
794 strcpy(command,
"GET PROGRAM NAME");
795 sprintf(ret_message,
"%s",program_name);
799 strcpy(command,
"GET AUTHOR NAME");
800 sprintf(ret_message,
"%s",author_name);
804 strcpy(command,
"GET DEBUG MODE");
805 sprintf(ret_message,
"%1d%1d",debug_mode,debug_output);
809 strcpy(command,
"GET SYSTEM WARNINGS");
810 sprintf(ret_message,
"%d",system_warnings);
817 strcpy(command,
"STORE BG. IR VALUES");
821 }
else command_status = 2;
824 strcpy(command,
"STORE IL. IR VALUES");
828 }
else command_status = 2;
831 strcpy(command,
"STORE IR VALUES");
835 }
else command_status = 2;
838 strcpy(command,
"STORE BG BASE IR VALUES");
842 }
else command_status = 2;
845 strcpy(command,
"STORE IL. BASE IR VALUES");
849 }
else command_status = 2;
852 strcpy(command,
"STORE BASE IR VALUES");
856 }
else command_status = 2;
859 strcpy(command,
"STORE ALL IR VALUES");
864 }
else command_status = 2;
867 sprintf(command,
"%s %d",
"GET BG IR VALUE",message[1]);
872 sprintf(command,
"%s %d",
"GET IL IR VALUE",message[1]);
877 strcpy(command,
"GET BG IR VALUES");
882 strcpy(command,
"GET ILLUMINATED IR VALUES");
887 sprintf(command,
"%s %d",
"GET BG BASE IR VALUE",message[1]);
892 sprintf(command,
"%s %d",
"GET IL BASE IR VALUE",message[1]);
897 strcpy(command,
"GET BG BASE IR VALUES");
902 strcpy(command,
"GET IL BASE IR VALUES");
907 strcpy(command,
"CALCULATE BASE IR VALUES");
912 strcpy(command,
"CALCULATE SIDE IR VALUES");
917 strcpy(command,
"START FILE TRANSFER MODE");
920 IF_start_file_transfer_mode();
921 sprintf(ret_message,
"OK");
923 }
else command_status = 2;
926 strcpy(command,
"GET FIRMWARE VERSION");
927 sprintf(ret_message,
"%1.2f",firmware_version);
931 strcpy(command,
"GET SERIAL NUMBER");
932 sprintf(ret_message,
"%2.3f",serial_number);
936 strcpy(command,
"GET HAS SIDE IR");
937 sprintf(ret_message,
"%d",has_side_ir);
941 strcpy(command,
"GET HAS BASE IR");
942 sprintf(ret_message,
"%d",has_base_ir);
946 strcpy(command,
"GET HAS ENCODERS");
947 sprintf(ret_message,
"%d",has_wheel_encoders);
951 strcpy(command,
"GET HAS AUDIO");
952 sprintf(ret_message,
"%d",has_audio_pic);
956 strcpy(command,
"GET HAS RECHARGING");
957 sprintf(ret_message,
"%d",has_recharging_circuit);
961 strcpy(command,
"GET HAS COMPASS");
962 sprintf(ret_message,
"%d",has_compass);
966 strcpy(command,
"GET HAS ULTRASONIC");
967 sprintf(ret_message,
"%d",has_ultrasonic_sensor);
971 strcpy(command,
"GET HAS TEMPERATURE");
972 sprintf(ret_message,
"%d",has_temperature_sensor);
976 strcpy(command,
"GET HAS BASE COLOUR");
977 sprintf(ret_message,
"%d",has_base_colour_sensor);
981 strcpy(command,
"GET HAS TOP COLOUR");
982 sprintf(ret_message,
"%d",has_top_colour_sensor);
986 strcpy(command,
"GET HAS RADIO");
987 sprintf(ret_message,
"%d",has_433_radio);
991 strcpy(command,
"GET FIRMWARE H-DESC");
994 if(has_side_ir)byte0+=128;
995 if(has_base_ir)byte0+=64;
996 if(has_wheel_encoders)byte0+=32;
997 if(has_audio_pic)byte0+=16;
998 if(has_recharging_circuit)byte0+=8;
999 if(has_compass)byte0+=4;
1000 if(has_ultrasonic_sensor)byte0+=2;
1001 if(has_temperature_sensor)byte0+=1;
1002 if(has_base_colour_sensor)byte1+=128;
1003 if(has_top_colour_sensor)byte1+=64;
1004 if(has_433_radio)byte1+=32;
1005 sprintf(ret_message,
"%c%c",byte0,byte1);
1010 strcpy(command,
"GET PCB VERSION");
1011 sprintf(ret_message,
"%1.2f",pcb_version);
1018 char message_length = strlen(ret_message);
1021 pc.printf(
"%c%c%s",RESPONSE_MESSAGE_BYTE,message_length,ret_message);
1024 bt.printf(
"%c%c%s",RESPONSE_MESSAGE_BYTE,message_length,ret_message);
1027 psi.
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);
1031 pc.printf(
"%c%c",ACKNOWLEDGE_MESSAGE_BYTE,command_status);
1034 bt.printf(
"%c%c",ACKNOWLEDGE_MESSAGE_BYTE,command_status);
1037 switch(command_status) {
1039 psi.
debug(
"Unrecognised %s command message [%02x%02x%02x]\n",iface,message[0],message[1],message[2]);
1042 psi.
debug(
"Actioned %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]);
1045 psi.
debug(
"Blocked %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]);
1048 psi.
debug(
"Invalid %s command message:%s %s [%02x%02x%02x]\n",iface, command, subcommand,message[0],message[1],message[2]);
1054 char * SerialControl::IF_nibble_to_binary_char(
char in)
1056 char * ret = (
char*)malloc(
sizeof(
char)*5);
1057 for(
int i=0; i<4; i++) {
1058 if(in & (128 >> i)) ret[i]=
'1';
1065 char * SerialControl::IF_char_to_binary_char(
char in)
1067 char * ret = (
char*)malloc(
sizeof(
char)*9);
1068 for(
int i=0; i<8; i++) {
1069 if(in & (128 >> i)) ret[i]=
'1';
1076 float SerialControl::IF_decode_unsigned_float(
char byte0,
char byte1)
1078 unsigned short sval = (byte0) << 8;
1080 float scaled = sval / 65535.0f;
1084 float SerialControl::IF_decode_float(
char byte0,
char byte1)
1087 char sign = byte0 / 128;
1088 short sval = (byte0 % 128) << 8;
1090 float scaled = sval / 32767.0f;
1091 if(sign == 0) scaled = 0-scaled;
1095 float SerialControl::IF_decode_unsigned_float(
char byte0)
1097 unsigned short sval = (byte0);
1098 float scaled = sval / 255.0f;
1102 float SerialControl::IF_decode_float(
char byte0)
1105 char sign = byte0 / 128;
1106 short sval = (byte0 % 128);
1107 float scaled = sval / 127.0f;
1108 if(sign == 0) scaled = 0-scaled;
1113 void SerialControl::IF_pc_rx_command_timeout()
1115 char message_array[6];
1116 char length = 1 + pc_command_message_byte;
1117 pc_command_message_started = 0;
1118 message_array[0] = COMMAND_MESSAGE_BYTE;
1119 for(
int k=0; k<pc_command_message_byte; k++) {
1120 message_array[k+1] = pc_command_message[k];
1122 IF_handle_user_serial_message(message_array, length, 0);
1125 void SerialControl::IF_bt_rx_command_timeout()
1127 char message_array[6];
1128 char length = 1 + bt_command_message_byte;
1129 bt_command_message_started = 0;
1130 message_array[0] = COMMAND_MESSAGE_BYTE;
1131 for(
int k=0; k<bt_command_message_byte; k++) {
1132 message_array[k+1] = bt_command_message[k];
1134 IF_handle_user_serial_message(message_array, length, 1);
1137 void SerialControl::IF_pc_rx_callback()
1140 char message_array[255];
1142 while(pc.readable()) {
1143 char tc = pc.getc();
1144 message_array[count] = tc;
1146 if(pc_command_message_started == 1) {
1147 if(pc_command_message_byte == 3) {
1148 pc_command_timeout.detach();
1149 if(tc == COMMAND_MESSAGE_BYTE) {
1151 pc_command_message_started = 0;
1153 IF_handle_command_serial_message(pc_command_message , 0);
1156 pc_command_message_started = 0;
1157 message_array[0] = COMMAND_MESSAGE_BYTE;
1158 message_array[1] = pc_command_message[0];
1159 message_array[2] = pc_command_message[1];
1160 message_array[3] = pc_command_message[2];
1161 message_array[4] = tc;
1165 pc_command_message[pc_command_message_byte] = tc;
1166 pc_command_message_byte ++;
1170 if(tc == COMMAND_MESSAGE_BYTE) {
1171 pc_command_timeout.attach(
this,&SerialControl::IF_pc_rx_command_timeout,command_timeout_period);
1172 pc_command_message_started = 1;
1173 pc_command_message_byte = 0;
1179 if(!pc_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 0);
1182 Timeout bt_message_timeout;
1184 char bt_buffer[255];
1185 int bt_buffer_index = 0;
1187 void SerialControl::IF_bt_message_timeout()
1191 sprintf(buffer, bt_buffer, bt_buffer_index);
1192 buffer[bt_buffer_index] = 0;
1193 if(file_transfer_mode == 1) {
1194 IF_handle_file_transfer_serial_message(bt_buffer, bt_buffer_index, 1);
1197 if(bt_buffer_index == 5 && buffer[0] == COMMAND_MESSAGE_BYTE && buffer[4] == COMMAND_MESSAGE_BYTE) {
1198 bt_command_message[0] = buffer[1];
1199 bt_command_message[1] = buffer[2];
1200 bt_command_message[2] = buffer[3];
1201 IF_handle_command_serial_message(bt_command_message , 1);
1202 }
else IF_handle_user_serial_message(bt_buffer, bt_buffer_index, 1);
1204 bt_buffer_index = 0;
1221 void SerialControl::IF_set_filename(
char * filename_in)
1223 strcpy(filename,filename_in);
1226 unsigned short SerialControl::IF_calculateCRC16(
int file_length)
1228 unsigned short crc16table[256] = {
1229 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
1230 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
1231 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
1232 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
1233 0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
1234 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
1235 0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
1236 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
1237 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
1238 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
1239 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
1240 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
1241 0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
1242 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
1243 0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
1244 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
1245 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
1246 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
1247 0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
1248 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
1249 0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
1250 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
1251 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
1252 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
1253 0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
1254 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
1255 0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
1256 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
1257 0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
1258 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
1259 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
1260 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
1264 unsigned short crc_value = 0;
1265 FILE *fp = fopen(filename,
"r");
1268 if(file_length < 512) limit = file_length;
1269 buffer = (
char*) malloc (
sizeof(
char)*limit);
1271 if(file_length > limit) blocks += file_length / limit;
1272 for(
int i=0; i<blocks; i++) {
1274 int blocksize = limit;
1276 if((file_length % limit) != 0) blocksize = file_length % limit;
1278 psi.
debug(
"Calculating %d bytes of CRC data...\n",blocksize);
1280 result = fread(buffer,1,blocksize,fp);
1281 psi.
debug(
"Data read: %d\n",result);
1282 for(
int j=0; j<blocksize; j++) {
1283 int subindex = ((crc_value>>8)^*(
char *)(buffer[j]))&0x00FF;
1285 unsigned short table_value = crc16table[subindex];
1286 crc_value=(crc_value<<8)^table_value;
1290 psi.
debug(
"CRC Calculated: %x\n",crc_value);
1294 void SerialControl::IF_bt_rx_callback()
1297 char message_array[255];
1301 while(bt.readable()) {
1302 char tc = bt.getc();
1303 message_array[count] = tc;
1305 if(bt_command_message_started == 1) {
1306 if(bt_command_message_byte == 3) {
1307 bt_command_timeout.detach();
1308 if(tc == COMMAND_MESSAGE_BYTE) {
1310 bt_command_message_started = 0;
1312 IF_handle_command_serial_message(bt_command_message , 1);
1315 bt_command_message_started = 0;
1316 message_array[0] = COMMAND_MESSAGE_BYTE;
1317 message_array[1] = bt_command_message[0];
1318 message_array[2] = bt_command_message[1];
1319 message_array[3] = bt_command_message[2];
1320 message_array[4] = tc;
1324 bt_command_timeout.attach(
this,&SerialControl::IF_bt_rx_command_timeout,command_timeout_period);
1325 bt_command_message[bt_command_message_byte] = tc;
1326 bt_command_message_byte ++;
1330 if(tc == COMMAND_MESSAGE_BYTE) {
1331 bt_command_message_started = 1;
1332 bt_command_message_byte = 0;
1338 if(!bt_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 1);
void write_string(char *message)
void store_illuminated_raw_ir_values(void)
void store_background_raw_ir_values(void)
unsigned short calculate_side_ir_value(char index)
unsigned short calculate_base_ir_value(char index)
void set_leds(char green, char red)
void set_center_led_brightness(float brightness)
#define ENABLE_BLUETOOTH
Enable if the BlueSmirf module is being used. 0=off 1=on.
void set_position(char row, char column)
void set_center_led(char state)
void brake_right_motor(void)
void store_ir_values(void)
unsigned short get_led_states(void)
void set_base_led(char state)
void setup_serial_interfaces(void)
unsigned short get_background_base_ir_value(char index)
void store_background_base_ir_values(void)
void set_backlight_brightness(float brightness)
unsigned short get_illuminated_base_ir_value(char index)
void set_left_motor_speed(float speed)
void set_led(char led, char state)
unsigned short get_background_raw_ir_value(char index)
void set_green_leds(char green)
void store_base_ir_values(void)
void brake_left_motor(void)
void pause_user_code(float period)
void store_illuminated_base_ir_values(void)
void debug(const char *format,...)
void set_right_motor_speed(float speed)
void forward(float speed)
void reset_encoders(void)
unsigned short get_illuminated_raw_ir_value(char index)
void blink_leds(float timeout)
void set_red_leds(char red)