Hello World program for this deprecated board, based on older versions of BLE_API (rev1130), X_NUCLEO_IDB0XA1 (rev212) and mbed (rev114) libraries.

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