//test comment
Dependencies: BLE_API mbed nRF51822
main.cpp@0:8db7a61ae7f7, 2014-06-23 (annotated)
- Committer:
- skarphedinnos
- Date:
- Mon Jun 23 12:57:46 2014 +0000
- Revision:
- 0:8db7a61ae7f7
//test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
skarphedinnos | 0:8db7a61ae7f7 | 1 | /* mbed Microcontroller Library |
skarphedinnos | 0:8db7a61ae7f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
skarphedinnos | 0:8db7a61ae7f7 | 3 | * |
skarphedinnos | 0:8db7a61ae7f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
skarphedinnos | 0:8db7a61ae7f7 | 5 | * you may not use this file except in compliance with the License. |
skarphedinnos | 0:8db7a61ae7f7 | 6 | * You may obtain a copy of the License at |
skarphedinnos | 0:8db7a61ae7f7 | 7 | * |
skarphedinnos | 0:8db7a61ae7f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
skarphedinnos | 0:8db7a61ae7f7 | 9 | * |
skarphedinnos | 0:8db7a61ae7f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
skarphedinnos | 0:8db7a61ae7f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
skarphedinnos | 0:8db7a61ae7f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
skarphedinnos | 0:8db7a61ae7f7 | 13 | * See the License for the specific language governing permissions and |
skarphedinnos | 0:8db7a61ae7f7 | 14 | * limitations under the License. |
skarphedinnos | 0:8db7a61ae7f7 | 15 | */ |
skarphedinnos | 0:8db7a61ae7f7 | 16 | //test |
skarphedinnos | 0:8db7a61ae7f7 | 17 | #include "mbed.h" |
skarphedinnos | 0:8db7a61ae7f7 | 18 | #include "BLEDevice.h" |
skarphedinnos | 0:8db7a61ae7f7 | 19 | |
skarphedinnos | 0:8db7a61ae7f7 | 20 | BLEDevice ble; |
skarphedinnos | 0:8db7a61ae7f7 | 21 | DigitalOut led1(LED1); |
skarphedinnos | 0:8db7a61ae7f7 | 22 | |
skarphedinnos | 0:8db7a61ae7f7 | 23 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
skarphedinnos | 0:8db7a61ae7f7 | 24 | * it will have an impact on code-size and power consumption. */ |
skarphedinnos | 0:8db7a61ae7f7 | 25 | |
skarphedinnos | 0:8db7a61ae7f7 | 26 | #if NEED_CONSOLE_OUTPUT |
skarphedinnos | 0:8db7a61ae7f7 | 27 | Serial pc(USBTX, USBRX); |
skarphedinnos | 0:8db7a61ae7f7 | 28 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
skarphedinnos | 0:8db7a61ae7f7 | 29 | #else |
skarphedinnos | 0:8db7a61ae7f7 | 30 | #define DEBUG(...) /* nothing */ |
skarphedinnos | 0:8db7a61ae7f7 | 31 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
skarphedinnos | 0:8db7a61ae7f7 | 32 | |
skarphedinnos | 0:8db7a61ae7f7 | 33 | /* Heart Rate Service */ |
skarphedinnos | 0:8db7a61ae7f7 | 34 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
skarphedinnos | 0:8db7a61ae7f7 | 35 | /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
skarphedinnos | 0:8db7a61ae7f7 | 36 | /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ |
skarphedinnos | 0:8db7a61ae7f7 | 37 | static uint8_t hrmCounter = 100; |
skarphedinnos | 0:8db7a61ae7f7 | 38 | static uint8_t bpm[2] = {0x00, hrmCounter}; |
skarphedinnos | 0:8db7a61ae7f7 | 39 | GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), |
skarphedinnos | 0:8db7a61ae7f7 | 40 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
skarphedinnos | 0:8db7a61ae7f7 | 41 | static const uint8_t location = 0x03; /* Finger */ |
skarphedinnos | 0:8db7a61ae7f7 | 42 | GattCharacteristic hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, |
skarphedinnos | 0:8db7a61ae7f7 | 43 | (uint8_t *)&location, sizeof(location), sizeof(location), |
skarphedinnos | 0:8db7a61ae7f7 | 44 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
skarphedinnos | 0:8db7a61ae7f7 | 45 | GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, }; |
skarphedinnos | 0:8db7a61ae7f7 | 46 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
skarphedinnos | 0:8db7a61ae7f7 | 47 | |
skarphedinnos | 0:8db7a61ae7f7 | 48 | void disconnectionCallback(void) |
skarphedinnos | 0:8db7a61ae7f7 | 49 | { |
skarphedinnos | 0:8db7a61ae7f7 | 50 | DEBUG("Disconnected!\n\r"); |
skarphedinnos | 0:8db7a61ae7f7 | 51 | DEBUG("Restarting the advertising process\n\r"); |
skarphedinnos | 0:8db7a61ae7f7 | 52 | ble.startAdvertising(); |
skarphedinnos | 0:8db7a61ae7f7 | 53 | } |
skarphedinnos | 0:8db7a61ae7f7 | 54 | |
skarphedinnos | 0:8db7a61ae7f7 | 55 | /** |
skarphedinnos | 0:8db7a61ae7f7 | 56 | * Triggered periodically by the 'ticker' interrupt; updates hrmCounter. |
skarphedinnos | 0:8db7a61ae7f7 | 57 | */ |
skarphedinnos | 0:8db7a61ae7f7 | 58 | void periodicCallback(void) |
skarphedinnos | 0:8db7a61ae7f7 | 59 | { |
skarphedinnos | 0:8db7a61ae7f7 | 60 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
skarphedinnos | 0:8db7a61ae7f7 | 61 | |
skarphedinnos | 0:8db7a61ae7f7 | 62 | if (ble.getGapState().connected) { |
skarphedinnos | 0:8db7a61ae7f7 | 63 | /* Update the HRM measurement */ |
skarphedinnos | 0:8db7a61ae7f7 | 64 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
skarphedinnos | 0:8db7a61ae7f7 | 65 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
skarphedinnos | 0:8db7a61ae7f7 | 66 | hrmCounter++; |
skarphedinnos | 0:8db7a61ae7f7 | 67 | if (hrmCounter == 175) { |
skarphedinnos | 0:8db7a61ae7f7 | 68 | hrmCounter = 100; |
skarphedinnos | 0:8db7a61ae7f7 | 69 | } |
skarphedinnos | 0:8db7a61ae7f7 | 70 | bpm[1] = hrmCounter; |
skarphedinnos | 0:8db7a61ae7f7 | 71 | ble.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm)); |
skarphedinnos | 0:8db7a61ae7f7 | 72 | } |
skarphedinnos | 0:8db7a61ae7f7 | 73 | } |
skarphedinnos | 0:8db7a61ae7f7 | 74 | |
skarphedinnos | 0:8db7a61ae7f7 | 75 | int main(void) |
skarphedinnos | 0:8db7a61ae7f7 | 76 | { |
skarphedinnos | 0:8db7a61ae7f7 | 77 | led1 = 1; |
skarphedinnos | 0:8db7a61ae7f7 | 78 | Ticker ticker; |
skarphedinnos | 0:8db7a61ae7f7 | 79 | ticker.attach(periodicCallback, 1); |
skarphedinnos | 0:8db7a61ae7f7 | 80 | |
skarphedinnos | 0:8db7a61ae7f7 | 81 | DEBUG("Initialising the nRF51822\n\r"); |
skarphedinnos | 0:8db7a61ae7f7 | 82 | ble.init(); |
skarphedinnos | 0:8db7a61ae7f7 | 83 | ble.onDisconnection(disconnectionCallback); |
skarphedinnos | 0:8db7a61ae7f7 | 84 | |
skarphedinnos | 0:8db7a61ae7f7 | 85 | /* setup advertising */ |
skarphedinnos | 0:8db7a61ae7f7 | 86 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
skarphedinnos | 0:8db7a61ae7f7 | 87 | ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); |
skarphedinnos | 0:8db7a61ae7f7 | 88 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
skarphedinnos | 0:8db7a61ae7f7 | 89 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
skarphedinnos | 0:8db7a61ae7f7 | 90 | ble.startAdvertising(); |
skarphedinnos | 0:8db7a61ae7f7 | 91 | |
skarphedinnos | 0:8db7a61ae7f7 | 92 | ble.addService(hrmService); |
skarphedinnos | 0:8db7a61ae7f7 | 93 | |
skarphedinnos | 0:8db7a61ae7f7 | 94 | while (true) { |
skarphedinnos | 0:8db7a61ae7f7 | 95 | ble.waitForEvent(); |
skarphedinnos | 0:8db7a61ae7f7 | 96 | } |
skarphedinnos | 0:8db7a61ae7f7 | 97 | } |