perform proper sign extension
All checks were successful
EIVE/eive-obsw/pipeline/pr-main This commit looks good

This commit is contained in:
Robin Müller 2023-06-14 18:51:06 +02:00
parent a05a8ffb50
commit 12bc9268f7
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -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<uint16_t>((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<uint16_t>((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<float>(tempValueRaw)) * 0.0625);
#if OBSW_DEBUG_TMP1075 == 1