adafruit / Adafruit_LIS3MDL

Adafruit library for LIS3MDL magnetometer
Other
11 stars 8 forks source link

read() and getEvent() are inefficient due to read of CTRL_REG2 (0x21) for range for every call #5

Open kevinjwalters opened 3 years ago

kevinjwalters commented 3 years ago

The read() member function which is also used by getEvent() calls getRange() which reads the value over the i2c bus:

https://github.com/adafruit/Adafruit_LIS3MDL/blob/d162c5759bfab9ed707fa89b92098169a04e01ac/Adafruit_LIS3MDL.cpp#L172

https://github.com/adafruit/Adafruit_LIS3MDL/blob/d162c5759bfab9ed707fa89b92098169a04e01ac/Adafruit_LIS3MDL.cpp#L380-L387

This value could easily be cached in the object using a tiny amount of memory on the reasonable assumption that this object is the only thing changing the range. This would reduce the amount of i2c traffic benefitting the performance of the magnetometer reads.

ladyada commented 3 years ago

hihi - if you can submit a PR to make the cache change we'll take a look