Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
75:1b357bee1839
Parent:
72:84365ba93d6b
--- a/proto_code.cpp	Wed Mar 02 03:32:40 2016 +0000
+++ b/proto_code.cpp	Sun Apr 03 08:56:32 2016 +0000
@@ -108,6 +108,8 @@
 patternState_t stateFromCone(uint8_t cone);
 void printSplit(unsigned long timer);
 uint8_t getRandomCone(void);
+void flushSend(void);          ////
+void forceSend( Message *m );  ////
 void clearCones(void);
 void powerupCones(uint8_t sound);
 void failCones(void);
@@ -949,9 +951,7 @@
     break;
   case 'u':
     // let any pending messages clear
-    while(ta.get_buffer_size()){
-      ta.spin();
-    }
+    flushSend();
     if(value == 1){
       writeToPhone("Course leader!\r\n");
       powerupCones(value);
@@ -963,6 +963,7 @@
       //ta.powerup(value);
     }
     if(value > 10 && value < 5000){
+      writeToPhone( "Beep %ums\r\n", value );  // Response added to prevent Bluetooth buffer problem - 20160402  ALS
       ta.beep(value);
     }
     break;
@@ -1056,6 +1057,24 @@
   return  cone;
 }
 
+static void flushSend()  ////
+{
+    // let any pending messages clear
+    while( ta.get_buffer_size() )
+      ta.spin();
+
+    while( ta.spin() ) ;  //// Wait for any pending ACKs.
+}
+
+static void forceSend( Message *m )  ////
+{
+    if( !ta.send( m ) )
+    {
+        flushSend();
+        ta.send( m );
+    }
+}
+
 static void clearCones(void)
 {
   uint8_t i;
@@ -1065,10 +1084,12 @@
   {
     m->cone = i;
     if(active_cones[i])
-        ta.send(m);
+        forceSend( m );  //// ta.send(m);
 ///    active_cones[i] = false;
     //ta.send("q", i);
   }
+
+  flushSend();  ////
   
   DEBUG("sent clear\r\n"); 
 }
@@ -1114,14 +1135,11 @@
     m->cone = i;
     
     if(active_cones[i])
-        ta.send(m);
+        forceSend( m );  //// ta.send(m);
     //ta.send("f", i);
   }
-  
-  while(ta.get_buffer_size())
-  {
-    ta.spin();
-  }
+
+  flushSend();
   
   //DEBUG("sent fail\n");
 }
@@ -1151,26 +1169,19 @@
   {
     m->cone = i;
     if(active_cones[i])
-        ta.send(m);
+        forceSend( m );  //// ta.send(m);
     //ta.send("s", i);
   }
   
-  while(ta.get_buffer_size())
-  {
-    ta.spin();
-  }
+  flushSend();
 }
 
 static void find_cones(void)
 {
   const int TRIES = 6;
 
-  while(ta.get_buffer_size())
-  {
-    ta.spin(); // wait for all messages to leave queue
-  }
+  flushSend();  //// wait for all messages to leave queue
   
-  ta.beep_off();
   uint8_t i;
   m->command = 'z';
   
@@ -1229,12 +1240,8 @@
 {
   const int TRIES = 6;
 
-  while(ta.get_buffer_size())
-  {
-    ta.spin(); // wait for all messages to leave queue
-  }
+  flushSend();  ////  wait for all messages to leave queue
 
-  ta.beep_off();
   uint8_t i;
   m->command = 'z';