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
main.cpp@1:259fe4d2ab16, 2014-09-29 (annotated)
- Committer:
- Bobty
- Date:
- Mon Sep 29 11:17:04 2014 +0000
- Revision:
- 1:259fe4d2ab16
- Parent:
- 0:ec36dec086bb
- Child:
- 2:7de236382497
Set "ticker" to 1ms - now fails (after random time) to return from service ble and ticker stops being called
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:259fe4d2ab16 | 37 | // Sample interval (uS) |
Bobty | 1:259fe4d2ab16 | 38 | volatile uint32_t sampleIntervalUs = 1000; |
Bobty | 1:259fe4d2ab16 | 39 | |
Bobty | 1:259fe4d2ab16 | 40 | // Timer to do the sampling etc |
Bobty | 1:259fe4d2ab16 | 41 | Timer intervalTimer; |
Bobty | 1:259fe4d2ab16 | 42 | Timeout sampleTimeout; |
Bobty | 1:259fe4d2ab16 | 43 | |
Bobty | 0:ec36dec086bb | 44 | const static char DEVICE_NAME[] = "HRMonitor"; |
Bobty | 0:ec36dec086bb | 45 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
Bobty | 0:ec36dec086bb | 46 | GattService::UUID_BATTERY_SERVICE, |
Bobty | 0:ec36dec086bb | 47 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
Bobty | 0:ec36dec086bb | 48 | static volatile bool triggerSensorPolling = false; |
Bobty | 0:ec36dec086bb | 49 | |
Bobty | 0:ec36dec086bb | 50 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
Bobty | 0:ec36dec086bb | 51 | { |
Bobty | 0:ec36dec086bb | 52 | DEBUG("Disconnected handle %u!\n\r", handle); |
Bobty | 0:ec36dec086bb | 53 | DEBUG("Restarting the advertising process\n\r"); |
Bobty | 0:ec36dec086bb | 54 | ble.startAdvertising(); |
Bobty | 0:ec36dec086bb | 55 | } |
Bobty | 0:ec36dec086bb | 56 | |
Bobty | 1:259fe4d2ab16 | 57 | static volatile int callbackCount = 0; |
Bobty | 0:ec36dec086bb | 58 | void periodicCallback(void) |
Bobty | 0:ec36dec086bb | 59 | { |
Bobty | 1:259fe4d2ab16 | 60 | callbackCount++; |
Bobty | 1:259fe4d2ab16 | 61 | if (callbackCount == 1000) |
Bobty | 1:259fe4d2ab16 | 62 | { |
Bobty | 1:259fe4d2ab16 | 63 | callbackCount = 0; |
Bobty | 1:259fe4d2ab16 | 64 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Bobty | 1:259fe4d2ab16 | 65 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
Bobty | 1:259fe4d2ab16 | 66 | * heavy-weight sensor polling from the main thread. */ |
Bobty | 1:259fe4d2ab16 | 67 | triggerSensorPolling = true; |
Bobty | 1:259fe4d2ab16 | 68 | } |
Bobty | 1:259fe4d2ab16 | 69 | |
Bobty | 1:259fe4d2ab16 | 70 | sampleTimeout.attach_us(periodicCallback, sampleIntervalUs); |
Bobty | 0:ec36dec086bb | 71 | } |
Bobty | 0:ec36dec086bb | 72 | |
Bobty | 0:ec36dec086bb | 73 | int main(void) |
Bobty | 0:ec36dec086bb | 74 | { |
Bobty | 0:ec36dec086bb | 75 | led1 = 1; |
Bobty | 0:ec36dec086bb | 76 | indicatorLed2 = 1; |
Bobty | 1:259fe4d2ab16 | 77 | |
Bobty | 1:259fe4d2ab16 | 78 | // Start timer and sampling |
Bobty | 1:259fe4d2ab16 | 79 | intervalTimer.start(); |
Bobty | 1:259fe4d2ab16 | 80 | sampleTimeout.attach_us(periodicCallback, sampleIntervalUs); |
Bobty | 1:259fe4d2ab16 | 81 | |
Bobty | 1:259fe4d2ab16 | 82 | // Ticker ticker; |
Bobty | 1:259fe4d2ab16 | 83 | // ticker.attach(periodicCallback, 1); |
Bobty | 0:ec36dec086bb | 84 | |
Bobty | 0:ec36dec086bb | 85 | DEBUG("Initialising the nRF51822\n\r"); |
Bobty | 0:ec36dec086bb | 86 | ble.init(); |
Bobty | 0:ec36dec086bb | 87 | ble.onDisconnection(disconnectionCallback); |
Bobty | 0:ec36dec086bb | 88 | |
Bobty | 0:ec36dec086bb | 89 | /* setup advertising */ |
Bobty | 0:ec36dec086bb | 90 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Bobty | 0:ec36dec086bb | 91 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Bobty | 0:ec36dec086bb | 92 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
Bobty | 0:ec36dec086bb | 93 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Bobty | 0:ec36dec086bb | 94 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Bobty | 0:ec36dec086bb | 95 | ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ |
Bobty | 0:ec36dec086bb | 96 | ble.startAdvertising(); |
Bobty | 0:ec36dec086bb | 97 | |
Bobty | 0:ec36dec086bb | 98 | uint8_t hrmCounter = 100; |
Bobty | 0:ec36dec086bb | 99 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
Bobty | 0:ec36dec086bb | 100 | BatteryService battery(ble); |
Bobty | 0:ec36dec086bb | 101 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Bobty | 0:ec36dec086bb | 102 | |
Bobty | 0:ec36dec086bb | 103 | while (true) { |
Bobty | 0:ec36dec086bb | 104 | if (triggerSensorPolling) { |
Bobty | 0:ec36dec086bb | 105 | triggerSensorPolling = false; |
Bobty | 0:ec36dec086bb | 106 | |
Bobty | 0:ec36dec086bb | 107 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
Bobty | 0:ec36dec086bb | 108 | /* In our case, we simply update the dummy HRM measurement. */ |
Bobty | 0:ec36dec086bb | 109 | hrmCounter++; |
Bobty | 0:ec36dec086bb | 110 | if (hrmCounter == 175) { |
Bobty | 0:ec36dec086bb | 111 | hrmCounter = 100; |
Bobty | 0:ec36dec086bb | 112 | } |
Bobty | 0:ec36dec086bb | 113 | |
Bobty | 0:ec36dec086bb | 114 | indicatorLed2 = !indicatorLed2; |
Bobty | 0:ec36dec086bb | 115 | hrService.updateHeartRate(hrmCounter); |
Bobty | 0:ec36dec086bb | 116 | } else { |
Bobty | 0:ec36dec086bb | 117 | ble.waitForEvent(); |
Bobty | 0:ec36dec086bb | 118 | } |
Bobty | 0:ec36dec086bb | 119 | } |
Bobty | 0:ec36dec086bb | 120 | } |