Skip to content

Commit d191a3f

Browse files
authored
Merge pull request #29 from adafruit/samyk-master
add optional Wire interface
2 parents aeb6e61 + 3b80276 commit d191a3f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Adafruit_BMP085.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@
3131
#include "Adafruit_BMP085.h"
3232
#include <Adafruit_I2CDevice.h>
3333

34-
Adafruit_BMP085::Adafruit_BMP085() : i2c_dev(BMP085_I2CADDR) {}
34+
Adafruit_BMP085::Adafruit_BMP085() {}
3535

36-
bool Adafruit_BMP085::begin(uint8_t mode) {
36+
bool Adafruit_BMP085::begin(uint8_t mode, TwoWire *wire) {
3737
if (mode > BMP085_ULTRAHIGHRES)
3838
mode = BMP085_ULTRAHIGHRES;
3939
oversampling = mode;
4040

41-
if (!i2c_dev.begin()) {
41+
if (i2c_dev) {
42+
delete i2c_dev; // remove old interface
43+
}
44+
45+
i2c_dev = new Adafruit_I2CDevice(BMP085_I2CADDR, wire);
46+
47+
if (!i2c_dev->begin()) {
4248
return false;
4349
}
4450

@@ -279,7 +285,7 @@ uint8_t Adafruit_BMP085::read8(uint8_t a) {
279285
uint8_t ret;
280286

281287
// send 1 byte, reset i2c, read 1 byte
282-
i2c_dev.write_then_read(&a, 1, &ret, 1, true);
288+
i2c_dev->write_then_read(&a, 1, &ret, 1, true);
283289

284290
return ret;
285291
}
@@ -291,7 +297,7 @@ uint16_t Adafruit_BMP085::read16(uint8_t a) {
291297
// send 1 byte, reset i2c, read 2 bytes
292298
// we could typecast uint16_t as uint8_t array but would need to ensure proper
293299
// endianness
294-
i2c_dev.write_then_read(&a, 1, retbuf, 2, true);
300+
i2c_dev->write_then_read(&a, 1, retbuf, 2, true);
295301

296302
// write_then_read uses uint8_t array
297303
ret = retbuf[1] | (retbuf[0] << 8);
@@ -301,5 +307,5 @@ uint16_t Adafruit_BMP085::read16(uint8_t a) {
301307

302308
void Adafruit_BMP085::write8(uint8_t a, uint8_t d) {
303309
// send d prefixed with a (a d [stop])
304-
i2c_dev.write(&d, 1, true, &a, 1);
310+
i2c_dev->write(&d, 1, true, &a, 1);
305311
}

Adafruit_BMP085.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class Adafruit_BMP085 {
5959
/*!
6060
* @brief Starts I2C connection
6161
* @param mode Mode to set, ultra high-res by default
62+
* @param wire The I2C interface to use, defaults to Wire
6263
* @return Returns true if successful
6364
*/
64-
bool begin(uint8_t mode = BMP085_ULTRAHIGHRES);
65+
bool begin(uint8_t mode = BMP085_ULTRAHIGHRES, TwoWire *wire = &Wire);
6566
/*!
6667
* @brief Gets the temperature over I2C from the BMP085
6768
* @return Returns the temperature
@@ -101,7 +102,7 @@ class Adafruit_BMP085 {
101102
uint16_t read16(uint8_t addr);
102103
void write8(uint8_t addr, uint8_t data);
103104

104-
Adafruit_I2CDevice i2c_dev;
105+
Adafruit_I2CDevice *i2c_dev;
105106
uint8_t oversampling;
106107

107108
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;

0 commit comments

Comments
 (0)