From 12bc9268f76e8b6f4dea94c8da74a80ca78351e4 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 14 Jun 2023 18:51:06 +0200 Subject: [PATCH] perform proper sign extension --- mission/tcs/Tmp1075Handler.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mission/tcs/Tmp1075Handler.cpp b/mission/tcs/Tmp1075Handler.cpp index 054b2118..30331e1f 100644 --- a/mission/tcs/Tmp1075Handler.cpp +++ b/mission/tcs/Tmp1075Handler.cpp @@ -86,14 +86,10 @@ ReturnValue_t Tmp1075Handler::scanForReply(const uint8_t *start, size_t remainin ReturnValue_t Tmp1075Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { switch (id) { case TMP1075::GET_TEMP: { - // Ignore the sign bit, subtract it later when applicable. - int16_t tempValueRaw = static_cast((packet[0] << 8) | packet[1]) & 0x7fff; - // 12 bit value, so perform the shift for correct values. - tempValueRaw >>= 4; - if(((packet[0] >> 7) & 0b1) == 0b1) { - // Perform two's complement by subtracting the sign - tempValueRaw -= 0x800; - } + // Convert 12 bit MSB first raw temperature to 16 bit first. + int16_t tempValueRaw = static_cast((packet[0] << 8) | packet[1]) >> 4; + // Sign extension to 16 bits: If the sign bit is set, fill up with ones on the left. + tempValueRaw = (packet[0] & 0x80) ? (tempValueRaw | 0xF000) : tempValueRaw; // 0.0625 is the sensor sensitivity. float tempValue = ((static_cast(tempValueRaw)) * 0.0625); #if OBSW_DEBUG_TMP1075 == 1