Example to simulate photoplethysmography for heart rate and transmit via BLE
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 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 "x_nucleo_6180xa1.h" 00021 00022 #define VL6180X_I2C_SDA D14 00023 #define VL6180X_I2C_SCL D15 00024 00025 static X_NUCLEO_6180XA1 *board=NULL; 00026 00027 DigitalOut led1(LED1, 1); 00028 00029 const static char DEVICE_NAME[] = "HRMFR16"; 00030 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; 00031 00032 static volatile bool triggerSensorPolling = false; 00033 00034 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00035 { 00036 (void)params; 00037 BLE::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 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00044 * heavy-weight sensor polling from the main thread. */ 00045 triggerSensorPolling = true; 00046 } 00047 00048 void onBleInitError(BLE &ble, ble_error_t error) 00049 { 00050 (void)ble; 00051 (void)error; 00052 /* Initialization error handling should go here */ 00053 } 00054 00055 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00056 { 00057 BLE& ble = params->ble; 00058 ble_error_t error = params->error; 00059 00060 if (error != BLE_ERROR_NONE) { 00061 onBleInitError(ble, error); 00062 return; 00063 } 00064 00065 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00066 return; 00067 } 00068 00069 ble.gap().onDisconnection(disconnectionCallback); 00070 00071 /* Setup primary service. */ 00072 uint8_t hrmCounter = 100; // init HRM to 100bps 00073 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); 00074 00075 uint32_t lux, dist; 00076 uint32_t luxThreshold = 250; 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 00087 // infinite loop 00088 while (true) { 00089 // check for trigger from periodicCallback() 00090 if (triggerSensorPolling && ble.getGapState().connected) { 00091 triggerSensorPolling = false; 00092 00093 board->sensor_top->GetDistance(&dist); 00094 board->sensor_top->GetLux(&lux); 00095 00096 if (lux<=luxThreshold) { 00097 hrmCounter = 95; 00098 } else { 00099 hrmCounter = 65; 00100 } 00101 00102 // update bps 00103 hrService.updateHeartRate(hrmCounter); 00104 } else { 00105 ble.waitForEvent(); // low power wait for event 00106 } 00107 } 00108 } 00109 00110 int main(void) 00111 { 00112 int status; 00113 00114 Ticker ticker; 00115 ticker.attach(periodicCallback, 1); // blink LED every second 00116 00117 DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); 00118 /* creates the 6180XA1 expansion board singleton obj */ 00119 board=X_NUCLEO_6180XA1::Instance(device_i2c, NC,NC,NC,NC);//A3, A2, D13, D2); 00120 /* init the 6180XA1 expansion board with default values */ 00121 status=board->InitBoard(); 00122 if(status) { printf("Failed to init board!\n\r"); return 0; } 00123 00124 BLE::Instance().init(bleInitComplete); 00125 } 00126
Generated on Sat Jul 16 2022 11:42:25 by 1.7.2