Skip to content

Commit 6ca268e

Browse files
authored
Merge pull request #51 from matthias-bs/master
Added getLength()
2 parents c46e811 + 06b54bd commit 6ca268e

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/LoraEncoder.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
LoraEncoder::LoraEncoder(byte *buffer) {
3434
_buffer = buffer;
35+
_offset = 0;
3536
}
3637

3738
void LoraEncoder::_intToBytes(byte *buf, int32_t i, uint8_t byteSize) {
@@ -41,38 +42,39 @@ void LoraEncoder::_intToBytes(byte *buf, int32_t i, uint8_t byteSize) {
4142
}
4243

4344
void LoraEncoder::writeUnixtime(uint32_t unixtime) {
44-
_intToBytes(_buffer, unixtime, 4);
45-
_buffer += 4;
45+
_intToBytes(_buffer + _offset, unixtime, 4);
46+
_offset += 4;
4647
}
4748

4849
void LoraEncoder::writeLatLng(double latitude, double longitude) {
4950
int32_t lat = latitude * 1e6;
5051
int32_t lng = longitude * 1e6;
5152

52-
_intToBytes(_buffer, lat, 4);
53-
_intToBytes(_buffer + 4, lng, 4);
54-
_buffer += 8;
53+
_intToBytes(_buffer + _offset, lat, 4);
54+
_offset += 4;
55+
_intToBytes(_buffer + _offset, lng, 4);
56+
_offset += 4;
5557
}
5658

5759
void LoraEncoder::writeUint32(uint32_t i) {
58-
_intToBytes(_buffer, i, 4);
59-
_buffer += 4;
60+
_intToBytes(_buffer + _offset, i, 4);
61+
_offset += 4;
6062
}
6163

6264
void LoraEncoder::writeUint16(uint16_t i) {
63-
_intToBytes(_buffer, i, 2);
64-
_buffer += 2;
65+
_intToBytes(_buffer + _offset, i, 2);
66+
_offset += 2;
6567
}
6668

6769
void LoraEncoder::writeUint8(uint8_t i) {
68-
_intToBytes(_buffer, i, 1);
69-
_buffer += 1;
70+
_intToBytes(_buffer + _offset, i, 1);
71+
_offset += 1;
7072
}
7173

7274
void LoraEncoder::writeHumidity(float humidity) {
7375
int16_t h = (int16_t) (humidity * 100);
74-
_intToBytes(_buffer, h, 2);
75-
_buffer += 2;
76+
_intToBytes(_buffer + _offset, h, 2);
77+
_offset += 2;
7678
}
7779

7880
/**
@@ -85,9 +87,9 @@ void LoraEncoder::writeTemperature(float temperature) {
8587
t = ~-t;
8688
t = t + 1;
8789
}
88-
_buffer[0] = (byte) ((t >> 8) & 0xFF);
89-
_buffer[1] = (byte) t & 0xFF;
90-
_buffer += 2;
90+
_buffer[_offset ] = (byte) ((t >> 8) & 0xFF);
91+
_buffer[_offset+1] = (byte) t & 0xFF;
92+
_offset += 2;
9193
}
9294

9395
void LoraEncoder::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h) {
@@ -106,6 +108,10 @@ void LoraEncoder::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, bo
106108

107109
void LoraEncoder::writeRawFloat(float value) {
108110
uint32_t asbytes=*(reinterpret_cast<uint32_t*>(&value));
109-
_intToBytes(_buffer, asbytes, 4);
110-
_buffer += 4;
111+
_intToBytes(_buffer + _offset, asbytes, 4);
112+
_offset += 4;
113+
}
114+
115+
int LoraEncoder::getLength(void) {
116+
return _offset;
111117
}

src/LoraEncoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ class LoraEncoder {
4747
void writeHumidity(float humidity);
4848
void writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h);
4949
void writeRawFloat(float value);
50+
int getLength(void);
5051
private:
5152
byte* _buffer;
53+
int _offset;
5254
void _intToBytes(byte *buf, int32_t i, uint8_t byteSize);
5355
};
5456

0 commit comments

Comments
 (0)