Merge pull request #405 from TD-er/development

Fix/cleanup of packed decoder code
This commit is contained in:
Verkehrsrot 2019-07-28 18:43:07 +02:00 committed by GitHub
commit dcc7784456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 58 deletions

View File

@ -67,9 +67,10 @@ private:
private: private:
uint8_t *buffer; uint8_t *buffer;
uint8_t cursor; uint8_t cursor;
void intToBytes(uint8_t pos, int32_t i, uint8_t byteSize); void uintToBytes(uint64_t i, uint8_t byteSize);
void writeUptime(uint64_t unixtime); void writeUptime(uint64_t unixtime);
void writeLatLng(double latitude, double longitude); void writeLatLng(double latitude, double longitude);
void writeUint64(uint64_t i);
void writeUint32(uint32_t i); void writeUint32(uint32_t i);
void writeUint16(uint16_t i); void writeUint16(uint16_t i);
void writeUint8(uint8_t i); void writeUint8(uint8_t i);

View File

@ -37,7 +37,7 @@ function Decoder(bytes, port) {
if (port === 3) { if (port === 3) {
// device config data // device config data
return decode(bytes, [uint8, uint8, uint16, uint8, uint8, uint8, uint8, bitmap1, bitmap2, version], ['lorasf', 'txpower', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags', 'payloadmask', 'version']); return decode(bytes, [uint8, uint8, int16, uint8, uint8, uint8, uint8, bitmap1, bitmap2, version], ['lorasf', 'txpower', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags', 'payloadmask', 'version']);
} }
if (port === 4) { if (port === 4) {
@ -52,7 +52,7 @@ function Decoder(bytes, port) {
if (port === 6) { if (port === 6) {
// beacon proximity alarm // beacon proximity alarm
return decode(bytes, [uint8, uint8], ['rssi', 'beacon']); return decode(bytes, [int8, uint8], ['rssi', 'beacon']);
} }
if (port === 7) { if (port === 7) {
@ -123,37 +123,72 @@ var uint32 = function (bytes) {
}; };
uint32.BYTES = 4; uint32.BYTES = 4;
var latLng = function (bytes) { var uint64 = function (bytes) {
if (bytes.length !== latLng.BYTES) { if (bytes.length !== uint64.BYTES) {
throw new Error('Lat/Long must have exactly 4 bytes'); throw new Error('uint64 must have exactly 8 bytes');
}
return bytesToInt(bytes) / 1e6;
};
latLng.BYTES = 4;
var uptime = function (bytes) {
if (bytes.length !== uptime.BYTES) {
throw new Error('Uptime must have exactly 8 bytes');
} }
return bytesToInt(bytes); return bytesToInt(bytes);
}; };
uptime.BYTES = 8; uint64.BYTES = 8;
var int8 = function (bytes) {
if (bytes.length !== int8.BYTES) {
throw new Error('int8 must have exactly 1 byte');
}
var value = +(bytesToInt(bytes));
if (value > 127) {
value -= 256;
}
return value;
};
int8.BYTES = 1;
var int16 = function (bytes) {
if (bytes.length !== int16.BYTES) {
throw new Error('int16 must have exactly 2 bytes');
}
var value = +(bytesToInt(bytes));
if (value > 32767) {
value -= 65536;
}
return value;
};
int16.BYTES = 2;
var int32 = function (bytes) {
if (bytes.length !== int32.BYTES) {
throw new Error('int32 must have exactly 4 bytes');
}
var value = +(bytesToInt(bytes));
if (value > 2147483647) {
value -= 4294967296;
}
return value;
};
int32.BYTES = 4;
var latLng = function (bytes) {
return +(int32(bytes) / 1e6).toFixed(6);
};
latLng.BYTES = int32.BYTES;
var uptime = function (bytes) {
return uint64(bytes);
};
uptime.BYTES = uint64.BYTES;
var hdop = function (bytes) { var hdop = function (bytes) {
if (bytes.length !== hdop.BYTES) { return +(uint16(bytes) / 100).toFixed(2);
throw new Error('hdop must have exactly 2 bytes');
}
return +(bytesToInt(bytes) / 100).toFixed(1);
}; };
hdop.BYTES = 2; hdop.BYTES = uint16.BYTES;
var altitude = function (bytes) { var altitude = function (bytes) {
if (bytes.length !== altitude.BYTES) { // Option to increase altitude resolution (also on encoder side)
throw new Error('Altitude must have exactly 2 bytes'); // return +(int16(bytes) / 4 - 1000).toFixed(1);
} return +(int16(bytes));
return +(alt / 4 - 1000).toFixed(1);
}; };
altitude.BYTES = 2; altitude.BYTES = int16.BYTES;
var float = function (bytes) { var float = function (bytes) {
if (bytes.length !== float.BYTES) { if (bytes.length !== float.BYTES) {
@ -176,29 +211,19 @@ var float = function (bytes) {
if (isNegative) { if (isNegative) {
t = -t; t = -t;
} }
return +(t / 100).toFixed(1); return +(t / 100).toFixed(2);
}; };
float.BYTES = 2; float.BYTES = 2;
var ufloat = function (bytes) { var ufloat = function (bytes) {
if (bytes.length !== ufloat.BYTES) { return +(uint16(bytes) / 100).toFixed(2);
throw new Error('Ufloat must have exactly 2 bytes');
}
var h = bytesToInt(bytes);
return +(h / 100).toFixed(1);
}; };
ufloat.BYTES = 2; ufloat.BYTES = uint16.BYTES;
var pressure = function (bytes) { var pressure = function (bytes) {
if (bytes.length !== pressure.BYTES) { return +(uint16(bytes) / 10).toFixed(1);
throw new Error('Pressure must have exactly 2 bytes');
}
var h = bytesToInt(bytes);
return +(h / 10).toFixed(1);
}; };
pressure.BYTES = 2; pressure.BYTES = uint16.BYTES;
var bitmap1 = function (byte) { var bitmap1 = function (byte) {
if (byte.length !== bitmap1.BYTES) { if (byte.length !== bitmap1.BYTES) {
@ -255,6 +280,9 @@ if (typeof module === 'object' && typeof module.exports !== 'undefined') {
uint8: uint8, uint8: uint8,
uint16: uint16, uint16: uint16,
uint32: uint32, uint32: uint32,
int8: int8,
int16: int16,
int32: int32,
uptime: uptime, uptime: uptime,
float: float, float: float,
ufloat: ufloat, ufloat: ufloat,

View File

@ -20,7 +20,7 @@ function Decoder(bytes, port) {
decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]); decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.sats = bytes[i++]; decoded.sats = bytes[i++];
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]); decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++])) / 4 - 1000; decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
} }
} }
@ -41,7 +41,7 @@ function Decoder(bytes, port) {
decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]); decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.sats = bytes[i++]; decoded.sats = bytes[i++];
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]); decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++])) / 4 - 1000; decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
} }
if (port === 5) { if (port === 5) {

View File

@ -92,8 +92,8 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
buffer[cursor++] = value.satellites; buffer[cursor++] = value.satellites;
buffer[cursor++] = highByte(value.hdop); buffer[cursor++] = highByte(value.hdop);
buffer[cursor++] = lowByte(value.hdop); buffer[cursor++] = lowByte(value.hdop);
buffer[cursor++] = highByte((value.altitude + 1000) * 4); buffer[cursor++] = highByte(value.altitude);
buffer[cursor++] = lowByte((value.altitude + 1000) * 4); buffer[cursor++] = lowByte(value.altitude);
#endif #endif
} }
@ -195,7 +195,7 @@ void PayloadConvert::addGPS(gpsStatus_t value) {
writeLatLng(value.latitude, value.longitude); writeLatLng(value.latitude, value.longitude);
writeUint8(value.satellites); writeUint8(value.satellites);
writeUint16(value.hdop); writeUint16(value.hdop);
writeUint16((value.altitude + 1000) * 4); writeUint16(value.altitude);
#endif #endif
} }
@ -227,15 +227,19 @@ void PayloadConvert::addTime(time_t value) {
writeUint32(time); writeUint32(time);
} }
void PayloadConvert::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) { void PayloadConvert::uintToBytes(uint64_t value, uint8_t byteSize) {
for (uint8_t x = 0; x < byteSize; x++) { for (uint8_t x = 0; x < byteSize; x++) {
buffer[x + pos] = (byte)(i >> (x * 8)); byte next = 0;
if (sizeof(value) > x) {
next = static_cast<byte>((value >> (x * 8)) & 0xFF);
}
buffer[cursor] = next;
++cursor;
} }
cursor += byteSize;
} }
void PayloadConvert::writeUptime(uint64_t uptime) { void PayloadConvert::writeUptime(uint64_t uptime) {
intToBytes(cursor, uptime, 8); writeUint64(uptime);
} }
void PayloadConvert::writeVersion(char *version) { void PayloadConvert::writeVersion(char *version) {
@ -244,24 +248,25 @@ void PayloadConvert::writeVersion(char *version) {
} }
void PayloadConvert::writeLatLng(double latitude, double longitude) { void PayloadConvert::writeLatLng(double latitude, double longitude) {
intToBytes(cursor, latitude, 4); // Tested to at least work with int32_t, which are processed correctly.
intToBytes(cursor, longitude, 4); writeUint32(latitude);
writeUint32(longitude);
} }
void PayloadConvert::writeUint32(uint32_t i) { intToBytes(cursor, i, 4); } void PayloadConvert::writeUint64(uint64_t i) { uintToBytes(i, 8); }
void PayloadConvert::writeUint16(uint16_t i) { intToBytes(cursor, i, 2); } void PayloadConvert::writeUint32(uint32_t i) { uintToBytes(i, 4); }
void PayloadConvert::writeUint8(uint8_t i) { intToBytes(cursor, i, 1); } void PayloadConvert::writeUint16(uint16_t i) { uintToBytes(i, 2); }
void PayloadConvert::writeUint8(uint8_t i) { uintToBytes(i, 1); }
void PayloadConvert::writeUFloat(float value) { void PayloadConvert::writeUFloat(float value) {
int16_t h = (int16_t)(value * 100); writeUint16(value * 100);
intToBytes(cursor, h, 2);
} }
void PayloadConvert::writePressure(float value) { void PayloadConvert::writePressure(float value) {
int16_t h = (int16_t)(value); writeUint16(value * 10);
intToBytes(cursor, h, 2);
} }
/** /**