football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
5:1b9734e68327
Parent:
4:17b8edf264c3
Child:
6:ef758ac3c928
--- a/main.cpp	Thu Apr 16 17:10:19 2015 +0000
+++ b/main.cpp	Fri Apr 17 04:20:07 2015 +0000
@@ -27,11 +27,12 @@
 #include "DeviceInformationService.h"
 
 #include "MTSSerialFlowControl.h"
+#include "PhoneAppIO.h"
 
 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
 
-#define LOOPBACK_MODE         // Loopback mode
+// #define LOOPBACK_MODE         // Loopback mode
 
 #if NEED_CONSOLE_OUTPUT
 #define DEBUG(...) { printf(__VA_ARGS__); }
@@ -41,6 +42,7 @@
 
 BLEDevice  ble;
 
+
 #define TXRX_BUF_LEN  20
 
 extern "C"
@@ -59,8 +61,8 @@
 //  Since MTSSerial now uses hardware flow control by default, MTSSerialFlowControl isn't needed
 //  unless the hardware doesn't have hardware flow control capability or sw is always fast enough.
 //
-// mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1280, 1280 );
-mts::MTSSerial pcfc( USBTX, USBRX, 1280, 1280 );
+// mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1536, 1536 );
+mts::MTSSerial pcfc( USBTX, USBRX, 384, 2688 );
 
 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
 
@@ -72,7 +74,10 @@
 DigitalOut rts( RTS_PIN_NUMBER );
 
 
-const char *deviceName = "TAF00";
+char deviceName[6];  // "TAF00";
+Gap::address_t   macAddr;
+Gap::addr_type_t *pAdType;
+
 
 const uint8_t DevInfoServiceUUID_rev[] =
 { 
@@ -81,6 +86,9 @@
 
 UARTService *uartServicePtr;
 
+// PhoneAppIO toPhone( &ble );
+
+
 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
 {
     DEBUG( "Disconnected!\n\r" );
@@ -88,6 +96,24 @@
     ble.startAdvertising();
 }
 
+bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
+{
+    ble_error_t err;
+
+//    toPhone.toPhoneBuf( (uint8_t *)data, bytesRead );
+
+    err = ble.updateCharacteristicValue( handle, data, bytesRead );
+
+    if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
+        (err == BLE_STACK_BUSY)            || 
+        (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
+        {
+            // pcfc.printf( "\r\nBLE %d!  ", err );
+        }
+
+    return  (err != BLE_ERROR_NONE);
+}
+
 void onDataWritten( const GattCharacteristicWriteCBParams *params )
 {
     if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
@@ -99,11 +125,12 @@
 
 #ifdef LOOPBACK_MODE
         // Loopback data from Central.
-        ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead );  // Notifies.
+        updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead );  // Notifies.
 #endif
 
+        ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
+
         // Also write to serial port...
-        ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
         memset( txPayload, 0, TXRX_BUF_LEN );
         memcpy( txPayload, buf, TXRX_BUF_LEN );
 //        pcfc.printf( "From app: " );
@@ -112,11 +139,10 @@
     }
 }
 
-// bool notReady;
+bool bleWasntReady;
 
 void onDataSent( unsigned count )
 {
-//    notReady = false;
 }
 
 void periodicCallback( void )
@@ -127,24 +153,24 @@
 
 void uartCB( void )
 {
-//    if( notReady )  return;
-
     if( 0 != rts.read() )  pcfc.puts( "\r\n!RTS disengaged.\r\n" );  // Can we read rts when in HW fc mode?
 
-
     // Set line from serial port to RX characteristic (From cone)
     while( pcfc.readable() )
     {
-        char ch;
-        pcfc.read( ch );
-        rx_buf[rx_len++] = ch;
-        if( rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
+        if( !bleWasntReady )
         {
-//            notReady = true;
+            char ch;
+            pcfc.read( ch );
+            rx_buf[rx_len++] = ch;
+        }
+        if( bleWasntReady || rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
+        {
+            bleWasntReady = updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len );  // Notifies.
 
-            ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len );  // Notifies.
 //            pcfc.printf( "RecHandler \r\n" );
-            rx_len = 0;
+
+            if( !bleWasntReady )  rx_len = 0;
             break;
         }
     }
@@ -160,11 +186,6 @@
     pcfc.baud( 57600 );
     pcfc.rxClear();
     // pc.attach( uartCB, pc.RxIrq );
-    pcfc.printf( "\r\nNano nano!\r\n" );
-#ifdef LOOPBACK_MODE
-    pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
-#endif
-
 
     DEBUG( "Initialising the nRF51822\n\r" );
 
@@ -177,6 +198,16 @@
     /* setup advertising */
     ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED );
     ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
+
+    // Get MAC addr so we can create a device name using it.
+    ble.getAddress( pAdType, macAddr );
+    sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
+
+    pcfc.printf( "\r\nNano nano!   I am \"%s\"\r\n", deviceName );
+#ifdef LOOPBACK_MODE
+    pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
+#endif
+
     ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
                                       (const uint8_t *)deviceName, strlen(deviceName) );
     ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,