added debugging

Fork of BLE_nRF8001 by RedBearLab

BLE_nRF8001/BLETypedCharacteristic.h

Committer:
jn80842
Date:
2014-11-10
Revision:
2:7805a5595aab
Parent:
0:075ea2812998

File content as of revision 2:7805a5595aab:

#ifndef _BLE_TYPED_CHARACTERISTIC_H_
#define _BLE_TYPED_CHARACTERISTIC_H_

#include "Arduino.h"

#include "BLECharacteristic.h"

template<typename T> class BLETypedCharacteristic : public BLECharacteristic
{
  public:
    BLETypedCharacteristic(const char* uuid, unsigned char properties);

    bool setValue(T value);
    T value();

    bool setValueLE(T value);
    T valueLE();

    bool setValueBE(T value);
    T valueBE();

  private:
    T byteSwap(T value);
};

template<typename T> BLETypedCharacteristic<T>::BLETypedCharacteristic(const char* uuid, unsigned char properties) :
  BLECharacteristic(uuid, properties, sizeof(T), true)
{
  T value;
  memset(&value, 0x00, sizeof(value));

  this->setValue(value);
}

template<typename T> bool BLETypedCharacteristic<T>::setValue(T value) {
  return this->BLECharacteristic::setValue((unsigned char*)&value, sizeof(T));
}

template<typename T> T BLETypedCharacteristic<T>::value() {
  T value;

  memcpy(&value, (unsigned char*)this->BLECharacteristic::value(), this->BLECharacteristic::valueSize());

  return value;
}

template<typename T> bool BLETypedCharacteristic<T>::setValueLE(T value) {
  return this->setValue(value);
}

template<typename T> T BLETypedCharacteristic<T>::valueLE() {
  return this->getValue();
}

template<typename T> bool BLETypedCharacteristic<T>::setValueBE(T value) {
  return this->setValue(this->byteSwap(value));
}

template<typename T> T BLETypedCharacteristic<T>::valueBE() {
  return this->byteSwap(this->value());
}

template<typename T> T BLETypedCharacteristic<T>::byteSwap(T value) {
  T result;
  unsigned char* src = (unsigned char*)&value;
  unsigned char* dst = (unsigned char*)&result;

  for (int i = 0; i < sizeof(T); i++) {
    dst[i] = src[sizeof(T) - i - 1];
  }

  return result;
}

#endif