Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_IDB0XA1 BLE_API
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 "HeartRateService.h" 00020 #include "BatteryService.h" 00021 #include "DeviceInformationService.h" 00022 00023 Serial pc1(USBTX, USBRX); 00024 #define DEBUG(...) { pc1.printf(__VA_ARGS__); } 00025 00026 // ??? Initialize Ticker and DigitalOut objects 00027 // 1 DigitalOut object for Led 1 00028 // 2 Ticker objects: for aliveness and updating measurements 00029 DigitalOut ??????????? 00030 Ticker ????????; 00031 Ticker ????????; 00032 00033 // Initialize variables and tables 00034 // Define the device name (add your group number) 00035 const static char ?????????[] = "MY_BLE_HRM??"; 00036 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00037 GattService::UUID_BATTERY_SERVICE, 00038 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00039 00040 // ??? define flag for measurement updating 00041 static volatile bool ????????????????????????? 00042 00043 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00044 { 00045 DEBUG("Disconnected!\n\r"); 00046 DEBUG("Restarting the advertising process\n\r"); 00047 (void)params; 00048 // ??? Restart advertising 00049 ?????????????????????????? 00050 } 00051 00052 // Connection Handler 00053 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00054 { 00055 DEBUG("Connected\r\n"); 00056 } 00057 00058 // ??? Define update Characteristics Handler update_handler 00059 // It must set the flag for update the heart rate 00060 void ???????????(void) 00061 { 00062 ?????????????????????????? 00063 } 00064 00065 // ??? Define the Aliveness Handler, it must toggle the led 1 00066 void aliveness_handler (void) 00067 { 00068 ???????????????????????? 00069 } 00070 00071 00072 void onBleInitError(BLE &ble, ble_error_t error) 00073 { 00074 (void)ble; 00075 (void)error; 00076 /* Initialization error handling should go here */ 00077 } 00078 00079 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00080 { 00081 BLE& ble = params->ble; 00082 ble_error_t error = params->error; 00083 00084 // ??? Attach ticker objects to functions 00085 // Two handlers: 00086 // 1. aliveness_handler, with 500 ms timing, which toggles led1 00087 // 2. update_handler, with 2 s timing, which sets a flag for the heart rate updating 00088 ??????????????????????????? 00089 ??????????????????????????? 00090 00091 DEBUG("Initialising \n\r"); 00092 00093 if (error != BLE_ERROR_NONE) { 00094 onBleInitError(ble, error); 00095 return; 00096 } 00097 00098 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00099 return; 00100 } 00101 00102 ble.gap().onDisconnection(disconnectionCallback); 00103 ble.gap().onConnection(connectionCallback); 00104 00105 // Setup BLE device services 00106 00107 /* ??? Setup primary service. */ 00108 // Define and init variable for heart rate simulation (it can be a uint8_t) 00109 uint8_t ????????????????? = ???; 00110 /* Declare and init (call constructor) HeartRateService object 00111 * Constructor is: 00112 * HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) 00113 * for location you can use HeartRateService::LOCATION_FINGER */ 00114 HeartRateService ????????????????; 00115 00116 /* ??? Setup battery level service. */ 00117 // Define and init variable for battery level simulation (it can be a uint8_t) 00118 uint8_t ?????????? = ???; 00119 /* Declare and init (call constructor) BatteryService object 00120 * Constructor is: 00121 * BatteryService(BLE &_ble, uint8_t level = 100) */ 00122 BatteryService ????????????????; 00123 /* Declare and init (call constructor) DeviceInformationService object 00124 * Constructor is: 00125 * DeviceInformationService(BLE &_ble, 00126 const char *manufacturersName = NULL, 00127 const char *modelNumber = NULL, 00128 const char *serialNumber = NULL, 00129 const char *hardwareRevision = NULL, 00130 const char *firmwareRevision = NULL, 00131 const char *softwareRevision = NULL) 00132 * You can use manufacturerName = "ST", modelNumber = "Nucleo", serialNumber = "SN1"*/ 00133 DeviceInformationService ???????????????; 00134 00135 /* Setup advertising. */ 00136 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00137 // ??? Add the complete list of services provided to the advertising payload (GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS) 00138 ble.gap().?????????????????????????????????? 00139 // ??? Add the heart rate service to the advertising payload (GapAdvertisingData::GENERIC_HEART_RATE_SENSOR) 00140 ble.gap().?????????????????????????????????? 00141 // ??? Add the device name to the advertising payload (GapAdvertisingData::COMPLETE_LOCAL_NAME) 00142 ble.gap().?????????????????????????????????? 00143 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00144 // Set advertising interval to 1 s 00145 ble.gap().?????????????????????????????????? 00146 // ??? Start advertising, call the appropriate methods of the GAP object 00147 ble.gap().????????????????; 00148 00149 // infinite loop 00150 while (true) { 00151 // check for from update_handler() 00152 if (???????????????????? && ble.getGapState().connected) { 00153 //??? Reset flag 00154 ????????????????????? 00155 00156 // ??? Update the heart rate counter 00157 // increase by 1. If equal to 150, then reset to 80 00158 ????????????? 00159 ????????????? 00160 ????????????? 00161 ????????????? 00162 00163 // ??? Update value of heart rate service 00164 ????????????????????????? 00165 } else { 00166 ble.waitForEvent(); // low power wait for event 00167 } 00168 } 00169 } 00170 00171 int main(void) 00172 { 00173 BLE::Instance().init(bleInitComplete); 00174 } 00175
Generated on Sun Aug 14 2022 17:21:06 by
1.7.2