Error 230

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
thegecko
Date:
Fri Aug 08 13:44:31 2014 +0000
Revision:
39:d0a7eb186652
Parent:
37:d310a72115c7
Child:
40:626757c0bb3b
error 230

Who changed what in which revision?

UserRevisionLine numberNew 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 39:d0a7eb186652 47 /* Heart Rate */
thegecko 39:d0a7eb186652 48 static uint8_t bpm[2] = {0x00, 80};
thegecko 39:d0a7eb186652 49 GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
thegecko 39:d0a7eb186652 50
Rohit Grover 34:44dc6efc0b50 51 static uint8_t location = BLE_HRS_BODY_SENSOR_LOCATION_FINGER;
thegecko 39:d0a7eb186652 52 GattCharacteristic hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&location, sizeof(location), sizeof(location), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
thegecko 39:d0a7eb186652 53
Rohit Grover 22:299658c5fa3c 54 GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, };
thegecko 39:d0a7eb186652 55 GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
thegecko 39:d0a7eb186652 56
thegecko 39:d0a7eb186652 57 /* Temperature */
thegecko 39:d0a7eb186652 58 static uint8_t temp[5] = {0, 0, 0, 0, 0};
thegecko 39:d0a7eb186652 59 GattCharacteristic recTemp(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, temp, sizeof(temp), sizeof(temp), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
ktownsend 0:87a7fc231fae 60
thegecko 39:d0a7eb186652 61 static uint8_t type = BLE_HTS_TEMP_TYPE_RECTUM;
thegecko 39:d0a7eb186652 62 GattCharacteristic recType(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&type, sizeof(type), sizeof(type), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
thegecko 39:d0a7eb186652 63
thegecko 39:d0a7eb186652 64 GattCharacteristic *recChars[] = {&recTemp, &recType, };
thegecko 39:d0a7eb186652 65 GattService recService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, recChars, sizeof(recChars) / sizeof(GattCharacteristic *));
thegecko 39:d0a7eb186652 66
thegecko 39:d0a7eb186652 67 /* List */
thegecko 39:d0a7eb186652 68 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_HEALTH_THERMOMETER_SERVICE};
Rohit Grover 27:97adf2b76b9c 69
Rohit Grover 36:ea2a1b4f51c1 70 static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that
Rohit Grover 36:ea2a1b4f51c1 71 * polling is necessary. */
Rohit Grover 37:d310a72115c7 72 static Gap::ConnectionParams_t connectionParams;
Rohit Grover 36:ea2a1b4f51c1 73
Rohit Grover 35:ba3e3174331a 74 void disconnectionCallback(Gap::Handle_t handle)
ktownsend 0:87a7fc231fae 75 {
Rohit Grover 35:ba3e3174331a 76 DEBUG("Disconnected handle %u!\n\r", handle);
Rohit Grover 9:5d693381e883 77 DEBUG("Restarting the advertising process\n\r");
rgrover1 7:daab8ba5139e 78 ble.startAdvertising();
rgrover1 7:daab8ba5139e 79 }
Rohit Grover 3:24e2b056d229 80
Rohit Grover 37:d310a72115c7 81 void onConnectionCallback(Gap::Handle_t handle)
Rohit Grover 37:d310a72115c7 82 {
Rohit Grover 37:d310a72115c7 83 DEBUG("connected. Got handle %u\r\n", handle);
Rohit Grover 37:d310a72115c7 84
Rohit Grover 37:d310a72115c7 85 connectionParams.slaveLatency = 1;
Rohit Grover 37:d310a72115c7 86 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
Rohit Grover 37:d310a72115c7 87 DEBUG("failed to update connection paramter\r\n");
Rohit Grover 37:d310a72115c7 88 }
Rohit Grover 37:d310a72115c7 89 }
Rohit Grover 37:d310a72115c7 90
Rohit Grover 13:3ca2045597e7 91 /**
Rohit Grover 36:ea2a1b4f51c1 92 * Triggered periodically by the 'ticker' interrupt.
Rohit Grover 13:3ca2045597e7 93 */
Rohit Grover 11:1d9aafee4984 94 void periodicCallback(void)
Rohit Grover 11:1d9aafee4984 95 {
Rohit Grover 11:1d9aafee4984 96 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
Rohit Grover 36:ea2a1b4f51c1 97 triggerSensorPolling = true; /* Note that the periodicCallback() executes in
Rohit Grover 36:ea2a1b4f51c1 98 * interrupt context, so it is safer to do
Rohit Grover 36:ea2a1b4f51c1 99 * heavy-weight sensor polling from the main
Rohit Grover 36:ea2a1b4f51c1 100 * thread.*/
Rohit Grover 11:1d9aafee4984 101 }
Rohit Grover 11:1d9aafee4984 102
thegecko 39:d0a7eb186652 103 uint32_t quick_ieee11073_from_float(float temperature)
thegecko 39:d0a7eb186652 104 {
thegecko 39:d0a7eb186652 105 uint8_t exponent = 0xFF; //exponent is -1
thegecko 39:d0a7eb186652 106 uint32_t mantissa = (uint32_t)(temperature*10);
thegecko 39:d0a7eb186652 107
thegecko 39:d0a7eb186652 108 return ( ((uint32_t)exponent) << 24) | mantissa;
thegecko 39:d0a7eb186652 109 }
thegecko 39:d0a7eb186652 110
ktownsend 0:87a7fc231fae 111 int main(void)
ktownsend 0:87a7fc231fae 112 {
Rohit Grover 3:24e2b056d229 113 led1 = 1;
Rohit Grover 11:1d9aafee4984 114 Ticker ticker;
Rohit Grover 11:1d9aafee4984 115 ticker.attach(periodicCallback, 1);
ktownsend 0:87a7fc231fae 116
Rohit Grover 15:7ba28817e31e 117 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 15:7ba28817e31e 118 ble.init();
rgrover1 7:daab8ba5139e 119 ble.onDisconnection(disconnectionCallback);
Rohit Grover 37:d310a72115c7 120 ble.onConnection(onConnectionCallback);
Rohit Grover 37:d310a72115c7 121
Rohit Grover 37:d310a72115c7 122 ble.getPreferredConnectionParams(&connectionParams);
ktownsend 0:87a7fc231fae 123
Rohit Grover 15:7ba28817e31e 124 /* setup advertising */
Rohit Grover 29:76d865c718a6 125 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Rohit Grover 27:97adf2b76b9c 126 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
rgrover1 7:daab8ba5139e 127 ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
Rohit Grover 29:76d865c718a6 128 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 7:daab8ba5139e 129 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
thegecko 39:d0a7eb186652 130 ble.setAdvertisingInterval(80); /* 100ms; in multiples of 0.625ms. */
rgrover1 7:daab8ba5139e 131 ble.startAdvertising();
Rohit Grover 3:24e2b056d229 132
Rohit Grover 11:1d9aafee4984 133 ble.addService(hrmService);
thegecko 39:d0a7eb186652 134 ble.addService(recService);
ktownsend 0:87a7fc231fae 135
Rohit Grover 11:1d9aafee4984 136 while (true) {
Rohit Grover 36:ea2a1b4f51c1 137 if (triggerSensorPolling) {
Rohit Grover 36:ea2a1b4f51c1 138 triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 139
Rohit Grover 36:ea2a1b4f51c1 140 /* Do blocking calls or whatever is necessary for sensor polling. */
thegecko 39:d0a7eb186652 141 /* In our case, we simply update the dummy HRM measurement. */
Rohit Grover 36:ea2a1b4f51c1 142 if (ble.getGapState().connected) {
Rohit Grover 36:ea2a1b4f51c1 143 /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
Rohit Grover 36:ea2a1b4f51c1 144 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
thegecko 39:d0a7eb186652 145 bpm[1] = rand() % 40 + 60;
Rohit Grover 36:ea2a1b4f51c1 146 ble.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm));
thegecko 39:d0a7eb186652 147
thegecko 39:d0a7eb186652 148 float f = 12.5f;//(float)(((float)bpm[1]) / 10.0f);//(rand() % 20 + 360) / 10.0f;
thegecko 39:d0a7eb186652 149 uint8_t tem = quick_ieee11073_from_float(f);
thegecko 39:d0a7eb186652 150 memcpy(temp+1, &tem, 4);
thegecko 39:d0a7eb186652 151 // temp[1] = f;
thegecko 39:d0a7eb186652 152 ble.updateCharacteristicValue(recTemp.getHandle(), temp, sizeof(temp));
thegecko 39:d0a7eb186652 153 }
Rohit Grover 36:ea2a1b4f51c1 154 } else {
Rohit Grover 36:ea2a1b4f51c1 155 ble.waitForEvent();
Rohit Grover 36:ea2a1b4f51c1 156 }
ktownsend 0:87a7fc231fae 157 }
ktownsend 0:87a7fc231fae 158 }