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

Committer:
Bobty
Date:
Mon Sep 29 10:33:30 2014 +0000
Revision:
0:ec36dec086bb
Child:
1:259fe4d2ab16
First working with indicators

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 0:ec36dec086bb 1 /* mbed Microcontroller Library
Bobty 0:ec36dec086bb 2 * Copyright (c) 2006-2013 ARM Limited
Bobty 0:ec36dec086bb 3 *
Bobty 0:ec36dec086bb 4 * Licensed under the Apache License, Version 2.0 (the "License");
Bobty 0:ec36dec086bb 5 * you may not use this file except in compliance with the License.
Bobty 0:ec36dec086bb 6 * You may obtain a copy of the License at
Bobty 0:ec36dec086bb 7 *
Bobty 0:ec36dec086bb 8 * http://www.apache.org/licenses/LICENSE-2.0
Bobty 0:ec36dec086bb 9 *
Bobty 0:ec36dec086bb 10 * Unless required by applicable law or agreed to in writing, software
Bobty 0:ec36dec086bb 11 * distributed under the License is distributed on an "AS IS" BASIS,
Bobty 0:ec36dec086bb 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bobty 0:ec36dec086bb 13 * See the License for the specific language governing permissions and
Bobty 0:ec36dec086bb 14 * limitations under the License.
Bobty 0:ec36dec086bb 15 */
Bobty 0:ec36dec086bb 16
Bobty 0:ec36dec086bb 17 #include "mbed.h"
Bobty 0:ec36dec086bb 18 #include "BLEDevice.h"
Bobty 0:ec36dec086bb 19 #include "HeartRateService.h"
Bobty 0:ec36dec086bb 20 #include "BatteryService.h"
Bobty 0:ec36dec086bb 21 #include "DeviceInformationService.h"
Bobty 0:ec36dec086bb 22
Bobty 0:ec36dec086bb 23 BLEDevice ble;
Bobty 0:ec36dec086bb 24 DigitalOut led1(LED1);
Bobty 0:ec36dec086bb 25 DigitalOut indicatorLed2(LED2);
Bobty 0:ec36dec086bb 26
Bobty 0:ec36dec086bb 27 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Bobty 0:ec36dec086bb 28 * it will have an impact on code-size and power consumption. */
Bobty 0:ec36dec086bb 29
Bobty 0:ec36dec086bb 30 #if NEED_CONSOLE_OUTPUT
Bobty 0:ec36dec086bb 31 Serial pc(USBTX, USBRX);
Bobty 0:ec36dec086bb 32 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Bobty 0:ec36dec086bb 33 #else
Bobty 0:ec36dec086bb 34 #define DEBUG(...) /* nothing */
Bobty 0:ec36dec086bb 35 #endif /* #if NEED_CONSOLE_OUTPUT */
Bobty 0:ec36dec086bb 36
Bobty 0:ec36dec086bb 37 const static char DEVICE_NAME[] = "HRMonitor";
Bobty 0:ec36dec086bb 38 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
Bobty 0:ec36dec086bb 39 GattService::UUID_BATTERY_SERVICE,
Bobty 0:ec36dec086bb 40 GattService::UUID_DEVICE_INFORMATION_SERVICE};
Bobty 0:ec36dec086bb 41 static volatile bool triggerSensorPolling = false;
Bobty 0:ec36dec086bb 42
Bobty 0:ec36dec086bb 43 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
Bobty 0:ec36dec086bb 44 {
Bobty 0:ec36dec086bb 45 DEBUG("Disconnected handle %u!\n\r", handle);
Bobty 0:ec36dec086bb 46 DEBUG("Restarting the advertising process\n\r");
Bobty 0:ec36dec086bb 47 ble.startAdvertising();
Bobty 0:ec36dec086bb 48 }
Bobty 0:ec36dec086bb 49
Bobty 0:ec36dec086bb 50 void periodicCallback(void)
Bobty 0:ec36dec086bb 51 {
Bobty 0:ec36dec086bb 52 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
Bobty 0:ec36dec086bb 53
Bobty 0:ec36dec086bb 54 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
Bobty 0:ec36dec086bb 55 * heavy-weight sensor polling from the main thread. */
Bobty 0:ec36dec086bb 56 triggerSensorPolling = true;
Bobty 0:ec36dec086bb 57 }
Bobty 0:ec36dec086bb 58
Bobty 0:ec36dec086bb 59 int main(void)
Bobty 0:ec36dec086bb 60 {
Bobty 0:ec36dec086bb 61 led1 = 1;
Bobty 0:ec36dec086bb 62 indicatorLed2 = 1;
Bobty 0:ec36dec086bb 63 Ticker ticker;
Bobty 0:ec36dec086bb 64 ticker.attach(periodicCallback, 1);
Bobty 0:ec36dec086bb 65
Bobty 0:ec36dec086bb 66 DEBUG("Initialising the nRF51822\n\r");
Bobty 0:ec36dec086bb 67 ble.init();
Bobty 0:ec36dec086bb 68 ble.onDisconnection(disconnectionCallback);
Bobty 0:ec36dec086bb 69
Bobty 0:ec36dec086bb 70 /* setup advertising */
Bobty 0:ec36dec086bb 71 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Bobty 0:ec36dec086bb 72 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Bobty 0:ec36dec086bb 73 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
Bobty 0:ec36dec086bb 74 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
Bobty 0:ec36dec086bb 75 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Bobty 0:ec36dec086bb 76 ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */
Bobty 0:ec36dec086bb 77 ble.startAdvertising();
Bobty 0:ec36dec086bb 78
Bobty 0:ec36dec086bb 79 uint8_t hrmCounter = 100;
Bobty 0:ec36dec086bb 80 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
Bobty 0:ec36dec086bb 81 BatteryService battery(ble);
Bobty 0:ec36dec086bb 82 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
Bobty 0:ec36dec086bb 83
Bobty 0:ec36dec086bb 84 while (true) {
Bobty 0:ec36dec086bb 85 if (triggerSensorPolling) {
Bobty 0:ec36dec086bb 86 triggerSensorPolling = false;
Bobty 0:ec36dec086bb 87
Bobty 0:ec36dec086bb 88 /* Do blocking calls or whatever is necessary for sensor polling. */
Bobty 0:ec36dec086bb 89 /* In our case, we simply update the dummy HRM measurement. */
Bobty 0:ec36dec086bb 90 hrmCounter++;
Bobty 0:ec36dec086bb 91 if (hrmCounter == 175) {
Bobty 0:ec36dec086bb 92 hrmCounter = 100;
Bobty 0:ec36dec086bb 93 }
Bobty 0:ec36dec086bb 94
Bobty 0:ec36dec086bb 95 indicatorLed2 = !indicatorLed2;
Bobty 0:ec36dec086bb 96 hrService.updateHeartRate(hrmCounter);
Bobty 0:ec36dec086bb 97 } else {
Bobty 0:ec36dec086bb 98 ble.waitForEvent();
Bobty 0:ec36dec086bb 99 }
Bobty 0:ec36dec086bb 100 }
Bobty 0:ec36dec086bb 101 }