Simple HRM

Dependencies:   BLE_API mbed nRF51822

Revision:
0:aa3968526b8c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jun 02 17:21:07 2016 +0000
@@ -0,0 +1,138 @@
+/*
+*
+ * Copyright (c) 2016 RedBear
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/**
+ * @note This demo is Nordic HRM example.
+ *       You could use nRF toolbox tool to test it.
+ */
+ 
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "GattCallbackParamTypes.h"
+
+//#include "ble/DiscoveredCharacteristic.h"
+//#include "ble/DiscoveredService.h"
+//#include "ble/GapScanningParams.h"
+//#include "ble_radio_notification.h"
+//#include "ble_gap.h"
+
+
+
+#define DEVICE_NAME       "Nordic_HRM"
+
+BLE             ble;
+Ticker                    ticker_task1;
+
+
+
+Serial pc(USBTX, USBRX);
+
+
+
+
+
+static uint8_t hrmCounter     = 100;
+static uint8_t bpm[2]         = {0x00, hrmCounter};
+static const uint8_t location = 0x03;
+static uint32_t cnt;
+
+static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+
+// Create characteristic and service
+GattCharacteristic   hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+GattCharacteristic   hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,(uint8_t *)&location, sizeof(location), sizeof(location),GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+GattCharacteristic   *hrmChars[] = {&hrmRate, &hrmLocation, };
+GattService          hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
+
+
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    pc.printf("Disconnected ! \r\n");
+    pc.printf("Restart advertising progress \r\n");
+    ble.startAdvertising();
+}
+
+
+void periodicCallback() {
+      pc.printf("periodicCallback ! \r\n");
+
+  if (ble.getGapState().connected) {
+    // Update the HRM measurement
+    // First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value
+    // See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
+    hrmCounter++;
+    if (hrmCounter == 175)
+        hrmCounter = 100;
+
+    bpm[1] = hrmCounter;
+    ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), bpm, sizeof(bpm));
+  }
+}
+
+
+
+int main(void)
+{   
+     // DElete 
+    //Ticker ticker;
+    //ticker.attach_us(m_status_check_handle, 200000);
+    
+    pc.baud(9600);
+    //wait(8.0);
+    pc.printf("start\r\n");
+    pc.printf("Nordic_HRM Demo \r\n");
+    
+    // Init timer task
+    ticker_task1.attach(periodicCallback, 1);
+    
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    
+   
+   // setup adv_data and srp_data
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
+  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+  // set adv_type
+  ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    // add service
+  ble.addService(hrmService);
+  // set device name
+  ble.setDeviceName((const uint8_t *)DEVICE_NAME);
+  // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
+  ble.setTxPower(4);
+   
+   
+    // 100ms; in multiples of 0.625ms. 
+    ble.setAdvertisingInterval(160);
+
+    //ble.addService(uartService);
+    
+    // set adv_timeout, in seconds
+    ble.setAdvertisingTimeout(0);
+    
+    ble.startAdvertising(); 
+    
+    pc.printf("Advertising Start \r\n");
+    
+    /*while(1)
+    {
+        pc.printf("wait for event \r\n");
+        ble.waitForEvent(); 
+    }*/
+}
\ No newline at end of file