Bluetooth communication for flocking.

Dependencies:   mbed

Fork of BeautifulMemeProject by James Hilder

Revision:
18:5921c1853e8a
Parent:
17:da524989b637
Child:
20:e08376c0b4ea
--- a/PsiSwarm/serial.cpp	Thu Jan 07 17:58:07 2016 +0000
+++ b/PsiSwarm/serial.cpp	Fri Jan 08 19:10:52 2016 +0000
@@ -20,106 +20,52 @@
 char bt_command_message_started = 0;
 char bt_command_message_byte = 0;
 char bt_command_message[3];
-char bt_single_byte_received = 0;
-char bt_single_byte = 0;
-
 
 char allow_commands = 1;
 char allow_requests = 1;
 
 Timeout pc_command_timeout;
 Timeout bt_command_timeout;
-//Timeout bt_single_byte_timeout;
-Timeout partial_message_timeout;
-
-
 
 // A predefined message structure for command messages is as follows:
 // [Byte 0][Byte 1][Byte 2][Byte 3][Byte 4]
 // Byte 0 and Byte 4 must be equal to COMMAND_MESSAGE_BYTE [in psiswarm.h] or message is treated as a user message
 
-
 void handle_user_serial_message(char * message, char length, char interface)
 {
-    //This is where user code for handling a (non-system) serial message should go
-    //By default, nothing is done
+    // This is where user code for handling a (non-system) serial message should go
     //
-    //message = pointer to message char array
-    //length = length of message
-    //interface = 0 for PC serial connection, 1 for Bluetooth
-//    if(interface){
-//    char my_message [128];
-//    strncpy(my_message, message, length);
-//    pc.printf("BT message recieved:%s \n
-//    }
-}
-
-char partial_message_received = 0;
-int partial_message_length = 0;
-char partial_message[255];
-char partial_message_interface = 0;
-
-void IF_partial_message_timeout()
-{
-    partial_message_received = 0;
+    // message = pointer to message char array
+    // length = length of message
+    // interface = 0 for PC serial connection, 1 for Bluetooth
     
-    char buffer[255];
-    sprintf(buffer, partial_message, partial_message_length);
-    buffer[partial_message_length] = 0;
-        
-    if(partial_message_interface)
-        debug("Received BT timed-out message: %s [%d chars]\n", buffer, partial_message_length);
-    else
-        debug("Received USB timed-out message: %s [%d chars]\n", buffer, partial_message_length);
-        
-    handle_user_serial_message(partial_message, partial_message_length, partial_message_interface);
+    if(interface)
+    {
+        if(length == 8)
+        {        
+            // Convert 360 to 255 values!
+            for(int i = 0; i < length; i++)
+            {
+                flocking_headings[i] = message[i];
+                debug("%d, ", message[i]);
+            }
+            
+            debug("\n");
+        }
+    }
 }
 
 void IF_handle_user_serial_message(char * message, char length, char interface)
 {
-    if(partial_message_received == 0)
-    {
-        wait_ms(500); // 0.5 ms delay
-        
-        partial_message_received = 1;
-        partial_message_interface = interface;
-        
-        for(int i = 0; i < length; i++)
-            partial_message[i] = message[i];
-            
-        partial_message_length = length;
-        partial_message_timeout.attach(&IF_partial_message_timeout, 0.1);
+    char buffer[255];
+    sprintf(buffer,message,length);
+    for(int i=0; i<length; i++) {
+        buffer[i]=message[i];
     }
-    else
-    {        
-        partial_message_timeout.detach();
-        partial_message_received = 0;
-        
-        char combined_message[255];
-        
-        for(int i = 0; i < partial_message_length; i++)
-            combined_message[i] = partial_message[i];
-        
-        int index = 0;
-        int combined_length = partial_message_length + length;
-        
-        for(int i = partial_message_length; i < combined_length; i++)
-        {
-            combined_message[i] = message[index];
-            index++;
-        }
-        
-        char buffer[255];
-        sprintf(buffer, combined_message, combined_length);
-        buffer[combined_length] = 0;
-        
-        if(interface)
-            debug("Received BT message: %s [%d chars]\n", buffer, combined_length);
-        else
-            debug("Received USB message: %s [%d chars]\n", buffer, combined_length);
-        
-        handle_user_serial_message(combined_message, combined_length, interface);
-    }
+    buffer[length]=0;
+    if(interface) debug("Received BT message:%s [%d chars]\n",buffer,length);
+    else debug("Received USB message:%s [%d chars]\n",buffer,length);
+    handle_user_serial_message(message,length,interface);
 }
 
 void IF_handle_command_serial_message(char message[3], char interface)
@@ -884,19 +830,6 @@
     IF_handle_user_serial_message(message_array, length, 1);
 }
 
-void IF_bt_rx_single_byte_timeout()
-{
-    if(bt_single_byte_received == 1)
-    {    
-        bt_single_byte_received = 0;
-        
-        char message_array[1];
-        message_array[0] = bt_single_byte;
-        
-        IF_handle_user_serial_message(message_array, 1, 1);
-    }
-}
-
 void IF_pc_rx_callback()
 {
     int count = 0;
@@ -941,77 +874,49 @@
     }
     if(!pc_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 0);
 }
-
 void IF_bt_rx_callback()
 {
-    int count = 0;
-    char message_array[255];
-
-    while(bt.readable()) {
-        char tc = bt.getc();
-        message_array[count] = tc;
-        count ++;
-        if(bt_command_message_started == 1) {
-            if(bt_command_message_byte == 3) {
-                bt_command_timeout.detach();
-                if(tc == COMMAND_MESSAGE_BYTE) {
-                    // A complete command message succesfully received, call handler
-                    bt_command_message_started = 0;
-                    count = 0;
-                    IF_handle_command_serial_message(bt_command_message , 1);
-                } else {
-                    // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message
-                    bt_command_message_started = 0;
-                    message_array[0] = COMMAND_MESSAGE_BYTE;
-                    message_array[1] = bt_command_message[0];
-                    message_array[2] = bt_command_message[1];
-                    message_array[3] = bt_command_message[2];
-                    message_array[4] = tc;
-                    count = 5;
-                }
-            } else {
-                bt_command_timeout.attach(&IF_bt_rx_command_timeout,command_timeout_period);
-                bt_command_message[bt_command_message_byte] = tc;
-                bt_command_message_byte ++;
-            }
-        } else {
-            if(count == 1) {
-                if(tc == COMMAND_MESSAGE_BYTE) {
-                    bt_command_message_started = 1;
-                    bt_command_message_byte = 0;
-
-                }
-            }
-        }
-    }
-    if(!bt_command_message_started && count > 0)
-        IF_handle_user_serial_message(message_array, count, 1);
-//    {
-////        debug("USER MESSAGE\n");
-//        
-//        if(count == 1)
-//        {
-////            debug("SINGLE BYTE RECEIVED\n");
-//            bt_single_byte_received = 1;
-//            bt_single_byte = message_array[0];
-////            bt_single_byte_timeout.attach(&IF_bt_rx_single_byte_timeout, 1);
-//        }
-//        else
-//        {
-//            if(bt_single_byte_received == 1)
-//            {
-////                debug("COMBINING MESSAGES\n");
-////                bt_single_byte_timeout.detach();            
-//                bt_single_byte_received = 0;
-//                
-//                for(int i = count; i > 0; i--)
-//                    message_array[i] = message_array[i-1];
-//                
-//                message_array[0] = bt_single_byte;
-//                count++;
+//    int count = 0;
+//    char message_array[255];
+//    
+//    wait_ms(500); // Wait 0.5ms to allow a complete message to arrive before atttempting to process it
+//
+//    while(bt.readable()) {
+//        char tc = bt.getc();
+//        message_array[count] = tc;
+//        count ++;
+//        if(bt_command_message_started == 1) {
+//            if(bt_command_message_byte == 3) {
+//                bt_command_timeout.detach();
+//                if(tc == COMMAND_MESSAGE_BYTE) {
+//                    // A complete command message succesfully received, call handler
+//                    bt_command_message_started = 0;
+//                    count = 0;
+//                    IF_handle_command_serial_message(bt_command_message , 1);
+//                } else {
+//                    // Message is not a valid command message as 5th byte is not correct; treat whole message as a user message
+//                    bt_command_message_started = 0;
+//                    message_array[0] = COMMAND_MESSAGE_BYTE;
+//                    message_array[1] = bt_command_message[0];
+//                    message_array[2] = bt_command_message[1];
+//                    message_array[3] = bt_command_message[2];
+//                    message_array[4] = tc;
+//                    count = 5;
+//                }
+//            } else {
+//                bt_command_timeout.attach(&IF_bt_rx_command_timeout,command_timeout_period);
+//                bt_command_message[bt_command_message_byte] = tc;
+//                bt_command_message_byte ++;
 //            }
-//            
-//            IF_handle_user_serial_message(message_array, count, 1);
+//        } else {
+//            if(count == 1) {
+//                if(tc == COMMAND_MESSAGE_BYTE) {
+//                    bt_command_message_started = 1;
+//                    bt_command_message_byte = 0;
+//
+//                }
+//            }
 //        }
 //    }
+//    if(!bt_command_message_started && count>0) IF_handle_user_serial_message(message_array, count, 1);
 }
\ No newline at end of file