Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Files at this revision

API Documentation at this revision

Comitter:
AntonLS
Date:
Sun Apr 03 08:56:32 2016 +0000
Parent:
74:9257d83bd36c
Child:
76:6638d83939d5
Commit message:
Prevent repeated BAD CRC on ACK msg. Response added for u beep cmd to prevent BT buffer problem. New radio-send flush methods.

Changed in this revision

PhoneAppIO.cpp Show annotated file Show diff for this revision Revisions of this file
Radio.cpp Show annotated file Show diff for this revision Revisions of this file
TA.cpp Show annotated file Show diff for this revision Revisions of this file
TA.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
proto_code.cpp Show annotated file Show diff for this revision Revisions of this file
types.h Show annotated file Show diff for this revision Revisions of this file
--- a/PhoneAppIO.cpp	Wed Mar 02 03:32:40 2016 +0000
+++ b/PhoneAppIO.cpp	Sun Apr 03 08:56:32 2016 +0000
@@ -47,7 +47,7 @@
                                                                                            //  but they might not be needed here.
         }
 
-        bleP->readCharacteristicValue( toConeHandle, buf, &bytesRead );
+        ble_error_t err = bleP->readCharacteristicValue( toConeHandle, buf, &bytesRead );
 
         // Copy data to accessible location for others and handleRead().
         memset( txPayload, 0, TXRX_BUF_LEN );
@@ -117,13 +117,14 @@
 
 void PhoneAppIO::handleRead()
 {
-    while( reading );  // Block.
+    //// while( reading );  // Block.
 
     int charsToBuf = rxBuffer.write( data, bytesRead );
     if( charsToBuf != bytesRead )
     {
         ::printf( "[ERROR] ToPhoneCharacteristic Data Lost, tried %d, got %d\r\n", bytesRead, charsToBuf );
     }
+    DEBUG( "Got:%s", data );
 }
 
 /** This method used to transfer data from the internal write buffer
@@ -137,7 +138,7 @@
 }
 
 // I think we don't need to use these because I'm guessing
-//  all reads happen during main loop ble.waitForEvent()?
+//  all reads happen during main loop ble.waitForEvent()?  -- Not using waitForEvent() now.
 
 // For locking out read() during write() to prevent buffer state corruption.
 void PhoneAppIO::disableTxIrq(){  writing = true;   }
--- a/Radio.cpp	Wed Mar 02 03:32:40 2016 +0000
+++ b/Radio.cpp	Sun Apr 03 08:56:32 2016 +0000
@@ -343,7 +343,7 @@
 ////bool retval = radio.ACKReceived(cone);
     bool retval = radio.receiveDone();
 
-    if( !get_crc_ok() )
+    if( retval && !get_crc_ok() )
     {
         writeToPhone( "BAD-CRC on ACK wait\r\n" );
         retval = false;
--- a/TA.cpp	Wed Mar 02 03:32:40 2016 +0000
+++ b/TA.cpp	Sun Apr 03 08:56:32 2016 +0000
@@ -412,7 +412,7 @@
 
 #define MAXSENDTRIES  15
 
-void TA::spin(void)
+bool TA::spin(void)
 {
   static byte payload [6];
   static bool message_in_queue = false;
@@ -638,7 +638,7 @@
           message_in_queue = true;
         }
        */
-        while( (send_buffer.getSize() > 4) && !message_in_queue )
+        while( (send_buffer.getSize() > 6 /* 4 */) && !message_in_queue )
         {
           payload[0] = send_buffer.get();
           payload[1] = send_buffer.get();
@@ -672,6 +672,8 @@
         }
       }
   radio_ensure_rx_mode();  //// Make sure we don't stay in standby mode.
+
+  return  message_in_queue;
 }
 
 bool TA::activated(void)
--- a/TA.h	Wed Mar 02 03:32:40 2016 +0000
+++ b/TA.h	Sun Apr 03 08:56:32 2016 +0000
@@ -125,7 +125,7 @@
 void send_immediate(Message *m);
 bool sendRaw(uint8_t *message, uint8_t len, uint8_t cone);
 bool recieve(Message *m);
-void spin(void);
+bool spin(void);
 bool activated(void);
 void resetTouch();
 bool tripped(void);
--- a/main.cpp	Wed Mar 02 03:32:40 2016 +0000
+++ b/main.cpp	Sun Apr 03 08:56:32 2016 +0000
@@ -188,7 +188,7 @@
 // mts::MTSSerialFlowControl pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
 //mts::MTSSerial pcfc( (trSwp ? SERRX : SERTX), (trSwp ? SERTX : SERRX), 128, 64, RTS_PIN_NUMBER, NC );  // 256, 1280  // 256, 2560
 
-uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
+uint8_t txPayload[TXRX_BUF_LEN +1] = { 0 };
 
 
 char deviceName[6];  // "TAF00";
@@ -349,7 +349,7 @@
     ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead );
 
     if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
-        (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
+        (err == BLE_ERROR_PARAM_OUT_OF_RANGE) )
     {
       // pcfc.printf( "\r\nBLE %d!  ", err );
 
--- 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';
 
--- a/types.h	Wed Mar 02 03:32:40 2016 +0000
+++ b/types.h	Sun Apr 03 08:56:32 2016 +0000
@@ -1,7 +1,7 @@
 #ifndef TYPES_H
 #define TYPES_H
 
- #define D
+// #define D
 // #define D_RA
 
 #ifdef D
@@ -41,8 +41,8 @@
     uint8_t cone;
 };
 
- #define MASTER
-//#define SLAVE
+#define MASTER
+// #define SLAVE
 
 // these must be here because of magic in the Arduino IDE (otherwise functions cannot retun these types