Skip to content

Commit ffe93fa

Browse files
authored
Merge pull request #56 from pschatzmann/master
Optionally Specify SPIClass in beginSPI
2 parents c42e780 + d7f0b8c commit ffe93fa

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/SparkFunBME280.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ May 20, 2015
77
88
Resources:
99
Uses Wire.h for i2c operation
10-
Uses SPI.h for SPI operation
10+
Uses _spiPort->h for SPI operation
1111
1212
Development environment specifics:
1313
Arduino IDE 1.8.5
@@ -87,7 +87,7 @@ uint8_t BME280::begin()
8787

8888
case SPI_MODE:
8989
// start the SPI library:
90-
SPI.begin();
90+
_spiPort->begin();
9191
// initialize the data ready and chip select pins:
9292
pinMode(settings.chipSelectPin, OUTPUT);
9393
digitalWrite(settings.chipSelectPin, HIGH);
@@ -138,8 +138,9 @@ uint8_t BME280::begin()
138138
}
139139

140140
//Begin comm with BME280 over SPI
141-
bool BME280::beginSPI(uint8_t csPin)
141+
bool BME280::beginSPI(uint8_t csPin, SPIClass &spiPort)
142142
{
143+
_spiPort = &spiPort;
143144
settings.chipSelectPin = csPin;
144145
settings.commInterface = SPI_MODE;
145146

@@ -691,21 +692,21 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t
691692
break;
692693

693694
case SPI_MODE:
694-
SPI.beginTransaction(settings.spiSettings);
695+
_spiPort->beginTransaction(settings.spiSettings);
695696
// take the chip select low to select the device:
696697
digitalWrite(settings.chipSelectPin, LOW);
697698
// send the device the register you want to read:
698-
SPI.transfer(offset | 0x80); //Ored with "read request" bit
699+
_spiPort->transfer(offset | 0x80); //Ored with "read request" bit
699700
while ( i < length ) // slave may send less than requested
700701
{
701-
c = SPI.transfer(0x00); // receive a byte as character
702+
c = _spiPort->transfer(0x00); // receive a byte as character
702703
*outputPointer = c;
703704
outputPointer++;
704705
i++;
705706
}
706707
// take the chip select high to de-select:
707708
digitalWrite(settings.chipSelectPin, HIGH);
708-
SPI.endTransaction();
709+
_spiPort->endTransaction();
709710
break;
710711

711712
default:
@@ -799,17 +800,17 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
799800
break;
800801

801802
case SPI_MODE:
802-
SPI.beginTransaction(settings.spiSettings);
803+
_spiPort->beginTransaction(settings.spiSettings);
803804
// take the chip select low to select the device:
804805
digitalWrite(settings.chipSelectPin, LOW);
805806
// send the device the register you want to read:
806-
SPI.transfer(offset & 0x7F);
807+
_spiPort->transfer(offset & 0x7F);
807808
// send a value of 0 to read the first byte returned:
808-
SPI.transfer(dataToWrite);
809+
_spiPort->transfer(dataToWrite);
809810
// decrement the number of bytes left to read:
810811
// take the chip select high to de-select:
811812
digitalWrite(settings.chipSelectPin, HIGH);
812-
SPI.endTransaction();
813+
_spiPort->endTransaction();
813814
break;
814815

815816
default:

src/SparkFunBME280.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class BME280
199199
//Call to apply BME280_SensorSettings.
200200
//This also gets the SensorCalibration constants
201201
uint8_t begin( void );
202-
bool beginSPI(uint8_t csPin); //Communicate using SPI
202+
bool beginSPI(uint8_t csPin, SPIClass &spiPort=SPI); //Communicate using SPI
203203
bool beginI2C(TwoWire &wirePort = Wire); //Called when user provides Wire port
204204

205205
#ifdef SoftwareWire_h
@@ -266,6 +266,7 @@ class BME280
266266

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

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

0 commit comments

Comments
 (0)