Skip to content

Commit dba3ce0

Browse files
committed
convert to busio
1 parent 0c4398f commit dba3ce0

File tree

3 files changed

+52
-144
lines changed

3 files changed

+52
-144
lines changed

Adafruit_BME280.cpp

Lines changed: 46 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030

3131
#include "Adafruit_BME280.h"
3232
#include "Arduino.h"
33-
#include <SPI.h>
34-
#include <Wire.h>
3533

3634
/*!
3735
* @brief class constructor
3836
*/
39-
Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {}
37+
Adafruit_BME280::Adafruit_BME280() {}
4038

4139
/*!
4240
* @brief class constructor if using hardware SPI
@@ -45,9 +43,8 @@ Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {}
4543
* optional SPI object
4644
*/
4745
Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
48-
_cs = cspin;
49-
_mosi = _miso = _sck = -1;
50-
_spi = theSPI;
46+
spi_dev = new Adafruit_SPIDevice(cspin, 1000000, SPI_BITORDER_MSBFIRST,
47+
SPI_MODE0, theSPI);
5148
}
5249

5350
/*!
@@ -58,8 +55,9 @@ Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
5855
* @param sckpin the SCK pin to use
5956
*/
6057
Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
61-
int8_t sckpin)
62-
: _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) {}
58+
int8_t sckpin) {
59+
spi_dev = new Adafruit_SPIDevice(cspin, sckpin, misopin, mosipin);
60+
}
6361

6462
Adafruit_BME280::~Adafruit_BME280(void) {
6563
if (temp_sensor) {
@@ -80,37 +78,22 @@ Adafruit_BME280::~Adafruit_BME280(void) {
8078
* @returns true on success, false otherwise
8179
*/
8280
bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
83-
bool status = false;
84-
_i2caddr = addr;
85-
_wire = theWire;
86-
status = init();
87-
88-
return status;
81+
if (spi_dev == NULL) {
82+
i2c_dev = new Adafruit_I2CDevice(addr, theWire);
83+
if (!i2c_dev->begin())
84+
return false;
85+
} else {
86+
if (!spi_dev->begin())
87+
return false;
88+
}
89+
return init();
8990
}
9091

9192
/*!
9293
* @brief Initialise sensor with given parameters / settings
9394
* @returns true on success, false otherwise
9495
*/
9596
bool Adafruit_BME280::init() {
96-
// init I2C or SPI sensor interface
97-
if (_cs == -1) {
98-
// I2C
99-
_wire->begin();
100-
} else {
101-
digitalWrite(_cs, HIGH);
102-
pinMode(_cs, OUTPUT);
103-
if (_sck == -1) {
104-
// hardware SPI
105-
_spi->begin();
106-
} else {
107-
// software SPI
108-
pinMode(_sck, OUTPUT);
109-
pinMode(_mosi, OUTPUT);
110-
pinMode(_miso, INPUT);
111-
}
112-
}
113-
11497
// check if sensor, i.e. the chip ID is correct
11598
_sensorID = read8(BME280_REGISTER_CHIPID);
11699
if (_sensorID != 0x60)
@@ -175,50 +158,20 @@ void Adafruit_BME280::setSampling(sensor_mode mode,
175158
write8(BME280_REGISTER_CONTROL, _measReg.get());
176159
}
177160

178-
/*!
179-
* @brief Encapsulate hardware and software SPI transfer into one
180-
* function
181-
* @param x the data byte to transfer
182-
* @returns the data byte read from the device
183-
*/
184-
uint8_t Adafruit_BME280::spixfer(uint8_t x) {
185-
// hardware SPI
186-
if (_sck == -1)
187-
return _spi->transfer(x);
188-
189-
// software SPI
190-
uint8_t reply = 0;
191-
for (int i = 7; i >= 0; i--) {
192-
reply <<= 1;
193-
digitalWrite(_sck, LOW);
194-
digitalWrite(_mosi, x & (1 << i));
195-
digitalWrite(_sck, HIGH);
196-
if (digitalRead(_miso))
197-
reply |= 1;
198-
}
199-
return reply;
200-
}
201-
202161
/*!
203162
* @brief Writes an 8 bit value over I2C or SPI
204163
* @param reg the register address to write to
205164
* @param value the value to write to the register
206165
*/
207166
void Adafruit_BME280::write8(byte reg, byte value) {
208-
if (_cs == -1) {
209-
_wire->beginTransmission((uint8_t)_i2caddr);
210-
_wire->write((uint8_t)reg);
211-
_wire->write((uint8_t)value);
212-
_wire->endTransmission();
167+
byte buffer[2];
168+
buffer[1] = value;
169+
if (i2c_dev) {
170+
buffer[0] = reg;
171+
i2c_dev->write(buffer, 2);
213172
} else {
214-
if (_sck == -1)
215-
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
216-
digitalWrite(_cs, LOW);
217-
spixfer(reg & ~0x80); // write, bit 7 low
218-
spixfer(value);
219-
digitalWrite(_cs, HIGH);
220-
if (_sck == -1)
221-
_spi->endTransaction(); // release the SPI bus
173+
buffer[0] = reg & ~0x80;
174+
spi_dev->write(buffer, 2);
222175
}
223176
}
224177

@@ -228,25 +181,15 @@ void Adafruit_BME280::write8(byte reg, byte value) {
228181
* @returns the data byte read from the device
229182
*/
230183
uint8_t Adafruit_BME280::read8(byte reg) {
231-
uint8_t value;
232-
233-
if (_cs == -1) {
234-
_wire->beginTransmission((uint8_t)_i2caddr);
235-
_wire->write((uint8_t)reg);
236-
_wire->endTransmission();
237-
_wire->requestFrom((uint8_t)_i2caddr, (byte)1);
238-
value = _wire->read();
184+
uint8_t buffer[1];
185+
if (i2c_dev) {
186+
buffer[0] = uint8_t(reg);
187+
i2c_dev->write_then_read(buffer, 1, buffer, 1);
239188
} else {
240-
if (_sck == -1)
241-
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
242-
digitalWrite(_cs, LOW);
243-
spixfer(reg | 0x80); // read, bit 7 high
244-
value = spixfer(0);
245-
digitalWrite(_cs, HIGH);
246-
if (_sck == -1)
247-
_spi->endTransaction(); // release the SPI bus
189+
buffer[0] = uint8_t(reg | 0x80);
190+
spi_dev->write_then_read(buffer, 1, buffer, 1);
248191
}
249-
return value;
192+
return buffer[0];
250193
}
251194

252195
/*!
@@ -255,26 +198,16 @@ uint8_t Adafruit_BME280::read8(byte reg) {
255198
* @returns the 16 bit data value read from the device
256199
*/
257200
uint16_t Adafruit_BME280::read16(byte reg) {
258-
uint16_t value;
259-
260-
if (_cs == -1) {
261-
_wire->beginTransmission((uint8_t)_i2caddr);
262-
_wire->write((uint8_t)reg);
263-
_wire->endTransmission();
264-
_wire->requestFrom((uint8_t)_i2caddr, (byte)2);
265-
value = (_wire->read() << 8) | _wire->read();
201+
uint8_t buffer[2];
202+
203+
if (i2c_dev) {
204+
buffer[0] = uint8_t(reg);
205+
i2c_dev->write_then_read(buffer, 1, buffer, 2);
266206
} else {
267-
if (_sck == -1)
268-
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
269-
digitalWrite(_cs, LOW);
270-
spixfer(reg | 0x80); // read, bit 7 high
271-
value = (spixfer(0) << 8) | spixfer(0);
272-
digitalWrite(_cs, HIGH);
273-
if (_sck == -1)
274-
_spi->endTransaction(); // release the SPI bus
207+
buffer[0] = uint8_t(reg | 0x80);
208+
spi_dev->write_then_read(buffer, 1, buffer, 2);
275209
}
276-
277-
return value;
210+
return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
278211
}
279212

280213
/*!
@@ -309,37 +242,18 @@ int16_t Adafruit_BME280::readS16_LE(byte reg) {
309242
* @returns the 24 bit data value read from the device
310243
*/
311244
uint32_t Adafruit_BME280::read24(byte reg) {
312-
uint32_t value;
313-
314-
if (_cs == -1) {
315-
_wire->beginTransmission((uint8_t)_i2caddr);
316-
_wire->write((uint8_t)reg);
317-
_wire->endTransmission();
318-
_wire->requestFrom((uint8_t)_i2caddr, (byte)3);
319-
320-
value = _wire->read();
321-
value <<= 8;
322-
value |= _wire->read();
323-
value <<= 8;
324-
value |= _wire->read();
245+
uint8_t buffer[3];
246+
247+
if (i2c_dev) {
248+
buffer[0] = uint8_t(reg);
249+
i2c_dev->write_then_read(buffer, 1, buffer, 3);
325250
} else {
326-
if (_sck == -1)
327-
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
328-
digitalWrite(_cs, LOW);
329-
spixfer(reg | 0x80); // read, bit 7 high
330-
331-
value = spixfer(0);
332-
value <<= 8;
333-
value |= spixfer(0);
334-
value <<= 8;
335-
value |= spixfer(0);
336-
337-
digitalWrite(_cs, HIGH);
338-
if (_sck == -1)
339-
_spi->endTransaction(); // release the SPI bus
251+
buffer[0] = uint8_t(reg | 0x80);
252+
spi_dev->write_then_read(buffer, 1, buffer, 3);
340253
}
341254

342-
return value;
255+
return uint32_t(buffer[0]) << 16 | uint32_t(buffer[1]) << 8 |
256+
uint32_t(buffer[2]);
343257
}
344258

345259
/*!

Adafruit_BME280.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
#include "Arduino.h"
2525

26+
#include <Adafruit_I2CDevice.h>
27+
#include <Adafruit_SPIDevice.h>
2628
#include <Adafruit_Sensor.h>
27-
#include <SPI.h>
28-
#include <Wire.h>
2929

3030
/*!
3131
* @brief default I2C address
@@ -242,8 +242,8 @@ class Adafruit_BME280 {
242242
Adafruit_Sensor *getHumiditySensor(void);
243243

244244
protected:
245-
TwoWire *_wire; //!< pointer to a TwoWire object
246-
SPIClass *_spi; //!< pointer to SPI object
245+
Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
246+
Adafruit_SPIDevice *spi_dev = NULL; ///< Pointer to SPI bus interface
247247

248248
Adafruit_BME280_Temp *temp_sensor = NULL;
249249
//!< Adafruit_Sensor compat temperature sensor component
@@ -256,7 +256,6 @@ class Adafruit_BME280 {
256256

257257
void readCoefficients(void);
258258
bool isReadingCalibration(void);
259-
uint8_t spixfer(uint8_t x);
260259

261260
void write8(byte reg, byte value);
262261
uint8_t read8(byte reg);
@@ -272,11 +271,6 @@ class Adafruit_BME280 {
272271
//!< as this is used for temperature compensation reading
273272
//!< humidity and pressure
274273

275-
int8_t _cs; //!< for the SPI interface
276-
int8_t _mosi; //!< for the SPI interface
277-
int8_t _miso; //!< for the SPI interface
278-
int8_t _sck; //!< for the SPI interface
279-
280274
int32_t t_fine_adjust = 0; //!< add to compensate temp readings and in turn
281275
//!< to pressure and humidity readings
282276

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=Adafruit BME280 Library
2-
version=2.1.4
2+
version=2.2.0
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino library for BME280 sensors.
66
paragraph=Arduino library for BME280 humidity and pressure sensors.
77
category=Sensors
88
url=https://github.com/adafruit/Adafruit_BME280_Library
99
architectures=*
10-
depends=Adafruit Unified Sensor
10+
depends=Adafruit Unified Sensor, Adafruit BusIO

0 commit comments

Comments
 (0)