Darien Figueroa / Mbed OS Final_Program

Dependencies:   USBDevice

Committer:
darienf
Date:
Sun May 02 23:09:04 2021 +0000
Revision:
5:bc128a16232f
Parent:
3:36de8b9e4b1a
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 "BluetoothLE.h"
darienf 3:36de8b9e4b1a 34 #include "Characteristic.h"
darienf 3:36de8b9e4b1a 35
darienf 3:36de8b9e4b1a 36 /**
darienf 3:36de8b9e4b1a 37 * @brief Constructor for class
darienf 3:36de8b9e4b1a 38 */
darienf 3:36de8b9e4b1a 39 BluetoothLE::BluetoothLE(BLE *ble, int numberOfCharacteristics)
darienf 3:36de8b9e4b1a 40 : ble(ble), runningIndex(0),
darienf 3:36de8b9e4b1a 41 numberOfCharacteristics(numberOfCharacteristics) {
darienf 3:36de8b9e4b1a 42 characteristics = new Characteristic *[numberOfCharacteristics];
darienf 3:36de8b9e4b1a 43 gattCharacteristics = new GattCharacteristic *[numberOfCharacteristics];
darienf 3:36de8b9e4b1a 44 }
darienf 3:36de8b9e4b1a 45
darienf 3:36de8b9e4b1a 46 /**
darienf 3:36de8b9e4b1a 47 * @brief Destructor for class
darienf 3:36de8b9e4b1a 48 */
darienf 3:36de8b9e4b1a 49 BluetoothLE::~BluetoothLE(void) {
darienf 3:36de8b9e4b1a 50 for (int i = 0; i < numberOfCharacteristics; i++) delete characteristics[i];
darienf 3:36de8b9e4b1a 51 delete[] gattCharacteristics;
darienf 3:36de8b9e4b1a 52 delete[] characteristics;
darienf 3:36de8b9e4b1a 53 }
darienf 3:36de8b9e4b1a 54
darienf 3:36de8b9e4b1a 55 /**
darienf 3:36de8b9e4b1a 56 * @brief Initialize the advertising, characteristics, service for this platform
darienf 3:36de8b9e4b1a 57 */
darienf 3:36de8b9e4b1a 58 void BluetoothLE::addCharacteristic(Characteristic *characteristic) {
darienf 3:36de8b9e4b1a 59 characteristic->setBLE(ble);
darienf 3:36de8b9e4b1a 60 characteristic->setIndex(runningIndex);
darienf 3:36de8b9e4b1a 61 characteristics[runningIndex] = characteristic;
darienf 3:36de8b9e4b1a 62 gattCharacteristics[runningIndex] = characteristic->getGattCharacteristic();
darienf 3:36de8b9e4b1a 63 runningIndex++;
darienf 3:36de8b9e4b1a 64 }
darienf 3:36de8b9e4b1a 65
darienf 3:36de8b9e4b1a 66 /**
darienf 3:36de8b9e4b1a 67 * @brief Initialize the advertising, characteristics, service for this platform
darienf 3:36de8b9e4b1a 68 */
darienf 3:36de8b9e4b1a 69 void BluetoothLE::initService(uint8_t *serialNumber, uint8_t *deviceName,
darienf 3:36de8b9e4b1a 70 int nameSize, uint8_t *serviceUUID) {
darienf 3:36de8b9e4b1a 71 ble->init();
darienf 3:36de8b9e4b1a 72 ble->onDisconnection(this, &BluetoothLE::disconnectionCallback);
darienf 3:36de8b9e4b1a 73
darienf 3:36de8b9e4b1a 74 ble->gap().setAddress((BLEProtocol::AddressType_t)0, serialNumber);
darienf 3:36de8b9e4b1a 75
darienf 3:36de8b9e4b1a 76 // Setup Advertising
darienf 3:36de8b9e4b1a 77 ble->accumulateAdvertisingPayload(
darienf 3:36de8b9e4b1a 78 GapAdvertisingData::BREDR_NOT_SUPPORTED |
darienf 3:36de8b9e4b1a 79 GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
darienf 3:36de8b9e4b1a 80 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
darienf 3:36de8b9e4b1a 81 (uint8_t *)deviceName, nameSize);
darienf 3:36de8b9e4b1a 82 ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
darienf 3:36de8b9e4b1a 83 ble->setAdvertisingInterval(1600); // 1000ms; in multiples of 0.625ms.
darienf 3:36de8b9e4b1a 84 GattService envService(serviceUUID, gattCharacteristics,
darienf 3:36de8b9e4b1a 85 numberOfCharacteristics);
darienf 3:36de8b9e4b1a 86 ble->gattServer().addService(envService);
darienf 3:36de8b9e4b1a 87 ble->onDataWritten(this, &BluetoothLE::onDataWritten);
darienf 3:36de8b9e4b1a 88 // Start Advertising
darienf 3:36de8b9e4b1a 89 ble->startAdvertising();
darienf 3:36de8b9e4b1a 90 }
darienf 3:36de8b9e4b1a 91
darienf 3:36de8b9e4b1a 92 static const Gap::ConnectionParams_t paramsLowPower = {
darienf 3:36de8b9e4b1a 93 400, /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 3:36de8b9e4b1a 94 400, /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 3:36de8b9e4b1a 95 60, /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
darienf 3:36de8b9e4b1a 96 3100 /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 3:36de8b9e4b1a 97 };
darienf 3:36de8b9e4b1a 98
darienf 3:36de8b9e4b1a 99 static Gap::Handle_t connHandle = 0;
darienf 3:36de8b9e4b1a 100
darienf 3:36de8b9e4b1a 101 /**
darienf 3:36de8b9e4b1a 102 * @brief Called on BLE connection
darienf 3:36de8b9e4b1a 103 */
darienf 3:36de8b9e4b1a 104 void BluetoothLE::connectionCallback(
darienf 3:36de8b9e4b1a 105 const Gap::ConnectionCallbackParams_t *params) {
darienf 3:36de8b9e4b1a 106 connHandle = params->handle;
darienf 3:36de8b9e4b1a 107 }
darienf 3:36de8b9e4b1a 108
darienf 3:36de8b9e4b1a 109 /**
darienf 3:36de8b9e4b1a 110 * @brief Start advertising on a disconnect
darienf 3:36de8b9e4b1a 111 */
darienf 3:36de8b9e4b1a 112 void BluetoothLE::disconnectionCallback(
darienf 3:36de8b9e4b1a 113 const Gap::DisconnectionCallbackParams_t *params) {
darienf 3:36de8b9e4b1a 114 _isConnected = false;
darienf 3:36de8b9e4b1a 115 ble->startAdvertising();
darienf 3:36de8b9e4b1a 116 }
darienf 3:36de8b9e4b1a 117
darienf 3:36de8b9e4b1a 118 /**
darienf 3:36de8b9e4b1a 119 * @brief Called when the client writes to a writable characteristic
darienf 3:36de8b9e4b1a 120 * @param params Pointer to a structure that contains details on what was
darienf 3:36de8b9e4b1a 121 * written
darienf 3:36de8b9e4b1a 122 */
darienf 3:36de8b9e4b1a 123 void BluetoothLE::onDataWritten(const GattWriteCallbackParams *params) {
darienf 3:36de8b9e4b1a 124 int i;
darienf 3:36de8b9e4b1a 125 int index = 0;
darienf 3:36de8b9e4b1a 126 // match the characteristic handle
darienf 3:36de8b9e4b1a 127 printf("BluetoothLE::onDataWritten ");
darienf 3:36de8b9e4b1a 128 for (i = 0; i < numberOfCharacteristics; i++) {
darienf 3:36de8b9e4b1a 129 if (params->handle == gattCharacteristics[i]->getValueHandle()) {
darienf 3:36de8b9e4b1a 130 characteristics[i]->copyDataWritten(params);
darienf 3:36de8b9e4b1a 131 index = i;
darienf 3:36de8b9e4b1a 132 break;
darienf 3:36de8b9e4b1a 133 }
darienf 3:36de8b9e4b1a 134 }
darienf 3:36de8b9e4b1a 135 (*_onDataWritten)(index);
darienf 3:36de8b9e4b1a 136 }
darienf 3:36de8b9e4b1a 137
darienf 3:36de8b9e4b1a 138 /**
darienf 3:36de8b9e4b1a 139 * @brief Update the characteristic notification
darienf 3:36de8b9e4b1a 140 * @param index Index of the characteristic
darienf 3:36de8b9e4b1a 141 * @param data Pointer to the byte data to update the charateristic payload
darienf 3:36de8b9e4b1a 142 */
darienf 3:36de8b9e4b1a 143 void BluetoothLE::notifyCharacteristic(int index, uint8_t *data) {
darienf 3:36de8b9e4b1a 144 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) {
darienf 3:36de8b9e4b1a 145 characteristics[index]->getPayloadBytes()[i] = data[i];
darienf 3:36de8b9e4b1a 146 }
darienf 3:36de8b9e4b1a 147 characteristics[index]->update();
darienf 3:36de8b9e4b1a 148 }
darienf 3:36de8b9e4b1a 149
darienf 3:36de8b9e4b1a 150 /**
darienf 3:36de8b9e4b1a 151 * @brief Update the characteristic notification
darienf 3:36de8b9e4b1a 152 * @param index Index of the characteristic
darienf 3:36de8b9e4b1a 153 * @param data Pointer to the byte data to update the charateristic payload
darienf 3:36de8b9e4b1a 154 */
darienf 3:36de8b9e4b1a 155 void BluetoothLE::notifyCharacteristicTest(int index) {
darienf 3:36de8b9e4b1a 156 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) {
darienf 3:36de8b9e4b1a 157 characteristics[index]->getPayloadBytes()[i]++;
darienf 3:36de8b9e4b1a 158 }
darienf 3:36de8b9e4b1a 159 characteristics[index]->update();
darienf 3:36de8b9e4b1a 160 }
darienf 3:36de8b9e4b1a 161
darienf 3:36de8b9e4b1a 162 uint8_t *BluetoothLE::getDataWritten(int index, int *length) {
darienf 3:36de8b9e4b1a 163 return characteristics[index]->getDataWritten(length);
darienf 3:36de8b9e4b1a 164 }
darienf 3:36de8b9e4b1a 165
darienf 3:36de8b9e4b1a 166 /**
darienf 3:36de8b9e4b1a 167 * @brief Function to query if a BLE connection is active
darienf 3:36de8b9e4b1a 168 * @return true if BLE connected, false if BLE is not connected
darienf 3:36de8b9e4b1a 169 */
darienf 3:36de8b9e4b1a 170 bool BluetoothLE::isConnected(void) {
darienf 3:36de8b9e4b1a 171 return (ble->getGapState().connected == 1 ? true : false);
darienf 3:36de8b9e4b1a 172 }