BTLE demo for MAXWSNENV.

Dependencies:   BLE_API BMP180 Si7020 mbed MaximBLE

Committer:
enginerd
Date:
Thu Aug 18 21:09:13 2016 +0000
Revision:
2:6f76d6160601
Parent:
0:f71931ae3db1
Updated library versions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kgills 0:f71931ae3db1 1 /*******************************************************************************
kgills 0:f71931ae3db1 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
kgills 0:f71931ae3db1 3 *
kgills 0:f71931ae3db1 4 * Permission is hereby granted, free of charge, to any person obtaining a
kgills 0:f71931ae3db1 5 * copy of this software and associated documentation files (the "Software"),
kgills 0:f71931ae3db1 6 * to deal in the Software without restriction, including without limitation
kgills 0:f71931ae3db1 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
kgills 0:f71931ae3db1 8 * and/or sell copies of the Software, and to permit persons to whom the
kgills 0:f71931ae3db1 9 * Software is furnished to do so, subject to the following conditions:
kgills 0:f71931ae3db1 10 *
kgills 0:f71931ae3db1 11 * The above copyright notice and this permission notice shall be included
kgills 0:f71931ae3db1 12 * in all copies or substantial portions of the Software.
kgills 0:f71931ae3db1 13 *
kgills 0:f71931ae3db1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
kgills 0:f71931ae3db1 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kgills 0:f71931ae3db1 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
kgills 0:f71931ae3db1 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
kgills 0:f71931ae3db1 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
kgills 0:f71931ae3db1 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
kgills 0:f71931ae3db1 20 * OTHER DEALINGS IN THE SOFTWARE.
kgills 0:f71931ae3db1 21 *
kgills 0:f71931ae3db1 22 * Except as contained in this notice, the name of Maxim Integrated
kgills 0:f71931ae3db1 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
kgills 0:f71931ae3db1 24 * Products, Inc. Branding Policy.
kgills 0:f71931ae3db1 25 *
kgills 0:f71931ae3db1 26 * The mere transfer of this software does not imply any licenses
kgills 0:f71931ae3db1 27 * of trade secrets, proprietary technology, copyrights, patents,
kgills 0:f71931ae3db1 28 * trademarks, maskwork rights, or any other form of intellectual
kgills 0:f71931ae3db1 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
kgills 0:f71931ae3db1 30 * ownership rights.
kgills 0:f71931ae3db1 31 *******************************************************************************
kgills 0:f71931ae3db1 32 */
kgills 0:f71931ae3db1 33
kgills 0:f71931ae3db1 34 #ifndef __BLE_PRESSURECHAR_H__
kgills 0:f71931ae3db1 35 #define __BLE_PRESSURECHAR_H__
kgills 0:f71931ae3db1 36
kgills 0:f71931ae3db1 37 #include "Characteristic.h"
kgills 0:f71931ae3db1 38 #include "UUID.h"
kgills 0:f71931ae3db1 39
kgills 0:f71931ae3db1 40 /**
kgills 0:f71931ae3db1 41 * @class PressureChar
kgills 0:f71931ae3db1 42 * @brief PressureChar characteristic class.
kgills 0:f71931ae3db1 43 * @details Sub class from the characteristic class.
kgills 0:f71931ae3db1 44 */
kgills 0:f71931ae3db1 45 class PressureChar : public Characteristic
kgills 0:f71931ae3db1 46 {
kgills 0:f71931ae3db1 47 public:
kgills 0:f71931ae3db1 48
kgills 0:f71931ae3db1 49 typedef enum {
kgills 0:f71931ae3db1 50 PRES_UNIT_KPA,
kgills 0:f71931ae3db1 51 PRES_UNIT_ATM,
kgills 0:f71931ae3db1 52 PRES_UNIT_MMHG
kgills 0:f71931ae3db1 53 } pres_unit_t;
kgills 0:f71931ae3db1 54
kgills 0:f71931ae3db1 55 static const unsigned OFFSET_OF_FLAGS = 0;
kgills 0:f71931ae3db1 56 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + 1;
kgills 0:f71931ae3db1 57 static const unsigned OFFSET_OF_TIME = OFFSET_OF_VALUE + 4;
kgills 0:f71931ae3db1 58 static const unsigned CHAR_BYTES = (1 + 4 + TIMESTAMP_BYTES);
kgills 0:f71931ae3db1 59
kgills 0:f71931ae3db1 60 static const unsigned UNIT_FLAG_POS = 0;
kgills 0:f71931ae3db1 61 static const unsigned UNIT_FLAG_MSK = (0x3 << UNIT_FLAG_POS);
kgills 0:f71931ae3db1 62 static const unsigned TIMESTAMP_FLAG_POS = 2;
kgills 0:f71931ae3db1 63 static const unsigned TIMESTAMP_FLAG_MSK = (0x1 << TIMESTAMP_FLAG_POS);
kgills 0:f71931ae3db1 64
kgills 0:f71931ae3db1 65 PressureChar(pres_unit_t unit) :
kgills 0:f71931ae3db1 66 gattChar(charUUID, bytes, CHAR_BYTES, CHAR_BYTES,
kgills 0:f71931ae3db1 67 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
kgills 0:f71931ae3db1 68 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
kgills 0:f71931ae3db1 69 {
kgills 0:f71931ae3db1 70 // Default no timestamp and zero for the value
kgills 0:f71931ae3db1 71 memset(bytes, 0x0, CHAR_BYTES);
kgills 0:f71931ae3db1 72 bytes[OFFSET_OF_FLAGS] = ((unit << UNIT_FLAG_POS) |
kgills 0:f71931ae3db1 73 (false << TIMESTAMP_FLAG_POS));
kgills 0:f71931ae3db1 74 }
kgills 0:f71931ae3db1 75
kgills 0:f71931ae3db1 76 ~PressureChar() {}
kgills 0:f71931ae3db1 77
kgills 0:f71931ae3db1 78 virtual GattCharacteristic *getChar(void)
kgills 0:f71931ae3db1 79 {
kgills 0:f71931ae3db1 80 return &gattChar;
kgills 0:f71931ae3db1 81 }
kgills 0:f71931ae3db1 82
kgills 0:f71931ae3db1 83 virtual uint8_t getNumBytes(void)
kgills 0:f71931ae3db1 84 {
kgills 0:f71931ae3db1 85 return CHAR_BYTES;
kgills 0:f71931ae3db1 86 }
kgills 0:f71931ae3db1 87
kgills 0:f71931ae3db1 88 virtual uint8_t *getBytes(void)
kgills 0:f71931ae3db1 89 {
kgills 0:f71931ae3db1 90 return bytes;
kgills 0:f71931ae3db1 91 }
kgills 0:f71931ae3db1 92
kgills 0:f71931ae3db1 93 // Update the characteristic value
kgills 0:f71931ae3db1 94 void update(float value, time_t time = 0)
kgills 0:f71931ae3db1 95 {
kgills 0:f71931ae3db1 96 // Convert value to bytes
kgills 0:f71931ae3db1 97 pres_unit_t unit = (pres_unit_t)(bytes[UNIT_FLAG_POS] & UNIT_FLAG_MSK);
kgills 0:f71931ae3db1 98 uint32_t value_bytes;
kgills 0:f71931ae3db1 99 if(unit == PRES_UNIT_KPA || unit == PRES_UNIT_MMHG) {
kgills 0:f71931ae3db1 100 value_bytes = float_bytes(value, 2);
kgills 0:f71931ae3db1 101 } else {
kgills 0:f71931ae3db1 102 value_bytes = float_bytes(value, 4);
kgills 0:f71931ae3db1 103 }
kgills 0:f71931ae3db1 104 memcpy(&bytes[OFFSET_OF_VALUE], &value_bytes, sizeof(uint32_t));
kgills 0:f71931ae3db1 105
kgills 0:f71931ae3db1 106 if (time != 0) {
kgills 0:f71931ae3db1 107 // Set timestamp flag
kgills 0:f71931ae3db1 108 bytes[OFFSET_OF_FLAGS] |= TIMESTAMP_FLAG_MSK;
kgills 0:f71931ae3db1 109
kgills 0:f71931ae3db1 110 // Convert time_t
kgills 0:f71931ae3db1 111 date_time(time, &bytes[OFFSET_OF_TIME]);
kgills 0:f71931ae3db1 112 }
kgills 0:f71931ae3db1 113 else {
kgills 0:f71931ae3db1 114 // Clear timestamp flag
kgills 0:f71931ae3db1 115 bytes[OFFSET_OF_FLAGS] &= ~TIMESTAMP_FLAG_MSK;
kgills 0:f71931ae3db1 116 }
kgills 0:f71931ae3db1 117 }
kgills 0:f71931ae3db1 118
kgills 0:f71931ae3db1 119 private:
kgills 0:f71931ae3db1 120 GattCharacteristic gattChar;
kgills 0:f71931ae3db1 121 uint8_t bytes[CHAR_BYTES];
kgills 0:f71931ae3db1 122 static const uint8_t charUUID[];
kgills 0:f71931ae3db1 123 };
kgills 0:f71931ae3db1 124
kgills 0:f71931ae3db1 125 // a8c094be-5b5e-4791-b2db-e9ccd2ff3442
kgills 0:f71931ae3db1 126 const uint8_t PressureChar::charUUID[] = {0x42,0x34,0xff,0xd2,0xcc,0xe9,0xdb,0xb2,0x91,0x47,0x5e,0x5b,0xbe,0x94,0xc0,0xa8};
kgills 0:f71931ae3db1 127
kgills 0:f71931ae3db1 128 #endif /* #ifndef __BLE_PRESSURECHAR_H__*/