heart rate monitor
Dependencies: BLE_API GroveEarbudSensor mbed nRF51822
Fork of BLE_HeartRate by
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 // Grove Earbud Sensor include 00023 #include "GroveEarbudSensor.h" 00024 BLE ble; 00025 DigitalOut led1(LED1); 00026 // Our sensor as an InterruptIn 00027 InterruptIn sensor(P0_0); 00028 00029 const static char DEVICE_NAME[] = "HRM1"; 00030 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00031 GattService::UUID_DEVICE_INFORMATION_SERVICE 00032 }; 00033 static volatile bool triggerSensorPolling = false; 00034 /* Setup primary service. */ 00035 uint8_t hrmCounter = 100; // init HRM to 100bps 00036 00037 00038 // callback for receiving heartrate values 00039 void heartrateCallback(float heartrate,void *data) 00040 { 00041 printf("Callback: heartrate = %.1f\r\n",heartrate); 00042 // we can also call directly 00043 hrmCounter=heartrate; 00044 00045 00046 // update bps 00047 //hrService.updateHeartRate(hrmCounter); 00048 } 00049 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00050 { 00051 ble.gap().startAdvertising(); // restart advertising 00052 } 00053 00054 void periodicCallback(void) 00055 { 00056 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00057 00058 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00059 * heavy-weight sensor polling from the main thread. */ 00060 triggerSensorPolling = true; 00061 } 00062 00063 int main(void) 00064 { 00065 led1 = 1; 00066 Ticker ticker; 00067 ticker.attach(periodicCallback, 1); // blink LED every second 00068 00069 ble.init(); 00070 ble.gap().onDisconnection(disconnectionCallback); 00071 00072 00073 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 00074 00075 /* Setup auxiliary service. */ 00076 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00077 00078 /* Setup advertising. */ 00079 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00080 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00081 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00082 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00083 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00084 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00085 ble.gap().startAdvertising(); 00086 // announce 00087 printf("Grove Earbud Sensor - Montoring Heart Rate\r\n"); 00088 00089 // allocate the earbud sensor 00090 printf("Allocating earbud sensor instance...\r\n"); 00091 GroveEarbudSensor earbud(&sensor); 00092 00093 // register our callback function 00094 printf("registering callback...\r\n"); 00095 earbud.registerCallback(heartrateCallback); 00096 00097 // infinite loop 00098 while (1) { 00099 // check for trigger from periodicCallback() 00100 if (triggerSensorPolling && ble.getGapState().connected) { 00101 triggerSensorPolling = false; 00102 00103 // Do blocking calls or whatever is necessary for sensor polling. 00104 // In our case, we simply update the HRM measurement. 00105 // hrmCounter++; 00106 00107 // 100 <= HRM bps <=175 00108 /*if (hrmCounter == 175) { 00109 hrmCounter = 100; 00110 }*/ 00111 00112 // blink... 00113 led1 = !led1; 00114 wait(0.5); 00115 hrService.updateHeartRate(hrmCounter); 00116 } else { 00117 ble.waitForEvent(); // low power wait for event 00118 } 00119 } 00120 }
Generated on Thu Jul 14 2022 00:53:13 by 1.7.2