From 699c6edc52242725ab9f7b1f6cbbe9b871a163e1 Mon Sep 17 00:00:00 2001 From: mjs513 Date: Tue, 12 Sep 2017 10:22:31 -0400 Subject: [PATCH] Added ability to select Wire bus in begin function --- .../I2C_DeltaAltitude/I2C_DeltaAltitude.ino | 2 +- examples/I2C_ReadAllData/I2C_ReadAllData.ino | 2 +- .../I2C_and_SPI_Multisensor.ino | 4 +- src/SparkFunBME280.cpp | 59 +++++++++++-------- src/SparkFunBME280.h | 9 ++- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/examples/I2C_DeltaAltitude/I2C_DeltaAltitude.ino b/examples/I2C_DeltaAltitude/I2C_DeltaAltitude.ino index 43c1b5e..63db412 100644 --- a/examples/I2C_DeltaAltitude/I2C_DeltaAltitude.ino +++ b/examples/I2C_DeltaAltitude/I2C_DeltaAltitude.ino @@ -125,7 +125,7 @@ void setup() Serial.print("Starting BME280... result of .begin(): 0x"); delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. //Calling .begin() causes the settings to be loaded - Serial.println(mySensor.begin(), HEX); + Serial.println(mySensor.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses Serial.println(); diff --git a/examples/I2C_ReadAllData/I2C_ReadAllData.ino b/examples/I2C_ReadAllData/I2C_ReadAllData.ino index 791faa8..a21700c 100644 --- a/examples/I2C_ReadAllData/I2C_ReadAllData.ino +++ b/examples/I2C_ReadAllData/I2C_ReadAllData.ino @@ -96,7 +96,7 @@ void setup() //Calling .begin() causes the settings to be loaded delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. - Serial.println(mySensor.begin(), HEX); + Serial.println(mySensor.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses Serial.print("Displaying ID, reset and ctrl regs\n"); diff --git a/examples/I2C_and_SPI_Multisensor/I2C_and_SPI_Multisensor.ino b/examples/I2C_and_SPI_Multisensor/I2C_and_SPI_Multisensor.ino index 30ca3f9..c71598d 100644 --- a/examples/I2C_and_SPI_Multisensor/I2C_and_SPI_Multisensor.ino +++ b/examples/I2C_and_SPI_Multisensor/I2C_and_SPI_Multisensor.ino @@ -92,9 +92,9 @@ void setup() delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. //Calling .begin() causes the settings to be loaded Serial.print("Sensor A: 0x"); - Serial.println(mySensorA.begin(), HEX); + Serial.println(mySensorA.begin(), HEX); //mySensor.begin(&Wire), use &Wire1, &Wire2 for different Wire Busses Serial.print("Sensor B: 0x"); - Serial.println(mySensorB.begin(), HEX); + Serial.println(mySensorB.begin(), HEX); //SensorB is in SPI Mode } diff --git a/src/SparkFunBME280.cpp b/src/SparkFunBME280.cpp index 3c57513..713fb5e 100644 --- a/src/SparkFunBME280.cpp +++ b/src/SparkFunBME280.cpp @@ -6,7 +6,7 @@ May 20, 2015 https://github.com/sparkfun/BME280_Breakout Resources: -Uses Wire.h for i2c operation +Uses wire -> h for i2c operation Uses SPI.h for SPI operation Development environment specifics: @@ -24,7 +24,7 @@ Distributed as-is; no warranty is given. #include "stdint.h" #include -#include "Wire.h" +#include "wire -> h" #include "SPI.h" //****************************************************************************// @@ -61,15 +61,18 @@ BME280::BME280( void ) // configure before calling .begin(); // //****************************************************************************// -uint8_t BME280::begin() +uint8_t BME280::begin(TwoWire *theWire) { + //Set wire bus to be used if in i2c mode, will default to Wire if not specified + wire = theWire; + //Check the settings structure values to determine how to setup the device uint8_t dataToWrite = 0; //Temporary variable switch (settings.commInterface) { case I2C_MODE: - Wire.begin(); + wire -> begin(); break; case SPI_MODE: @@ -123,7 +126,7 @@ uint8_t BME280::begin() calibration.dig_H3 = ((uint8_t)(readRegister(BME280_DIG_H3_REG))); calibration.dig_H4 = ((int16_t)((readRegister(BME280_DIG_H4_MSB_REG) << 4) + (readRegister(BME280_DIG_H4_LSB_REG) & 0x0F))); calibration.dig_H5 = ((int16_t)((readRegister(BME280_DIG_H5_MSB_REG) << 4) + ((readRegister(BME280_DIG_H4_LSB_REG) >> 4) & 0x0F))); - calibration.dig_H6 = ((uint8_t)readRegister(BME280_DIG_H6_REG)); + calibration.dig_H6 = ((int8_t)readRegister(BME280_DIG_H6_REG)); //Set the oversampling control words. //config will only be writeable in sleep mode, so first insure that. @@ -168,7 +171,9 @@ float BME280::readFloatPressure( void ) // Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits). // Output value of “24674867” represents 24674867/256 = 96386.2 Pa = 963.862 hPa - int32_t adc_P = ((uint32_t)readRegister(BME280_PRESSURE_MSB_REG) << 12) | ((uint32_t)readRegister(BME280_PRESSURE_LSB_REG) << 4) | ((readRegister(BME280_PRESSURE_XLSB_REG) >> 4) & 0x0F); + uint8_t buffer[3]; + readRegisterRegion(buffer, BME280_PRESSURE_MSB_REG, 3); + int32_t adc_P = ((uint32_t)buffer[0] << 12) | ((uint32_t)buffer[1] << 4) | ((buffer[2] >> 4) & 0x0F); int64_t var1, var2, p_acc; var1 = ((int64_t)t_fine) - 128000; @@ -219,7 +224,9 @@ float BME280::readFloatHumidity( void ) // Returns humidity in %RH as unsigned 32 bit integer in Q22. 10 format (22 integer and 10 fractional bits). // Output value of “47445” represents 47445/1024 = 46. 333 %RH - int32_t adc_H = ((uint32_t)readRegister(BME280_HUMIDITY_MSB_REG) << 8) | ((uint32_t)readRegister(BME280_HUMIDITY_LSB_REG)); + uint8_t buffer[2]; + readRegisterRegion(buffer, BME280_HUMIDITY_MSB_REG, 2); + int32_t adc_H = ((uint32_t)buffer[0] << 8) | ((uint32_t)buffer[1]); int32_t var1; var1 = (t_fine - ((int32_t)76800)); @@ -248,7 +255,9 @@ float BME280::readTempC( void ) // t_fine carries fine temperature as global value //get the reading (adc_T); - int32_t adc_T = ((uint32_t)readRegister(BME280_TEMPERATURE_MSB_REG) << 12) | ((uint32_t)readRegister(BME280_TEMPERATURE_LSB_REG) << 4) | ((readRegister(BME280_TEMPERATURE_XLSB_REG) >> 4) & 0x0F); + uint8_t buffer[3]; + readRegisterRegion(buffer, BME280_TEMPERATURE_MSB_REG, 3); + int32_t adc_T = ((uint32_t)buffer[0] << 12) | ((uint32_t)buffer[1] << 4) | ((buffer[2] >> 4) & 0x0F); //By datasheet, calibrate int64_t var1, var2; @@ -287,15 +296,15 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t { case I2C_MODE: - Wire.beginTransmission(settings.I2CAddress); - Wire.write(offset); - Wire.endTransmission(); + wire -> beginTransmission(settings.I2CAddress); + wire -> write(offset); + wire -> endTransmission(); // request bytes from slave device - Wire.requestFrom(settings.I2CAddress, length); - while ( (Wire.available()) && (i < length)) // slave may send less than requested + wire -> requestFrom(settings.I2CAddress, length); + while ( (wire -> available()) && (i < length)) // slave may send less than requested { - c = Wire.read(); // receive a byte as character + c = wire -> read(); // receive a byte as character *outputPointer = c; outputPointer++; i++; @@ -332,14 +341,14 @@ uint8_t BME280::readRegister(uint8_t offset) switch (settings.commInterface) { case I2C_MODE: - Wire.beginTransmission(settings.I2CAddress); - Wire.write(offset); - Wire.endTransmission(); + wire -> beginTransmission(settings.I2CAddress); + wire -> write(offset); + wire -> endTransmission(); - Wire.requestFrom(settings.I2CAddress, numBytes); - while ( Wire.available() ) // slave may send less than requested + wire -> requestFrom(settings.I2CAddress, numBytes); + while ( wire -> available() ) // slave may send less than requested { - result = Wire.read(); // receive a byte as a proper uint8_t + result = wire -> read(); // receive a byte as a proper uint8_t } break; @@ -375,10 +384,10 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite) { case I2C_MODE: //Write the byte - Wire.beginTransmission(settings.I2CAddress); - Wire.write(offset); - Wire.write(dataToWrite); - Wire.endTransmission(); + wire -> beginTransmission(settings.I2CAddress); + wire -> write(offset); + wire -> write(dataToWrite); + wire -> endTransmission(); break; case SPI_MODE: @@ -396,4 +405,4 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite) default: break; } -} +} \ No newline at end of file diff --git a/src/SparkFunBME280.h b/src/SparkFunBME280.h index f1a53d1..55f0ff6 100644 --- a/src/SparkFunBME280.h +++ b/src/SparkFunBME280.h @@ -24,6 +24,7 @@ Distributed as-is; no warranty is given. #define __BME280_H__ #include "stdint.h" +#include "Wire.h" #define I2C_MODE 0 #define SPI_MODE 1 @@ -129,7 +130,7 @@ struct SensorCalibration uint8_t dig_H3; int16_t dig_H4; int16_t dig_H5; - uint8_t dig_H6; + int8_t dig_H6; }; @@ -150,7 +151,7 @@ class BME280 //Call to apply SensorSettings. //This also gets the SensorCalibration constants - uint8_t begin( void ); + uint8_t begin(TwoWire *theWire = &Wire); //Software reset routine void reset( void ); @@ -178,7 +179,9 @@ class BME280 int16_t readRegisterInt16( uint8_t offset ); //Writes a byte; void writeRegister(uint8_t, uint8_t); - + + private: + TwoWire *wire; };