Heart Rate Monitor example for the BLE API using ST BlueNRG native drivers
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
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 00021 #if !defined(IDB0XA1_D13_PATCH) 00022 DigitalOut led1(LED1, 1); // LED conflicts SPI_CLK in case of D13 patch 00023 #endif 00024 00025 const static char DEVICE_NAME[] = "HRM1"; 00026 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; 00027 00028 static volatile bool triggerSensorPolling = false; 00029 00030 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00031 { 00032 (void)params; 00033 BLE::Instance().gap().startAdvertising(); // restart advertising 00034 } 00035 00036 void periodicCallback(void) 00037 { 00038 #if !defined(IDB0XA1_D13_PATCH) 00039 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00040 #endif 00041 00042 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00043 * heavy-weight sensor polling from the main thread. */ 00044 triggerSensorPolling = true; 00045 } 00046 00047 void onBleInitError(BLE &ble, ble_error_t error) 00048 { 00049 (void)ble; 00050 (void)error; 00051 /* Initialization error handling should go here */ 00052 } 00053 00054 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00055 { 00056 BLE& ble = params->ble; 00057 ble_error_t error = params->error; 00058 00059 if (error != BLE_ERROR_NONE) { 00060 onBleInitError(ble, error); 00061 return; 00062 } 00063 00064 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00065 return; 00066 } 00067 00068 ble.gap().onDisconnection(disconnectionCallback); 00069 00070 /* Setup primary service. */ 00071 uint8_t hrmCounter = 60; // init HRM to 60bps 00072 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 00073 00074 /* Setup advertising. */ 00075 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00076 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00077 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00078 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00079 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00080 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00081 ble.gap().startAdvertising(); 00082 00083 // infinite loop 00084 while (true) { 00085 // check for trigger from periodicCallback() 00086 if (triggerSensorPolling && ble.getGapState().connected) { 00087 triggerSensorPolling = false; 00088 00089 // Do blocking calls or whatever is necessary for sensor polling. 00090 // In our case, we simply update the HRM measurement. 00091 hrmCounter++; 00092 00093 // 60 <= HRM bps <= 100 00094 if (hrmCounter == 100) { 00095 hrmCounter = 60; 00096 } 00097 00098 // update bps 00099 hrService.updateHeartRate(hrmCounter); 00100 } else { 00101 ble.waitForEvent(); // low power wait for event 00102 } 00103 } 00104 } 00105 00106 int main(void) 00107 { 00108 Ticker ticker; 00109 ticker.attach(periodicCallback, 1); // blink LED every second 00110 00111 BLE::Instance().init(bleInitComplete); 00112 } 00113
Generated on Tue Jul 12 2022 14:53:28 by 1.7.2