forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLETypedCharacteristic.h
75 lines (54 loc) · 1.78 KB
/
BLETypedCharacteristic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef _BLE_TYPED_CHARACTERISTIC_H_
#define _BLE_TYPED_CHARACTERISTIC_H_
#include "Arduino.h"
#include "BLEFixedLengthCharacteristic.h"
template<typename T> class BLETypedCharacteristic : public BLEFixedLengthCharacteristic
{
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) :
BLEFixedLengthCharacteristic(uuid, properties, sizeof(T))
{
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