Heart Rate Monitor example for the BLE API using nRF51822 native mode drivers
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 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 "ble/BLE.h" 00019 #include "ble/services/HeartRateService.h" 00020 #include "ble/services/BatteryService.h" 00021 #include "ble/services/DeviceInformationService.h" 00022 00023 DigitalOut led1(LED1); 00024 00025 const static char DEVICE_NAME[] = "HRM1"; 00026 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00027 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00028 static volatile bool triggerSensorPolling = false; 00029 00030 uint8_t hrmCounter = 100; // init HRM to 100bps 00031 00032 HeartRateService *hrService; 00033 DeviceInformationService *deviceInfo; 00034 00035 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00036 { 00037 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising 00038 } 00039 00040 void periodicCallback(void) 00041 { 00042 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00043 00044 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00045 * heavy-weight sensor polling from the main thread. */ 00046 triggerSensorPolling = true; 00047 } 00048 00049 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00050 { 00051 BLE &ble = params->ble; 00052 ble_error_t error = params->error; 00053 00054 if (error != BLE_ERROR_NONE) { 00055 return; 00056 } 00057 00058 ble.gap().onDisconnection(disconnectionCallback); 00059 00060 /* Setup primary service. */ 00061 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 00062 00063 /* Setup auxiliary service. */ 00064 deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00065 00066 /* Setup advertising. */ 00067 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00068 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00069 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00070 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00071 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00072 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00073 ble.gap().startAdvertising(); 00074 } 00075 00076 int main(void) 00077 { 00078 led1 = 1; 00079 Ticker ticker; 00080 ticker.attach(periodicCallback, 1); // blink LED every second 00081 00082 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00083 ble.init(bleInitComplete); 00084 00085 /* SpinWait for initialization to complete. This is necessary because the 00086 * BLE object is used in the main loop below. */ 00087 while (ble.hasInitialized() == false) { /* spin loop */ } 00088 00089 // infinite loop 00090 while (1) { 00091 // check for trigger from periodicCallback() 00092 if (triggerSensorPolling && ble.getGapState().connected) { 00093 triggerSensorPolling = false; 00094 00095 // Do blocking calls or whatever is necessary for sensor polling. 00096 // In our case, we simply update the HRM measurement. 00097 hrmCounter++; 00098 if (hrmCounter == 175) { // 100 <= HRM bps <=175 00099 hrmCounter = 100; 00100 } 00101 00102 hrService->updateHeartRate(hrmCounter); 00103 } else { 00104 ble.waitForEvent(); // low power wait for event 00105 } 00106 } 00107 }
Generated on Tue Jul 12 2022 17:36:28 by 1.7.2