Skip to content

Commit

Permalink
Merge pull request #29 from adafruit/samyk-master
Browse files Browse the repository at this point in the history
add optional Wire interface
  • Loading branch information
ladyada committed Dec 21, 2020
2 parents aeb6e61 + 3b80276 commit d191a3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
18 changes: 12 additions & 6 deletions Adafruit_BMP085.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
#include "Adafruit_BMP085.h"
#include <Adafruit_I2CDevice.h>

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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
}
5 changes: 3 additions & 2 deletions Adafruit_BMP085.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d191a3f

Please sign in to comment.