From 3b8027668be989dcea3cddb09d2483ca29ee7e7b Mon Sep 17 00:00:00 2001 From: lady ada Date: Mon, 21 Dec 2020 12:21:48 -0500 Subject: [PATCH] add optional Wire interface --- Adafruit_BMP085.cpp | 18 ++++++++++++------ Adafruit_BMP085.h | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Adafruit_BMP085.cpp b/Adafruit_BMP085.cpp index f806910..f1f8b32 100644 --- a/Adafruit_BMP085.cpp +++ b/Adafruit_BMP085.cpp @@ -31,14 +31,20 @@ #include "Adafruit_BMP085.h" #include -Adafruit_BMP085::Adafruit_BMP085() : i2c_dev(BMP085_I2CADDR) {} +Adafruit_BMP085::Adafruit_BMP085() {} -bool Adafruit_BMP085::begin(uint8_t mode) { +bool Adafruit_BMP085::begin(uint8_t mode, TwoWire *wire) { if (mode > BMP085_ULTRAHIGHRES) mode = BMP085_ULTRAHIGHRES; oversampling = mode; - if (!i2c_dev.begin()) { + if (i2c_dev) { + delete i2c_dev; // remove old interface + } + + i2c_dev = new Adafruit_I2CDevice(BMP085_I2CADDR, wire); + + if (!i2c_dev->begin()) { return false; } @@ -279,7 +285,7 @@ uint8_t Adafruit_BMP085::read8(uint8_t a) { uint8_t ret; // send 1 byte, reset i2c, read 1 byte - i2c_dev.write_then_read(&a, 1, &ret, 1, true); + i2c_dev->write_then_read(&a, 1, &ret, 1, true); return ret; } @@ -291,7 +297,7 @@ uint16_t Adafruit_BMP085::read16(uint8_t a) { // send 1 byte, reset i2c, read 2 bytes // we could typecast uint16_t as uint8_t array but would need to ensure proper // endianness - i2c_dev.write_then_read(&a, 1, retbuf, 2, true); + i2c_dev->write_then_read(&a, 1, retbuf, 2, true); // write_then_read uses uint8_t array ret = retbuf[1] | (retbuf[0] << 8); @@ -301,5 +307,5 @@ uint16_t Adafruit_BMP085::read16(uint8_t a) { void Adafruit_BMP085::write8(uint8_t a, uint8_t d) { // send d prefixed with a (a d [stop]) - i2c_dev.write(&d, 1, true, &a, 1); + i2c_dev->write(&d, 1, true, &a, 1); } diff --git a/Adafruit_BMP085.h b/Adafruit_BMP085.h index 5e3a6df..c9986ea 100644 --- a/Adafruit_BMP085.h +++ b/Adafruit_BMP085.h @@ -59,9 +59,10 @@ class Adafruit_BMP085 { /*! * @brief Starts I2C connection * @param mode Mode to set, ultra high-res by default + * @param wire The I2C interface to use, defaults to Wire * @return Returns true if successful */ - bool begin(uint8_t mode = BMP085_ULTRAHIGHRES); + bool begin(uint8_t mode = BMP085_ULTRAHIGHRES, TwoWire *wire = &Wire); /*! * @brief Gets the temperature over I2C from the BMP085 * @return Returns the temperature @@ -101,7 +102,7 @@ class Adafruit_BMP085 { uint16_t read16(uint8_t addr); void write8(uint8_t addr, uint8_t data); - Adafruit_I2CDevice i2c_dev; + Adafruit_I2CDevice *i2c_dev; uint8_t oversampling; int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;