Tested with Nucleo L476RG

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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