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:
Sun May 02 23:09:04 2021 +0000
Revision:
5:bc128a16232f
Parent:
4:1fcd660d72fe
This is the program that was last used, that has the working temperature and some comments

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