-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute.cpp
More file actions
157 lines (138 loc) · 2.56 KB
/
Copy pathAttribute.cpp
File metadata and controls
157 lines (138 loc) · 2.56 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Author: Sebastian Wolf
* Created: August 2018
*/
#include "Attribute.h"
Attribute::Attribute(int ID)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
}
Attribute::Attribute(int ID, byte *DATA, int sizeData)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
this->DATA = DATA;
this->sizeDATA = sizeData;
}
Attribute::Attribute(int ID, int DATA)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
this->DATA = new byte[2];
this->DATA[0] = (byte)DATA;
this->DATA[1] = (byte)(DATA >> 8);
sizeDATA = 2;
}
Attribute::Attribute(int ID, String DATA)
{
this->ID = new byte[2];
this->ID[0] = (byte)ID;
this->ID[1] = (byte)(ID >> 8);
this->DATA = new byte[DATA.length()]();
DATA.getBytes(this->DATA, DATA.length() + 1);
sizeDATA = DATA.length();
}
Attribute::Attribute(byte *ID, byte *DATA, int sizeData)
{
this->ID = ID;
this->DATA = DATA;
this->sizeDATA = sizeData;
}
Attribute::~Attribute()
{
if (ID != NULL)
{
delete ID;
}
if (DATA != NULL)
{
delete DATA;
}
}
byte *Attribute::get()
{
PACKET_SIZE = sizeID + sizeDATA;
byte *result = new byte[4 + PACKET_SIZE]();
result[0] = (byte)(PACKET_SIZE & 0xFF);
result[1] = (byte)((PACKET_SIZE >> 8) & 0xFF);
result[2] = (byte)((PACKET_SIZE >> 16) & 0xFF);
result[3] = (byte)((PACKET_SIZE >> 24) & 0xFF);
result[4] = ID[0];
result[5] = ID[1];
for (int i = 0; i < sizeDATA; i++)
{
result[i + 6] = DATA[i];
}
Serial.println((char *)result);
return result;
}
int Attribute::getSize()
{
PACKET_SIZE = sizeID + sizeDATA;
return (4 + PACKET_SIZE);
}
int Attribute::getDATASize()
{
return sizeDATA;
}
byte *Attribute::getID()
{
return ID;
}
int Attribute::getIDAsInt()
{
return (ID[0] & 0xFF) | ((ID[1] & 0xFF) << 8);
}
byte *Attribute::getDATA()
{
return DATA;
}
String Attribute::getDATAAsString()
{
String tmp = (char *)DATA;
return tmp.substring(0, sizeDATA);
}
int Attribute::getDATAAsInt()
{
switch (sizeDATA)
{
case 1:
return (DATA[0] & 0xFF);
break;
case 2:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8);
break;
case 3:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8 | (DATA[2] & 0xFF) << 16);
break;
case 4:
return ((DATA[0] & 0xFF) | (DATA[1] & 0xFF) << 8 | (DATA[2] & 0xFF) << 16 | (DATA[3] & 0xFF) << 24);
}
}
float Attribute::getDATAAsFloat()
{
union {
byte b[4];
float f;
} tmp;
for (int i = 0; i < sizeDATA; i++)
{
tmp.b[i] = DATA[i];
}
return tmp.f;
}
boolean Attribute::getDATAAsBoolean()
{
if (getDATAAsInt() == 0)
{
return false;
}
else
{
return true;
}
}