repo time
Dependencies: mbed MAX14720 MAX30205 USBDevice
e4a10ed6eb92/HSP/Hsp_BLE/HspBLE.cpp@20:6d2af70c92ab, 2021-04-06 (annotated)
- Committer:
- darienf
- Date:
- Tue Apr 06 06:41:40 2021 +0000
- Revision:
- 20:6d2af70c92ab
another repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darienf | 20:6d2af70c92ab | 1 | /******************************************************************************* |
darienf | 20:6d2af70c92ab | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
darienf | 20:6d2af70c92ab | 3 | * |
darienf | 20:6d2af70c92ab | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
darienf | 20:6d2af70c92ab | 5 | * copy of this software and associated documentation files (the "Software"), |
darienf | 20:6d2af70c92ab | 6 | * to deal in the Software without restriction, including without limitation |
darienf | 20:6d2af70c92ab | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
darienf | 20:6d2af70c92ab | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
darienf | 20:6d2af70c92ab | 9 | * Software is furnished to do so, subject to the following conditions: |
darienf | 20:6d2af70c92ab | 10 | * |
darienf | 20:6d2af70c92ab | 11 | * The above copyright notice and this permission notice shall be included |
darienf | 20:6d2af70c92ab | 12 | * in all copies or substantial portions of the Software. |
darienf | 20:6d2af70c92ab | 13 | * |
darienf | 20:6d2af70c92ab | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
darienf | 20:6d2af70c92ab | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
darienf | 20:6d2af70c92ab | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
darienf | 20:6d2af70c92ab | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
darienf | 20:6d2af70c92ab | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
darienf | 20:6d2af70c92ab | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
darienf | 20:6d2af70c92ab | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
darienf | 20:6d2af70c92ab | 21 | * |
darienf | 20:6d2af70c92ab | 22 | * Except as contained in this notice, the name of Maxim Integrated |
darienf | 20:6d2af70c92ab | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
darienf | 20:6d2af70c92ab | 24 | * Products, Inc. Branding Policy. |
darienf | 20:6d2af70c92ab | 25 | * |
darienf | 20:6d2af70c92ab | 26 | * The mere transfer of this software does not imply any licenses |
darienf | 20:6d2af70c92ab | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
darienf | 20:6d2af70c92ab | 28 | * trademarks, maskwork rights, or any other form of intellectual |
darienf | 20:6d2af70c92ab | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
darienf | 20:6d2af70c92ab | 30 | * ownership rights. |
darienf | 20:6d2af70c92ab | 31 | ******************************************************************************* |
darienf | 20:6d2af70c92ab | 32 | */ |
darienf | 20:6d2af70c92ab | 33 | #include "HspBLE.h" |
darienf | 20:6d2af70c92ab | 34 | #include "Peripherals.h" |
darienf | 20:6d2af70c92ab | 35 | #include "MAX30001_helper.h" |
darienf | 20:6d2af70c92ab | 36 | |
darienf | 20:6d2af70c92ab | 37 | #define LOW_BYTE(x) ((uint8_t)((x)&0xFF)) |
darienf | 20:6d2af70c92ab | 38 | #define HIGH_BYTE(x) ((uint8_t)(((x) >> 8) & 0xFF)) |
darienf | 20:6d2af70c92ab | 39 | |
darienf | 20:6d2af70c92ab | 40 | /// define all of the characteristic UUIDs |
darienf | 20:6d2af70c92ab | 41 | uint8_t HspBLE::temperatureTopCharUUID[] = {0x35,0x44,0x53,0x1b,0x00,0xc3,0x43,0x42,0x97,0x55,0xb5,0x6a,0xbe,0x8e,0x6c,0x67}; |
darienf | 20:6d2af70c92ab | 42 | uint8_t HspBLE::temperatureBottomCharUUID[] = {0x35,0x44,0x53,0x1b,0x00,0xc3,0x43,0x42,0x97,0x55,0xb5,0x6a,0xbe,0x8e,0x6a,0x66}; |
darienf | 20:6d2af70c92ab | 43 | uint8_t HspBLE::accelerometerCharUUID[] = {0xe6,0xc9,0xda,0x1a,0x80,0x96,0x48,0xbc,0x83,0xa4,0x3f,0xca,0x38,0x37,0x05,0xaf}; |
darienf | 20:6d2af70c92ab | 44 | uint8_t HspBLE::heartrateCharUUID[] = {0x62,0x1a,0x00,0xe3,0xb0,0x93,0x46,0xbf,0xaa,0xdc,0xab,0xe4,0xc6,0x48,0xc5,0x69}; |
darienf | 20:6d2af70c92ab | 45 | uint8_t HspBLE::pressureCharUUID[] = {0x1d,0x8a,0x19,0x32,0xda,0x49,0x49,0xad,0x91,0xd8,0x80,0x08,0x32,0xe7,0xe9,0x40}; |
darienf | 20:6d2af70c92ab | 46 | uint8_t HspBLE::dataCharUUID[] = {0xaa,0x8a,0x19,0x32,0xda,0x49,0x49,0xad,0x91,0xd8,0x80,0x08,0x32,0xe7,0xe9,0x40}; |
darienf | 20:6d2af70c92ab | 47 | uint8_t HspBLE::commandCharUUID[] = {0x36,0xe5,0x5e,0x37,0x6b,0x5b,0x42,0x0b,0x91,0x07,0x0d,0x34,0xa0,0xe8,0x67,0x5a}; |
darienf | 20:6d2af70c92ab | 48 | |
darienf | 20:6d2af70c92ab | 49 | |
darienf | 20:6d2af70c92ab | 50 | /// define the BLE device name |
darienf | 20:6d2af70c92ab | 51 | uint8_t HspBLE::deviceName[] = "MAXREFDES100"; |
darienf | 20:6d2af70c92ab | 52 | /// define the BLE serial number |
darienf | 20:6d2af70c92ab | 53 | uint8_t HspBLE::serialNumber[] = {0x77, 0x22, 0x33, 0x45, 0x67, 0x89}; |
darienf | 20:6d2af70c92ab | 54 | /// define the BLE service UUID |
darienf | 20:6d2af70c92ab | 55 | uint8_t HspBLE::envServiceUUID[] = {0x5c,0x6e,0x40,0xe8,0x3b,0x7f,0x42,0x86,0xa5,0x2f,0xda,0xec,0x46,0xab,0xe8,0x51}; |
darienf | 20:6d2af70c92ab | 56 | |
darienf | 20:6d2af70c92ab | 57 | HspBLE *HspBLE::instance = NULL; |
darienf | 20:6d2af70c92ab | 58 | |
darienf | 20:6d2af70c92ab | 59 | /** |
darienf | 20:6d2af70c92ab | 60 | * @brief Constructor that inits the BLE helper object |
darienf | 20:6d2af70c92ab | 61 | * @param ble Pointer to the mbed BLE object |
darienf | 20:6d2af70c92ab | 62 | */ |
darienf | 20:6d2af70c92ab | 63 | HspBLE::HspBLE(BLE *ble) { |
darienf | 20:6d2af70c92ab | 64 | bluetoothLE = new BluetoothLE(ble, NUMBER_OF_CHARACTERISTICS); |
darienf | 20:6d2af70c92ab | 65 | instance = this; |
darienf | 20:6d2af70c92ab | 66 | notificationUpdateRoundRobin = 0; |
darienf | 20:6d2af70c92ab | 67 | } |
darienf | 20:6d2af70c92ab | 68 | |
darienf | 20:6d2af70c92ab | 69 | /** |
darienf | 20:6d2af70c92ab | 70 | * @brief Constructor that deletes the bluetoothLE object |
darienf | 20:6d2af70c92ab | 71 | */ |
darienf | 20:6d2af70c92ab | 72 | HspBLE::~HspBLE(void) { delete bluetoothLE; } |
darienf | 20:6d2af70c92ab | 73 | |
darienf | 20:6d2af70c92ab | 74 | /** |
darienf | 20:6d2af70c92ab | 75 | * @brief Initialize all of the HSP characteristics, initialize the ble service |
darienf | 20:6d2af70c92ab | 76 | * and attach callbacks |
darienf | 20:6d2af70c92ab | 77 | */ |
darienf | 20:6d2af70c92ab | 78 | void HspBLE::init(void) { |
darienf | 20:6d2af70c92ab | 79 | uint8_t *serialNumberPtr; |
darienf | 20:6d2af70c92ab | 80 | // uint8_t serialNumberBuffer[6]; |
darienf | 20:6d2af70c92ab | 81 | serialNumberPtr = MAX30001_Helper_getVersion(); |
darienf | 20:6d2af70c92ab | 82 | printf("MAX30001 Version = %02X:%02X:%02X:%02X:%02X:%02X...\n", |
darienf | 20:6d2af70c92ab | 83 | serialNumberPtr[0], serialNumberPtr[1], serialNumberPtr[2], |
darienf | 20:6d2af70c92ab | 84 | serialNumberPtr[3], serialNumberPtr[4], serialNumberPtr[5]); |
darienf | 20:6d2af70c92ab | 85 | serialNumberPtr[3] = 0x00; |
darienf | 20:6d2af70c92ab | 86 | serialNumberPtr[4] = 0x00; |
darienf | 20:6d2af70c92ab | 87 | serialNumberPtr[5] = 0x03; |
darienf | 20:6d2af70c92ab | 88 | printf("BLE DeviceID = %02X:%02X:%02X:%02X:%02X:%02X...\n", |
darienf | 20:6d2af70c92ab | 89 | serialNumberPtr[0], serialNumberPtr[1], serialNumberPtr[2], |
darienf | 20:6d2af70c92ab | 90 | serialNumberPtr[3], serialNumberPtr[4], serialNumberPtr[5]); |
darienf | 20:6d2af70c92ab | 91 | |
darienf | 20:6d2af70c92ab | 92 | bluetoothLE->addCharacteristic(new Characteristic(2 /* number of bytes */,temperatureTopCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)); |
darienf | 20:6d2af70c92ab | 93 | bluetoothLE->addCharacteristic(new Characteristic(2 /* number of bytes */,temperatureBottomCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)); |
darienf | 20:6d2af70c92ab | 94 | bluetoothLE->addCharacteristic(new Characteristic(6 /* number of bytes */,accelerometerCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)); |
darienf | 20:6d2af70c92ab | 95 | bluetoothLE->addCharacteristic(new Characteristic(4 /* number of bytes */,heartrateCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)); |
darienf | 20:6d2af70c92ab | 96 | bluetoothLE->addCharacteristic(new Characteristic(8 /* number of bytes */,pressureCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)); |
darienf | 20:6d2af70c92ab | 97 | bluetoothLE->addCharacteristic(new Characteristic(32 /* number of bytes */,dataCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)); |
darienf | 20:6d2af70c92ab | 98 | bluetoothLE->addCharacteristic(new Characteristic(1 /* number of bytes */,commandCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)); |
darienf | 20:6d2af70c92ab | 99 | bluetoothLE->initService(serialNumberPtr, deviceName, sizeof(deviceName),envServiceUUID); |
darienf | 20:6d2af70c92ab | 100 | |
darienf | 20:6d2af70c92ab | 101 | bluetoothLE->onDataWritten(&HspBLE::_onDataWritten); |
darienf | 20:6d2af70c92ab | 102 | ticker.attach(this, &HspBLE::tickerHandler, 1); |
darienf | 20:6d2af70c92ab | 103 | } |
darienf | 20:6d2af70c92ab | 104 | |
darienf | 20:6d2af70c92ab | 105 | void HspBLE::_onDataWritten(int index) { |
darienf | 20:6d2af70c92ab | 106 | HspBLE::instance->onDataWritten(index); |
darienf | 20:6d2af70c92ab | 107 | } |
darienf | 20:6d2af70c92ab | 108 | |
darienf | 20:6d2af70c92ab | 109 | /** |
darienf | 20:6d2af70c92ab | 110 | * @brief Callback for written characteristics |
darienf | 20:6d2af70c92ab | 111 | * @param index Index of whose characteristic is written to |
darienf | 20:6d2af70c92ab | 112 | */ |
darienf | 20:6d2af70c92ab | 113 | void HspBLE::onDataWritten(int index) { |
darienf | 20:6d2af70c92ab | 114 | int length; |
darienf | 20:6d2af70c92ab | 115 | uint8_t *data; |
darienf | 20:6d2af70c92ab | 116 | printf("onDataWritten "); |
darienf | 20:6d2af70c92ab | 117 | if (index == CHARACTERISTIC_CMD) { |
darienf | 20:6d2af70c92ab | 118 | data = bluetoothLE->getDataWritten(index, &length); |
darienf | 20:6d2af70c92ab | 119 | if (length >= 1) { |
darienf | 20:6d2af70c92ab | 120 | if (data[0] == 0x00) startDataLogging = false; |
darienf | 20:6d2af70c92ab | 121 | if (data[0] == 0x01) startDataLogging = true; |
darienf | 20:6d2af70c92ab | 122 | printf("onDataWritten index %d, data %02X, length %d ",index,data[0],length); fflush(stdout); |
darienf | 20:6d2af70c92ab | 123 | |
darienf | 20:6d2af70c92ab | 124 | } |
darienf | 20:6d2af70c92ab | 125 | } |
darienf | 20:6d2af70c92ab | 126 | } |
darienf | 20:6d2af70c92ab | 127 | |
darienf | 20:6d2af70c92ab | 128 | void HspBLE::pollSensor(int sensorId, uint8_t *data) { |
darienf | 20:6d2af70c92ab | 129 | |
darienf | 20:6d2af70c92ab | 130 | switch (sensorId) { |
darienf | 20:6d2af70c92ab | 131 | case CHARACTERISTIC_TEMP_TOP: { |
darienf | 20:6d2af70c92ab | 132 | uint16_t uShort; |
darienf | 20:6d2af70c92ab | 133 | Peripherals::max30205_top()->readTemperature(&uShort); |
darienf | 20:6d2af70c92ab | 134 | data[0] = HIGH_BYTE(uShort); |
darienf | 20:6d2af70c92ab | 135 | data[1] = LOW_BYTE(uShort); |
darienf | 20:6d2af70c92ab | 136 | } break; |
darienf | 20:6d2af70c92ab | 137 | case CHARACTERISTIC_TEMP_BOTTOM: { |
darienf | 20:6d2af70c92ab | 138 | uint16_t uShort; |
darienf | 20:6d2af70c92ab | 139 | Peripherals::max30205_bottom()->readTemperature(&uShort); |
darienf | 20:6d2af70c92ab | 140 | data[0] = HIGH_BYTE(uShort); |
darienf | 20:6d2af70c92ab | 141 | data[1] = LOW_BYTE(uShort); |
darienf | 20:6d2af70c92ab | 142 | } break; |
darienf | 20:6d2af70c92ab | 143 | case CHARACTERISTIC_ACCELEROMETER: { |
darienf | 20:6d2af70c92ab | 144 | int i; |
darienf | 20:6d2af70c92ab | 145 | uint8_t *bytePtr; |
darienf | 20:6d2af70c92ab | 146 | int16_t acclPtr[3]; |
darienf | 20:6d2af70c92ab | 147 | Peripherals::lis2dh()->get_motion_cached(&acclPtr[0], &acclPtr[1], |
darienf | 20:6d2af70c92ab | 148 | &acclPtr[2]); |
darienf | 20:6d2af70c92ab | 149 | bytePtr = reinterpret_cast<uint8_t *>(&acclPtr); |
darienf | 20:6d2af70c92ab | 150 | for (i = 0; i < sizeof(acclPtr); i++) |
darienf | 20:6d2af70c92ab | 151 | data[i] = bytePtr[i]; |
darienf | 20:6d2af70c92ab | 152 | } break; |
darienf | 20:6d2af70c92ab | 153 | case CHARACTERISTIC_PRESSURE: { |
darienf | 20:6d2af70c92ab | 154 | int i; |
darienf | 20:6d2af70c92ab | 155 | uint8_t *bytePtr; |
darienf | 20:6d2af70c92ab | 156 | float temperature; |
darienf | 20:6d2af70c92ab | 157 | float pressure; |
darienf | 20:6d2af70c92ab | 158 | Peripherals::bmp280()->ReadCompData(&temperature, &pressure); |
darienf | 20:6d2af70c92ab | 159 | bytePtr = reinterpret_cast<uint8_t *>(&temperature); |
darienf | 20:6d2af70c92ab | 160 | for (i = 0; i < sizeof(float); i++) |
darienf | 20:6d2af70c92ab | 161 | data[i] = bytePtr[i]; |
darienf | 20:6d2af70c92ab | 162 | bytePtr = reinterpret_cast<uint8_t *>(&pressure); |
darienf | 20:6d2af70c92ab | 163 | for (i = 0; i < sizeof(float); i++) |
darienf | 20:6d2af70c92ab | 164 | data[i + sizeof(float)] = bytePtr[i]; |
darienf | 20:6d2af70c92ab | 165 | } break; |
darienf | 20:6d2af70c92ab | 166 | case CHARACTERISTIC_HEARTRATE: { |
darienf | 20:6d2af70c92ab | 167 | int i; |
darienf | 20:6d2af70c92ab | 168 | uint8_t *bytePtr; |
darienf | 20:6d2af70c92ab | 169 | MAX30001::max30001_t heartrateData; |
darienf | 20:6d2af70c92ab | 170 | Peripherals::max30001()->max30001_ReadHeartrateData(&heartrateData); |
darienf | 20:6d2af70c92ab | 171 | bytePtr = reinterpret_cast<uint8_t *>(&heartrateData); |
darienf | 20:6d2af70c92ab | 172 | for (i = 0; i < sizeof(MAX30001::max30001_t); i++) |
darienf | 20:6d2af70c92ab | 173 | data[i] = bytePtr[i]; |
darienf | 20:6d2af70c92ab | 174 | } break; |
darienf | 20:6d2af70c92ab | 175 | } |
darienf | 20:6d2af70c92ab | 176 | } |
darienf | 20:6d2af70c92ab | 177 | |
darienf | 20:6d2af70c92ab | 178 | bool HspBLE::getStartDataLogging(void) { return startDataLogging; } |
darienf | 20:6d2af70c92ab | 179 | |
darienf | 20:6d2af70c92ab | 180 | /** |
darienf | 20:6d2af70c92ab | 181 | * @brief Timer Callback that updates all sensor characteristic notifications |
darienf | 20:6d2af70c92ab | 182 | */ |
darienf | 20:6d2af70c92ab | 183 | void HspBLE::tickerHandler(void) { |
darienf | 20:6d2af70c92ab | 184 | uint8_t data[8]; |
darienf | 20:6d2af70c92ab | 185 | if (bluetoothLE->isConnected()) { |
darienf | 20:6d2af70c92ab | 186 | pollSensor(CHARACTERISTIC_TEMP_TOP, data); |
darienf | 20:6d2af70c92ab | 187 | bluetoothLE->notifyCharacteristic(CHARACTERISTIC_TEMP_TOP, data); |
darienf | 20:6d2af70c92ab | 188 | pollSensor(CHARACTERISTIC_TEMP_BOTTOM, data); |
darienf | 20:6d2af70c92ab | 189 | bluetoothLE->notifyCharacteristic(CHARACTERISTIC_TEMP_BOTTOM, data); |
darienf | 20:6d2af70c92ab | 190 | pollSensor(CHARACTERISTIC_ACCELEROMETER, data); |
darienf | 20:6d2af70c92ab | 191 | bluetoothLE->notifyCharacteristic(CHARACTERISTIC_ACCELEROMETER, data); |
darienf | 20:6d2af70c92ab | 192 | pollSensor(CHARACTERISTIC_HEARTRATE, data); |
darienf | 20:6d2af70c92ab | 193 | bluetoothLE->notifyCharacteristic(CHARACTERISTIC_HEARTRATE, data); |
darienf | 20:6d2af70c92ab | 194 | pollSensor(CHARACTERISTIC_PRESSURE, data); |
darienf | 20:6d2af70c92ab | 195 | bluetoothLE->notifyCharacteristic(CHARACTERISTIC_PRESSURE, data); |
darienf | 20:6d2af70c92ab | 196 | } |
darienf | 20:6d2af70c92ab | 197 | } |