Aidan Jabbarzadeh / Mbed 2 deprecated Receiver

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers receiver_main.cpp Source File

receiver_main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include <time.h>
00004 
00005 #define LED_RED     p21
00006 #define LED_GREEN   p22
00007 #define LED_BLUE    p23
00008 #define BUTTON_PIN  p17
00009 #define BATTERY_PIN p1
00010 
00011 #define NUM_UNIQUE_PACKETS 5
00012 
00013 Serial pc(USBTX, USBRX);
00014 
00015 DigitalOut redLed(LED_RED);
00016 DigitalOut blueLed(LED_BLUE);
00017 
00018 const uint8_t TRANSMITTER_MAC_ADDRESS = 0xF34F887FED4E;
00019 //const static char DEVICE_NAME[] = "Receiver";
00020 volatile bool received[NUM_UNIQUE_PACKETS];
00021 
00022 struct Data {
00023   uint32_t seqNum;
00024 };
00025 
00026 // void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00027 
00028 void scanCallback(const Gap::AdvertisementCallbackParams_t *params)
00029 {
00030     static int numMeas;
00031     static double avgMeas;
00032     
00033     static clock_t clock1, clock2;
00034     
00035     if (*(params->peerAddr) == (uint8_t) TRANSMITTER_MAC_ADDRESS) {
00036         
00037         // BREDR_NOT_SUPPORTED                = 0x04
00038         // LE_GENERAL_DISCOVERABLE            = 0x02
00039         // COMPLETE_LOCAL_NAME                = 0x09
00040         // MANUFACTURER_SPECIFIC_DATA         = 0xFF
00041         
00042         // Data received: 02 ff (MANUFACTURER_SPECIFIC_DATA) 0  (Sequence Number)
00043         
00044         // Turn off the blue led
00045         blueLed = 1;
00046         
00047         const uint8_t * data = params->advertisingData;
00048         
00049         int seqNum = (int)data[2];
00050         received[seqNum] = true;
00051         // pc.printf("Received packet with seq num: %d\n", seqNum);
00052         
00053         bool done = true;
00054         for(int i=0; i<NUM_UNIQUE_PACKETS; i++) {
00055             done = done && received[i];
00056         }
00057         
00058         if(done == true) {
00059             // Set the blue LED
00060             blueLed = 0;
00061             
00062             clock2 = clock1;
00063             clock1 = clock();
00064             
00065             // Print the time
00066             if (clock2 != 0) {
00067                 double timeUsed = ((double) (clock1 - clock2)) / CLOCKS_PER_SEC;
00068                 printf("Time to get full set: %f\n", timeUsed);
00069                 
00070                 avgMeas = ((float)numMeas * avgMeas + timeUsed) / (numMeas + 1);
00071                 numMeas++;
00072                 pc.printf("Average time: %f seconds\n", avgMeas);
00073            }
00074             
00075             // Reset the bool array
00076             for (int i=0; i<NUM_UNIQUE_PACKETS; i++) {
00077                 received[i] = false;   
00078             }            
00079         }
00080     }
00081     return;
00082 }
00083 
00084 
00085 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00086 {
00087     BLE &localBle          = params->ble;
00088     ble_error_t error = params->error;
00089 
00090     if (error != BLE_ERROR_NONE) {
00091         return;
00092     }
00093     
00094     redLed = 1;
00095     blueLed = 1;
00096     
00097     /* Set up scanning prodedure */
00098     localBle.gap().setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, GapScanningParams::SCAN_WINDOW_MAX, 0, false);
00099     localBle.gap().startScan(scanCallback);
00100     
00101     for(int i=0; i<NUM_UNIQUE_PACKETS; i++) {
00102         received[i] = false;
00103     }
00104         
00105     pc.printf("Init Completed\n");
00106 }
00107 
00108 
00109 int main(void)
00110 {
00111     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00112     ble.init(bleInitComplete);
00113     
00114     /* SpinWait for initialization to complete. This is necessary because the
00115      * BLE object is used in the main loop below. */
00116     while (ble.hasInitialized() == false) {
00117         pc.printf("Intiailizing BLE\n");
00118     }
00119     
00120     pc.printf("We are done initializing\n");
00121     
00122     while (1) {
00123         ble.waitForEvent(); // low power wait for event
00124     }
00125 }