aaaa

Dependencies:   BLE_API mbed-events-master mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
fbdp1202
Date:
Mon Jun 12 04:52:55 2017 +0000
Parent:
79:8b7c8c240540
Commit message:
aaaa

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-events-master.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 8b7c8c240540 -r e989b2f090f4 main.cpp
--- a/main.cpp	Tue Sep 20 12:36:16 2016 +0100
+++ b/main.cpp	Mon Jun 12 04:52:55 2017 +0000
@@ -1,107 +1,136 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* mbed Microcontroller Library 
+* Copyright (c) 2006-2015 ARM Limited 
+* 
+* Licensed under the Apache License, Version 2.0 (the "License"); 
+* you may not use this file except in compliance with the License. 
+* You may obtain a copy of the License at 
+* 
+*     http://www.apache.org/licenses/LICENSE-2.0 
+* 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License. 
+ */ 
+
 
-#include "mbed.h"
-#include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
-#include "ble/services/BatteryService.h"
-#include "ble/services/DeviceInformationService.h"
+#include <mbed.h> 
+#include "mbed-events-master/mbed_events.h" 
+#include "ble/BLE.h" 
+#include "ble/Gap.h" 
+#include "ble/services/HeartRateService.h" 
+
+DigitalOut led1(LED1, 1); 
+
+
+const static char     DEVICE_NAME[] = "HRM"; 
+static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; 
 
-DigitalOut led1(LED1);
+
+static uint8_t hrmCounter = 100; // init HRM to 100bps 
+static HeartRateService *hrServicePtr; 
+
+static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 
+
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 
+{ 
+    BLE::Instance().gap().startAdvertising(); // restart advertising 
+} 
 
-const static char     DEVICE_NAME[]        = "HRM1";
-static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
-                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
-static volatile bool  triggerSensorPolling = false;
-
-uint8_t hrmCounter = 100; // init HRM to 100bps
+//void  connectionCallback ( const  Gap :: ConnectionCallbackParams_t * params)
+//{
+//    Gap::Handle_t gap_handle = params-> handle;
+//    Gap::ConnectionParams_t new_params;
+// 
+//    new_params.minConnectionInterval =  1000 ;
+//    new_params.maxConnectionInterval =  1000 ; 
+//    new_params.connectionSupervisionTimeout =  30000;//3200 ; 
+//    new_params.slaveLatency =  29 ;
+//    ble.gap().updateConnectionParams(gap_handle, & new_params);
+//}
+ 
 
-HeartRateService         *hrService;
-DeviceInformationService *deviceInfo;
+void updateSensorValue() { 
+    // Do blocking calls or whatever is necessary for sensor polling. 
+    // In our case, we simply update the HRM measurement. 
+    hrmCounter++; 
+
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
-}
+    //  100 <= HRM bps <=175 
+    if (hrmCounter == 175) { 
+        hrmCounter = 100; 
+    } 
+
+
+    hrServicePtr->updateHeartRate(hrmCounter); 
+} 
+
 
-void periodicCallback(void)
-{
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+void periodicCallback(void) 
+{ 
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 
 
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
-}
+    if (BLE::Instance().getGapState().connected) { 
+        eventQueue.call(updateSensorValue); 
+    } 
+} 
+
 
-void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
-{
-    BLE &ble          = params->ble;
-    ble_error_t error = params->error;
+void onBleInitError(BLE &ble, ble_error_t error) 
+{ 
+    (void)ble; 
+    (void)error; 
+   /* Initialization error handling should go here */ 
+} 
+
 
-    if (error != BLE_ERROR_NONE) {
-        return;
-    }
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
+{ 
+    BLE&        ble   = params->ble; 
+    ble_error_t error = params->error; 
+
+    if (error != BLE_ERROR_NONE) { 
+        onBleInitError(ble, error); 
+        return; 
+    } 
+
+    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 
+        return; 
+    } 
 
     ble.gap().onDisconnection(disconnectionCallback);
 
-    /* Setup primary service. */
-    hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
-
-    /* Setup auxiliary service. */
-    deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+    /* Setup primary service. */ 
+    hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 
 
-    /* Setup advertising. */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
-    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 */
-    ble.gap().startAdvertising();
+    /* Setup advertising. */ 
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 
+    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 */ 
+    ble.gap().startAdvertising(); 
+    printf("Hello!");
 }
 
-int main(void)
-{
-    led1 = 1;
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
-
-    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
-    ble.init(bleInitComplete);
-
-    /* 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 */ }
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 
+    BLE &ble = BLE::Instance(); 
+    eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 
+} 
 
-    // infinite loop
-    while (1) {
-        // check for trigger from periodicCallback()
-        if (triggerSensorPolling && ble.getGapState().connected) {
-            triggerSensorPolling = false;
+int main() 
+{ 
+    printf("Hello!");
+    eventQueue.call_every(500, periodicCallback); 
+    BLE &ble = BLE::Instance();
+    ble.onEventsToProcess(scheduleBleEventsProcessing); 
+    ble.init(bleInitComplete); 
+//    ble.gap().onConnection(connectionCallback);
 
-            // Do blocking calls or whatever is necessary for sensor polling.
-            // In our case, we simply update the HRM measurement.
-            hrmCounter++;
-            if (hrmCounter == 175) { //  100 <= HRM bps <=175
-                hrmCounter = 100;
-            }
+     eventQueue.dispatch_forever(); 
 
-            hrService->updateHeartRate(hrmCounter);
-        } else {
-            ble.waitForEvent(); // low power wait for event
-        }
-    }
-}
+     return 0; 
+ }
\ No newline at end of file
diff -r 8b7c8c240540 -r e989b2f090f4 mbed-events-master.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-events-master.lib	Mon Jun 12 04:52:55 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/fbdp1202/code/mbed-events-master/#8a719b35d2c3
diff -r 8b7c8c240540 -r e989b2f090f4 mbed.bld
--- a/mbed.bld	Tue Sep 20 12:36:16 2016 +0100
+++ b/mbed.bld	Mon Jun 12 04:52:55 2017 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/abea610beb85
+https://mbed.org/users/mbed_official/code/mbed/builds/0f02307a0877
\ No newline at end of file