Created
March 10, 2022 23:18
-
-
Save LeoDJ/a0b9a0bd32054f4c696a73353b66599a to your computer and use it in GitHub Desktop.
Uplink Payload Formatter for TTN Mapper LoRa node (including generic function for converting raw C struct integer values to JavaScript number)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// convert raw data to integer | |
function getVar(inputBytes, offset, varLen, isSigned = false) { | |
let outVal = 0; | |
for (let i = 0; i < varLen; i++) { | |
outVal |= inputBytes[offset + i] << i * 8; | |
} | |
if (isSigned) { // parse as signed value | |
if (inputBytes[offset + varLen - 1] & 0x80) { // sign bit is set | |
outVal = ~(outVal) + 1; // calculate two's complement | |
} | |
} | |
return outVal; | |
} | |
function decodeUplink(input) { | |
const ib = input.bytes; | |
let decodedPayload = { | |
latitude: getVar(ib, 0, 3) / 0xFFFFFF * 180 - 90, | |
longitude: getVar(ib, 3, 3) / 0xFFFFFF * 360 - 180, | |
altitude: getVar(ib, 6, 2, true) / 10, | |
hdop: getVar(ib, 8, 1) / 2.5, | |
}; | |
return { | |
data: decodedPayload, | |
warnings: [ | |
(decodedPayload.altitude < -3270) ? "No inital GPS fix!" : null | |
], | |
errors: [] | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment