BTLE demo for MAXWSNENV.

Dependencies:   BLE_API BMP180 Si7020 mbed MaximBLE

Committer:
kgills
Date:
Fri Jul 10 21:28:56 2015 +0000
Revision:
0:f71931ae3db1
Adding MAXWSNENV_demo.

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_TEMPERATURECHAR_H__
kgills 0:f71931ae3db1 35 #define __BLE_TEMPERATURECHAR_H__
kgills 0:f71931ae3db1 36
kgills 0:f71931ae3db1 37 #include "Characteristic.h"
kgills 0:f71931ae3db1 38
kgills 0:f71931ae3db1 39 /**
kgills 0:f71931ae3db1 40 * @class TemperatureChar
kgills 0:f71931ae3db1 41 * @brief TemperatureChar characteristic class.
kgills 0:f71931ae3db1 42 * @details Sub class from the characteristic class.
kgills 0:f71931ae3db1 43 */
kgills 0:f71931ae3db1 44
kgills 0:f71931ae3db1 45 class TemperatureChar : 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 TEMP_UNIT_C,
kgills 0:f71931ae3db1 51 TEMP_UNIT_F
kgills 0:f71931ae3db1 52 } temp_unit_t;
kgills 0:f71931ae3db1 53
kgills 0:f71931ae3db1 54 static const unsigned OFFSET_OF_FLAGS = 0;
kgills 0:f71931ae3db1 55 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + 1;
kgills 0:f71931ae3db1 56 static const unsigned OFFSET_OF_TIME = OFFSET_OF_VALUE + 4;
kgills 0:f71931ae3db1 57 static const unsigned CHAR_BYTES = (1 + 4 + TIMESTAMP_BYTES);
kgills 0:f71931ae3db1 58
kgills 0:f71931ae3db1 59 static const unsigned UNIT_FLAG_POS = 0;
kgills 0:f71931ae3db1 60 static const unsigned UNIT_FLAG_MSK = (0x1 << UNIT_FLAG_POS);
kgills 0:f71931ae3db1 61 static const unsigned TIMESTAMP_FLAG_POS = 1;
kgills 0:f71931ae3db1 62 static const unsigned TIMESTAMP_FLAG_MSK = (0x1 << TIMESTAMP_FLAG_POS);
kgills 0:f71931ae3db1 63
kgills 0:f71931ae3db1 64 TemperatureChar(temp_unit_t unit) :
kgills 0:f71931ae3db1 65 gattChar(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, bytes,CHAR_BYTES, CHAR_BYTES,
kgills 0:f71931ae3db1 66 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
kgills 0:f71931ae3db1 67 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
kgills 0:f71931ae3db1 68 {
kgills 0:f71931ae3db1 69 // Default no timestamp and zero for the value
kgills 0:f71931ae3db1 70 memset(bytes, 0x0, CHAR_BYTES);
kgills 0:f71931ae3db1 71 bytes[OFFSET_OF_FLAGS] = ((false << TIMESTAMP_FLAG_POS) |
kgills 0:f71931ae3db1 72 (unit << UNIT_FLAG_POS));
kgills 0:f71931ae3db1 73 }
kgills 0:f71931ae3db1 74
kgills 0:f71931ae3db1 75 ~TemperatureChar() {}
kgills 0:f71931ae3db1 76
kgills 0:f71931ae3db1 77 virtual GattCharacteristic *getChar(void)
kgills 0:f71931ae3db1 78 {
kgills 0:f71931ae3db1 79 return &gattChar;
kgills 0:f71931ae3db1 80 }
kgills 0:f71931ae3db1 81
kgills 0:f71931ae3db1 82 virtual uint8_t getNumBytes(void)
kgills 0:f71931ae3db1 83 {
kgills 0:f71931ae3db1 84 return CHAR_BYTES;
kgills 0:f71931ae3db1 85 }
kgills 0:f71931ae3db1 86
kgills 0:f71931ae3db1 87 virtual uint8_t *getBytes(void)
kgills 0:f71931ae3db1 88 {
kgills 0:f71931ae3db1 89 return bytes;
kgills 0:f71931ae3db1 90 }
kgills 0:f71931ae3db1 91
kgills 0:f71931ae3db1 92 // Update the characteristic value
kgills 0:f71931ae3db1 93 void update(float value, time_t time = 0)
kgills 0:f71931ae3db1 94 {
kgills 0:f71931ae3db1 95 // Convert value to bytes
kgills 0:f71931ae3db1 96 uint32_t value_bytes = float_bytes(value, 2);
kgills 0:f71931ae3db1 97 memcpy(&bytes[OFFSET_OF_VALUE], &value_bytes, sizeof(uint32_t));
kgills 0:f71931ae3db1 98
kgills 0:f71931ae3db1 99 if (time != 0) {
kgills 0:f71931ae3db1 100 // Set timestamp flag
kgills 0:f71931ae3db1 101 bytes[OFFSET_OF_FLAGS] |= TIMESTAMP_FLAG_MSK;
kgills 0:f71931ae3db1 102
kgills 0:f71931ae3db1 103 // Convert time_t
kgills 0:f71931ae3db1 104 date_time(time, &bytes[OFFSET_OF_TIME]);
kgills 0:f71931ae3db1 105 }
kgills 0:f71931ae3db1 106 else {
kgills 0:f71931ae3db1 107 // Clear timestamp flag
kgills 0:f71931ae3db1 108 bytes[OFFSET_OF_FLAGS] &= ~TIMESTAMP_FLAG_MSK;
kgills 0:f71931ae3db1 109 }
kgills 0:f71931ae3db1 110 }
kgills 0:f71931ae3db1 111
kgills 0:f71931ae3db1 112 private:
kgills 0:f71931ae3db1 113 GattCharacteristic gattChar;
kgills 0:f71931ae3db1 114 uint8_t bytes[CHAR_BYTES];
kgills 0:f71931ae3db1 115 };
kgills 0:f71931ae3db1 116
kgills 0:f71931ae3db1 117 #endif /* #ifndef __BLE_TEMPERATURECHAR_H__*/