This is an example Heart Rate Service created using a C-like approach. Rather than creating the service as a class and then using an instance of it, the service is entirely done with simple functions and variables.

Fork of nRF5-DK-HeartRateDemo by Bill Siever

Files at this revision

API Documentation at this revision

Comitter:
bsiever
Date:
Mon Nov 14 20:05:44 2016 +0000
Parent:
3:f593ad98fe21
Commit message:
Initial Release;

Changed in this revision

mbed_app.json Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Mon Nov 14 20:05:44 2016 +0000
@@ -0,0 +1,10 @@
+{
+    "target_overrides": {
+        "NRF51_DK": {
+           "target.uart_hwfc": 0
+        },
+        "NRF52_DK": {
+            "target.uart_hwfc": 0
+        }    
+    }
+}
\ No newline at end of file
--- a/source/main.cpp	Fri Nov 11 20:33:07 2016 +0000
+++ b/source/main.cpp	Mon Nov 14 20:05:44 2016 +0000
@@ -11,6 +11,7 @@
 // If debug is "true", include code to print debugging messages to the "console"
 #define DEBUG 1
 #if DEBUG
+#define LOG_FN_START() eventQueue.call(printf, "%s\r\n", __func__);
 #define LOG_PRINTF(...) eventQueue.call(printf, __VA_ARGS__);
 #else
 #define LOG_PRINTF(...) ;
@@ -29,22 +30,24 @@
 const UUID HRMS_BODYSENSELOC_CHAR(0x2A38);
 const UUID HRMS_CONTROLPOINT_CHAR(0x2A39);
 
-// B. Create variables for any actual data
+// B. Create/initialize variables for any actual data
 uint8_t hrmHeartRateData[4] = {0x08,0,0,0};  // 1st byte: flags, 2nd byte heart date, 3-4th bytes: energy expended
                                              // Flags 0x8 indicates energy expended is included and heart rate is an 8-bit value.
 uint8_t hrmBodySensorLocation = 0; // 0 is code for "Other". See https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml
 uint8_t hrmControlPoint = 0;
 
 
-// C. Create "pointers" (references) to the characteristics
-//    (The actual objects are constructed at run-time)
+// C. Create characteristics
+// The first example uses the generic "GattCharacteristic" object constructor. 
+// See: https://developer.mbed.org/teams/mbed/code/ble-api/docs/d19a554823af/classGattCharacteristic.html
 GattCharacteristic  hrmRateChar(HRMS_HRM_CHAR,     // UUID to use
                                 hrmHeartRateData,  // Pointer to data to use (arrays ARE pointers)
                                 4,                 // Number of bytes (current data)
                                 4,                 // Number of bytes (max)
                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);  // Permissions
  
-// The versions below use templated classes (a congenience for a few "common" characteristic permissions)
+// The versions below use templated classes (a conveniencce for a few "common" characteristic permissions)
+// See: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/docs/tip/ and search for "GattCharacteristic" to see others.
 ReadOnlyGattCharacteristic<uint8_t>  hrmLocationChar(HRMS_BODYSENSELOC_CHAR,  // UUID to use
                                                      &hrmBodySensorLocation); // Data to use
 
@@ -58,9 +61,15 @@
 /* Total size of space for eventQueue = event count * event size  */
 EventQueue eventQueue( 16 * 32);
 
-// D. Setup the interrupt pins for any buttons
+// E. Setup the interrupt pins for any buttons
 InterruptIn buttons[4] = { InterruptIn(P0_13), InterruptIn(P0_14), InterruptIn(P0_15), InterruptIn(P0_16) };
 
+// F. Setup the LEDs (in case needed)
+DigitalOut led1(LED1, 1);
+DigitalOut led2(LED2, 1);
+DigitalOut led3(LED3, 1);
+DigitalOut led4(LED4, 1);
+
 
 
 /***********************************************************************
@@ -71,7 +80,7 @@
 
 // A. Callback for things to do when a device disconnects
 void bleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
-    LOG_PRINTF(__func__);  // Print the function's name
+    LOG_FN_START();  // Print the function's name
     
     // Start advertising for a new connection
     BLE::Instance().gap().startAdvertising();
@@ -80,13 +89,13 @@
 
 
 void onDataWritten(const GattWriteCallbackParams *params) {
-    LOG_PRINTF(__func__);  // Print the function's name
+    LOG_FN_START();  // Print the function's name
     // See: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/docs/65474dc93927/GattCallbackParamTypes_8h_source.html for structure
     // A write of 1 to the "control point" should reset the energy expended
     if (params->handle == hrmControlPointChar.getValueAttribute().getHandle()) {
-        LOG_PRINTF("Writing to HRM Control Point");  
+        LOG_PRINTF("Writing to HRM Control Point\r\n");  
         if(params->len == 1 && params->data[0]==1) {
-            LOG_PRINTF("Clearing HRM Control Point");  
+            LOG_PRINTF("Clearing HRM Control Point\r\n");  
             // If it has the correct length and data, reset
             hrmHeartRateData[2] = 0;
             hrmHeartRateData[3] = 0;
@@ -114,7 +123,7 @@
 
 // B. Callback for things to do when the BLE object is ready
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
-    LOG_PRINTF(__func__);  // Print the function's name
+    LOG_FN_START();  // Print the function's name
     
     // Get the BLE object
     BLE&        ble   = BLE::Instance();
@@ -150,7 +159,7 @@
 }
 
 void buttonsPress() {
-    LOG_PRINTF(__func__);  // Print the function's name
+    LOG_FN_START();  // Print the function's name
 
     // Get access to the BLE object
     BLE &ble = BLE::Instance();
@@ -201,8 +210,9 @@
    ************************************************************************/
 
 
+
 int main() {
-    LOG_PRINTF(__func__);  // Print the function's name
+    LOG_FN_START();  // Print the function's name
     // Configure the buttons.  
     for(int i=0;i<sizeof(buttons)/sizeof(InterruptIn); i++) {
         // Pull the button voltages "up" to 3v by default