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