-
Notifications
You must be signed in to change notification settings - Fork 4
/
LoraMessage.cpp
78 lines (64 loc) · 1.73 KB
/
LoraMessage.cpp
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
#if ARDUINO >= 100
#include "Arduino.h"
#endif
#include <stdlib.h>
#include "LoraMessage.h"
#include "LoraEncoder.h"
LoraMessage::LoraMessage() {
_currentSize = 0;
_buffer = (byte*) malloc(_currentSize);
}
LoraMessage::~LoraMessage() {
free(_buffer);
}
LoraEncoder LoraMessage::_reallocBuffer(int delta) {
void* temp = realloc(_buffer, (_currentSize + delta) * sizeof(byte));
if (temp == NULL) {
free(_buffer);
// printf("bad memory allocation!");
while(true);
} else {
_buffer = (byte*) temp;
}
LoraEncoder encoder(_buffer + _currentSize);
_currentSize += delta;
return encoder;
}
LoraMessage& LoraMessage::addUnixtime(uint32_t unixtime) {
_reallocBuffer(4).writeUnixtime(unixtime);
return *this;
}
LoraMessage& LoraMessage::addFloat(uint32_t i) {
_reallocBuffer(4).writeFloat(i);
return *this;
}
LoraMessage& LoraMessage::addUint16(uint16_t i) {
_reallocBuffer(2).writeUint16(i);
return *this;
}
LoraMessage& LoraMessage::addTemperature(float temperature) {
_reallocBuffer(2).writeTemperature(temperature);
return *this;
}
LoraMessage& LoraMessage::addUint8(uint8_t i) {
_reallocBuffer(1).writeUint8(i);
return *this;
}
LoraMessage& LoraMessage::addUint32(uint32_t i) {
_reallocBuffer(4).writeUint32(i);
return *this;
}
LoraMessage& LoraMessage::addHumidity(float humidity) {
_reallocBuffer(2).writeHumidity(humidity);
return *this;
}
LoraMessage& LoraMessage::addBitmap(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h) {
_reallocBuffer(1).writeBitmap(a, b, c, d, e, f, g, h);
return *this;
}
int LoraMessage::getLength() {
return _currentSize;
}
byte* LoraMessage::getBytes() {
return _buffer;
}