aidanReceiver

Dependencies:   mbed BLE_API nRF51822

Revision:
6:55c87c9bc90e
Parent:
5:45c48a82f202
Child:
7:435abd25362e
--- a/receiver_main.cpp	Wed Apr 17 21:42:16 2019 +0000
+++ b/receiver_main.cpp	Fri Apr 19 07:07:21 2019 +0000
@@ -12,10 +12,14 @@
 #define BUTTON_PIN  p17
 #define BATTERY_PIN p1
 
-DigitalOut led1(LED_RED);
+DigitalOut redLed(LED_RED);
 DigitalOut blueLed(LED_BLUE);
 
-const static char DEVICE_NAME[] = "Joe-Aidan";
+const uint8_t TRANSMITTER_MAC_ADDRESS = 0xF34F887FED4E;
+
+const static char DEVICE_NAME[] = "Receiver";
+
+int latestSeqNum;
 
 struct Data {
   uint32_t seqNum;
@@ -25,47 +29,56 @@
 
 void scanCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-    if (*(params->Gap::AdvertisementCallbackParams_t::peerAddr) == (std::uint8_t)0xF34F887FED4E) {
-        uint8_t * buf = (uint8_t *) params->advertisingData;
+    if (*(params->peerAddr) == (uint8_t) TRANSMITTER_MAC_ADDRESS) {
         
-        if (buf[0] == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
-            blueLed = 0;
-        }
-//        pc.printf("Hello\n");
+        // BREDR_NOT_SUPPORTED                = 0x04
+        // LE_GENERAL_DISCOVERABLE            = 0x02
+        // COMPLETE_LOCAL_NAME                = 0x09
+        // MANUFACTURER_SPECIFIC_DATA         = 0xFF
+        
+        // Data received: 02 ff (MANUFACTURER_SPECIFIC_DATA) 0  (Sequence Number)
+        
+        const uint8_t * data = params->advertisingData;
         
-        for (int i=0; i < params->advertisingDataLen; i++) {
-            //if ( i >= params->advertisingDataLen-2 && buf[i] == 'n' ) {
-//                blueLed = 0;
-//            }
-            pc.printf("%02x ", buf[i]);
+        //only print when new packet received!
+        if (latestSeqNum < (int)data[2]) {
+            pc.printf("%d bytes data received from transmitter\n", params->advertisingDataLen);
+            
+            for (int i = 0; i < params->advertisingDataLen; i++) {
+                if ( i >= params->advertisingDataLen-2 && data[i] == 'n' ) {
+                   blueLed = 0;
+                }
+                pc.printf("%x ", data[i]);
+            }
+            pc.printf("Seq num: %d", (int)data[2]);
+            pc.printf("\n");
+            latestSeqNum = (int)data[2];
         }
-        pc.printf("\n");
         
-        led1 = 0;
+        redLed = 0;
     }
-    
-//    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-
     return;
 }
 
 
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    BLE &ble          = params->ble;
+    BLE &localBle          = params->ble;
     ble_error_t error = params->error;
 
     if (error != BLE_ERROR_NONE) {
         return;
     }
     
-    led1 = 1;
+    redLed = 1;
     blueLed = 1;
-
     
     /* Set up scanning prodedure */
-    ble.gap().setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, GapScanningParams::SCAN_WINDOW_MAX, 0, false);
-    ble.gap().startScan(scanCallback);
+    localBle.gap().setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, GapScanningParams::SCAN_WINDOW_MAX, 0, false);
+    latestSeqNum = 0;
+    localBle.gap().startScan(scanCallback);
+    
+    pc.printf("Init Completed\n");
 }
 
 
@@ -74,11 +87,11 @@
     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
     ble.init(bleInitComplete);
     
-//    pc.printf("Hello World!\n");
-    
     /* SpinWait for initialization to complete. This is necessary because the
      * BLE object is used in the main loop below. */
-    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    while (ble.hasInitialized() == false) {
+        pc.printf("Intiailizing BLE\n");
+    }
     
     while (1) {
         ble.waitForEvent(); // low power wait for event