add dfu

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

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 #include "ble/services/DFUService.h"
00021 #include "ble/services/BatteryService.h"
00022 #include "ble/services/DeviceInformationService.h"
00023 
00024 DigitalOut led1(LED1);
00025 
00026 const static char     DEVICE_NAME[]        = "HRM1";
00027 static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
00028                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
00029 static volatile bool  triggerSensorPolling = false;
00030 
00031 uint8_t hrmCounter = 100; // init HRM to 100bps
00032 
00033 HeartRateService         *hrService;
00034 DeviceInformationService *deviceInfo;
00035 DFUService *dfuService;
00036 
00037 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00038 {
00039     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
00040 }
00041 
00042 void periodicCallback(void)
00043 {
00044     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00045 
00046     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00047      * heavy-weight sensor polling from the main thread. */
00048     triggerSensorPolling = true;
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         return;
00058     }
00059 
00060     ble.gap().onDisconnection(disconnectionCallback);
00061 
00062     dfuService = new DFUService(ble);
00063     
00064     /* Setup primary service. */
00065     hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
00066 
00067     /* Setup auxiliary service. */
00068     deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00069 
00070     /* Setup advertising. */
00071     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00072     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00073     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
00074     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00075     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00076     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00077     ble.gap().startAdvertising();
00078 }
00079 
00080 int main(void)
00081 {
00082     led1 = 1;
00083     Ticker ticker;
00084     ticker.attach(periodicCallback, 1); // blink LED every second
00085 
00086     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00087     ble.init(bleInitComplete);
00088 
00089     /* SpinWait for initialization to complete. This is necessary because the
00090      * BLE object is used in the main loop below. */
00091     while (ble.hasInitialized()  == false) { /* spin loop */ }
00092 
00093     // infinite loop
00094     while (1) {
00095         // check for trigger from periodicCallback()
00096         if (triggerSensorPolling && ble.getGapState().connected) {
00097             triggerSensorPolling = false;
00098 
00099             // Do blocking calls or whatever is necessary for sensor polling.
00100             // In our case, we simply update the HRM measurement.
00101             hrmCounter++;
00102             if (hrmCounter == 175) { //  100 <= HRM bps <=175
00103                 hrmCounter = 100;
00104             }
00105 
00106             hrService->updateHeartRate(hrmCounter);
00107         } else {
00108             ble.waitForEvent(); // low power wait for event
00109         }
00110     }
00111 }