forked from arduino-libraries/ArduinoBLE
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBLETypedCharacteristic.h
More file actions
102 lines (78 loc) · 2.65 KB
/
Copy pathBLETypedCharacteristic.h
File metadata and controls
102 lines (78 loc) · 2.65 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _BLE_TYPED_CHARACTERISTIC_H_
#define _BLE_TYPED_CHARACTERISTIC_H_
#include "BLECharacteristic.h"
template<typename T> class BLETypedCharacteristic : public BLECharacteristic
{
public:
BLETypedCharacteristic(const char* uuid, unsigned int permissions);
int writeValue(T value);
int setValue(T value) { return writeValue(value); }
T value(void);
int writeValueLE(T value);
int setValueLE(T value) { return writeValueLE(value); }
T valueLE(void);
int writeValueBE(T value);
int setValueBE(T value) { return writeValueBE(value); }
T valueBE(void);
private:
T byteSwap(T value);
};
template<typename T> BLETypedCharacteristic<T>::BLETypedCharacteristic(const char* uuid, unsigned int permissions) :
BLECharacteristic(uuid, permissions, sizeof(T), true)
{
T value;
memset(&value, 0x00, sizeof(value));
writeValue(value);
}
template<typename T> int BLETypedCharacteristic<T>::writeValue(T value)
{
return BLECharacteristic::writeValue((uint8_t*)&value, sizeof(T));
}
template<typename T> T BLETypedCharacteristic<T>::value()
{
T value;
memcpy(&value, (unsigned char*)BLECharacteristic::value(), BLECharacteristic::valueSize());
return value;
}
template<typename T> int BLETypedCharacteristic<T>::writeValueLE(T value)
{
return writeValue(value);
}
template<typename T> T BLETypedCharacteristic<T>::valueLE()
{
return value();
}
template<typename T> int BLETypedCharacteristic<T>::writeValueBE(T value)
{
return writeValue(byteSwap(value));
}
template<typename T> T BLETypedCharacteristic<T>::valueBE()
{
return byteSwap(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