aaaa

Dependencies:   BLE_API mbed-events-master mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
fbdp1202
Date:
Mon Jun 12 04:52:55 2017 +0000
Revision:
80:e989b2f090f4
Parent:
75:8128a06c0a21
aaaa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fbdp1202 80:e989b2f090f4 1 /* mbed Microcontroller Library
fbdp1202 80:e989b2f090f4 2 * Copyright (c) 2006-2015 ARM Limited
fbdp1202 80:e989b2f090f4 3 *
fbdp1202 80:e989b2f090f4 4 * Licensed under the Apache License, Version 2.0 (the "License");
fbdp1202 80:e989b2f090f4 5 * you may not use this file except in compliance with the License.
fbdp1202 80:e989b2f090f4 6 * You may obtain a copy of the License at
fbdp1202 80:e989b2f090f4 7 *
fbdp1202 80:e989b2f090f4 8 * http://www.apache.org/licenses/LICENSE-2.0
fbdp1202 80:e989b2f090f4 9 *
fbdp1202 80:e989b2f090f4 10 * Unless required by applicable law or agreed to in writing, software
fbdp1202 80:e989b2f090f4 11 * distributed under the License is distributed on an "AS IS" BASIS,
fbdp1202 80:e989b2f090f4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fbdp1202 80:e989b2f090f4 13 * See the License for the specific language governing permissions and
fbdp1202 80:e989b2f090f4 14 * limitations under the License.
fbdp1202 80:e989b2f090f4 15 */
fbdp1202 80:e989b2f090f4 16
ktownsend 0:87a7fc231fae 17
fbdp1202 80:e989b2f090f4 18 #include <mbed.h>
fbdp1202 80:e989b2f090f4 19 #include "mbed-events-master/mbed_events.h"
fbdp1202 80:e989b2f090f4 20 #include "ble/BLE.h"
fbdp1202 80:e989b2f090f4 21 #include "ble/Gap.h"
fbdp1202 80:e989b2f090f4 22 #include "ble/services/HeartRateService.h"
fbdp1202 80:e989b2f090f4 23
fbdp1202 80:e989b2f090f4 24 DigitalOut led1(LED1, 1);
fbdp1202 80:e989b2f090f4 25
fbdp1202 80:e989b2f090f4 26
fbdp1202 80:e989b2f090f4 27 const static char DEVICE_NAME[] = "HRM";
fbdp1202 80:e989b2f090f4 28 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
ktownsend 0:87a7fc231fae 29
fbdp1202 80:e989b2f090f4 30
fbdp1202 80:e989b2f090f4 31 static uint8_t hrmCounter = 100; // init HRM to 100bps
fbdp1202 80:e989b2f090f4 32 static HeartRateService *hrServicePtr;
fbdp1202 80:e989b2f090f4 33
fbdp1202 80:e989b2f090f4 34 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
fbdp1202 80:e989b2f090f4 35
fbdp1202 80:e989b2f090f4 36
fbdp1202 80:e989b2f090f4 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
fbdp1202 80:e989b2f090f4 38 {
fbdp1202 80:e989b2f090f4 39 BLE::Instance().gap().startAdvertising(); // restart advertising
fbdp1202 80:e989b2f090f4 40 }
ktownsend 0:87a7fc231fae 41
fbdp1202 80:e989b2f090f4 42 //void connectionCallback ( const Gap :: ConnectionCallbackParams_t * params)
fbdp1202 80:e989b2f090f4 43 //{
fbdp1202 80:e989b2f090f4 44 // Gap::Handle_t gap_handle = params-> handle;
fbdp1202 80:e989b2f090f4 45 // Gap::ConnectionParams_t new_params;
fbdp1202 80:e989b2f090f4 46 //
fbdp1202 80:e989b2f090f4 47 // new_params.minConnectionInterval = 1000 ;
fbdp1202 80:e989b2f090f4 48 // new_params.maxConnectionInterval = 1000 ;
fbdp1202 80:e989b2f090f4 49 // new_params.connectionSupervisionTimeout = 30000;//3200 ;
fbdp1202 80:e989b2f090f4 50 // new_params.slaveLatency = 29 ;
fbdp1202 80:e989b2f090f4 51 // ble.gap().updateConnectionParams(gap_handle, & new_params);
fbdp1202 80:e989b2f090f4 52 //}
fbdp1202 80:e989b2f090f4 53
rgrover1 72:99c283dfe28d 54
fbdp1202 80:e989b2f090f4 55 void updateSensorValue() {
fbdp1202 80:e989b2f090f4 56 // Do blocking calls or whatever is necessary for sensor polling.
fbdp1202 80:e989b2f090f4 57 // In our case, we simply update the HRM measurement.
fbdp1202 80:e989b2f090f4 58 hrmCounter++;
fbdp1202 80:e989b2f090f4 59
rgrover1 72:99c283dfe28d 60
fbdp1202 80:e989b2f090f4 61 // 100 <= HRM bps <=175
fbdp1202 80:e989b2f090f4 62 if (hrmCounter == 175) {
fbdp1202 80:e989b2f090f4 63 hrmCounter = 100;
fbdp1202 80:e989b2f090f4 64 }
fbdp1202 80:e989b2f090f4 65
fbdp1202 80:e989b2f090f4 66
fbdp1202 80:e989b2f090f4 67 hrServicePtr->updateHeartRate(hrmCounter);
fbdp1202 80:e989b2f090f4 68 }
fbdp1202 80:e989b2f090f4 69
Rohit Grover 3:24e2b056d229 70
fbdp1202 80:e989b2f090f4 71 void periodicCallback(void)
fbdp1202 80:e989b2f090f4 72 {
fbdp1202 80:e989b2f090f4 73 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 47:430545f41113 74
fbdp1202 80:e989b2f090f4 75 if (BLE::Instance().getGapState().connected) {
fbdp1202 80:e989b2f090f4 76 eventQueue.call(updateSensorValue);
fbdp1202 80:e989b2f090f4 77 }
fbdp1202 80:e989b2f090f4 78 }
fbdp1202 80:e989b2f090f4 79
Rohit Grover 11:1d9aafee4984 80
fbdp1202 80:e989b2f090f4 81 void onBleInitError(BLE &ble, ble_error_t error)
fbdp1202 80:e989b2f090f4 82 {
fbdp1202 80:e989b2f090f4 83 (void)ble;
fbdp1202 80:e989b2f090f4 84 (void)error;
fbdp1202 80:e989b2f090f4 85 /* Initialization error handling should go here */
fbdp1202 80:e989b2f090f4 86 }
fbdp1202 80:e989b2f090f4 87
rgrover1 74:c9d58e7847c4 88
fbdp1202 80:e989b2f090f4 89 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
fbdp1202 80:e989b2f090f4 90 {
fbdp1202 80:e989b2f090f4 91 BLE& ble = params->ble;
fbdp1202 80:e989b2f090f4 92 ble_error_t error = params->error;
fbdp1202 80:e989b2f090f4 93
fbdp1202 80:e989b2f090f4 94 if (error != BLE_ERROR_NONE) {
fbdp1202 80:e989b2f090f4 95 onBleInitError(ble, error);
fbdp1202 80:e989b2f090f4 96 return;
fbdp1202 80:e989b2f090f4 97 }
fbdp1202 80:e989b2f090f4 98
fbdp1202 80:e989b2f090f4 99 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
fbdp1202 80:e989b2f090f4 100 return;
fbdp1202 80:e989b2f090f4 101 }
ktownsend 0:87a7fc231fae 102
rgrover1 65:cb76569f74f6 103 ble.gap().onDisconnection(disconnectionCallback);
ktownsend 0:87a7fc231fae 104
fbdp1202 80:e989b2f090f4 105 /* Setup primary service. */
fbdp1202 80:e989b2f090f4 106 hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
rgrover1 45:98c5a34b07a4 107
fbdp1202 80:e989b2f090f4 108 /* Setup advertising. */
fbdp1202 80:e989b2f090f4 109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
fbdp1202 80:e989b2f090f4 110 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
fbdp1202 80:e989b2f090f4 111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
fbdp1202 80:e989b2f090f4 112 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
fbdp1202 80:e989b2f090f4 113 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
fbdp1202 80:e989b2f090f4 114 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
fbdp1202 80:e989b2f090f4 115 ble.gap().startAdvertising();
fbdp1202 80:e989b2f090f4 116 printf("Hello!");
rgrover1 72:99c283dfe28d 117 }
rgrover1 72:99c283dfe28d 118
fbdp1202 80:e989b2f090f4 119 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
fbdp1202 80:e989b2f090f4 120 BLE &ble = BLE::Instance();
fbdp1202 80:e989b2f090f4 121 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
fbdp1202 80:e989b2f090f4 122 }
rgrover1 73:49b6090478e2 123
fbdp1202 80:e989b2f090f4 124 int main()
fbdp1202 80:e989b2f090f4 125 {
fbdp1202 80:e989b2f090f4 126 printf("Hello!");
fbdp1202 80:e989b2f090f4 127 eventQueue.call_every(500, periodicCallback);
fbdp1202 80:e989b2f090f4 128 BLE &ble = BLE::Instance();
fbdp1202 80:e989b2f090f4 129 ble.onEventsToProcess(scheduleBleEventsProcessing);
fbdp1202 80:e989b2f090f4 130 ble.init(bleInitComplete);
fbdp1202 80:e989b2f090f4 131 // ble.gap().onConnection(connectionCallback);
Rohit Grover 36:ea2a1b4f51c1 132
fbdp1202 80:e989b2f090f4 133 eventQueue.dispatch_forever();
rgrover1 67:b2d2dee347c0 134
fbdp1202 80:e989b2f090f4 135 return 0;
fbdp1202 80:e989b2f090f4 136 }