Merge pull request #97 from caternuson/iss23_busio

Convert to BusIO
This commit is contained in:
Carter Nelson 2021-07-31 14:30:59 -07:00 committed by GitHub
commit 587329ab35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 142 deletions

View file

@ -30,13 +30,11 @@
#include "Adafruit_BME280.h" #include "Adafruit_BME280.h"
#include "Arduino.h" #include "Arduino.h"
#include <SPI.h>
#include <Wire.h>
/*! /*!
* @brief class constructor * @brief class constructor
*/ */
Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {} Adafruit_BME280::Adafruit_BME280() {}
/*! /*!
* @brief class constructor if using hardware SPI * @brief class constructor if using hardware SPI
@ -45,9 +43,8 @@ Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {}
* optional SPI object * optional SPI object
*/ */
Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) { Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
_cs = cspin; spi_dev = new Adafruit_SPIDevice(cspin, 1000000, SPI_BITORDER_MSBFIRST,
_mosi = _miso = _sck = -1; SPI_MODE0, theSPI);
_spi = theSPI;
} }
/*! /*!
@ -58,8 +55,9 @@ Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
* @param sckpin the SCK pin to use * @param sckpin the SCK pin to use
*/ */
Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
int8_t sckpin) int8_t sckpin) {
: _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) {} spi_dev = new Adafruit_SPIDevice(cspin, sckpin, misopin, mosipin);
}
Adafruit_BME280::~Adafruit_BME280(void) { Adafruit_BME280::~Adafruit_BME280(void) {
if (temp_sensor) { if (temp_sensor) {
@ -80,12 +78,15 @@ Adafruit_BME280::~Adafruit_BME280(void) {
* @returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) { bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
bool status = false; if (spi_dev == NULL) {
_i2caddr = addr; i2c_dev = new Adafruit_I2CDevice(addr, theWire);
_wire = theWire; if (!i2c_dev->begin())
status = init(); return false;
} else {
return status; if (!spi_dev->begin())
return false;
}
return init();
} }
/*! /*!
@ -93,24 +94,6 @@ bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
* @returns true on success, false otherwise * @returns true on success, false otherwise
*/ */
bool Adafruit_BME280::init() { bool Adafruit_BME280::init() {
// init I2C or SPI sensor interface
if (_cs == -1) {
// I2C
_wire->begin();
} else {
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
if (_sck == -1) {
// hardware SPI
_spi->begin();
} else {
// software SPI
pinMode(_sck, OUTPUT);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
}
}
// check if sensor, i.e. the chip ID is correct // check if sensor, i.e. the chip ID is correct
_sensorID = read8(BME280_REGISTER_CHIPID); _sensorID = read8(BME280_REGISTER_CHIPID);
if (_sensorID != 0x60) if (_sensorID != 0x60)
@ -175,50 +158,20 @@ void Adafruit_BME280::setSampling(sensor_mode mode,
write8(BME280_REGISTER_CONTROL, _measReg.get()); write8(BME280_REGISTER_CONTROL, _measReg.get());
} }
/*!
* @brief Encapsulate hardware and software SPI transfer into one
* function
* @param x the data byte to transfer
* @returns the data byte read from the device
*/
uint8_t Adafruit_BME280::spixfer(uint8_t x) {
// hardware SPI
if (_sck == -1)
return _spi->transfer(x);
// software SPI
uint8_t reply = 0;
for (int i = 7; i >= 0; i--) {
reply <<= 1;
digitalWrite(_sck, LOW);
digitalWrite(_mosi, x & (1 << i));
digitalWrite(_sck, HIGH);
if (digitalRead(_miso))
reply |= 1;
}
return reply;
}
/*! /*!
* @brief Writes an 8 bit value over I2C or SPI * @brief Writes an 8 bit value over I2C or SPI
* @param reg the register address to write to * @param reg the register address to write to
* @param value the value to write to the register * @param value the value to write to the register
*/ */
void Adafruit_BME280::write8(byte reg, byte value) { void Adafruit_BME280::write8(byte reg, byte value) {
if (_cs == -1) { byte buffer[2];
_wire->beginTransmission((uint8_t)_i2caddr); buffer[1] = value;
_wire->write((uint8_t)reg); if (i2c_dev) {
_wire->write((uint8_t)value); buffer[0] = reg;
_wire->endTransmission(); i2c_dev->write(buffer, 2);
} else { } else {
if (_sck == -1) buffer[0] = reg & ~0x80;
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); spi_dev->write(buffer, 2);
digitalWrite(_cs, LOW);
spixfer(reg & ~0x80); // write, bit 7 low
spixfer(value);
digitalWrite(_cs, HIGH);
if (_sck == -1)
_spi->endTransaction(); // release the SPI bus
} }
} }
@ -228,25 +181,15 @@ void Adafruit_BME280::write8(byte reg, byte value) {
* @returns the data byte read from the device * @returns the data byte read from the device
*/ */
uint8_t Adafruit_BME280::read8(byte reg) { uint8_t Adafruit_BME280::read8(byte reg) {
uint8_t value; uint8_t buffer[1];
if (i2c_dev) {
if (_cs == -1) { buffer[0] = uint8_t(reg);
_wire->beginTransmission((uint8_t)_i2caddr); i2c_dev->write_then_read(buffer, 1, buffer, 1);
_wire->write((uint8_t)reg);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (byte)1);
value = _wire->read();
} else { } else {
if (_sck == -1) buffer[0] = uint8_t(reg | 0x80);
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); spi_dev->write_then_read(buffer, 1, buffer, 1);
digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high
value = spixfer(0);
digitalWrite(_cs, HIGH);
if (_sck == -1)
_spi->endTransaction(); // release the SPI bus
} }
return value; return buffer[0];
} }
/*! /*!
@ -255,26 +198,16 @@ uint8_t Adafruit_BME280::read8(byte reg) {
* @returns the 16 bit data value read from the device * @returns the 16 bit data value read from the device
*/ */
uint16_t Adafruit_BME280::read16(byte reg) { uint16_t Adafruit_BME280::read16(byte reg) {
uint16_t value; uint8_t buffer[2];
if (_cs == -1) { if (i2c_dev) {
_wire->beginTransmission((uint8_t)_i2caddr); buffer[0] = uint8_t(reg);
_wire->write((uint8_t)reg); i2c_dev->write_then_read(buffer, 1, buffer, 2);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (byte)2);
value = (_wire->read() << 8) | _wire->read();
} else { } else {
if (_sck == -1) buffer[0] = uint8_t(reg | 0x80);
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); spi_dev->write_then_read(buffer, 1, buffer, 2);
digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high
value = (spixfer(0) << 8) | spixfer(0);
digitalWrite(_cs, HIGH);
if (_sck == -1)
_spi->endTransaction(); // release the SPI bus
} }
return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
return value;
} }
/*! /*!
@ -309,37 +242,18 @@ int16_t Adafruit_BME280::readS16_LE(byte reg) {
* @returns the 24 bit data value read from the device * @returns the 24 bit data value read from the device
*/ */
uint32_t Adafruit_BME280::read24(byte reg) { uint32_t Adafruit_BME280::read24(byte reg) {
uint32_t value; uint8_t buffer[3];
if (_cs == -1) { if (i2c_dev) {
_wire->beginTransmission((uint8_t)_i2caddr); buffer[0] = uint8_t(reg);
_wire->write((uint8_t)reg); i2c_dev->write_then_read(buffer, 1, buffer, 3);
_wire->endTransmission();
_wire->requestFrom((uint8_t)_i2caddr, (byte)3);
value = _wire->read();
value <<= 8;
value |= _wire->read();
value <<= 8;
value |= _wire->read();
} else { } else {
if (_sck == -1) buffer[0] = uint8_t(reg | 0x80);
_spi->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); spi_dev->write_then_read(buffer, 1, buffer, 3);
digitalWrite(_cs, LOW);
spixfer(reg | 0x80); // read, bit 7 high
value = spixfer(0);
value <<= 8;
value |= spixfer(0);
value <<= 8;
value |= spixfer(0);
digitalWrite(_cs, HIGH);
if (_sck == -1)
_spi->endTransaction(); // release the SPI bus
} }
return value; return uint32_t(buffer[0]) << 16 | uint32_t(buffer[1]) << 8 |
uint32_t(buffer[2]);
} }
/*! /*!

View file

@ -23,9 +23,9 @@
#include "Arduino.h" #include "Arduino.h"
#include <Adafruit_I2CDevice.h>
#include <Adafruit_SPIDevice.h>
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <SPI.h>
#include <Wire.h>
/*! /*!
* @brief default I2C address * @brief default I2C address
@ -242,8 +242,8 @@ public:
Adafruit_Sensor *getHumiditySensor(void); Adafruit_Sensor *getHumiditySensor(void);
protected: protected:
TwoWire *_wire; //!< pointer to a TwoWire object Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
SPIClass *_spi; //!< pointer to SPI object Adafruit_SPIDevice *spi_dev = NULL; ///< Pointer to SPI bus interface
Adafruit_BME280_Temp *temp_sensor = NULL; Adafruit_BME280_Temp *temp_sensor = NULL;
//!< Adafruit_Sensor compat temperature sensor component //!< Adafruit_Sensor compat temperature sensor component
@ -256,7 +256,6 @@ protected:
void readCoefficients(void); void readCoefficients(void);
bool isReadingCalibration(void); bool isReadingCalibration(void);
uint8_t spixfer(uint8_t x);
void write8(byte reg, byte value); void write8(byte reg, byte value);
uint8_t read8(byte reg); uint8_t read8(byte reg);
@ -272,11 +271,6 @@ protected:
//!< as this is used for temperature compensation reading //!< as this is used for temperature compensation reading
//!< humidity and pressure //!< humidity and pressure
int8_t _cs; //!< for the SPI interface
int8_t _mosi; //!< for the SPI interface
int8_t _miso; //!< for the SPI interface
int8_t _sck; //!< for the SPI interface
int32_t t_fine_adjust = 0; //!< add to compensate temp readings and in turn int32_t t_fine_adjust = 0; //!< add to compensate temp readings and in turn
//!< to pressure and humidity readings //!< to pressure and humidity readings

View file

@ -1,5 +1,5 @@
name=Adafruit BME280 Library name=Adafruit BME280 Library
version=2.1.4 version=2.2.0
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for BME280 sensors. sentence=Arduino library for BME280 sensors.
@ -7,4 +7,4 @@ paragraph=Arduino library for BME280 humidity and pressure sensors.
category=Sensors category=Sensors
url=https://github.com/adafruit/Adafruit_BME280_Library url=https://github.com/adafruit/Adafruit_BME280_Library
architectures=* architectures=*
depends=Adafruit Unified Sensor depends=Adafruit Unified Sensor, Adafruit BusIO