aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Branch:
iBeacon
Revision:
18:442bc914996b
Parent:
17:18f4bf2a368a
Child:
20:fc639ef579b6
Child:
21:71f2f1b314f2
--- a/main.cpp	Wed Aug 01 17:43:34 2018 +0200
+++ b/main.cpp	Thu Aug 02 10:18:51 2018 +0200
@@ -50,8 +50,6 @@
 
 uint8_t gConnected = 0;
 
-DigitalOut redLed(p22);
-
 static NRF52_SAADC analogIn;
 static NRF52_DigitalOut lightPower(p28);
 static NRF52_DigitalOut temperaturePower(p31);
@@ -148,46 +146,6 @@
 /**
  * Callback triggered when the ble initialization process has finished
  */
-void bleInitCompleteIBeacon(BLE::InitializationCompleteCallbackContext *params)
-{
-    BLE&        ble   = params->ble;
-    ble_error_t error = params->error;
-
-    if (error != BLE_ERROR_NONE){
-        return;
-    }
-
-    /* Ensure that it is the default instance of BLE */
-    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE){
-        return;
-    }
-
-    uint8_t mac[6] = {0,0,0,0,0,0};
-    BLEProtocol::AddressType_t temp_address_type;
-    ble.gap().getAddress(&temp_address_type, myMacAddress);
-    macServicePtr = new MACService(ble, mac);
-    macServicePtr->updateMacAddress(myMacAddress);    // Update MAC address
-
-    ble.gap().onConnection(onConnectionCallback);
-    ble.gap().onDisconnection(disconnectionCallback);
-
-    /*
-     *  iBeacon format
-     */
-     ble.gap().accumulateAdvertisingPayload(
-        GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.gap().accumulateAdvertisingPayload(
-        GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
-        (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
-
-    ble.gap().setAdvertisingInterval(ADV_INTERVAL);
-	ble.gap().setTxPower(TX_POWER_DB);        // Set TX power to TX_POWER_DB
-    ble.gap().startAdvertising();
-}
-
-/**
- * Callback triggered when the ble initialization process has finished
- */
 void bleInitCompleteSensors(BLE::InitializationCompleteCallbackContext *params)
 {
     BLE&        ble   = params->ble;
@@ -202,7 +160,7 @@
         return;
     }
 
-    uint8_t mac[6] = {0,0,0,0,0,0};
+	uint8_t mac[6] = {0,0,0,0,0,0};
     BLEProtocol::AddressType_t temp_address_type;
     ble.gap().getAddress(&temp_address_type, myMacAddress);
     macServicePtr = new MACService(ble, mac);
@@ -220,8 +178,10 @@
     ble.gap().setAdvertisingType(
         GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
 	ble.gap().setAdvertisingInterval(ADV_INTERVAL);
+	printf("Init started....\t\t");
 	ble.gap().setTxPower(TX_POWER_DB);        // Set TX power to TX_POWER_DB
     ble.gap().startAdvertising();
+	printf("Init done.\n");
 }
 
 float getLight()
@@ -327,8 +287,16 @@
     if(!advertisementType && !gConnected)
     {
         printf("Sensor format 1.\n");
-        while(BLE_ERROR_NONE != ble.shutdown());
-        ble.init(bleInitCompleteSensors);
+        ble.gap().clearAdvertisingPayload();
+		/* setup advertising */
+	    ble.gap().accumulateAdvertisingPayload(
+	        GapAdvertisingData::BREDR_NOT_SUPPORTED);
+	    ble.gap().accumulateAdvertisingPayload(
+	        GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+	        (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
+	    ble.gap().setAdvertisingType(
+	        GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+
         adv_data = ble.getAdvertisingData();
         advertisementPacket.type = 0x00;
         readGyroscope((vector3_s *)advertisementPacket.gyroscope);
@@ -366,17 +334,15 @@
     else if (!gConnected)
     {
         printf("Beacon format!\n");
-        while(BLE_ERROR_NONE != ble.shutdown());
-        ble.init(bleInitCompleteIBeacon);
+		ble.gap().clearAdvertisingPayload();
+		ble.gap().accumulateAdvertisingPayload(
+	        GapAdvertisingData::BREDR_NOT_SUPPORTED);
+	    ble.gap().accumulateAdvertisingPayload(
+	        GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+	        (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
+		ble.gap().startAdvertising();
     }
-
     if(++advertisementType > 2) advertisementType = 0;
-
-}
-
-void blinky()
-{
-    redLed = !redLed;
 }
 
 int main()
@@ -407,11 +373,9 @@
 
     BLE &ble = BLE::Instance();
     ble.init(bleInitCompleteSensors);
-    //ble.init(bleInitCompleteIBeacon);
     while(ble.hasInitialized() == false){
         /* spin loop */
     }
-    //bleT.start(callback(bleEventsC));
     ble.onEventsToProcess(scheduleBleEventsProcessing);
 
     I2C i2c(I2C_DATA, I2C_CLK);
@@ -429,8 +393,7 @@
     Ticker ticker;
     ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
 
-    eventQueue.call_every(500, blinky);
-    eventQueue.call_every(1000, updateData);
+    eventQueue.call_every(500, updateData);
 
     // This call stops main thread
     eventQueue.dispatch_forever();