From 69f2ce645d3906fad365247cc89c04379ae9d099 Mon Sep 17 00:00:00 2001 From: stijncarelsbergh Date: Thu, 8 Oct 2026 00:04:30 +0200 Subject: [PATCH] fix(magnetic sensor i2c): mask and shift the left-aligned LSB bits correctly For a left-aligned sensor the remaining bits sit in the *upper* part of the low byte, so both the mask and the shift have to account for the unused low bits. The constructor applied the right-aligned mask (0x3F for 6 remaining bits) and then shifted the result right by 8-lsb_used, so it kept the status/parity bits and dropped the real data - the angle is wrong for every sensor configured through this constructor. The preset configurations in the header (e.g. MT6701 with lsb_mask 0xFC, lsb_shift 2) show the intended convention. --- src/sensors/MagneticSensorI2C.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sensors/MagneticSensorI2C.cpp b/src/sensors/MagneticSensorI2C.cpp index 9298413a2..a9de26994 100644 --- a/src/sensors/MagneticSensorI2C.cpp +++ b/src/sensors/MagneticSensorI2C.cpp @@ -46,11 +46,15 @@ MagneticSensorI2C::MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution, _conf.msb_mask = (uint8_t)( (1 << _bits_used_msb) - 1 ); uint8_t lsb_used = _bit_resolution - _bits_used_msb; // used bits in LSB - _conf.lsb_mask = (uint8_t)( (1 << (lsb_used)) - 1 ); - if (!lsb_right_aligned) + if (!lsb_right_aligned){ + // left aligned: the remaining bits are in the upper part of the low byte, + // e.g. 6 bits -> 0xFC, read with >> 2 + _conf.lsb_mask = (uint8_t)( ((1 << (lsb_used)) - 1) << (8 - lsb_used) ); _conf.lsb_shift = 8-lsb_used; - else + }else{ + _conf.lsb_mask = (uint8_t)( (1 << (lsb_used)) - 1 ); _conf.lsb_shift = 0; + } _conf.msb_shift = lsb_used; cpr = _powtwo(_bit_resolution);