payload.cpp fixes

This commit is contained in:
Klaus K Wilting 2018-06-18 09:15:57 +02:00
parent f3ca06fa31
commit d2739426e5
4 changed files with 21 additions and 27 deletions

View File

@ -18,12 +18,12 @@ function Decoder(bytes, port) {
if (port === 2) {
// device status data
if (bytes.length == 10) {
return decode(bytes, [uint16, unixtime, temperature], ['voltage', 'uptime', 'cputemp']);
if (bytes.length == 12) {
return decode(bytes, [uint16, uptime, temperature], ['voltage', 'uptime', 'cputemp']);
}
// device config data
if (bytes.length == 8) {
return decode(bytes, [uint8, uint16, uint8, uint8, uint8, bitmap], ['lorasf', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags']);
return decode(bytes, [uint8, uint16, uint8, uint8, uint8, uint8, bitmap], ['lorasf', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags']);
}
}
@ -41,13 +41,13 @@ var bytesToInt = function (bytes) {
return i;
};
var unixtime = function (bytes) {
if (bytes.length !== unixtime.BYTES) {
throw new Error('Unix time must have exactly 4 bytes');
var uptime = function (bytes) {
if (bytes.length !== uptime.BYTES) {
throw new Error('uptime must have exactly 8 bytes');
}
return bytesToInt(bytes);
};
unixtime.BYTES = 4;
uptime.BYTES = 4;
var uint8 = function (bytes) {
if (bytes.length !== uint8.BYTES) {
@ -150,7 +150,7 @@ var decode = function (bytes, mask, names) {
if (typeof module === 'object' && typeof module.exports !== 'undefined') {
module.exports = {
unixtime: unixtime,
uptime: uptime,
uint8: uint8,
uint16: uint16,
temperature: temperature,

View File

@ -8,8 +8,8 @@ static const char TAG[] = "main";
// read GPS data and cast to global struct
void gps_read() {
gps_status.latitude = (uint32_t)(gps.location.lat() * 1000000);
gps_status.longitude = (uint32_t)(gps.location.lng() * 1000000);
gps_status.latitude = (uint32_t)(gps.location.lat() * 1e6);
gps_status.longitude = (uint32_t)(gps.location.lng() * 1e6);
gps_status.satellites = (uint8_t)gps.satellites.value();
gps_status.hdop = (uint16_t)gps.hdop.value();
gps_status.altitude = (uint16_t)gps.altitude.meters();

View File

@ -125,39 +125,33 @@ void TTNpacked::addConfig(configData_t value) {
void TTNpacked::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
writeUint16(voltage);
writeUnixtime(uptime);
writeUptime(uptime);
writeTemperature(cputemp);
}
void TTNpacked::_intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) {
void TTNpacked::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) {
for (uint8_t x = 0; x < byteSize; x++) {
buffer[x + pos] = (byte)(i >> (x * 8));
}
cursor += byteSize;
}
void TTNpacked::writeUnixtime(uint32_t unixtime) {
_intToBytes(cursor, unixtime, 4);
void TTNpacked::writeUptime(uint64_t uptime) {
intToBytes(cursor, uptime, 8);
}
void TTNpacked::writeLatLng(double latitude, double longitude) {
int32_t lat = latitude * 1e6;
int32_t lng = longitude * 1e6;
_intToBytes(cursor, lat, 4);
_intToBytes(cursor, lng, 4);
intToBytes(cursor, latitude, 4);
intToBytes(cursor, longitude, 4);
}
void TTNpacked::writeUint16(uint16_t i) {
_intToBytes(cursor, i, 2);
}
void TTNpacked::writeUint16(uint16_t i) { intToBytes(cursor, i, 2); }
void TTNpacked::writeUint8(uint8_t i) {
_intToBytes(cursor, i, 1);
}
void TTNpacked::writeUint8(uint8_t i) { intToBytes(cursor, i, 1); }
void TTNpacked::writeHumidity(float humidity) {
int16_t h = (int16_t)(humidity * 100);
_intToBytes(cursor, h, 2);
intToBytes(cursor, h, 2);
}
/**

View File

@ -59,8 +59,8 @@ public:
private:
uint8_t *buffer;
uint8_t cursor;
void _intToBytes(uint8_t pos, int32_t i, uint8_t byteSize);
void writeUnixtime(uint32_t unixtime);
void intToBytes(uint8_t pos, int32_t i, uint8_t byteSize);
void writeUptime(uint64_t unixtime);
void writeLatLng(double latitude, double longitude);
void writeUint16(uint16_t i);
void writeUint8(uint8_t i);