Skip to content

Commit

Permalink
Add more convenient constructors for I2C connection
Browse files Browse the repository at this point in the history
  • Loading branch information
okalachev committed Dec 9, 2024
1 parent 0b6e35d commit 74f7ffa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions ICM20948.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class ICM20948 : public IMUInterface, public Logger
/* Constructors */
ICM20948(uint8_t addr = 0x68) : _wire{&Wire}, i2cAddress{addr}, useSPI{false} {}
ICM20948(TwoWire *w, uint8_t addr = 0x68) : _wire{w}, i2cAddress{addr}, useSPI{false} {}
ICM20948(TwoWire& w, uint8_t addr = 0x68) : _wire{&w}, i2cAddress{addr}, useSPI{false} {}
ICM20948(SPIClass& s, int cs = -1) : _spi{&s}, csPin{cs}, useSPI{true} {}

bool begin() override;
Expand Down
10 changes: 4 additions & 6 deletions MPU9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ class MPU9250 : public IMUInterface, public Logger {
WOM_RATE_500HZ = 0x0B
};
MPU9250() {}
MPU9250(TwoWire *i2c, const I2cAddr addr) :
imu_(i2c, static_cast<uint8_t>(addr)) {}
MPU9250(SPIClass *spi, const uint8_t cs = 255) :
imu_(spi, cs) {}
MPU9250(SPIClass& spi, const uint8_t cs = 255) :
imu_(&spi, cs) {}
MPU9250(TwoWire *i2c, const I2cAddr addr) : imu_(i2c, static_cast<uint8_t>(addr)) {}
MPU9250(TwoWire& i2c, const I2cAddr addr = I2C_ADDR_PRIM) : imu_(&i2c, static_cast<uint8_t>(addr)) {}
MPU9250(SPIClass *spi, const uint8_t cs = 255) : imu_(spi, cs) {}
MPU9250(SPIClass& spi, const uint8_t cs = 255) : imu_(&spi, cs) {}
void config(TwoWire *i2c, const I2cAddr addr);
void config(SPIClass *spi, const uint8_t cs);
bool begin() override;
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ Notice, that MPU6500 does not include a magnetometer, so magnetometer data will

You can also use `<MPU6500.h>` header and `MPU6500` class, which is an alias for `MPU9250` class.

#### I²C

In case of using I²C connection (not recommended), the initialization would look like this:

```cpp
#include <MPU9250.h>
#include <Wire.h>

MPU9250 IMU(Wire); // the default address is used automatically

void setup() {
Wire.begin();
Wire.setClock(400000); // 400 kHz I²C clock
IMU.begin();
}
```
### ICM-20948
The ICM-20948 IMU driver has the same interface. Only the declaration is changed in the example above:
Expand Down

0 comments on commit 74f7ffa

Please sign in to comment.