adafruit / Adafruit_BME280_Library

Arduino Library for BME280 sensors
Other
328 stars 301 forks source link

Add alternate address to 'begfin(TwoWire)' #68

Closed hhk7734 closed 4 years ago

hhk7734 commented 4 years ago

When calling begin(void), if failed to init() with i2caddr=BME280_ADDRESS, it tries again with i2caddr=BME280_ADDRESS_ALTERNATE. But it does not try again when calling begin(TwoWire). So I added that.

examples/bme280test/bme280test.ino

line 46
-  status = bme.begin();
+  status = bme.begin(&Wire);  
caternuson commented 4 years ago

Thanks for spotting that. My two cents on this - the overrides should be arranged differently to avoid the code duplication. Something like this:

bool Adafruit_BME280::begin(void) {
  return begin(BME280_ADDRESS, &Wire);
}

bool Adafruit_BME280::begin(TwoWire *theWire) {
  return begin(BME280_ADDRESS, theWire); 
}

bool Adafruit_BME280::begin(uint8_t addr) {
  return begin(addr, &Wire);
}

bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
  bool status = false;
  _i2caddr = addr;
  _wire = theWire;
  status = init();
  if (!status) {
    _i2caddr = BME280_ADDRESS_ALTERNATE;
    status = init();
  }
  return status;
}
hhk7734 commented 4 years ago

@caternuson Your suggestion is so good. I corrected it.

caternuson commented 4 years ago

@hhk7734 Thanks. This looks good. Did you test this on hardware?

hhk7734 commented 4 years ago

@caternuson Yes, I did.

examples/bme280test/bme280test.ino

line 46
-  status = bme.begin();
+  status = bme.begin(0x01, &Wire);  
line 46
-  status = bme.begin();
+  status = bme.begin(&Wire3);  
line 46
-  status = bme.begin();
+  status = bme.begin(0x01);  
caternuson commented 4 years ago

Cool. Thanks.

caternuson commented 4 years ago

It will be in the 1.1.0 release when it becomes available: https://github.com/adafruit/Adafruit_BME280_Library/releases/tag/1.1.0