Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/SparkFunBME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ May 20, 2015

Resources:
Uses Wire.h for i2c operation
Uses SPI.h for SPI operation
Uses _spiPort->h for SPI operation

Development environment specifics:
Arduino IDE 1.8.5
Expand Down Expand Up @@ -87,7 +87,7 @@ uint8_t BME280::begin()

case SPI_MODE:
// start the SPI library:
SPI.begin();
_spiPort->begin();
// initialize the data ready and chip select pins:
pinMode(settings.chipSelectPin, OUTPUT);
digitalWrite(settings.chipSelectPin, HIGH);
Expand Down Expand Up @@ -138,8 +138,9 @@ uint8_t BME280::begin()
}

//Begin comm with BME280 over SPI
bool BME280::beginSPI(uint8_t csPin)
bool BME280::beginSPI(uint8_t csPin, SPIClass &spiPort)
{
_spiPort = &spiPort;
settings.chipSelectPin = csPin;
settings.commInterface = SPI_MODE;

Expand Down Expand Up @@ -691,21 +692,21 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t
break;

case SPI_MODE:
SPI.beginTransaction(settings.spiSettings);
_spiPort->beginTransaction(settings.spiSettings);
// take the chip select low to select the device:
digitalWrite(settings.chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset | 0x80); //Ored with "read request" bit
_spiPort->transfer(offset | 0x80); //Ored with "read request" bit
while ( i < length ) // slave may send less than requested
{
c = SPI.transfer(0x00); // receive a byte as character
c = _spiPort->transfer(0x00); // receive a byte as character
*outputPointer = c;
outputPointer++;
i++;
}
// take the chip select high to de-select:
digitalWrite(settings.chipSelectPin, HIGH);
SPI.endTransaction();
_spiPort->endTransaction();
break;

default:
Expand Down Expand Up @@ -799,17 +800,17 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
break;

case SPI_MODE:
SPI.beginTransaction(settings.spiSettings);
_spiPort->beginTransaction(settings.spiSettings);
// take the chip select low to select the device:
digitalWrite(settings.chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(offset & 0x7F);
_spiPort->transfer(offset & 0x7F);
// send a value of 0 to read the first byte returned:
SPI.transfer(dataToWrite);
_spiPort->transfer(dataToWrite);
// decrement the number of bytes left to read:
// take the chip select high to de-select:
digitalWrite(settings.chipSelectPin, HIGH);
SPI.endTransaction();
_spiPort->endTransaction();
break;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/SparkFunBME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class BME280
//Call to apply BME280_SensorSettings.
//This also gets the SensorCalibration constants
uint8_t begin( void );
bool beginSPI(uint8_t csPin); //Communicate using SPI
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI); //Communicate using SPI
bool beginI2C(TwoWire &wirePort = Wire); //Called when user provides Wire port

#ifdef SoftwareWire_h
Expand Down Expand Up @@ -266,6 +266,7 @@ class BME280

uint8_t _wireType = HARD_WIRE; //Default to Wire.h
TwoWire *_hardPort = NO_WIRE; //The generic connection to user's chosen I2C hardware
SPIClass *_spiPort = &SPI; //The generic connection to user's chosen SPI hardware

#ifdef SoftwareWire_h
SoftwareWire *_softPort = NO_WIRE; //Or, the generic connection to software wire port
Expand Down