Skip to content

Conversation

maxmclau
Copy link
Contributor

@maxmclau maxmclau commented Oct 6, 2025

This change fixes and issue where negative temperatures wrap and return 250C when the sensor gets below zero. The implementation is pulled from Boschs official BMP5_SensorAPI and has been tested to work down to -40.

This change fixes and issue where negative temperatures wrap and
return 250C when the sensor gets below zero. The implementation is
pulled from Boschs official BMP5_SensorAPI and has been tested to
work down to -40.

Signed-off-by: Maxmillion McLaughlin <[email protected]>
@maxmclau maxmclau force-pushed the bmp581-temp-negative-fix branch from b33746c to a85567f Compare October 6, 2025 23:48
Copy link

sonarqubecloud bot commented Oct 6, 2025

Comment on lines +408 to +414
/* convert raw sensor data to sensor_value.
* BMP581 temperature data is 24-bit signed with LSB = 1/65536 °C
*/
drv->last_sample.temperature.val1 = data[2];
drv->last_sample.temperature.val2 = (data[1] << 8 | data[0]) * 10;
int32_t raw_temp = ((int32_t)((uint32_t)(((uint32_t)data[2] << 16) |
((uint16_t)data[1] << 8) | data[0])
<< 8) >>
8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of shifting 8 up and 8 down, I suggest using sign_extend(), similar to how it's done in the decoder:

case SENSOR_CHAN_AMBIENT_TEMP: {
/* Temperature is in data[2:0], data[2] is integer part */
uint32_t raw_temp = ((uint32_t)frame[*fit].payload[2] << 16) |
((uint16_t)frame[*fit].payload[1] << 8) |
frame[*fit].payload[0];
int32_t raw_temp_signed = sign_extend(raw_temp, 23);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants