publishing

Dependencies:   USBDevice

Committer:
asilva14
Date:
Thu Mar 09 23:54:04 2017 +0000
Revision:
0:fd5a76956754
Child:
1:9532b2cc79aa
commit

Who changed what in which revision?

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