Error 230
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
main.cpp@40:626757c0bb3b, 2014-08-08 (annotated)
- Committer:
- thegecko
- Date:
- Fri Aug 08 16:10:15 2014 +0000
- Revision:
- 40:626757c0bb3b
- Parent:
- 39:d0a7eb186652
Fixed error 230 and added temp capability
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" |
Rohit Grover |
34:44dc6efc0b50 | 19 | #include "ble_hrs.h" |
thegecko | 39:d0a7eb186652 | 20 | #include "ble_hts.h" |
ktownsend | 0:87a7fc231fae | 21 | |
Rohit Grover |
10:2436164b692e | 22 | BLEDevice ble; |
Rohit Grover |
3:24e2b056d229 | 23 | DigitalOut led1(LED1); |
Rohit Grover |
9:5d693381e883 | 24 | |
Rohit Grover |
9:5d693381e883 | 25 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Rohit Grover |
22:299658c5fa3c | 26 | * it will have an impact on code-size and power consumption. */ |
Rohit Grover |
9:5d693381e883 | 27 | |
Rohit Grover |
9:5d693381e883 | 28 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
9:5d693381e883 | 29 | Serial pc(USBTX, USBRX); |
Rohit Grover |
9:5d693381e883 | 30 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Rohit Grover |
9:5d693381e883 | 31 | #else |
Rohit Grover |
9:5d693381e883 | 32 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
9:5d693381e883 | 33 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
ktownsend | 0:87a7fc231fae | 34 | |
thegecko | 39:d0a7eb186652 | 35 | const static char DEVICE_NAME[] = "Rob's Tool"; |
Rohit Grover |
26:e6ad33b227c6 | 36 | |
ktownsend | 0:87a7fc231fae | 37 | /* Heart Rate Service */ |
ktownsend | 0:87a7fc231fae | 38 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
ktownsend | 0:87a7fc231fae | 39 | /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
ktownsend | 0:87a7fc231fae | 40 | /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ |
thegecko | 39:d0a7eb186652 | 41 | |
thegecko | 39:d0a7eb186652 | 42 | /* Temperature Service */ |
thegecko | 39:d0a7eb186652 | 43 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml */ |
thegecko | 39:d0a7eb186652 | 44 | /* Temp Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
thegecko | 39:d0a7eb186652 | 45 | /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */ |
thegecko | 39:d0a7eb186652 | 46 | |
thegecko | 40:626757c0bb3b | 47 | /* Heart Rate 1 */ |
thegecko | 40:626757c0bb3b | 48 | static uint8_t bpm1[2] = {0x00, 80}; |
thegecko | 40:626757c0bb3b | 49 | GattCharacteristic hrmRate1(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm1, sizeof(bpm1), sizeof(bpm1), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
thegecko | 40:626757c0bb3b | 50 | |
thegecko | 40:626757c0bb3b | 51 | static uint8_t location1 = BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE; |
thegecko | 40:626757c0bb3b | 52 | GattCharacteristic hrmLocation1(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location1, sizeof(location1), sizeof(location1), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
thegecko | 40:626757c0bb3b | 53 | |
thegecko | 40:626757c0bb3b | 54 | GattCharacteristic *hrmChars1[] = {&hrmRate1, &hrmLocation1, }; |
thegecko | 40:626757c0bb3b | 55 | GattService hrmService1(GattService::UUID_HEART_RATE_SERVICE, hrmChars1, sizeof(hrmChars1) / sizeof(GattCharacteristic *)); |
thegecko | 39:d0a7eb186652 | 56 | |
thegecko | 40:626757c0bb3b | 57 | /* Heart Rate 2 */ |
thegecko | 40:626757c0bb3b | 58 | static uint8_t bpm2[2] = {0x00, 80}; |
thegecko | 40:626757c0bb3b | 59 | GattCharacteristic hrmRate2(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm2, sizeof(bpm2), sizeof(bpm2), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
thegecko | 40:626757c0bb3b | 60 | |
thegecko | 40:626757c0bb3b | 61 | static uint8_t location2 = BLE_HRS_BODY_SENSOR_LOCATION_HAND; |
thegecko | 40:626757c0bb3b | 62 | GattCharacteristic hrmLocation2(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location2, sizeof(location2), sizeof(location2), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
thegecko | 39:d0a7eb186652 | 63 | |
thegecko | 40:626757c0bb3b | 64 | GattCharacteristic *hrmChars2[] = {&hrmRate2, &hrmLocation2, }; |
thegecko | 40:626757c0bb3b | 65 | GattService hrmService2(GattService::UUID_HEART_RATE_SERVICE, hrmChars2, sizeof(hrmChars2) / sizeof(GattCharacteristic *)); |
thegecko | 40:626757c0bb3b | 66 | |
thegecko | 39:d0a7eb186652 | 67 | |
thegecko | 39:d0a7eb186652 | 68 | /* Temperature */ |
thegecko | 39:d0a7eb186652 | 69 | static uint8_t temp[5] = {0, 0, 0, 0, 0}; |
thegecko | 40:626757c0bb3b | 70 | GattCharacteristic recTemp(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, temp, sizeof(temp), sizeof(temp), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
ktownsend | 0:87a7fc231fae | 71 | |
thegecko | 39:d0a7eb186652 | 72 | static uint8_t type = BLE_HTS_TEMP_TYPE_RECTUM; |
thegecko | 39:d0a7eb186652 | 73 | GattCharacteristic recType(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&type, sizeof(type), sizeof(type), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
thegecko | 39:d0a7eb186652 | 74 | |
thegecko | 39:d0a7eb186652 | 75 | GattCharacteristic *recChars[] = {&recTemp, &recType, }; |
thegecko | 39:d0a7eb186652 | 76 | GattService recService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, recChars, sizeof(recChars) / sizeof(GattCharacteristic *)); |
thegecko | 39:d0a7eb186652 | 77 | |
thegecko | 39:d0a7eb186652 | 78 | /* List */ |
thegecko | 40:626757c0bb3b | 79 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
Rohit Grover |
27:97adf2b76b9c | 80 | |
Rohit Grover |
36:ea2a1b4f51c1 | 81 | static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that |
Rohit Grover |
36:ea2a1b4f51c1 | 82 | * polling is necessary. */ |
Rohit Grover |
37:d310a72115c7 | 83 | static Gap::ConnectionParams_t connectionParams; |
Rohit Grover |
36:ea2a1b4f51c1 | 84 | |
Rohit Grover |
35:ba3e3174331a | 85 | void disconnectionCallback(Gap::Handle_t handle) |
ktownsend | 0:87a7fc231fae | 86 | { |
Rohit Grover |
35:ba3e3174331a | 87 | DEBUG("Disconnected handle %u!\n\r", handle); |
Rohit Grover |
9:5d693381e883 | 88 | DEBUG("Restarting the advertising process\n\r"); |
rgrover1 | 7:daab8ba5139e | 89 | ble.startAdvertising(); |
rgrover1 | 7:daab8ba5139e | 90 | } |
Rohit Grover |
3:24e2b056d229 | 91 | |
Rohit Grover |
37:d310a72115c7 | 92 | void onConnectionCallback(Gap::Handle_t handle) |
Rohit Grover |
37:d310a72115c7 | 93 | { |
Rohit Grover |
37:d310a72115c7 | 94 | DEBUG("connected. Got handle %u\r\n", handle); |
Rohit Grover |
37:d310a72115c7 | 95 | |
Rohit Grover |
37:d310a72115c7 | 96 | connectionParams.slaveLatency = 1; |
Rohit Grover |
37:d310a72115c7 | 97 | if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { |
Rohit Grover |
37:d310a72115c7 | 98 | DEBUG("failed to update connection paramter\r\n"); |
Rohit Grover |
37:d310a72115c7 | 99 | } |
Rohit Grover |
37:d310a72115c7 | 100 | } |
Rohit Grover |
37:d310a72115c7 | 101 | |
Rohit Grover |
13:3ca2045597e7 | 102 | /** |
Rohit Grover |
36:ea2a1b4f51c1 | 103 | * Triggered periodically by the 'ticker' interrupt. |
Rohit Grover |
13:3ca2045597e7 | 104 | */ |
Rohit Grover |
11:1d9aafee4984 | 105 | void periodicCallback(void) |
Rohit Grover |
11:1d9aafee4984 | 106 | { |
Rohit Grover |
11:1d9aafee4984 | 107 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Rohit Grover |
36:ea2a1b4f51c1 | 108 | triggerSensorPolling = true; /* Note that the periodicCallback() executes in |
Rohit Grover |
36:ea2a1b4f51c1 | 109 | * interrupt context, so it is safer to do |
Rohit Grover |
36:ea2a1b4f51c1 | 110 | * heavy-weight sensor polling from the main |
Rohit Grover |
36:ea2a1b4f51c1 | 111 | * thread.*/ |
Rohit Grover |
11:1d9aafee4984 | 112 | } |
Rohit Grover |
11:1d9aafee4984 | 113 | |
thegecko | 39:d0a7eb186652 | 114 | uint32_t quick_ieee11073_from_float(float temperature) |
thegecko | 39:d0a7eb186652 | 115 | { |
thegecko | 39:d0a7eb186652 | 116 | uint8_t exponent = 0xFF; //exponent is -1 |
thegecko | 39:d0a7eb186652 | 117 | uint32_t mantissa = (uint32_t)(temperature*10); |
thegecko | 39:d0a7eb186652 | 118 | |
thegecko | 39:d0a7eb186652 | 119 | return ( ((uint32_t)exponent) << 24) | mantissa; |
thegecko | 39:d0a7eb186652 | 120 | } |
thegecko | 39:d0a7eb186652 | 121 | |
ktownsend | 0:87a7fc231fae | 122 | int main(void) |
ktownsend | 0:87a7fc231fae | 123 | { |
Rohit Grover |
3:24e2b056d229 | 124 | led1 = 1; |
Rohit Grover |
11:1d9aafee4984 | 125 | Ticker ticker; |
Rohit Grover |
11:1d9aafee4984 | 126 | ticker.attach(periodicCallback, 1); |
ktownsend | 0:87a7fc231fae | 127 | |
Rohit Grover |
15:7ba28817e31e | 128 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
15:7ba28817e31e | 129 | ble.init(); |
rgrover1 | 7:daab8ba5139e | 130 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
37:d310a72115c7 | 131 | ble.onConnection(onConnectionCallback); |
Rohit Grover |
37:d310a72115c7 | 132 | |
Rohit Grover |
37:d310a72115c7 | 133 | ble.getPreferredConnectionParams(&connectionParams); |
ktownsend | 0:87a7fc231fae | 134 | |
Rohit Grover |
15:7ba28817e31e | 135 | /* setup advertising */ |
Rohit Grover |
29:76d865c718a6 | 136 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Rohit Grover |
27:97adf2b76b9c | 137 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 7:daab8ba5139e | 138 | ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); |
Rohit Grover |
29:76d865c718a6 | 139 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:daab8ba5139e | 140 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
thegecko | 39:d0a7eb186652 | 141 | ble.setAdvertisingInterval(80); /* 100ms; in multiples of 0.625ms. */ |
rgrover1 | 7:daab8ba5139e | 142 | ble.startAdvertising(); |
Rohit Grover |
3:24e2b056d229 | 143 | |
thegecko | 40:626757c0bb3b | 144 | ble.addService(hrmService1); |
thegecko | 40:626757c0bb3b | 145 | ble.addService(hrmService2); |
thegecko | 39:d0a7eb186652 | 146 | ble.addService(recService); |
ktownsend | 0:87a7fc231fae | 147 | |
Rohit Grover |
11:1d9aafee4984 | 148 | while (true) { |
Rohit Grover |
36:ea2a1b4f51c1 | 149 | if (triggerSensorPolling) { |
Rohit Grover |
36:ea2a1b4f51c1 | 150 | triggerSensorPolling = false; |
Rohit Grover |
36:ea2a1b4f51c1 | 151 | |
Rohit Grover |
36:ea2a1b4f51c1 | 152 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
thegecko | 39:d0a7eb186652 | 153 | /* In our case, we simply update the dummy HRM measurement. */ |
Rohit Grover |
36:ea2a1b4f51c1 | 154 | if (ble.getGapState().connected) { |
Rohit Grover |
36:ea2a1b4f51c1 | 155 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
Rohit Grover |
36:ea2a1b4f51c1 | 156 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
thegecko | 40:626757c0bb3b | 157 | bpm1[1] = rand() % 40 + 60; |
thegecko | 40:626757c0bb3b | 158 | ble.updateCharacteristicValue(hrmRate1.getHandle(), bpm1, sizeof(bpm1)); |
thegecko | 40:626757c0bb3b | 159 | |
thegecko | 40:626757c0bb3b | 160 | bpm2[1] = rand() % 40 + 60; |
thegecko | 40:626757c0bb3b | 161 | ble.updateCharacteristicValue(hrmRate2.getHandle(), bpm2, sizeof(bpm2)); |
thegecko | 40:626757c0bb3b | 162 | |
thegecko | 40:626757c0bb3b | 163 | float f = (rand() % 20 + 360) / 10.0f; |
thegecko | 40:626757c0bb3b | 164 | uint32_t tem = quick_ieee11073_from_float(f); |
thegecko | 39:d0a7eb186652 | 165 | memcpy(temp+1, &tem, 4); |
thegecko | 39:d0a7eb186652 | 166 | ble.updateCharacteristicValue(recTemp.getHandle(), temp, sizeof(temp)); |
thegecko | 39:d0a7eb186652 | 167 | } |
Rohit Grover |
36:ea2a1b4f51c1 | 168 | } else { |
Rohit Grover |
36:ea2a1b4f51c1 | 169 | ble.waitForEvent(); |
Rohit Grover |
36:ea2a1b4f51c1 | 170 | } |
ktownsend | 0:87a7fc231fae | 171 | } |
ktownsend | 0:87a7fc231fae | 172 | } |