diff --git a/hooks/seatalk/0x99.js b/hooks/seatalk/0x99.js index 10bc7e5..080fc33 100644 --- a/hooks/seatalk/0x99.js +++ b/hooks/seatalk/0x99.js @@ -28,31 +28,31 @@ const utils = require('@signalk/nmea0183-utilities') Corresponding NMEA sentences: RMC, HDG */ + module.exports = function (input) { const { id, sentence, parts, tags } = input var XX = parseInt(parts[2], 16) - var value = 128 - (XX & 0x7f) - var s = -1 - if (XX & (0x80 != 0)) { - s = 1 - } - var magneticVariation = s * value - - var pathValues = [] - - pathValues.push({ + + // Convert 8-bit two's complement to signed integer + var magneticVariation = XX > 127 ? XX - 256 : XX + + // Documentation says: + // Positive = West (negative in navigation convention) + // Negative = East (positive in navigation convention) + // So we need to invert the sign + magneticVariation = -magneticVariation + + var pathValues = [{ path: 'navigation.magneticVariation', value: utils.transform(utils.float(magneticVariation), 'deg', 'rad'), - }) + }] return { - updates: [ - { - source: tags.source, - timestamp: tags.timestamp, - values: pathValues, - }, - ], + updates: [{ + source: tags.source, + timestamp: tags.timestamp, + values: pathValues, + }] } }