aidanReceiver

Dependencies:   mbed BLE_API nRF51822

Files at this revision

API Documentation at this revision

Comitter:
jzabins2
Date:
Mon Apr 29 06:23:03 2019 +0000
Parent:
7:435abd25362e
Commit message:
Receiver measuring average time to receive five messages

Changed in this revision

receiver_main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/receiver_main.cpp	Fri Apr 26 20:55:18 2019 +0000
+++ b/receiver_main.cpp	Mon Apr 29 06:23:03 2019 +0000
@@ -1,10 +1,6 @@
 #include "mbed.h"
 #include "ble/BLE.h"
-#include <string>
-
-using namespace std;
-
-Serial pc(USBTX, USBRX);
+#include <time.h>
 
 #define LED_RED     p21
 #define LED_GREEN   p22
@@ -14,16 +10,15 @@
 
 #define NUM_UNIQUE_PACKETS 5
 
+Serial pc(USBTX, USBRX);
+
 DigitalOut redLed(LED_RED);
 DigitalOut blueLed(LED_BLUE);
 
 const uint8_t TRANSMITTER_MAC_ADDRESS = 0xF34F887FED4E;
-
-const static char DEVICE_NAME[] = "Receiver";
+//const static char DEVICE_NAME[] = "Receiver";
 volatile bool received[NUM_UNIQUE_PACKETS];
 
-int latestSeqNum;
-
 struct Data {
   uint32_t seqNum;
 };
@@ -32,6 +27,11 @@
 
 void scanCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
+    static int numMeas;
+    static double avgMeas;
+    
+    static clock_t clock1, clock2;
+    
     if (*(params->peerAddr) == (uint8_t) TRANSMITTER_MAC_ADDRESS) {
         
         // BREDR_NOT_SUPPORTED                = 0x04
@@ -41,22 +41,41 @@
         
         // Data received: 02 ff (MANUFACTURER_SPECIFIC_DATA) 0  (Sequence Number)
         
+        // Turn off the blue led
+        blueLed = 1;
+        
         const uint8_t * data = params->advertisingData;
         
         int seqNum = (int)data[2];
         received[seqNum] = true;
-        pc.printf("Seq num: %d\n", seqNum);
+        // pc.printf("Received packet with seq num: %d\n", seqNum);
         
         bool done = true;
         for(int i=0; i<NUM_UNIQUE_PACKETS; i++) {
-            if(received[i] == false) {
-                done = false;
-                break;
-            }
+            done = done && received[i];
         }
         
         if(done == true) {
+            // Set the blue LED
             blueLed = 0;
+            
+            clock2 = clock1;
+            clock1 = clock();
+            
+            // Print the time
+            if (clock2 != 0) {
+                double timeUsed = ((double) (clock1 - clock2)) / CLOCKS_PER_SEC;
+                printf("Time to get full set: %f\n", timeUsed);
+                
+                avgMeas = ((float)numMeas * avgMeas + timeUsed) / (numMeas + 1);
+                numMeas++;
+                pc.printf("Average time: %f seconds\n", avgMeas);
+           }
+            
+            // Reset the bool array
+            for (int i=0; i<NUM_UNIQUE_PACKETS; i++) {
+                received[i] = false;   
+            }            
         }
     }
     return;
@@ -77,13 +96,12 @@
     
     /* Set up scanning prodedure */
     localBle.gap().setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, GapScanningParams::SCAN_WINDOW_MAX, 0, false);
-    latestSeqNum = 0;
     localBle.gap().startScan(scanCallback);
     
     for(int i=0; i<NUM_UNIQUE_PACKETS; i++) {
         received[i] = false;
     }
-    
+        
     pc.printf("Init Completed\n");
 }