Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Adafruit_MPU6050.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Adafruit_MPU6050::~Adafruit_MPU6050(void) {

/*!
* @brief Sets up the hardware and initializes I2C
* This call, unlike begin(), doesn't do an initialization of the
* MPU6050 settings, and assumes that the user wants to maintain
* the last settings.
* @param i2c_address
* The I2C address to be used.
* @param wire
Expand All @@ -66,9 +69,46 @@ Adafruit_MPU6050::~Adafruit_MPU6050(void) {
* The user-defined ID to differentiate different sensors
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_MPU6050::init(uint8_t i2c_address, TwoWire *wire,
int32_t sensor_id) {
if (!initI2C(i2c_address, wire)) {
return false;
}

_sensorid_accel = sensor_id;
_sensorid_gyro = sensor_id + 1;
_sensorid_temp = sensor_id + 2;
return true;
}

/*!
* @brief Sets up the hardware and initializes I2C
* @param i2c_address
* The I2C address to be used.
* @param wire
* The Wire object to be used for I2C connections.
* @param sensor_id
* The user-defined ID to differentiate different sensors
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_MPU6050::begin(uint8_t i2c_address, TwoWire *wire,
int32_t sensor_id) {
if (!initI2C(i2c_address, wire)) {
return false;
}

return _init(sensor_id);
}

/*!
* @brief Initializes I2C
* @param i2c_address
* The I2C address to be used.
* @param wire
* The Wire object to be used for I2C connections.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_MPU6050::initI2C(uint8_t i2c_address, TwoWire *wire) {
if (i2c_dev) {
delete i2c_dev; // remove old interface
}
Expand All @@ -87,7 +127,7 @@ bool Adafruit_MPU6050::begin(uint8_t i2c_address, TwoWire *wire,
return false;
}

return _init(sensor_id);
return true;
}

/*! @brief Initilizes the sensor
Expand Down
4 changes: 4 additions & 0 deletions Adafruit_MPU6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class Adafruit_MPU6050 {
Adafruit_MPU6050();
~Adafruit_MPU6050();

bool init(uint8_t i2c_addr = MPU6050_I2CADDR_DEFAULT, TwoWire *wire = &Wire,
int32_t sensorID = 0);

bool begin(uint8_t i2c_addr = MPU6050_I2CADDR_DEFAULT, TwoWire *wire = &Wire,
int32_t sensorID = 0);

Expand Down Expand Up @@ -259,6 +262,7 @@ class Adafruit_MPU6050 {
private:
void _getRawSensorData(void);
void _scaleSensorData(void);
bool initI2C(uint8_t i2c_address, TwoWire *wire);

protected:
float temperature, ///< Last reading's temperature (C)
Expand Down