Skip to content

Commit ec525f1

Browse files
committed
Added option to use non-default I2C interface
1 parent 9891428 commit ec525f1

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/LSM9DS1_Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Distributed as-is; no warranty is given.
2222
#define __LSM9DS1_Types_H__
2323

2424
#include "LSM9DS1_Registers.h"
25+
#include <Wire.h>
2526

2627
// The LSM9DS1 functions over both I2C or SPI. This library supports both.
2728
// But the interface mode used must be sent to the LSM9DS1 constructor. Use
@@ -199,6 +200,7 @@ struct deviceSettings
199200
uint8_t commInterface; // Can be I2C, SPI 4-wire or SPI 3-wire
200201
uint8_t agAddress; // I2C address or SPI CS pin
201202
uint8_t mAddress; // I2C address or SPI CS pin
203+
TwoWire* i2c; // pointer to an instance of I2C interface
202204
};
203205

204206
struct accelSettings
@@ -247,4 +249,4 @@ struct IMUSettings
247249
temperatureSettings temp;
248250
};
249251

250-
#endif
252+
#endif

src/SparkFunLSM9DS1.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
6363
settings.device.commInterface = interface;
6464
settings.device.agAddress = xgAddr;
6565
settings.device.mAddress = mAddr;
66+
settings.device.i2c = &Wire;
6667

6768
settings.gyro.enabled = true;
6869
settings.gyro.enableX = true;
@@ -335,8 +336,7 @@ void LSM9DS1::initAccel()
335336
// remove errors due to imprecise or varying initial placement. Calibration of sensor data in this manner
336337
// is good practice.
337338
void LSM9DS1::calibrate(bool autoCalc)
338-
{
339-
uint8_t data[6] = {0, 0, 0, 0, 0, 0};
339+
{
340340
uint8_t samples = 0;
341341
int ii;
342342
int32_t aBiasRawTemp[3] = {0, 0, 0};
@@ -1150,47 +1150,47 @@ uint8_t LSM9DS1::SPIreadBytes(uint8_t csPin, uint8_t subAddress,
11501150

11511151
void LSM9DS1::initI2C()
11521152
{
1153-
Wire.begin(); // Initialize I2C library
1153+
settings.device.i2c->begin(); // Initialize I2C library
11541154
}
11551155

11561156
// Wire.h read and write protocols
11571157
void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
11581158
{
1159-
Wire.beginTransmission(address); // Initialize the Tx buffer
1160-
Wire.write(subAddress); // Put slave register address in Tx buffer
1161-
Wire.write(data); // Put data in Tx buffer
1162-
Wire.endTransmission(); // Send the Tx buffer
1159+
settings.device.i2c->beginTransmission(address); // Initialize the Tx buffer
1160+
settings.device.i2c->write(subAddress); // Put slave register address in Tx buffer
1161+
settings.device.i2c->write(data); // Put data in Tx buffer
1162+
settings.device.i2c->endTransmission(); // Send the Tx buffer
11631163
}
11641164

11651165
uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress)
11661166
{
1167-
uint8_t data; // `data` will store the register data
1168-
1169-
Wire.beginTransmission(address); // Initialize the Tx buffer
1170-
Wire.write(subAddress); // Put slave register address in Tx buffer
1171-
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
1172-
Wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address
1173-
1174-
data = Wire.read(); // Fill Rx buffer with result
1167+
uint8_t data; // `data` will store the register data
1168+
1169+
settings.device.i2c->beginTransmission(address); // Initialize the Tx buffer
1170+
settings.device.i2c->write(subAddress); // Put slave register address in Tx buffer
1171+
settings.device.i2c->endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
1172+
settings.device.i2c->requestFrom(address, (uint8_t) 1); // Read one byte from slave register address
1173+
1174+
data = settings.device.i2c->read(); // Fill Rx buffer with result
11751175
return data; // Return data read from slave register
11761176
}
11771177

11781178
uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count)
11791179
{
11801180
byte retVal;
1181-
Wire.beginTransmission(address); // Initialize the Tx buffer
1181+
settings.device.i2c->beginTransmission(address); // Initialize the Tx buffer
11821182
// Next send the register to be read. OR with 0x80 to indicate multi-read.
1183-
Wire.write(subAddress | 0x80); // Put slave register address in Tx buffer
1184-
retVal = Wire.endTransmission(false); // Send Tx buffer, send a restart to keep connection alive
1183+
settings.device.i2c->write(subAddress | 0x80); // Put slave register address in Tx buffer
1184+
retVal = settings.device.i2c->endTransmission(false); // Send Tx buffer, send a restart to keep connection alive
11851185
if (retVal != 0) // endTransmission should return 0 on success
11861186
return 0;
1187-
1188-
retVal = Wire.requestFrom(address, count); // Read bytes from slave register address
1187+
1188+
retVal = settings.device.i2c->requestFrom(address, count); // Read bytes from slave register address
11891189
if (retVal != count)
11901190
return 0;
1191-
1191+
11921192
for (int i=0; i<count;)
1193-
dest[i++] = Wire.read();
1194-
1193+
dest[i++] = settings.device.i2c->read();
1194+
11951195
return count;
11961196
}

0 commit comments

Comments
 (0)