C++ Library for the PsiSwarm Robot - Version 0.8
Dependents: PsiSwarm_V8_Blank_CPP Autonomia_RndmWlk
Fork of PsiSwarmV7_CPP by
Diff: serial.cpp
- Revision:
- 11:312663037b8c
- Parent:
- 10:e58323951c08
- Child:
- 12:878c6e9d9e60
--- a/serial.cpp Sun Oct 16 16:00:20 2016 +0000 +++ b/serial.cpp Sun Oct 16 21:06:15 2016 +0000 @@ -1,4 +1,4 @@ -/* University of York Robotics Laboratory PsiSwarm Library: Serial Control Source File +/* University of York Robotics Laboratory PsiSwarm Library: SerialControlControlControlControlControl Control Source File * * Copyright 2016 University of York * @@ -51,9 +51,20 @@ // Byte 0 and Byte 4 must be equal to COMMAND_MESSAGE_BYTE [in psiswarm.h] or message is treated as a user message +void SerialControl::setup_serial_interfaces() +{ + if(ENABLE_PC_SERIAL) { + pc.baud(PC_BAUD); + pc.attach(this,&SerialControl::IF_pc_rx_callback, Serial::RxIrq); + } + if(ENABLE_BLUETOOTH) { + bt.baud(BLUETOOTH_BAUD); + bt.attach(this,&SerialControl::IF_bt_rx_callback, Serial::RxIrq); + } +} -void IF_start_file_transfer_mode() +void SerialControl::IF_start_file_transfer_mode() { display.clear_display(); display.set_position(0,0); @@ -66,11 +77,11 @@ file_length = 0; user_code_restore_mode = user_code_running; user_code_running = 0; - ft_timeout.attach(IF_file_transfer_timeout,2.0); + ft_timeout.attach(this,&SerialControl::IF_file_transfer_timeout,2.0); } -void IF_invalid_transfer(void) +void SerialControl::IF_invalid_transfer(void) { debug("File transfer failed\n"); if(data_written == 1) { @@ -84,7 +95,7 @@ IF_end_file_transfer_mode(); } -void IF_file_transfer_timeout(void) +void SerialControl::IF_file_transfer_timeout(void) { debug("File transfer failed: timeout\n"); display.clear_display(); @@ -94,7 +105,7 @@ IF_end_file_transfer_mode(); } -void IF_end_file_transfer_mode(void) +void SerialControl::IF_end_file_transfer_mode(void) { display.clear_display(); file_transfer_mode = 0; @@ -102,7 +113,7 @@ } -void IF_handle_file_transfer_serial_message(char * message, char length, char interface) +void SerialControl::IF_handle_file_transfer_serial_message(char * message, char length, char interface) { // Code for handling a serial (Bluetooth) message when in file-transfer mode // @@ -127,7 +138,7 @@ debug("Target filename:%s\n",filename); //Send acknowledge ("FN") ft_timeout.detach(); - ft_timeout.attach(IF_file_transfer_timeout,2.0); + ft_timeout.attach(this,&SerialControl::IF_file_transfer_timeout,2.0); bt.printf("%c%c%s",RESPONSE_MESSAGE_BYTE,2,"FN"); file_transfer_state = 1; } @@ -160,7 +171,7 @@ //file_data = (char *) malloc(target_size); debug("File size %d bytes (%d blocks of %d bytes)\n",file_length,final_block,block_size); ft_timeout.detach(); - ft_timeout.attach(IF_file_transfer_timeout,1.0); + ft_timeout.attach(this,&SerialControl::IF_file_transfer_timeout,1.0); //Send acknowledge (size of file) bt.printf("%c%c%c%c%c",RESPONSE_MESSAGE_BYTE,3,message[1],message[2],message[3]); } @@ -198,7 +209,7 @@ debug("Message packet %d received and written\n",block_index); //Send acknowledge ("D") ft_timeout.detach(); - ft_timeout.attach(IF_file_transfer_timeout,1.0); + ft_timeout.attach(this,&SerialControl::IF_file_transfer_timeout,1.0); bt.printf("%c%c%s",RESPONSE_MESSAGE_BYTE,1,"D"); } else { //Last data block written @@ -226,7 +237,7 @@ } -void IF_handle_user_serial_message(char * message, char length, char interface) +void SerialControl::IF_handle_user_serial_message(char * message, char length, char interface) { char buffer[255]; sprintf(buffer,message,length); @@ -239,7 +250,7 @@ handle_user_serial_message(message,length,interface); } -void IF_handle_command_serial_message(char message[3], char interface) +void SerialControl::IF_handle_command_serial_message(char message[3], char interface) { char iface [4]; if(interface) strcpy(iface,"BT"); @@ -1040,7 +1051,7 @@ } } -char * IF_nibble_to_binary_char(char in) +char * SerialControl::IF_nibble_to_binary_char(char in) { char * ret = (char*)malloc(sizeof(char)*5); for(int i=0; i<4; i++) { @@ -1051,7 +1062,7 @@ return ret; } -char * IF_char_to_binary_char(char in) +char * SerialControl::IF_char_to_binary_char(char in) { char * ret = (char*)malloc(sizeof(char)*9); for(int i=0; i<8; i++) { @@ -1062,7 +1073,7 @@ return ret; } -float IF_decode_unsigned_float(char byte0, char byte1) +float SerialControl::IF_decode_unsigned_float(char byte0, char byte1) { unsigned short sval = (byte0) << 8; sval += byte1; @@ -1070,7 +1081,7 @@ return scaled; } -float IF_decode_float(char byte0, char byte1) +float SerialControl::IF_decode_float(char byte0, char byte1) { // MSB is byte 0 is sign, rest is linear spread between 0 and 1 char sign = byte0 / 128; @@ -1081,14 +1092,14 @@ return scaled; } -float IF_decode_unsigned_float(char byte0) +float SerialControl::IF_decode_unsigned_float(char byte0) { unsigned short sval = (byte0); float scaled = sval / 255.0f; return scaled; } -float IF_decode_float(char byte0) +float SerialControl::IF_decode_float(char byte0) { // MSB is byte 0 is sign, rest is linear spread between 0 and 1 char sign = byte0 / 128; @@ -1098,19 +1109,8 @@ return scaled; } -void IF_setup_serial_interfaces() -{ - if(ENABLE_PC_SERIAL) { - pc.baud(PC_BAUD); - pc.attach(&IF_pc_rx_callback, Serial::RxIrq); - } - if(ENABLE_BLUETOOTH) { - bt.baud(BLUETOOTH_BAUD); - bt.attach(&IF_bt_rx_callback, Serial::RxIrq); - } -} -void IF_pc_rx_command_timeout() +void SerialControl::IF_pc_rx_command_timeout() { char message_array[6]; char length = 1 + pc_command_message_byte; @@ -1122,7 +1122,7 @@ IF_handle_user_serial_message(message_array, length, 0); } -void IF_bt_rx_command_timeout() +void SerialControl::IF_bt_rx_command_timeout() { char message_array[6]; char length = 1 + bt_command_message_byte; @@ -1134,7 +1134,7 @@ IF_handle_user_serial_message(message_array, length, 1); } -void IF_pc_rx_callback() +void SerialControl::IF_pc_rx_callback() { int count = 0; char message_array[255]; @@ -1168,7 +1168,7 @@ } else { if(count == 1) { if(tc == COMMAND_MESSAGE_BYTE) { - pc_command_timeout.attach(&IF_pc_rx_command_timeout,command_timeout_period); + pc_command_timeout.attach(this,&SerialControl::IF_pc_rx_command_timeout,command_timeout_period); pc_command_message_started = 1; pc_command_message_byte = 0; @@ -1184,7 +1184,7 @@ char bt_buffer[255]; int bt_buffer_index = 0; -void IF_bt_message_timeout() +void SerialControl::IF_bt_message_timeout() { char buffer[255]; @@ -1218,12 +1218,12 @@ //} -void IF_set_filename(char * filename_in) +void SerialControl::IF_set_filename(char * filename_in) { strcpy(filename,filename_in); } -unsigned short IF_calculateCRC16(int file_length) +unsigned short SerialControl::IF_calculateCRC16(int file_length) { unsigned short crc16table[256] = { 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, @@ -1291,7 +1291,7 @@ return crc_value; } -void IF_bt_rx_callback() +void SerialControl::IF_bt_rx_callback() { int count = 0; char message_array[255]; @@ -1321,7 +1321,7 @@ count = 5; } } else { - bt_command_timeout.attach(&IF_bt_rx_command_timeout,command_timeout_period); + bt_command_timeout.attach(this,&SerialControl::IF_bt_rx_command_timeout,command_timeout_period); bt_command_message[bt_command_message_byte] = tc; bt_command_message_byte ++; }