From bc6b044b4cdb8e82fdb4a453bafda45992e1403a Mon Sep 17 00:00:00 2001 From: NCPlyn Date: Fri, 10 Feb 2023 23:25:01 +0100 Subject: [PATCH] Ability to specify I2C pins in .begin() --- library.properties | 2 +- src/SparkFunLSM6DS3.cpp | 15 +++++++++------ src/SparkFunLSM6DS3.h | 8 ++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/library.properties b/library.properties index 967876e..4abac84 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun LSM6DS3 Breakout -version=1.0.3 +version=1.0.4 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=A library to drive the STmicro LSM6DS3 by SPI or I2C. diff --git a/src/SparkFunLSM6DS3.cpp b/src/SparkFunLSM6DS3.cpp index a099fc5..16d6f00 100644 --- a/src/SparkFunLSM6DS3.cpp +++ b/src/SparkFunLSM6DS3.cpp @@ -45,7 +45,7 @@ Distributed as-is; no warranty is given. // Default construction is I2C mode, address 0x6B. // //****************************************************************************// -LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg) : commInterface(I2C_MODE), I2CAddress(0x6B), chipSelectPin(10) +LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg ) : commInterface(I2C_MODE), I2CAddress(0x6B), chipSelectPin(10) { commInterface = busType; if( commInterface == I2C_MODE ) @@ -59,15 +59,18 @@ LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg) : commInterface(I2C } -status_t LSM6DS3Core::beginCore(void) +status_t LSM6DS3Core::beginCore( int8_t inputSDA, int8_t inputSCL ) { status_t returnError = IMU_SUCCESS; - uint32_t spiPortSpeed = 5000000; + uint32_t spiPortSpeed = 5000000; switch (commInterface) { case I2C_MODE: - Wire.begin(); + if( inputSDA != -1 && inputSCL != -1 ) + Wire.begin( inputSDA, inputSCL ); + else + Wire.begin(); break; case SPI_MODE: @@ -378,13 +381,13 @@ LSM6DS3::LSM6DS3( uint8_t busType, uint8_t inputArg ) : LSM6DS3Core( busType, in // "myIMU.settings.accelEnabled = 1;" to configure before calling .begin(); // //****************************************************************************// -status_t LSM6DS3::begin(SensorSettings* pSettingsYouWanted) +status_t LSM6DS3::begin(SensorSettings* pSettingsYouWanted, int8_t inputSDA, int8_t inputSCL) { //Check the settings structure values to determine how to setup the device uint8_t dataToWrite = 0; //Temporary variable //Begin the inherited core. This gets the physical wires connected - status_t returnError = beginCore(); + status_t returnError = beginCore(inputSDA,inputSCL); // Copy the values from the user's settings into the output 'pSettingsYouWanted' // compare settings with 'pSettingsYouWanted' after 'begin' to see if anything changed diff --git a/src/SparkFunLSM6DS3.h b/src/SparkFunLSM6DS3.h index fcc47f7..6c4614b 100644 --- a/src/SparkFunLSM6DS3.h +++ b/src/SparkFunLSM6DS3.h @@ -58,7 +58,7 @@ class LSM6DS3Core LSM6DS3Core( uint8_t, uint8_t ); ~LSM6DS3Core() = default; - status_t beginCore( void ); + status_t beginCore( int8_t, int8_t ); //The following utilities read and write to the IMU @@ -81,7 +81,7 @@ class LSM6DS3Core //Change to base page status_t basePage( void ); - SPISettings mySpiSettings; + SPISettings mySpiSettings; private: @@ -146,11 +146,11 @@ class LSM6DS3 : public LSM6DS3Core //Constructor generates default SensorSettings. //(over-ride after construction if desired) - LSM6DS3( uint8_t busType = I2C_MODE, uint8_t inputArg = 0x6B ); + LSM6DS3( uint8_t busType = I2C_MODE, uint8_t inputArg = 0x6B); ~LSM6DS3() = default; //Call to apply SensorSettings - status_t begin(SensorSettings* pSettingsYouWanted = NULL); + status_t begin(SensorSettings* pSettingsYouWanted = NULL, int8_t inputSDA = -1, int8_t inputSCL = -1); //Returns the raw bits from the sensor cast as 16-bit signed integers int16_t readRawAccelX( void );