Skip to content

Negative temperatures are parsed incorrectly #3

@sparvanders

Description

@sparvanders

The code in bmp5.c:

raw_data_t =
                    (int32_t)((uint32_t)fifo->data[*data_index + 2] << 16 | (uint16_t)fifo->data[*data_index + 1] << 8 |
                        fifo->data[*data_index]);

does not parse negative numbers correctly. The code reinterprets a uint32_t number as int32_t, relying on the two-complement form of the bytes to match the two-complement representation of int32_t. However, the bytes are 24-bit and not 32-bit, so the two two-complement formats do not coincide for negative numbers.

I have verified that adding these lines afterwards fixes the problem:

if (raw_data_t & 1 << 23)  //Check for negative two-complement number
    raw_data_t = raw_data_t - (1 << 24);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions