Test using the base BLE_HeartRate example to see the effect of increasing the "ticker" rate on stability (intending to do AnalogIn around 1ms rate)

Dependencies:   BLE_API mbed nRF51822

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 "BLEDevice.h"
00019 #include "HeartRateService.h"
00020 #include "BatteryService.h"
00021 #include "DeviceInformationService.h"
00022 
00023 BLEDevice  ble;
00024 DigitalOut indicatorLed1(LED1);
00025 DigitalOut indicatorLed2(LED2);
00026 
00027 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
00028                                * it will have an impact on code-size and power consumption. */
00029 
00030 #if NEED_CONSOLE_OUTPUT
00031 Serial  pc(USBTX, USBRX);
00032 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
00033 #else
00034 #define DEBUG(...) /* nothing */
00035 #endif /* #if NEED_CONSOLE_OUTPUT */
00036 
00037 // Sample interval (uS)
00038 volatile uint32_t sampleIntervalUs = 5000;
00039 
00040 // Timer to do the sampling etc
00041 Timer intervalTimer;
00042 Timeout sampleTimeout;
00043 
00044 const static char     DEVICE_NAME[]        = "HRMonitor";
00045 static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
00046                                               GattService::UUID_BATTERY_SERVICE,
00047                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
00048 static volatile bool  triggerSensorPolling = false;
00049 
00050 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00051 {
00052     DEBUG("Disconnected handle %u!\n\r", handle);
00053     DEBUG("Restarting the advertising process\n\r");
00054     ble.startAdvertising();
00055 }
00056 
00057 static volatile int callbackCount = 0;
00058 void periodicCallback(void)
00059 {
00060     callbackCount++;
00061     if (callbackCount == 200)
00062     {
00063         callbackCount = 0;
00064         indicatorLed1 = !indicatorLed1;
00065     }       
00066     
00067     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00068      * heavy-weight sensor polling from the main thread. */
00069     triggerSensorPolling = true;
00070 //    sampleTimeout.attach_us(periodicCallback, sampleIntervalUs);
00071 }
00072 
00073 int bleUpdateCount = 0;
00074 int main(void)
00075 {
00076     indicatorLed1 = 1;
00077     indicatorLed2 = 1;
00078     
00079     // Start timer and sampling
00080     intervalTimer.start();
00081 //    sampleTimeout.attach_us(periodicCallback, sampleIntervalUs);
00082     
00083     Ticker ticker;
00084 //    ticker.attach(periodicCallback, 1);
00085     ticker.attach_us(periodicCallback, sampleIntervalUs);
00086 
00087     DEBUG("Initialising the nRF51822\n\r");
00088     ble.init();
00089     ble.onDisconnection(disconnectionCallback);
00090 
00091     /* setup advertising */
00092     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00093     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00094     ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
00095     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00096     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00097     ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */
00098     ble.startAdvertising();
00099 
00100     uint8_t hrmCounter = 100;
00101     HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
00102     BatteryService battery(ble);
00103     DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00104 
00105     while (true) 
00106     {
00107         bool updateNow = false;
00108         if (triggerSensorPolling) 
00109         {
00110             triggerSensorPolling = false;
00111             bleUpdateCount++;
00112             if (bleUpdateCount >= 200)
00113             {
00114                 bleUpdateCount = 0;
00115                 indicatorLed2 = !indicatorLed2;
00116                 updateNow = true;
00117             }
00118         }
00119         if (updateNow)
00120         {
00121             /* Do blocking calls or whatever is necessary for sensor polling. */
00122             /* In our case, we simply update the dummy HRM measurement. */
00123             hrmCounter++;
00124             if (hrmCounter == 175) 
00125             {
00126                 hrmCounter = 100;
00127             }
00128 
00129             hrService.updateHeartRate(hrmCounter);
00130         } 
00131         else 
00132         {
00133             ble.waitForEvent();
00134         }
00135     }
00136 }