Skip to content

Commit

Permalink
Merge pull request #51 from matthias-bs/master
Browse files Browse the repository at this point in the history
Added getLength()
  • Loading branch information
joscha authored Oct 9, 2022
2 parents c46e811 + 06b54bd commit 6ca268e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/LoraEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

LoraEncoder::LoraEncoder(byte *buffer) {
_buffer = buffer;
_offset = 0;
}

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

void LoraEncoder::writeUnixtime(uint32_t unixtime) {
_intToBytes(_buffer, unixtime, 4);
_buffer += 4;
_intToBytes(_buffer + _offset, unixtime, 4);
_offset += 4;
}

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

_intToBytes(_buffer, lat, 4);
_intToBytes(_buffer + 4, lng, 4);
_buffer += 8;
_intToBytes(_buffer + _offset, lat, 4);
_offset += 4;
_intToBytes(_buffer + _offset, lng, 4);
_offset += 4;
}

void LoraEncoder::writeUint32(uint32_t i) {
_intToBytes(_buffer, i, 4);
_buffer += 4;
_intToBytes(_buffer + _offset, i, 4);
_offset += 4;
}

void LoraEncoder::writeUint16(uint16_t i) {
_intToBytes(_buffer, i, 2);
_buffer += 2;
_intToBytes(_buffer + _offset, i, 2);
_offset += 2;
}

void LoraEncoder::writeUint8(uint8_t i) {
_intToBytes(_buffer, i, 1);
_buffer += 1;
_intToBytes(_buffer + _offset, i, 1);
_offset += 1;
}

void LoraEncoder::writeHumidity(float humidity) {
int16_t h = (int16_t) (humidity * 100);
_intToBytes(_buffer, h, 2);
_buffer += 2;
_intToBytes(_buffer + _offset, h, 2);
_offset += 2;
}

/**
Expand All @@ -85,9 +87,9 @@ void LoraEncoder::writeTemperature(float temperature) {
t = ~-t;
t = t + 1;
}
_buffer[0] = (byte) ((t >> 8) & 0xFF);
_buffer[1] = (byte) t & 0xFF;
_buffer += 2;
_buffer[_offset ] = (byte) ((t >> 8) & 0xFF);
_buffer[_offset+1] = (byte) t & 0xFF;
_offset += 2;
}

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

void LoraEncoder::writeRawFloat(float value) {
uint32_t asbytes=*(reinterpret_cast<uint32_t*>(&value));
_intToBytes(_buffer, asbytes, 4);
_buffer += 4;
_intToBytes(_buffer + _offset, asbytes, 4);
_offset += 4;
}

int LoraEncoder::getLength(void) {
return _offset;
}
2 changes: 2 additions & 0 deletions src/LoraEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class LoraEncoder {
void writeHumidity(float humidity);
void writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h);
void writeRawFloat(float value);
int getLength(void);
private:
byte* _buffer;
int _offset;
void _intToBytes(byte *buf, int32_t i, uint8_t byteSize);
};

Expand Down

0 comments on commit 6ca268e

Please sign in to comment.