added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/BLETypedCharacteristic.h@0:075ea2812998, 2014-10-17 (annotated)
- Committer:
- RedBearLab
- Date:
- Fri Oct 17 22:40:32 2014 +0800
- Revision:
- 0:075ea2812998
BLE_nRF8001 library first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | #ifndef _BLE_TYPED_CHARACTERISTIC_H_ |
RedBearLab | 0:075ea2812998 | 2 | #define _BLE_TYPED_CHARACTERISTIC_H_ |
RedBearLab | 0:075ea2812998 | 3 | |
RedBearLab | 0:075ea2812998 | 4 | #include "Arduino.h" |
RedBearLab | 0:075ea2812998 | 5 | |
RedBearLab | 0:075ea2812998 | 6 | #include "BLECharacteristic.h" |
RedBearLab | 0:075ea2812998 | 7 | |
RedBearLab | 0:075ea2812998 | 8 | template<typename T> class BLETypedCharacteristic : public BLECharacteristic |
RedBearLab | 0:075ea2812998 | 9 | { |
RedBearLab | 0:075ea2812998 | 10 | public: |
RedBearLab | 0:075ea2812998 | 11 | BLETypedCharacteristic(const char* uuid, unsigned char properties); |
RedBearLab | 0:075ea2812998 | 12 | |
RedBearLab | 0:075ea2812998 | 13 | bool setValue(T value); |
RedBearLab | 0:075ea2812998 | 14 | T value(); |
RedBearLab | 0:075ea2812998 | 15 | |
RedBearLab | 0:075ea2812998 | 16 | bool setValueLE(T value); |
RedBearLab | 0:075ea2812998 | 17 | T valueLE(); |
RedBearLab | 0:075ea2812998 | 18 | |
RedBearLab | 0:075ea2812998 | 19 | bool setValueBE(T value); |
RedBearLab | 0:075ea2812998 | 20 | T valueBE(); |
RedBearLab | 0:075ea2812998 | 21 | |
RedBearLab | 0:075ea2812998 | 22 | private: |
RedBearLab | 0:075ea2812998 | 23 | T byteSwap(T value); |
RedBearLab | 0:075ea2812998 | 24 | }; |
RedBearLab | 0:075ea2812998 | 25 | |
RedBearLab | 0:075ea2812998 | 26 | template<typename T> BLETypedCharacteristic<T>::BLETypedCharacteristic(const char* uuid, unsigned char properties) : |
RedBearLab | 0:075ea2812998 | 27 | BLECharacteristic(uuid, properties, sizeof(T), true) |
RedBearLab | 0:075ea2812998 | 28 | { |
RedBearLab | 0:075ea2812998 | 29 | T value; |
RedBearLab | 0:075ea2812998 | 30 | memset(&value, 0x00, sizeof(value)); |
RedBearLab | 0:075ea2812998 | 31 | |
RedBearLab | 0:075ea2812998 | 32 | this->setValue(value); |
RedBearLab | 0:075ea2812998 | 33 | } |
RedBearLab | 0:075ea2812998 | 34 | |
RedBearLab | 0:075ea2812998 | 35 | template<typename T> bool BLETypedCharacteristic<T>::setValue(T value) { |
RedBearLab | 0:075ea2812998 | 36 | return this->BLECharacteristic::setValue((unsigned char*)&value, sizeof(T)); |
RedBearLab | 0:075ea2812998 | 37 | } |
RedBearLab | 0:075ea2812998 | 38 | |
RedBearLab | 0:075ea2812998 | 39 | template<typename T> T BLETypedCharacteristic<T>::value() { |
RedBearLab | 0:075ea2812998 | 40 | T value; |
RedBearLab | 0:075ea2812998 | 41 | |
RedBearLab | 0:075ea2812998 | 42 | memcpy(&value, (unsigned char*)this->BLECharacteristic::value(), this->BLECharacteristic::valueSize()); |
RedBearLab | 0:075ea2812998 | 43 | |
RedBearLab | 0:075ea2812998 | 44 | return value; |
RedBearLab | 0:075ea2812998 | 45 | } |
RedBearLab | 0:075ea2812998 | 46 | |
RedBearLab | 0:075ea2812998 | 47 | template<typename T> bool BLETypedCharacteristic<T>::setValueLE(T value) { |
RedBearLab | 0:075ea2812998 | 48 | return this->setValue(value); |
RedBearLab | 0:075ea2812998 | 49 | } |
RedBearLab | 0:075ea2812998 | 50 | |
RedBearLab | 0:075ea2812998 | 51 | template<typename T> T BLETypedCharacteristic<T>::valueLE() { |
RedBearLab | 0:075ea2812998 | 52 | return this->getValue(); |
RedBearLab | 0:075ea2812998 | 53 | } |
RedBearLab | 0:075ea2812998 | 54 | |
RedBearLab | 0:075ea2812998 | 55 | template<typename T> bool BLETypedCharacteristic<T>::setValueBE(T value) { |
RedBearLab | 0:075ea2812998 | 56 | return this->setValue(this->byteSwap(value)); |
RedBearLab | 0:075ea2812998 | 57 | } |
RedBearLab | 0:075ea2812998 | 58 | |
RedBearLab | 0:075ea2812998 | 59 | template<typename T> T BLETypedCharacteristic<T>::valueBE() { |
RedBearLab | 0:075ea2812998 | 60 | return this->byteSwap(this->value()); |
RedBearLab | 0:075ea2812998 | 61 | } |
RedBearLab | 0:075ea2812998 | 62 | |
RedBearLab | 0:075ea2812998 | 63 | template<typename T> T BLETypedCharacteristic<T>::byteSwap(T value) { |
RedBearLab | 0:075ea2812998 | 64 | T result; |
RedBearLab | 0:075ea2812998 | 65 | unsigned char* src = (unsigned char*)&value; |
RedBearLab | 0:075ea2812998 | 66 | unsigned char* dst = (unsigned char*)&result; |
RedBearLab | 0:075ea2812998 | 67 | |
RedBearLab | 0:075ea2812998 | 68 | for (int i = 0; i < sizeof(T); i++) { |
RedBearLab | 0:075ea2812998 | 69 | dst[i] = src[sizeof(T) - i - 1]; |
RedBearLab | 0:075ea2812998 | 70 | } |
RedBearLab | 0:075ea2812998 | 71 | |
RedBearLab | 0:075ea2812998 | 72 | return result; |
RedBearLab | 0:075ea2812998 | 73 | } |
RedBearLab | 0:075ea2812998 | 74 | |
RedBearLab | 0:075ea2812998 | 75 | #endif |