football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
10:72ceef287b0f
Parent:
9:95dc84e9fb7f
Child:
11:d3aa5fca2330
--- a/main.cpp	Sun Apr 19 19:59:46 2015 +0000
+++ b/main.cpp	Tue Apr 21 07:00:55 2015 +0000
@@ -27,6 +27,7 @@
 #include "DeviceInformationService.h"
 
 #include "MTSSerialFlowControl.h"
+#include "Vars.h"
 #include "PhoneAppIO.h"
 
 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
@@ -73,7 +74,7 @@
 static uint8_t rx_len = 0;
 
 
-DigitalOut led1( LED1 );
+DigitalOut _led1( LED1 );
 // DigitalOut rts( RTS_PIN_NUMBER );
 
 
@@ -110,7 +111,7 @@
     if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
         (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
     {
-            pcfc.printf( "\r\nBLE %d!  ", err );
+        pcfc.printf( "\r\nBLE %d!  ", err );
 
     } else if ( err == BLE_STACK_BUSY )
       {
@@ -124,7 +125,7 @@
     if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
     {
         uint8_t buf[TXRX_BUF_LEN];
-        uint16_t bytesRead = params->len;
+        uint16_t bytesRead = MIN( params->len, TXRX_BUF_LEN );
 
         DEBUG( "received %u bytes\n\r", bytesRead );
 
@@ -137,7 +138,7 @@
 
         // Also write to serial port...
         memset( txPayload, 0, TXRX_BUF_LEN );
-        memcpy( txPayload, buf, TXRX_BUF_LEN );
+        memcpy( txPayload, buf, bytesRead );
 //        pcfc.printf( "From app: " );
         pcfc.write( (char *)txPayload, bytesRead );
 //        pcfc.printf( "\r\n" );
@@ -152,7 +153,7 @@
 
 void periodicCallback( void )
 {
-    led1 = !led1;
+    _led1 = !_led1;
 //    rts  = !rts;
 }
 
@@ -161,21 +162,31 @@
 //    if( 0 != rts.read() )  pcfc.puts( "\r\n!RTS disengaged.\r\n" );  // When not using HWFC.
 
     // Set line from serial port to RX characteristic (From cone)
-    while( pcfc.readable() )
+    while( true )
     {
         if( !bleWasntReady )
         {
+            int cReadCnt = 0;
             char ch;
-            pcfc.read( ch );
+
+            pcfc.disableRxIrq();
+            if( pcfc.MTSBufferedIO::readable() )  cReadCnt = pcfc.read( ch );
+            pcfc.enableRxIrq();
+
+            if( 0 == cReadCnt )  break;
+
+//            if( '\n' == ch )  continue;  // Filter linefeeds.
             rx_buf[rx_len++] = ch;
         }
-        if( bleWasntReady || rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
+        if( bleWasntReady || rx_len>=TXRX_BUF_LEN || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
         {
             bleWasntReady = updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len );  // Notifies.
 
 //            pcfc.printf( "RecHandler \r\n" );
 
-            if( !bleWasntReady )  rx_len = 0;
+            if( bleWasntReady )  break;  // Don't loop on not ready (Don't starve main loop.)
+
+            rx_len = 0;
             break;
         }
     }
@@ -183,7 +194,7 @@
 
 int main( void )
 {
-    led1 = 1;
+    _led1 = 1;
     Ticker ticker;
     ticker.attach( periodicCallback, 1 );