asd

Dependencies:   BLE_API mbed nRF51822

Fork of Central_1 by -deleted-

Revision:
2:1f1ab7f55a45
Parent:
1:df5573997c91
--- a/main.cpp	Tue Aug 18 10:40:55 2015 +0000
+++ b/main.cpp	Tue Aug 18 12:37:28 2015 +0000
@@ -24,33 +24,38 @@
 
 /* Declare the array */
 
+static const unsigned NUM_LIST = 3;
 static const unsigned ADDR_LEN = 6;
-static const unsigned NUM_LIST = 3;
-typedef uint8_t Address_t[ADDR_LEN]; /* 48-bit address, LSB format. */
-typedef Address_t address_t;         /* @Note: deprecated. Use Address_t instead. */
 
 /* Declare the pointer */
+
+int8_t temp;
 uint8_t pt_addr = 0;
 uint8_t list_addr[NUM_LIST][ADDR_LEN]={0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0};
 int8_t list_rssi[NUM_LIST];
+
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
 }
 
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
-   
-    pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
+    unsigned i;
+           
+    for(i = 0; i < NUM_LIST; i++) 
+    {
+        if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3] && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) 
+        {          
+            /* Only RSSI Value update */
+            list_rssi[i] = params->rssi;
+            //pc.printf("%dth RSSI Update!\r\n",i+1);
+            break;
+        }
+        if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) 
+        {
+            pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
            params->rssi, params->isScanResponse, params->type);
-           
-    for(unsigned i = 0; i < NUM_LIST; i++) {
-         if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3] && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) {
-            /* Only RSSI Value update */
-            list_rssi[i] = params->rssi;
-            break;
-        }
-        if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) {
             /* Save the address */
             list_addr[i][5] = params->peerAddr[5];
             list_addr[i][4] = params->peerAddr[4];
@@ -60,37 +65,43 @@
             list_addr[i][0] = params->peerAddr[0];
             /* Save RSSI value */
             list_rssi[i] = params->rssi;
-            if ( i==2 ) {
+            pc.printf("Address & RSSI Save!\r\n");
+            if ( i==NUM_LIST-1 ) 
+            {
+                pc.printf("Find Maximum value!\r\n");
                 /* Find Maximum RSSI value */
-                for(unsigned j=0;j<NUM_LIST;j++) {
-                    for(unsigned k=j ; k<NUM_LIST ; k++) {
-                        if(list_rssi[j] > list_rssi[j+1]) {
+                for(unsigned j=0;j<NUM_LIST;j++) 
+                {
+                    for(unsigned k=j ; k<NUM_LIST ; k++) 
+                    {
+                        if(list_rssi[j] < list_rssi[k]) 
+                        {
                             /* Swap RSSI Value */
                             temp = list_rssi[j];
-                            list_rssi[j] = list_rssi[j+1];
-                            list_rssi[j+1] = temp;
+                            list_rssi[j] = list_rssi[k];
+                            list_rssi[k] = temp;
                             
                             /* Swap the address */
-                            for(unsigned l = 0; l < 6 ; l++) {
-                            temp = list_addr[j][l];
-                            list_addr[j][l] = list_addr[j+1][l];
-                            list_addr[j+1][l] = temp;
+                            for(unsigned l = 0; l < 6 ; l++) 
+                            {
+                                temp = list_addr[j][l];
+                                list_addr[j][l] = list_addr[k][l];
+                                list_addr[k][l] = temp;
                             }
                         }
                     }
-                }                        
+                }
+                for(unsigned j=0; j<NUM_LIST ; j++)
+                    pc.printf("Address %d : %02x %02x %02x %02x %02x %02x RSSI : %d\r\n", j, list_addr[j][5], list_addr[j][4], list_addr[j][3], list_addr[j][2], list_addr[j][1], list_addr[j][0], list_rssi[j]);                        
+            }
             break;
         }
-        
     }
-    
-    
-    
-    
-    pc.printf("%s", params->advertisingData);
-    
-    pc.printf("\r\n");
-
+    if( i == NUM_LIST-1)
+    {
+        ble.gap().stopScan();
+        ble.gap().connect(list_addr[0], Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+    }
 }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
@@ -115,7 +126,7 @@
 }
  
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
-    pc.printf("Connected!!\r\n");
+    pc.printf("Connection success to %02x %02x %02x %02x %02x %02x\r\n", list_addr[0][5], list_addr[0][4], list_addr[0][3], list_addr[0][2], list_addr[0][1], list_addr[0][0]);
     ble.stopAdvertising();
     wait(1);
     if (params->role == Gap::CENTRAL) {
@@ -126,10 +137,15 @@
 }
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
+    /* Initialize address list */
+    for(unsigned i=0; i<NUM_LIST ; i++) {
+        list_rssi[i] = 0;
+        for(unsigned j=0; j<ADDR_LEN ; j++)
+            list_addr[i][j] = 0;
+    }
     pc.printf("Disconnected\r\n");
     pc.printf("Rescan\r\n");
     ble.startScan(advertisementCallback);
-    ble.startAdvertising();
 }
     
 
@@ -157,7 +173,7 @@
     //ble.gap().setAdvertisingInterval(100); /* 1second. */
     //ble.gap().startAdvertising();
 
-    ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
+    ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
     ble.gap().startScan(advertisementCallback);
     
     while (true) {