semaphore_BLE

Dependencies:   BLE_API mbed-rtos mbed nRF51822 SDFileSystem X_NUCLEO_IDB0XA1

Revision:
0:6679d693f1af
Child:
1:91870059ced7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 19 05:28:43 2016 +0000
@@ -0,0 +1,103 @@
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "ble/services/HeartRateService.h"
+#include "ble/services/BatteryService.h"
+#include "ble/services/DeviceInformationService.h"
+#include "rtos.h"
+#include <string.h>
+
+Semaphore one_slot(1);
+ 
+ 
+ BLE  ble;
+int address[10][5]={};
+const GapScanningParams scanningParams;
+
+
+int counter= 0;
+void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
+{
+    for(int i=0; i<6; i++){
+        address[0][i]=params->peerAddr[5-i];
+    }
+    for(int b=0; b<6 ; b++){
+        if(b==0)printf("DEV:");
+        printf("%02x ", address[0][b]);
+        if(b==5)printf("\n\r");
+    }        
+}
+
+
+void test_thread(void const *name) {    
+    
+    
+    while (true) {
+        Timer timer;
+        one_slot.wait();
+        printf("%s\n\r", (const char*)name);
+//1
+        if(!strcmp((const char*)name, "1")){
+            printf("**this is 1**\n\r");
+            ble.gap().stopAdvertising();
+            ble.startScan(&onScanCallback);
+        
+        }
+
+//2        
+        if(!strcmp((const char*)name, "2")){
+            printf("**this is 2**\n\r");
+            timer.start();
+            while(1){
+                ble.waitForEvent();
+                if(timer.read() > 9){
+                        timer.stop();
+                        timer.reset();
+                        ble.stopScan();
+                        break;
+                }
+            }
+        }
+//3        
+        if(!strcmp((const char*)name, "3")){
+            printf("**this is 3**\n\r");
+            ble.gap().startAdvertising();
+
+        }
+        
+        
+        Thread::wait(10000);
+        one_slot.release();
+    }
+}
+ 
+// const static char     DEVICE_NAME[]        = "BLE1";
+    
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE &ble          = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+    const uint8_t address1[] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
+    ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1);
+//  ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ //   ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ //   ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+}
+ 
+int main (void) {
+    //ble.init();
+    ble.init(bleInitComplete);
+    ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX,
+                      GapScanningParams::SCAN_WINDOW_MAX,
+                      0);
+
+
+    Thread t2(test_thread, (void *)"2");
+    Thread t3(test_thread, (void *)"3");
+    
+    test_thread((void *)"1");
+
+}
\ No newline at end of file