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@8:7407b18dfc5d, 2014-10-01 (annotated)
- Committer:
- Bobty
- Date:
- Wed Oct 01 07:34:46 2014 +0000
- Revision:
- 8:7407b18dfc5d
- Parent:
- 7:ec41c916136a
- Child:
- 9:1cfd6c7055b5
Appears to work - modified logic slightly to permit sampling change
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 | 8:7407b18dfc5d | 24 | DigitalOut indicatorLed1(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 | 7:ec41c916136a | 38 | volatile uint32_t sampleIntervalUs = 2000; |
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 | 7:ec41c916136a | 61 | if (callbackCount == 500) |
Bobty | 1:259fe4d2ab16 | 62 | { |
Bobty | 1:259fe4d2ab16 | 63 | callbackCount = 0; |
Bobty | 8:7407b18dfc5d | 64 | indicatorLed1 = !indicatorLed1; |
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 | 5:6f5f1b37bd59 | 70 | // sampleTimeout.attach_us(periodicCallback, sampleIntervalUs); |
Bobty | 0:ec36dec086bb | 71 | } |
Bobty | 0:ec36dec086bb | 72 | |
Bobty | 8:7407b18dfc5d | 73 | int bleUpdateCount = 0; |
Bobty | 0:ec36dec086bb | 74 | int main(void) |
Bobty | 0:ec36dec086bb | 75 | { |
Bobty | 8:7407b18dfc5d | 76 | indicatorLed1 = 1; |
Bobty | 0:ec36dec086bb | 77 | indicatorLed2 = 1; |
Bobty | 1:259fe4d2ab16 | 78 | |
Bobty | 1:259fe4d2ab16 | 79 | // Start timer and sampling |
Bobty | 1:259fe4d2ab16 | 80 | intervalTimer.start(); |
Bobty | 5:6f5f1b37bd59 | 81 | // sampleTimeout.attach_us(periodicCallback, sampleIntervalUs); |
Bobty | 1:259fe4d2ab16 | 82 | |
Bobty | 5:6f5f1b37bd59 | 83 | Ticker ticker; |
Bobty | 1:259fe4d2ab16 | 84 | // ticker.attach(periodicCallback, 1); |
Bobty | 5:6f5f1b37bd59 | 85 | ticker.attach_us(periodicCallback, sampleIntervalUs); |
Bobty | 0:ec36dec086bb | 86 | |
Bobty | 0:ec36dec086bb | 87 | DEBUG("Initialising the nRF51822\n\r"); |
Bobty | 0:ec36dec086bb | 88 | ble.init(); |
Bobty | 0:ec36dec086bb | 89 | ble.onDisconnection(disconnectionCallback); |
Bobty | 0:ec36dec086bb | 90 | |
Bobty | 0:ec36dec086bb | 91 | /* setup advertising */ |
Bobty | 0:ec36dec086bb | 92 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Bobty | 0:ec36dec086bb | 93 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Bobty | 0:ec36dec086bb | 94 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
Bobty | 0:ec36dec086bb | 95 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Bobty | 0:ec36dec086bb | 96 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Bobty | 0:ec36dec086bb | 97 | ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ |
Bobty | 0:ec36dec086bb | 98 | ble.startAdvertising(); |
Bobty | 0:ec36dec086bb | 99 | |
Bobty | 0:ec36dec086bb | 100 | uint8_t hrmCounter = 100; |
Bobty | 0:ec36dec086bb | 101 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
Bobty | 0:ec36dec086bb | 102 | BatteryService battery(ble); |
Bobty | 0:ec36dec086bb | 103 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Bobty | 0:ec36dec086bb | 104 | |
Bobty | 8:7407b18dfc5d | 105 | while (true) |
Bobty | 8:7407b18dfc5d | 106 | { |
Bobty | 8:7407b18dfc5d | 107 | bool updateNow = false; |
Bobty | 8:7407b18dfc5d | 108 | if (triggerSensorPolling) |
Bobty | 8:7407b18dfc5d | 109 | { |
Bobty | 0:ec36dec086bb | 110 | triggerSensorPolling = false; |
Bobty | 8:7407b18dfc5d | 111 | bleUpdateCount++; |
Bobty | 8:7407b18dfc5d | 112 | if (bleUpdateCount >= 1) |
Bobty | 8:7407b18dfc5d | 113 | { |
Bobty | 8:7407b18dfc5d | 114 | bleUpdateCount = 0; |
Bobty | 8:7407b18dfc5d | 115 | updateNow = true; |
Bobty | 8:7407b18dfc5d | 116 | } |
Bobty | 8:7407b18dfc5d | 117 | } |
Bobty | 8:7407b18dfc5d | 118 | if (updateNow) |
Bobty | 8:7407b18dfc5d | 119 | { |
Bobty | 0:ec36dec086bb | 120 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
Bobty | 0:ec36dec086bb | 121 | /* In our case, we simply update the dummy HRM measurement. */ |
Bobty | 0:ec36dec086bb | 122 | hrmCounter++; |
Bobty | 8:7407b18dfc5d | 123 | if (hrmCounter == 175) |
Bobty | 8:7407b18dfc5d | 124 | { |
Bobty | 0:ec36dec086bb | 125 | hrmCounter = 100; |
Bobty | 0:ec36dec086bb | 126 | } |
Bobty | 0:ec36dec086bb | 127 | |
Bobty | 0:ec36dec086bb | 128 | indicatorLed2 = !indicatorLed2; |
Bobty | 7:ec41c916136a | 129 | hrService.updateHeartRate(hrmCounter); |
Bobty | 8:7407b18dfc5d | 130 | } |
Bobty | 8:7407b18dfc5d | 131 | else |
Bobty | 8:7407b18dfc5d | 132 | { |
Bobty | 0:ec36dec086bb | 133 | ble.waitForEvent(); |
Bobty | 0:ec36dec086bb | 134 | } |
Bobty | 0:ec36dec086bb | 135 | } |
Bobty | 0:ec36dec086bb | 136 | } |