Heart Rate Monitor example for the BLE API using nRF51822 native mode drivers
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "BLEDevice.h" 00019 #include "HeartRateService.h" 00020 #include "DeviceInformationService.h" 00021 00022 /* Enable the following if you need to throttle the connection interval. This has 00023 * the effect of reducing energy consumption after a connection is made; 00024 * particularly for applications where the central may want a fast connection 00025 * interval.*/ 00026 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0 00027 00028 BLEDevice ble; 00029 DigitalOut led1(LED1); 00030 00031 const static char DEVICE_NAME[] = "HRM1"; 00032 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00033 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00034 static volatile bool triggerSensorPolling = false; 00035 00036 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00037 { 00038 ble.startAdvertising(); // restart advertising 00039 } 00040 00041 void periodicCallback(void) 00042 { 00043 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00044 00045 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00046 * heavy-weight sensor polling from the main thread. */ 00047 triggerSensorPolling = true; 00048 } 00049 00050 int main(void) 00051 { 00052 led1 = 1; 00053 Ticker ticker; 00054 ticker.attach(periodicCallback, 1); // blink LED every second 00055 00056 ble.init(); 00057 ble.onDisconnection(disconnectionCallback); 00058 00059 /* Setup primary service. */ 00060 uint8_t hrmCounter = 100; // init HRM to 100bps 00061 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 00062 00063 /* Setup auxiliary service. */ 00064 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00065 00066 /* Setup advertising. */ 00067 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00068 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00069 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00070 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00071 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00072 ble.setAdvertisingInterval(1000); 00073 ble.startAdvertising(); 00074 00075 // infinite loop 00076 while (1) { 00077 // check for trigger from periodicCallback() 00078 if (triggerSensorPolling && ble.getGapState().connected) { 00079 triggerSensorPolling = false; 00080 00081 // Do blocking calls or whatever is necessary for sensor polling. 00082 // In our case, we simply update the HRM measurement. 00083 hrmCounter++; 00084 00085 // 100 <= HRM bps <=175 00086 if (hrmCounter == 175) { 00087 hrmCounter = 100; 00088 } 00089 00090 // update bps 00091 hrService.updateHeartRate(hrmCounter); 00092 } else { 00093 ble.waitForEvent(); // low power wait for event 00094 } 00095 } 00096 }
Generated on Mon Aug 1 2022 05:11:01 by 1.7.2