BTLE demo for MAXWSNENV.
Dependencies: BLE_API BMP180 Si7020 mbed MaximBLE
Diff: TemperatureChar.h
- Revision:
- 0:f71931ae3db1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TemperatureChar.h Fri Jul 10 21:28:56 2015 +0000 @@ -0,0 +1,117 @@ +/******************************************************************************* + * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#ifndef __BLE_TEMPERATURECHAR_H__ +#define __BLE_TEMPERATURECHAR_H__ + +#include "Characteristic.h" + +/** + * @class TemperatureChar + * @brief TemperatureChar characteristic class. + * @details Sub class from the characteristic class. + */ + +class TemperatureChar : public Characteristic +{ +public: + + typedef enum { + TEMP_UNIT_C, + TEMP_UNIT_F + } temp_unit_t; + + static const unsigned OFFSET_OF_FLAGS = 0; + static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + 1; + static const unsigned OFFSET_OF_TIME = OFFSET_OF_VALUE + 4; + static const unsigned CHAR_BYTES = (1 + 4 + TIMESTAMP_BYTES); + + static const unsigned UNIT_FLAG_POS = 0; + static const unsigned UNIT_FLAG_MSK = (0x1 << UNIT_FLAG_POS); + static const unsigned TIMESTAMP_FLAG_POS = 1; + static const unsigned TIMESTAMP_FLAG_MSK = (0x1 << TIMESTAMP_FLAG_POS); + + TemperatureChar(temp_unit_t unit) : + gattChar(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, bytes,CHAR_BYTES, CHAR_BYTES, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) + { + // Default no timestamp and zero for the value + memset(bytes, 0x0, CHAR_BYTES); + bytes[OFFSET_OF_FLAGS] = ((false << TIMESTAMP_FLAG_POS) | + (unit << UNIT_FLAG_POS)); + } + + ~TemperatureChar() {} + + virtual GattCharacteristic *getChar(void) + { + return &gattChar; + } + + virtual uint8_t getNumBytes(void) + { + return CHAR_BYTES; + } + + virtual uint8_t *getBytes(void) + { + return bytes; + } + + // Update the characteristic value + void update(float value, time_t time = 0) + { + // Convert value to bytes + uint32_t value_bytes = float_bytes(value, 2); + memcpy(&bytes[OFFSET_OF_VALUE], &value_bytes, sizeof(uint32_t)); + + if (time != 0) { + // Set timestamp flag + bytes[OFFSET_OF_FLAGS] |= TIMESTAMP_FLAG_MSK; + + // Convert time_t + date_time(time, &bytes[OFFSET_OF_TIME]); + } + else { + // Clear timestamp flag + bytes[OFFSET_OF_FLAGS] &= ~TIMESTAMP_FLAG_MSK; + } + } + +private: + GattCharacteristic gattChar; + uint8_t bytes[CHAR_BYTES]; +}; + +#endif /* #ifndef __BLE_TEMPERATURECHAR_H__*/