adafruit / Adafruit_LSM6DS

Arduino library for LSM6DS
Other
47 stars 39 forks source link

Make temperature readings work without underflow on micros with 32-bi… #16

Closed jim-sokoloff closed 4 years ago

jim-sokoloff commented 4 years ago

Scope of change:

Make temperature readings below 25ºC work on microcontrollers with integers greater than 16 bits. The existing code works on architectures where int is 16 bits wide, due to an implicit conversion of a uint_8 << 8 + uint_8 to integer. When that int16 is negative on a 16-bit architecture (such as Arduino where it was originally written), it works. What that quantity is negative on a 32-bit architecture (such as ESP32), then anytime that "would have been negative", it is instead 256ºC higher. As a result, when the device is cooled in 0.1ºC increments from 25.3ºC, the output is 25.3, 25.2, 25.1, 25.0, 280.9, 280.8, etc.

This patch fixes that bug and, in addition, sets the existing (previously unused) rawTemp private variable, consistent with how other raw sensor measurements are done.

jim-sokoloff commented 4 years ago

Fixes issue #17 (which I can't seem to get to link using the sidebar, so linking "via plaintext comment")

ladyada commented 4 years ago

thanks :) i think we had some changes in temp reading code for the LSM6DSO32 so this PR would have to be tested before merging :)

jim-sokoloff commented 4 years ago

You are correct. That would have failed, but now will not. (As the existing comment suggests, a cleanup and merging some of the subclass code into the superclass would probably be cleaner, but this should pass on all devices now. I don't have any LSM6DSO32s to test with, but the code change is extremely simplistic.)

Hope this helps.

siddacious commented 4 years ago

Looks good to me! Previously: image With fix: image