Team HUB / Mbed 2 deprecated nRF51822_Fresh

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_Updated by Nicholas Hazen

Revision:
5:5e55081e4d7a
Parent:
4:2f9c7d53a099
Child:
6:6e0eedc8e0a9
--- a/main.cpp	Mon Jul 27 18:08:46 2015 +0000
+++ b/main.cpp	Wed Aug 05 19:59:45 2015 +0000
@@ -32,12 +32,12 @@
 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
 
-#define TXRX_BUF_LEN                     20
+#define TXRX_BUF_LEN                     20 //maximum payload: 20 bytes per packet
 
-#define MIN_CONN_INTERVAL 6
-#define MAX_CONN_INTERVAL 10
-#define SLAVE_LATENCY 4
-#define CONN_SUP_TIMEOUT 6000
+#define MIN_CONN_INTERVAL 6 //lowest possible connection interval (6 * 1.25 = 7.5ms)
+#define MAX_CONN_INTERVAL 6
+#define SLAVE_LATENCY 0 //lowest possible
+#define CONN_SUP_TIMEOUT 6000 //default; changing this value has not been shown to make a difference in data transfer rate
 
 BLE ble;
 
@@ -61,9 +61,16 @@
 uint8_t packet2[]  =  {0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48};
 uint8_t packet3[]  =  {0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c};
 
-Timer t;
+int i = 0;
+int currentTrial = 0;
+int totalTrials = 100;
 
-GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+Timer t;
+float times[100];
+
+DigitalOut myled(LED1); //corresponds to pin 13 on nRF51822
+
+GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);//GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
 
 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
 
@@ -71,16 +78,13 @@
 
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
-int i = 0;
-int currentTrial = 0;
-int totalTrials = 10;
 
 void writePackets(void);
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    pc.printf("Disconnected \r\n");
-    pc.printf("Restart advertising \r\n");
+    //pc.printf("Disconnected \r\n");
+    //pc.("Restart advertising \r\n");
     ble.startAdvertising();
 }
 
@@ -91,38 +95,66 @@
     
     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
     {
-        pc.printf("Time before packet received in seconds: %f\n", t.read());
+        //myled = 1;
+        //pc.("T b4 pckt received: %f\r\n", t.read()); //time before packet received in seconds
+        ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
+        
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
-
-        t.stop();
-        pc.printf("Time in seconds: %f\n", t.read());
+        //t.stop();
+        myled = 0;
+        times[currentTrial] = t.read();
+        //pc.printf("%f\r\n", t.read()); //Total time in seconds
 
         bytesRead = 20;
         memset(txPayload, 0, TXRX_BUF_LEN);
         memcpy(txPayload, buf, TXRX_BUF_LEN);
-        pc.printf("WriteHandler \r\n");
-        pc.printf("Length: ");
-        pc.putc(bytesRead);
-        pc.printf("\r\n");
-        pc.printf("Data: ");
-        for(index=0; index<bytesRead; index++) {
-            pc.putc((char)txPayload[index]);
+        //pc.("WriteHandler \r\n");
+        //pc.("Length: ");
+        //pc.putc(bytesRead);
+        //pc.("\r\n");
+        //pc.("Data: ");
+        /*for(index=0; index<bytesRead; index++) {
+            //pc.putc((char)txPayload[index]);
         }
-        pc.printf("\r\n");
+        //pc.("\r\n");
         
-         t.reset();
+        //t.reset();
+        //pc.("T RESET \r\n");*/
         currentTrial++;
         
         if(currentTrial < totalTrials)
         {
-            wait(15);
+            //pc.("CT < TT \r\n"); //Current trial less than total trials
+            //wait(1);
             
             writePackets();
         }
         else
         {
-            pc.printf("Done. %d trials completed.\r\n", currentTrial);
-        }   
+            float difference = 0.0;
+            
+            for(int j = 0; j<totalTrials; j++)
+            {
+                if(j == 0)
+                {
+                    pc.printf("%5.3f\r\n", times[j]);
+                }
+                    else
+                    {
+                        difference = times[j] - times[j - 1];
+                        pc.printf("%5.3f\r\n", difference);
+                    }
+            }
+            /*float total = 0.0, avg = 0.0;
+            for(int m = 0; m<totalTrials; m++)
+            {
+                total = total + times[m];
+            }
+                
+            avg = total/100.0;
+            pc.printf("AVG: %5.3f\r\n", avg);
+            //pc.("Done. %d trials completed.\r\n", currentTrial);*/
+        }  
     }
 }
 
@@ -134,10 +166,10 @@
         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
         {
             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
+            //pc.("RecHandler \r\n");
+            //pc.("Length: ");
             pc.putc(rx_len);
-            pc.printf("\r\n");
+            //pc.("\r\n");
             rx_len = 0;
             break;
         }
@@ -153,28 +185,45 @@
         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
         {
             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
+            pc.("RecHandler \r\n");
+            pc.("Length: ");
             pc.putc(rx_len);
-            pc.printf("\r\n");
+            pc.("\r\n");
             rx_len = 0;
             break;
         }
     }
     */
 
-    pc.printf("Updating Characteristic... \r\n");
+    //pc.("Updating Characteristic... \r\n");
 
-    wait(1);
-
+    if(currentTrial < 1)
+    {
+    wait(1); //trials have shown that the code will not run without a delay before the first trial
     t.start();
+    }
+    //t.start();
+    //pc.("T START \r\n");
 
+    myled = 1;
     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet1, 20);
-    pc.printf("Time after first packet sent in seconds: %f\n", t.read());
+    //myled = 0;
+    //pc.("T after pckt 1 sent: %f\r\n", t.read());
+    //myled = 1;
     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet2, 20);
-    pc.printf("Time after second packet sent in seconds: %f\n", t.read());
+    //myled = 0;
+    //pc.("T after pckt 2 sent: %f\r\n", t.read());
+    //myled = 1;
     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet3, 20);
-    pc.printf("Time after third packet sent in seconds: %f\n", t.read());
+    //myled = 0;
+    //pc.("T after pckt 3 sent: %f\r\n", t.read());
+    
+    currentTrial++;
+    
+    if 
+    
+    myled = 0;
+    times[currentTrial] = t.read();
 }
 
 int main(void)
@@ -183,8 +232,8 @@
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(WrittenHandler);  
     
-    pc.baud(9600);
-    pc.printf("SimpleChat Init \r\n");
+    pc.baud(460800);
+    //pc.("SimpleChat Init \r\n");
     
     pc.attach( uartCB , pc.RxIrq);
    // setup advertising 
@@ -200,14 +249,17 @@
     ble.addService(uartService);
     
     ble.startAdvertising(); 
-    pc.printf("Advertising Start \r\n");
+    //pc.("Advertising Start \r\n");
     
    while(1) 
     {
+        //wait(1);
+        //pc.("BEFORE BLE WAIT 4 EVENTS \r\n");
         ble.waitForEvent();
-
+        //pc.("AFTER BLE WAIT 4 EVENTS \r\n");
         if(i == 0) 
         {
+            //pc.("LAST IF STATEMENT TRUE \r\n");
             writePackets();
             i++;
         }