This is the latest working repository used in our demo video for the Maxim to display temperature readings on Bluetooth

Dependencies:   USBDevice

Committer:
darienf
Date:
Sat Apr 10 07:51:22 2021 +0000
Revision:
4:1fcd660d72fe
Parent:
3:36de8b9e4b1a
Child:
5:bc128a16232f
"UUID no show on ipad, but phone yes" - ale; ; now I can sleep

Who changed what in which revision?

UserRevisionLine numberNew 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 4:1fcd660d72fe 48 //our ECG data characteristic
darienf 4:1fcd660d72fe 49 uint8_t HspBLE::ecgCharUUID[] = {0x30,0xc1,0x3a,0x49,0x5d,0x2d,0x42,0x1b,0x8a,0x30,0x7f,0x21,0xb7,0x0f,0x8a,0x94};
darienf 4:1fcd660d72fe 50 // "30c13a49-5d2d-421b-8a30-7f21b70f8a94"
darienf 3:36de8b9e4b1a 51
darienf 3:36de8b9e4b1a 52 /// define the BLE device name
darienf 3:36de8b9e4b1a 53 uint8_t HspBLE::deviceName[] = "MAXREFDES100";
darienf 3:36de8b9e4b1a 54 /// define the BLE serial number
darienf 3:36de8b9e4b1a 55 uint8_t HspBLE::serialNumber[] = {0x77, 0x22, 0x33, 0x45, 0x67, 0x89};
darienf 3:36de8b9e4b1a 56 /// define the BLE service UUID
darienf 3:36de8b9e4b1a 57 uint8_t HspBLE::envServiceUUID[] = {0x5c,0x6e,0x40,0xe8,0x3b,0x7f,0x42,0x86,0xa5,0x2f,0xda,0xec,0x46,0xab,0xe8,0x51};
darienf 3:36de8b9e4b1a 58
darienf 3:36de8b9e4b1a 59 HspBLE *HspBLE::instance = NULL;
darienf 3:36de8b9e4b1a 60
darienf 3:36de8b9e4b1a 61 /**
darienf 3:36de8b9e4b1a 62 * @brief Constructor that inits the BLE helper object
darienf 3:36de8b9e4b1a 63 * @param ble Pointer to the mbed BLE object
darienf 3:36de8b9e4b1a 64 */
darienf 3:36de8b9e4b1a 65 HspBLE::HspBLE(BLE *ble) {
darienf 3:36de8b9e4b1a 66 bluetoothLE = new BluetoothLE(ble, NUMBER_OF_CHARACTERISTICS);
darienf 3:36de8b9e4b1a 67 instance = this;
darienf 3:36de8b9e4b1a 68 notificationUpdateRoundRobin = 0;
darienf 3:36de8b9e4b1a 69 }
darienf 3:36de8b9e4b1a 70
darienf 3:36de8b9e4b1a 71 /**
darienf 3:36de8b9e4b1a 72 * @brief Constructor that deletes the bluetoothLE object
darienf 3:36de8b9e4b1a 73 */
darienf 3:36de8b9e4b1a 74 HspBLE::~HspBLE(void) { delete bluetoothLE; }
darienf 3:36de8b9e4b1a 75
darienf 3:36de8b9e4b1a 76 /**
darienf 3:36de8b9e4b1a 77 * @brief Initialize all of the HSP characteristics, initialize the ble service
darienf 3:36de8b9e4b1a 78 * and attach callbacks
darienf 3:36de8b9e4b1a 79 */
darienf 3:36de8b9e4b1a 80 void HspBLE::init(void) {
darienf 3:36de8b9e4b1a 81 uint8_t *serialNumberPtr;
darienf 3:36de8b9e4b1a 82 // uint8_t serialNumberBuffer[6];
darienf 3:36de8b9e4b1a 83 serialNumberPtr = MAX30001_Helper_getVersion();
darienf 3:36de8b9e4b1a 84 printf("MAX30001 Version = %02X:%02X:%02X:%02X:%02X:%02X...\n",
darienf 3:36de8b9e4b1a 85 serialNumberPtr[0], serialNumberPtr[1], serialNumberPtr[2],
darienf 3:36de8b9e4b1a 86 serialNumberPtr[3], serialNumberPtr[4], serialNumberPtr[5]);
darienf 3:36de8b9e4b1a 87 serialNumberPtr[3] = 0x00;
darienf 3:36de8b9e4b1a 88 serialNumberPtr[4] = 0x00;
darienf 3:36de8b9e4b1a 89 serialNumberPtr[5] = 0x03;
darienf 3:36de8b9e4b1a 90 printf("BLE DeviceID = %02X:%02X:%02X:%02X:%02X:%02X...\n",
darienf 3:36de8b9e4b1a 91 serialNumberPtr[0], serialNumberPtr[1], serialNumberPtr[2],
darienf 3:36de8b9e4b1a 92 serialNumberPtr[3], serialNumberPtr[4], serialNumberPtr[5]);
darienf 3:36de8b9e4b1a 93
darienf 3:36de8b9e4b1a 94 bluetoothLE->addCharacteristic(new Characteristic(2 /* number of bytes */,temperatureTopCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 3:36de8b9e4b1a 95 bluetoothLE->addCharacteristic(new Characteristic(2 /* number of bytes */,temperatureBottomCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 3:36de8b9e4b1a 96 bluetoothLE->addCharacteristic(new Characteristic(6 /* number of bytes */,accelerometerCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 3:36de8b9e4b1a 97 bluetoothLE->addCharacteristic(new Characteristic(4 /* number of bytes */,heartrateCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 3:36de8b9e4b1a 98 bluetoothLE->addCharacteristic(new Characteristic(8 /* number of bytes */,pressureCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 3:36de8b9e4b1a 99 bluetoothLE->addCharacteristic(new Characteristic(32 /* number of bytes */,dataCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE));
darienf 4:1fcd660d72fe 100 bluetoothLE->addCharacteristic(new Characteristic(32 /* number of bytes */,ecgCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY));
darienf 4:1fcd660d72fe 101 //^that's our boy
darienf 3:36de8b9e4b1a 102 bluetoothLE->addCharacteristic(new Characteristic(1 /* number of bytes */,commandCharUUID,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE));
darienf 4:1fcd660d72fe 103
darienf 3:36de8b9e4b1a 104 bluetoothLE->initService(serialNumberPtr, deviceName, sizeof(deviceName),envServiceUUID);
darienf 4:1fcd660d72fe 105
darienf 3:36de8b9e4b1a 106 bluetoothLE->onDataWritten(&HspBLE::_onDataWritten);
darienf 3:36de8b9e4b1a 107 ticker.attach(this, &HspBLE::tickerHandler, 1);
darienf 3:36de8b9e4b1a 108 }
darienf 3:36de8b9e4b1a 109
darienf 3:36de8b9e4b1a 110 void HspBLE::_onDataWritten(int index) {
darienf 3:36de8b9e4b1a 111 HspBLE::instance->onDataWritten(index);
darienf 3:36de8b9e4b1a 112 }
darienf 3:36de8b9e4b1a 113
darienf 3:36de8b9e4b1a 114 /**
darienf 3:36de8b9e4b1a 115 * @brief Callback for written characteristics
darienf 3:36de8b9e4b1a 116 * @param index Index of whose characteristic is written to
darienf 3:36de8b9e4b1a 117 */
darienf 3:36de8b9e4b1a 118 void HspBLE::onDataWritten(int index) {
darienf 3:36de8b9e4b1a 119 int length;
darienf 3:36de8b9e4b1a 120 uint8_t *data;
darienf 3:36de8b9e4b1a 121 printf("onDataWritten ");
darienf 3:36de8b9e4b1a 122 if (index == CHARACTERISTIC_CMD) {
darienf 3:36de8b9e4b1a 123 data = bluetoothLE->getDataWritten(index, &length);
darienf 3:36de8b9e4b1a 124 if (length >= 1) {
darienf 3:36de8b9e4b1a 125 if (data[0] == 0x00) startDataLogging = false;
darienf 3:36de8b9e4b1a 126 if (data[0] == 0x01) startDataLogging = true;
darienf 3:36de8b9e4b1a 127 printf("onDataWritten index %d, data %02X, length %d ",index,data[0],length); fflush(stdout);
darienf 3:36de8b9e4b1a 128
darienf 3:36de8b9e4b1a 129 }
darienf 3:36de8b9e4b1a 130 }
darienf 3:36de8b9e4b1a 131 }
darienf 3:36de8b9e4b1a 132
darienf 3:36de8b9e4b1a 133 void HspBLE::pollSensor(int sensorId, uint8_t *data) {
darienf 3:36de8b9e4b1a 134
darienf 3:36de8b9e4b1a 135 switch (sensorId) {
darienf 3:36de8b9e4b1a 136 case CHARACTERISTIC_TEMP_TOP: {
darienf 3:36de8b9e4b1a 137 uint16_t uShort;
darienf 3:36de8b9e4b1a 138 Peripherals::max30205_top()->readTemperature(&uShort);
darienf 3:36de8b9e4b1a 139 data[0] = HIGH_BYTE(uShort);
darienf 3:36de8b9e4b1a 140 data[1] = LOW_BYTE(uShort);
darienf 3:36de8b9e4b1a 141 } break;
darienf 3:36de8b9e4b1a 142 case CHARACTERISTIC_TEMP_BOTTOM: {
darienf 3:36de8b9e4b1a 143 uint16_t uShort;
darienf 3:36de8b9e4b1a 144 Peripherals::max30205_bottom()->readTemperature(&uShort);
darienf 3:36de8b9e4b1a 145 data[0] = HIGH_BYTE(uShort);
darienf 3:36de8b9e4b1a 146 data[1] = LOW_BYTE(uShort);
darienf 3:36de8b9e4b1a 147 } break;
darienf 3:36de8b9e4b1a 148 case CHARACTERISTIC_ACCELEROMETER: {
darienf 3:36de8b9e4b1a 149 int i;
darienf 3:36de8b9e4b1a 150 uint8_t *bytePtr;
darienf 3:36de8b9e4b1a 151 int16_t acclPtr[3];
darienf 3:36de8b9e4b1a 152 Peripherals::lis2dh()->get_motion_cached(&acclPtr[0], &acclPtr[1],
darienf 3:36de8b9e4b1a 153 &acclPtr[2]);
darienf 3:36de8b9e4b1a 154 bytePtr = reinterpret_cast<uint8_t *>(&acclPtr);
darienf 3:36de8b9e4b1a 155 for (i = 0; i < sizeof(acclPtr); i++)
darienf 3:36de8b9e4b1a 156 data[i] = bytePtr[i];
darienf 3:36de8b9e4b1a 157 } break;
darienf 3:36de8b9e4b1a 158 case CHARACTERISTIC_PRESSURE: {
darienf 3:36de8b9e4b1a 159 int i;
darienf 3:36de8b9e4b1a 160 uint8_t *bytePtr;
darienf 3:36de8b9e4b1a 161 float temperature;
darienf 3:36de8b9e4b1a 162 float pressure;
darienf 3:36de8b9e4b1a 163 Peripherals::bmp280()->ReadCompData(&temperature, &pressure);
darienf 3:36de8b9e4b1a 164 bytePtr = reinterpret_cast<uint8_t *>(&temperature);
darienf 3:36de8b9e4b1a 165 for (i = 0; i < sizeof(float); i++)
darienf 3:36de8b9e4b1a 166 data[i] = bytePtr[i];
darienf 3:36de8b9e4b1a 167 bytePtr = reinterpret_cast<uint8_t *>(&pressure);
darienf 3:36de8b9e4b1a 168 for (i = 0; i < sizeof(float); i++)
darienf 3:36de8b9e4b1a 169 data[i + sizeof(float)] = bytePtr[i];
darienf 3:36de8b9e4b1a 170 } break;
darienf 3:36de8b9e4b1a 171 case CHARACTERISTIC_HEARTRATE: {
darienf 3:36de8b9e4b1a 172 int i;
darienf 3:36de8b9e4b1a 173 uint8_t *bytePtr;
darienf 3:36de8b9e4b1a 174 MAX30001::max30001_t heartrateData;
darienf 3:36de8b9e4b1a 175 Peripherals::max30001()->max30001_ReadHeartrateData(&heartrateData);
darienf 3:36de8b9e4b1a 176 bytePtr = reinterpret_cast<uint8_t *>(&heartrateData);
darienf 3:36de8b9e4b1a 177 for (i = 0; i < sizeof(MAX30001::max30001_t); i++)
darienf 3:36de8b9e4b1a 178 data[i] = bytePtr[i];
darienf 3:36de8b9e4b1a 179 } break;
darienf 4:1fcd660d72fe 180 case CHARACTERISTIC_ECG: {
darienf 4:1fcd660d72fe 181 int i;
darienf 4:1fcd660d72fe 182 uint8_t *bytePtr;
darienf 4:1fcd660d72fe 183 MAX30001::max30001_t hopeData;
darienf 4:1fcd660d72fe 184 Peripherals::max30001()->max30001_ReadECGData(&hopeData);
darienf 4:1fcd660d72fe 185 bytePtr = reinterpret_cast<uint8_t *>(&hopeData);
darienf 4:1fcd660d72fe 186 for (i = 0; i < sizeof(MAX30001::max30001_t); i++)
darienf 4:1fcd660d72fe 187 data[i] = bytePtr[i];
darienf 4:1fcd660d72fe 188 } break;
darienf 3:36de8b9e4b1a 189 }
darienf 3:36de8b9e4b1a 190 }
darienf 3:36de8b9e4b1a 191
darienf 3:36de8b9e4b1a 192 bool HspBLE::getStartDataLogging(void) { return startDataLogging; }
darienf 3:36de8b9e4b1a 193
darienf 3:36de8b9e4b1a 194 /**
darienf 3:36de8b9e4b1a 195 * @brief Timer Callback that updates all sensor characteristic notifications
darienf 3:36de8b9e4b1a 196 */
darienf 3:36de8b9e4b1a 197 void HspBLE::tickerHandler(void) {
darienf 3:36de8b9e4b1a 198 uint8_t data[8];
darienf 3:36de8b9e4b1a 199 if (bluetoothLE->isConnected()) {
darienf 3:36de8b9e4b1a 200 pollSensor(CHARACTERISTIC_TEMP_TOP, data);
darienf 3:36de8b9e4b1a 201 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_TEMP_TOP, data);
darienf 3:36de8b9e4b1a 202 pollSensor(CHARACTERISTIC_TEMP_BOTTOM, data);
darienf 3:36de8b9e4b1a 203 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_TEMP_BOTTOM, data);
darienf 3:36de8b9e4b1a 204 pollSensor(CHARACTERISTIC_ACCELEROMETER, data);
darienf 3:36de8b9e4b1a 205 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_ACCELEROMETER, data);
darienf 3:36de8b9e4b1a 206 pollSensor(CHARACTERISTIC_HEARTRATE, data);
darienf 3:36de8b9e4b1a 207 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_HEARTRATE, data);
darienf 3:36de8b9e4b1a 208 pollSensor(CHARACTERISTIC_PRESSURE, data);
darienf 3:36de8b9e4b1a 209 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_PRESSURE, data);
darienf 4:1fcd660d72fe 210 pollSensor(CHARACTERISTIC_ECG, data);
darienf 4:1fcd660d72fe 211 bluetoothLE->notifyCharacteristic(CHARACTERISTIC_ECG, data);
darienf 3:36de8b9e4b1a 212 }
darienf 3:36de8b9e4b1a 213 }