I've got some basic filter code setup (but not yet tested).
Dependencies: BLE_API Queue mbed nRF51822
Fork of BLE_HeartRate by
main.cpp@61:1de72bdab0ef, 2015-05-17 (annotated)
- Committer:
- roysandberg
- Date:
- Sun May 17 00:27:16 2015 +0000
- Revision:
- 61:1de72bdab0ef
- Parent:
- 56:83623419d5e4
- Child:
- 62:8e2fbe131b53
Initial release;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:87a7fc231fae | 1 | /* mbed Microcontroller Library |
ktownsend | 0:87a7fc231fae | 2 | * Copyright (c) 2006-2013 ARM Limited |
ktownsend | 0:87a7fc231fae | 3 | * |
ktownsend | 0:87a7fc231fae | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ktownsend | 0:87a7fc231fae | 5 | * you may not use this file except in compliance with the License. |
ktownsend | 0:87a7fc231fae | 6 | * You may obtain a copy of the License at |
ktownsend | 0:87a7fc231fae | 7 | * |
ktownsend | 0:87a7fc231fae | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ktownsend | 0:87a7fc231fae | 9 | * |
ktownsend | 0:87a7fc231fae | 10 | * Unless required by applicable law or agreed to in writing, software |
ktownsend | 0:87a7fc231fae | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ktownsend | 0:87a7fc231fae | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ktownsend | 0:87a7fc231fae | 13 | * See the License for the specific language governing permissions and |
ktownsend | 0:87a7fc231fae | 14 | * limitations under the License. |
ktownsend | 0:87a7fc231fae | 15 | */ |
ktownsend | 0:87a7fc231fae | 16 | |
ktownsend | 0:87a7fc231fae | 17 | #include "mbed.h" |
Rohit Grover |
10:2436164b692e | 18 | #include "BLEDevice.h" |
rgrover1 | 39:6390604f904c | 19 | #include "HeartRateService.h" |
rgrover1 | 42:06ebef2e0e44 | 20 | #include "DeviceInformationService.h" |
ktownsend | 0:87a7fc231fae | 21 | |
rgrover1 | 52:6bbf62943106 | 22 | /* Enable the following if you need to throttle the connection interval. This has |
rgrover1 | 52:6bbf62943106 | 23 | * the effect of reducing energy consumption after a connection is made; |
rgrover1 | 52:6bbf62943106 | 24 | * particularly for applications where the central may want a fast connection |
rgrover1 | 52:6bbf62943106 | 25 | * interval.*/ |
rgrover1 | 52:6bbf62943106 | 26 | #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0 |
rgrover1 | 52:6bbf62943106 | 27 | |
roysandberg | 61:1de72bdab0ef | 28 | extern void setup_sampler(void (*function)(uint32_t)); |
roysandberg | 61:1de72bdab0ef | 29 | |
Rohit Grover |
10:2436164b692e | 30 | BLEDevice ble; |
rgrover1 | 47:430545f41113 | 31 | DigitalOut led1(LED1); |
ktownsend | 0:87a7fc231fae | 32 | |
roysandberg | 61:1de72bdab0ef | 33 | volatile uint8_t hrmCounter = 100; // init HRM to 100bps |
roysandberg | 61:1de72bdab0ef | 34 | |
mbedAustin | 55:3a7d497a3e03 | 35 | const static char DEVICE_NAME[] = "HRM1"; |
rgrover1 | 42:06ebef2e0e44 | 36 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
roysandberg | 61:1de72bdab0ef | 37 | GattService::UUID_DEVICE_INFORMATION_SERVICE |
roysandberg | 61:1de72bdab0ef | 38 | }; |
rgrover1 | 39:6390604f904c | 39 | static volatile bool triggerSensorPolling = false; |
Rohit Grover |
36:ea2a1b4f51c1 | 40 | |
rgrover1 | 41:9cef0129da5f | 41 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
ktownsend | 0:87a7fc231fae | 42 | { |
rgrover1 | 46:ee7c55907f36 | 43 | ble.startAdvertising(); // restart advertising |
rgrover1 | 7:daab8ba5139e | 44 | } |
Rohit Grover |
3:24e2b056d229 | 45 | |
roysandberg | 61:1de72bdab0ef | 46 | //void periodicCallback(void) |
roysandberg | 61:1de72bdab0ef | 47 | //{ |
roysandberg | 61:1de72bdab0ef | 48 | // led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
roysandberg | 61:1de72bdab0ef | 49 | // |
roysandberg | 61:1de72bdab0ef | 50 | // /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
roysandberg | 61:1de72bdab0ef | 51 | // * heavy-weight sensor polling from the main thread. */ |
roysandberg | 61:1de72bdab0ef | 52 | // triggerSensorPolling = true; |
roysandberg | 61:1de72bdab0ef | 53 | //} |
roysandberg | 61:1de72bdab0ef | 54 | |
roysandberg | 61:1de72bdab0ef | 55 | void sampler_callback(uint32_t value) |
Rohit Grover |
11:1d9aafee4984 | 56 | { |
roysandberg | 61:1de72bdab0ef | 57 | hrmCounter = (uint8_t) (value); |
roysandberg | 61:1de72bdab0ef | 58 | // led1 = !led1; |
rgrover1 | 39:6390604f904c | 59 | triggerSensorPolling = true; |
Rohit Grover |
11:1d9aafee4984 | 60 | } |
Rohit Grover |
11:1d9aafee4984 | 61 | |
ktownsend | 0:87a7fc231fae | 62 | int main(void) |
ktownsend | 0:87a7fc231fae | 63 | { |
rgrover1 | 47:430545f41113 | 64 | led1 = 1; |
roysandberg | 61:1de72bdab0ef | 65 | // Ticker ticker; |
roysandberg | 61:1de72bdab0ef | 66 | //ticker.attach(periodicCallback, 0.1); // blink LED every second |
roysandberg | 61:1de72bdab0ef | 67 | |
roysandberg | 61:1de72bdab0ef | 68 | printf("Executing code...\n"); |
roysandberg | 61:1de72bdab0ef | 69 | |
roysandberg | 61:1de72bdab0ef | 70 | setup_sampler(&sampler_callback); |
ktownsend | 0:87a7fc231fae | 71 | |
Rohit Grover |
15:7ba28817e31e | 72 | ble.init(); |
rgrover1 | 7:daab8ba5139e | 73 | ble.onDisconnection(disconnectionCallback); |
ktownsend | 0:87a7fc231fae | 74 | |
rgrover1 | 45:98c5a34b07a4 | 75 | /* Setup primary service. */ |
roysandberg | 61:1de72bdab0ef | 76 | //uint8_t hrmCounter = 100; // init HRM to 100bps |
rgrover1 | 45:98c5a34b07a4 | 77 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
rgrover1 | 45:98c5a34b07a4 | 78 | |
mbedAustin | 55:3a7d497a3e03 | 79 | /* Setup auxiliary service. */ |
rgrover1 | 45:98c5a34b07a4 | 80 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
rgrover1 | 45:98c5a34b07a4 | 81 | |
rgrover1 | 45:98c5a34b07a4 | 82 | /* Setup advertising. */ |
Rohit Grover |
29:76d865c718a6 | 83 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 40:e73130c6f2bb | 84 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 42:06ebef2e0e44 | 85 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
Rohit Grover |
29:76d865c718a6 | 86 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:daab8ba5139e | 87 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 56:83623419d5e4 | 88 | ble.setAdvertisingInterval(1000); |
rgrover1 | 7:daab8ba5139e | 89 | ble.startAdvertising(); |
Rohit Grover |
3:24e2b056d229 | 90 | |
mbedAustin | 55:3a7d497a3e03 | 91 | // infinite loop |
mbedAustin | 55:3a7d497a3e03 | 92 | while (1) { |
mbedAustin | 55:3a7d497a3e03 | 93 | // check for trigger from periodicCallback() |
rgrover1 | 50:477004d54431 | 94 | if (triggerSensorPolling && ble.getGapState().connected) { |
Rohit Grover |
36:ea2a1b4f51c1 | 95 | triggerSensorPolling = false; |
Rohit Grover |
36:ea2a1b4f51c1 | 96 | |
roysandberg | 61:1de72bdab0ef | 97 | led1 = !led1; |
roysandberg | 61:1de72bdab0ef | 98 | |
roysandberg | 61:1de72bdab0ef | 99 | printf("v=%d\n", hrmCounter); |
roysandberg | 61:1de72bdab0ef | 100 | // // Do blocking calls or whatever is necessary for sensor polling. |
roysandberg | 61:1de72bdab0ef | 101 | // // In our case, we simply update the HRM measurement. |
roysandberg | 61:1de72bdab0ef | 102 | // hrmCounter++; |
roysandberg | 61:1de72bdab0ef | 103 | // |
roysandberg | 61:1de72bdab0ef | 104 | // // 100 <= HRM bps <=175 |
roysandberg | 61:1de72bdab0ef | 105 | // if (hrmCounter == 175) { |
roysandberg | 61:1de72bdab0ef | 106 | // hrmCounter = 100; |
roysandberg | 61:1de72bdab0ef | 107 | // } |
roysandberg | 61:1de72bdab0ef | 108 | |
mbedAustin | 55:3a7d497a3e03 | 109 | // update bps |
rgrover1 | 39:6390604f904c | 110 | hrService.updateHeartRate(hrmCounter); |
Rohit Grover |
36:ea2a1b4f51c1 | 111 | } else { |
mbedAustin | 55:3a7d497a3e03 | 112 | ble.waitForEvent(); // low power wait for event |
Rohit Grover |
36:ea2a1b4f51c1 | 113 | } |
ktownsend | 0:87a7fc231fae | 114 | } |
ktownsend | 0:87a7fc231fae | 115 | } |