PaulStoffregen / NXPMotionSense

NXP Motion Sensors for Teensy and Arduino
69 stars 51 forks source link

Parameterise ic2 addresses #16

Open IanBUK opened 11 months ago

IanBUK commented 11 months ago

This change allows the consumer to specify the i2c addresses for the FXOS8700, FXAS21002 and MPL3115. It also allows the consumer of the library to specify whether the MPL3115, the temperature/pressure sensor is enabled or disabled.

The new public functions are:

bool begin(const uint8_t i2cAddressFxos8700, const uint8_t i2cAddressFxas21002, const uint8_t i2cAddressMPL311);
bool begin(const uint8_t i2cAddressFxos8700, const uint8_t i2cAddressFxas21002, const uint8_t i2cAddressMPL311, bool useTemperatureSensor);
void OutputConfig();

The first two provide overrides to the existing begin function, one to provide the addresses and a second that also allows specifying whether to use the MPL3115.

These values are stored private class level variables:

uint8_t _i2cAddressFxos8700 = FXOS8700_I2C_ADDR0;
uint8_t _i2cAddressFxas21002 = FXAS21002_I2C_ADDR0;
uint8_t _i2cAddressMPL3115 = MPL3115_I2C_ADDR;
bool _useTemperatureSensor = true;

The examples have been updated to show the new overrides, with my sensors. This is because the i2c addresses on my boards are at non-default addresses, and they don't have an MPL3115. This means I call the begin with:

imu.begin(FXOS8700_I2C_ADDR3, FXAS21002_I2C_ADDR1, MPL3115_I2C_ADDR, false);
IanBUK commented 11 months ago

Update to PR, to:

1) Fix bug in OutputConfig that showed the _i2cAddressFxos8700 rather than the _i2cAddressMPL3115. 2) Add an additional override for begin:

bool NXPMotionSense::begin(const uint8_t i2cAddressFxos8700, const uint8_t i2cAddressFxas21002)

This allows the class consumer to omit the MPL3115 address. When this is called, the MPL3115's active flag is set to false and so it is never read.