BLE demo for the Health-Thermometer service.

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

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-2013 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/HealthThermometerService.h"
00020 
00021 DigitalOut led1(LED1);
00022 
00023 static HealthThermometerService *thermometerServicePtr;
00024 
00025 static const char     DEVICE_NAME[]        = "Therm";
00026 static const uint16_t uuid16_list[]        = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
00027 static volatile bool  triggerSensorPolling = false;
00028 static float          currentTemperature   = 39.6;
00029 
00030 /* Restart Advertising on disconnection*/
00031 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00032 {
00033     BLE::Instance().gap().startAdvertising();
00034 }
00035 
00036 void periodicCallback(void)
00037 {
00038     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00039 
00040     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00041      * heavy-weight sensor polling from the main thread. */
00042     triggerSensorPolling = true;
00043 }
00044 
00045 /**
00046  * This function is called when the ble initialization process has failed
00047  */
00048 void onBleInitError(BLE &ble, ble_error_t error)
00049 {
00050     /* Avoid compiler warnings */
00051     (void) ble;
00052     (void) error;
00053     /* Initialization error handling should go here */
00054 }
00055 
00056 /**
00057  * Callback triggered when the ble initialization process has finished
00058  */
00059 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00060 {
00061     BLE&        ble   = params->ble;
00062     ble_error_t error = params->error;
00063 
00064     if (error != BLE_ERROR_NONE) {
00065         /* In case of error, forward the error handling to onBleInitError */
00066         onBleInitError(ble, error);
00067         return;
00068     }
00069 
00070     /* Ensure that it is the default instance of BLE */
00071     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00072         return;
00073     }
00074 
00075     ble.gap().onDisconnection(disconnectionCallback);
00076 
00077     /* Setup primary service. */
00078     thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR);
00079 
00080     /* setup advertising */
00081     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00082     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00083     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
00084     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00085     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00086     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00087     ble.gap().startAdvertising();    
00088 }
00089 
00090 int main(void)
00091 {
00092     led1 = 1;
00093     Ticker ticker;
00094     ticker.attach(periodicCallback, 1);
00095 
00096     BLE &ble = BLE::Instance();
00097     ble.init(bleInitComplete);
00098     
00099     /* SpinWait for initialization to complete. This is necessary because the
00100      * BLE object is used in the main loop below. */
00101     while (ble.hasInitialized()  == false) { /* spin loop */ }
00102 
00103     while (true) {
00104         if (triggerSensorPolling && ble.gap().getState().connected) {
00105             triggerSensorPolling = false;
00106 
00107             /* In our case, we simply update the dummy temperature measurement. */
00108             currentTemperature += 0.1;
00109             thermometerServicePtr->updateTemperature(currentTemperature);
00110         } else {
00111             ble.waitForEvent();
00112         }
00113     }
00114 }