Heart Rate Monitor example for the BLE API using nRF51822 native mode drivers

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
ankuraga
Date:
Mon Aug 31 05:47:55 2015 +0000
Revision:
72:fa6520343b72
Parent:
71:ecf479422c04
Child:
73:82447ab9efd8
Added while loop for ble event

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 /* mbed Microcontroller Library
rgrover1 67:b2d2dee347c0 2 * Copyright (c) 2006-2015 ARM Limited
ktownsend 0:87a7fc231fae 3 *
ktownsend 0:87a7fc231fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:87a7fc231fae 5 * you may not use this file except in compliance with the License.
ktownsend 0:87a7fc231fae 6 * You may obtain a copy of the License at
ktownsend 0:87a7fc231fae 7 *
ktownsend 0:87a7fc231fae 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:87a7fc231fae 9 *
ktownsend 0:87a7fc231fae 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:87a7fc231fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:87a7fc231fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:87a7fc231fae 13 * See the License for the specific language governing permissions and
ktownsend 0:87a7fc231fae 14 * limitations under the License.
ktownsend 0:87a7fc231fae 15 */
ktownsend 0:87a7fc231fae 16
ktownsend 0:87a7fc231fae 17 #include "mbed.h"
rgrover1 67:b2d2dee347c0 18 #include "ble/BLE.h"
rgrover1 67:b2d2dee347c0 19 #include "ble/services/HeartRateService.h"
rgrover1 67:b2d2dee347c0 20 #include "ble/services/BatteryService.h"
rgrover1 67:b2d2dee347c0 21 #include "ble/services/DeviceInformationService.h"
ankuraga 71:ecf479422c04 22 #include "PulseSensor.h"
ktownsend 0:87a7fc231fae 23
rgrover1 67:b2d2dee347c0 24 BLE ble;
rgrover1 47:430545f41113 25 DigitalOut led1(LED1);
ktownsend 0:87a7fc231fae 26
mbedAustin 55:3a7d497a3e03 27 const static char DEVICE_NAME[] = "HRM1";
rgrover1 42:06ebef2e0e44 28 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
rgrover1 42:06ebef2e0e44 29 GattService::UUID_DEVICE_INFORMATION_SERVICE};
rgrover1 39:6390604f904c 30 static volatile bool triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 31
rgrover1 41:9cef0129da5f 32 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
ktownsend 0:87a7fc231fae 33 {
rgrover1 66:c09ddf226b9c 34 ble.gap().startAdvertising(); // restart advertising
rgrover1 7:daab8ba5139e 35 }
Rohit Grover 3:24e2b056d229 36
ankuraga 71:ecf479422c04 37 HeartRateService * hrService;
ankuraga 71:ecf479422c04 38 void sendDataToProcessing(int hrmCounter);
Rohit Grover 11:1d9aafee4984 39
ktownsend 0:87a7fc231fae 40 int main(void)
ktownsend 0:87a7fc231fae 41 {
rgrover1 47:430545f41113 42 led1 = 1;
ankuraga 71:ecf479422c04 43
Rohit Grover 15:7ba28817e31e 44 ble.init();
rgrover1 65:cb76569f74f6 45 ble.gap().onDisconnection(disconnectionCallback);
ktownsend 0:87a7fc231fae 46
rgrover1 45:98c5a34b07a4 47 /* Setup primary service. */
mbedAustin 55:3a7d497a3e03 48 uint8_t hrmCounter = 100; // init HRM to 100bps
ankuraga 71:ecf479422c04 49 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
rgrover1 45:98c5a34b07a4 50
mbedAustin 55:3a7d497a3e03 51 /* Setup auxiliary service. */
rgrover1 45:98c5a34b07a4 52 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
rgrover1 45:98c5a34b07a4 53
rgrover1 45:98c5a34b07a4 54 /* Setup advertising. */
rgrover1 65:cb76569f74f6 55 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 65:cb76569f74f6 56 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 65:cb76569f74f6 57 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rgrover1 65:cb76569f74f6 58 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 65:cb76569f74f6 59 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 67:b2d2dee347c0 60 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
rgrover1 65:cb76569f74f6 61 ble.gap().startAdvertising();
ankuraga 71:ecf479422c04 62
ankuraga 71:ecf479422c04 63 PulseSensor sensor(P0_5, sendDataToProcessing);
ankuraga 71:ecf479422c04 64 sensor.start();
ankuraga 72:fa6520343b72 65
ankuraga 72:fa6520343b72 66 while (1){
ankuraga 72:fa6520343b72 67 ble.waitForEvent();
ankuraga 72:fa6520343b72 68 }
ankuraga 72:fa6520343b72 69
Rohit Grover 3:24e2b056d229 70
mbedAustin 55:3a7d497a3e03 71 // infinite loop
ankuraga 71:ecf479422c04 72 /*
mbedAustin 55:3a7d497a3e03 73 while (1) {
mbedAustin 55:3a7d497a3e03 74 // check for trigger from periodicCallback()
rgrover1 50:477004d54431 75 if (triggerSensorPolling && ble.getGapState().connected) {
Rohit Grover 36:ea2a1b4f51c1 76 triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 77
mbedAustin 55:3a7d497a3e03 78 // Do blocking calls or whatever is necessary for sensor polling.
rgrover1 67:b2d2dee347c0 79 // In our case, we simply update the HRM measurement.
Rohit Grover 36:ea2a1b4f51c1 80 hrmCounter++;
rgrover1 67:b2d2dee347c0 81
mbedAustin 55:3a7d497a3e03 82 // 100 <= HRM bps <=175
Rohit Grover 36:ea2a1b4f51c1 83 if (hrmCounter == 175) {
Rohit Grover 36:ea2a1b4f51c1 84 hrmCounter = 100;
Rohit Grover 36:ea2a1b4f51c1 85 }
rgrover1 67:b2d2dee347c0 86
mbedAustin 55:3a7d497a3e03 87 // update bps
ankuraga 71:ecf479422c04 88 sendDataToProcessing(hrmCounter);
Rohit Grover 36:ea2a1b4f51c1 89 } else {
mbedAustin 55:3a7d497a3e03 90 ble.waitForEvent(); // low power wait for event
Rohit Grover 36:ea2a1b4f51c1 91 }
ankuraga 71:ecf479422c04 92 }*/
ktownsend 0:87a7fc231fae 93 }
ankuraga 71:ecf479422c04 94
ankuraga 71:ecf479422c04 95 //PulseSensor.h expects a function with argument (int)..you can't use uint8 here
ankuraga 71:ecf479422c04 96 void sendDataToProcessing(int hrmCounter){
ankuraga 71:ecf479422c04 97 hrService->updateHeartRate((uint8_t)hrmCounter);
ankuraga 71:ecf479422c04 98 //or you could just cast it to unit8..this just eases debugging
ankuraga 71:ecf479422c04 99 //int* ans = &hrmCounter;
ankuraga 71:ecf479422c04 100 }
ankuraga 71:ecf479422c04 101