diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml new file mode 100644 index 0000000..863911e --- /dev/null +++ b/.github/workflows/githubci.yml @@ -0,0 +1,32 @@ +name: Arduino Library CI + +on: [pull_request, push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/setup-python@v1 + with: + python-version: '3.x' + - uses: actions/checkout@v2 + - uses: actions/checkout@v2 + with: + repository: adafruit/ci-arduino + path: ci + + - name: pre-install + run: bash ci/actions_install.sh + + - name: test platforms + run: python3 ci/build_platform.py main_platforms + + - name: clang + run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . + + - name: doxygen + env: + GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} + PRETTYNAME : "Adafruit BME280 Library" + run: bash ci/doxy_gen_and_deploy.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bdf8cd1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: c -sudo: false -cache: - directories: - - ~/arduino_ide - - ~/.arduino15/packages/ -git: - depth: false - quiet: true -env: - global: - - PRETTYNAME="Adafruit BME280 Arduino Library" - -before_install: - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh) - -install: - - arduino --install-library "Adafruit Unified Sensor" - -script: - - build_main_platforms - -# Generate and deploy documentation -after_success: - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/library_check.sh) - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/doxy_gen_and_deploy.sh) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index bc0bdad..3be3280 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -61,6 +61,18 @@ Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin) : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) {} +Adafruit_BME280::~Adafruit_BME280(void) { + if (temp_sensor) { + delete temp_sensor; + } + if (pressure_sensor) { + delete pressure_sensor; + } + if (humidity_sensor) { + delete humidity_sensor; + } +} + /*! * @brief Initialise sensor with given parameters / settings * @param addr the I2C address the device can be found on @@ -153,7 +165,6 @@ void Adafruit_BME280::setSampling(sensor_mode mode, // making sure sensor is in sleep mode before setting configuration // as it otherwise may be ignored write8(BME280_REGISTER_CONTROL, MODE_SLEEP); - // you must make sure to also set REGISTER_CONTROL after setting the // CONTROLHUMID register, otherwise the values won't be applied (see @@ -528,3 +539,159 @@ float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric) { * @returns Sensor ID 0x60 for BME280, 0x56, 0x57, 0x58 BMP280 */ uint32_t Adafruit_BME280::sensorID(void) { return _sensorID; } + +/*! + @brief Gets an Adafruit Unified Sensor object for the temp sensor component + @return Adafruit_Sensor pointer to temperature sensor + */ +Adafruit_Sensor *Adafruit_BME280::getTemperatureSensor(void) { + if (!temp_sensor) { + temp_sensor = new Adafruit_BME280_Temp(this); + } + + return temp_sensor; +} + +/*! + @brief Gets an Adafruit Unified Sensor object for the pressure sensor + component + @return Adafruit_Sensor pointer to pressure sensor + */ +Adafruit_Sensor *Adafruit_BME280::getPressureSensor(void) { + if (!pressure_sensor) { + pressure_sensor = new Adafruit_BME280_Pressure(this); + } + return pressure_sensor; +} + +/*! + @brief Gets an Adafruit Unified Sensor object for the humidity sensor + component + @return Adafruit_Sensor pointer to humidity sensor + */ +Adafruit_Sensor *Adafruit_BME280::getHumiditySensor(void) { + if (!humidity_sensor) { + humidity_sensor = new Adafruit_BME280_Humidity(this); + } + return humidity_sensor; +} + +/**************************************************************************/ +/*! + @brief Gets the sensor_t data for the BME280's temperature sensor +*/ +/**************************************************************************/ +void Adafruit_BME280_Temp::getSensor(sensor_t *sensor) { + /* Clear the sensor_t object */ + memset(sensor, 0, sizeof(sensor_t)); + + /* Insert the sensor name in the fixed length char array */ + strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1); + sensor->name[sizeof(sensor->name) - 1] = 0; + sensor->version = 1; + sensor->sensor_id = _sensorID; + sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; + sensor->min_delay = 0; + sensor->min_value = -40.0; /* Temperature range -40 ~ +85 C */ + sensor->max_value = +85.0; + sensor->resolution = 0.01; /* 0.01 C */ +} + +/**************************************************************************/ +/*! + @brief Gets the temperature as a standard sensor event + @param event Sensor event object that will be populated + @returns True +*/ +/**************************************************************************/ +bool Adafruit_BME280_Temp::getEvent(sensors_event_t *event) { + /* Clear the event */ + memset(event, 0, sizeof(sensors_event_t)); + + event->version = sizeof(sensors_event_t); + event->sensor_id = _sensorID; + event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; + event->timestamp = millis(); + event->temperature = _theBME280->readTemperature(); + return true; +} + +/**************************************************************************/ +/*! + @brief Gets the sensor_t data for the BME280's pressure sensor +*/ +/**************************************************************************/ +void Adafruit_BME280_Pressure::getSensor(sensor_t *sensor) { + /* Clear the sensor_t object */ + memset(sensor, 0, sizeof(sensor_t)); + + /* Insert the sensor name in the fixed length char array */ + strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1); + sensor->name[sizeof(sensor->name) - 1] = 0; + sensor->version = 1; + sensor->sensor_id = _sensorID; + sensor->type = SENSOR_TYPE_PRESSURE; + sensor->min_delay = 0; + sensor->min_value = 300.0; /* 300 ~ 1100 hPa */ + sensor->max_value = 1100.0; + sensor->resolution = 0.012; /* 0.12 hPa relative */ +} + +/**************************************************************************/ +/*! + @brief Gets the pressure as a standard sensor event + @param event Sensor event object that will be populated + @returns True +*/ +/**************************************************************************/ +bool Adafruit_BME280_Pressure::getEvent(sensors_event_t *event) { + /* Clear the event */ + memset(event, 0, sizeof(sensors_event_t)); + + event->version = sizeof(sensors_event_t); + event->sensor_id = _sensorID; + event->type = SENSOR_TYPE_PRESSURE; + event->timestamp = millis(); + event->pressure = _theBME280->readPressure() / 100; // convert Pa to hPa + return true; +} + +/**************************************************************************/ +/*! + @brief Gets the sensor_t data for the BME280's humidity sensor +*/ +/**************************************************************************/ +void Adafruit_BME280_Humidity::getSensor(sensor_t *sensor) { + /* Clear the sensor_t object */ + memset(sensor, 0, sizeof(sensor_t)); + + /* Insert the sensor name in the fixed length char array */ + strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1); + sensor->name[sizeof(sensor->name) - 1] = 0; + sensor->version = 1; + sensor->sensor_id = _sensorID; + sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY; + sensor->min_delay = 0; + sensor->min_value = 0; + sensor->max_value = 100; /* 0 - 100 % */ + sensor->resolution = 3; /* 3% accuracy */ +} + +/**************************************************************************/ +/*! + @brief Gets the humidity as a standard sensor event + @param event Sensor event object that will be populated + @returns True +*/ +/**************************************************************************/ +bool Adafruit_BME280_Humidity::getEvent(sensors_event_t *event) { + /* Clear the event */ + memset(event, 0, sizeof(sensors_event_t)); + + event->version = sizeof(sensors_event_t); + event->sensor_id = _sensorID; + event->type = SENSOR_TYPE_RELATIVE_HUMIDITY; + event->timestamp = millis(); + event->relative_humidity = _theBME280->readHumidity(); + return true; +} diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index db1cdda..5721a9a 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -105,26 +105,49 @@ typedef struct { } bme280_calib_data; /*=========================================================================*/ -/* -class Adafruit_BME280_Unified : public Adafruit_Sensor -{ - public: - Adafruit_BME280_Unified(int32_t sensorID = -1); - - bool begin(uint8_t addr = BME280_ADDRESS); - void getTemperature(float *temp); - void getPressure(float *pressure); - float pressureToAltitude(float seaLevel, float atmospheric, float temp); - float seaLevelForAltitude(float altitude, float atmospheric, float temp); - void getEvent(sensors_event_t*); - void getSensor(sensor_t*); - - private: - uint8_t _i2c_addr; - int32_t _sensorID; +class Adafruit_BME280; + +/** Adafruit Unified Sensor interface for temperature component of BME280 */ +class Adafruit_BME280_Temp : public Adafruit_Sensor { +public: + /** @brief Create an Adafruit_Sensor compatible object for the temp sensor + @param parent A pointer to the BME280 class */ + Adafruit_BME280_Temp(Adafruit_BME280 *parent) { _theBME280 = parent; } + bool getEvent(sensors_event_t *); + void getSensor(sensor_t *); + +private: + int _sensorID = 280; + Adafruit_BME280 *_theBME280 = NULL; }; -*/ +/** Adafruit Unified Sensor interface for pressure component of BME280 */ +class Adafruit_BME280_Pressure : public Adafruit_Sensor { +public: + /** @brief Create an Adafruit_Sensor compatible object for the pressure sensor + @param parent A pointer to the BME280 class */ + Adafruit_BME280_Pressure(Adafruit_BME280 *parent) { _theBME280 = parent; } + bool getEvent(sensors_event_t *); + void getSensor(sensor_t *); + +private: + int _sensorID = 280; + Adafruit_BME280 *_theBME280 = NULL; +}; + +/** Adafruit Unified Sensor interface for humidity component of BME280 */ +class Adafruit_BME280_Humidity : public Adafruit_Sensor { +public: + /** @brief Create an Adafruit_Sensor compatible object for the humidity sensor + @param parent A pointer to the BME280 class */ + Adafruit_BME280_Humidity(Adafruit_BME280 *parent) { _theBME280 = parent; } + bool getEvent(sensors_event_t *); + void getSensor(sensor_t *); + +private: + int _sensorID = 280; + Adafruit_BME280 *_theBME280 = NULL; +}; /**************************************************************************/ /*! @@ -190,10 +213,9 @@ class Adafruit_BME280 { // constructors Adafruit_BME280(); Adafruit_BME280(int8_t cspin, SPIClass *theSPI = &SPI); - Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, - int8_t sckpin); - - bool begin(uint8_t addr=BME280_ADDRESS, TwoWire *theWire=&Wire); + Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); + ~Adafruit_BME280(void); + bool begin(uint8_t addr = BME280_ADDRESS, TwoWire *theWire = &Wire); bool init(); void setSampling(sensor_mode mode = MODE_NORMAL, @@ -212,9 +234,23 @@ class Adafruit_BME280 { float seaLevelForAltitude(float altitude, float pressure); uint32_t sensorID(void); + Adafruit_Sensor *getTemperatureSensor(void); + Adafruit_Sensor *getPressureSensor(void); + Adafruit_Sensor *getHumiditySensor(void); + protected: TwoWire *_wire; //!< pointer to a TwoWire object SPIClass *_spi; //!< pointer to SPI object + + Adafruit_BME280_Temp *temp_sensor = NULL; + //!< Adafruit_Sensor compat temperature sensor component + + Adafruit_BME280_Pressure *pressure_sensor = NULL; + //!< Adafruit_Sensor compat pressure sensor component + + Adafruit_BME280_Humidity *humidity_sensor = NULL; + //!< Adafruit_Sensor compat humidity sensor component + void readCoefficients(void); bool isReadingCalibration(void); uint8_t spixfer(uint8_t x); diff --git a/README.md b/README.md index 2ba2668..397b400 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Adafruit BME280 Library [![Build Status](https://travis-ci.com/adafruit/Adafruit_BME280_Library.svg?branch=master)](https://travis-ci.com/adafruit/Adafruit_BME280_Library) +# Adafruit BME280 Library ![Build Status](https://github.com/adafruit/Adafruit_BME280_Library/workflows/Arduino%20Library%20CI/badge.svg) diff --git a/examples/advancedsettings/advancedsettings.ino b/examples/advancedsettings/advancedsettings.ino index b4f0810..df1f5eb 100644 --- a/examples/advancedsettings/advancedsettings.ino +++ b/examples/advancedsettings/advancedsettings.ino @@ -38,7 +38,7 @@ void setup() { Serial.begin(9600); Serial.println(F("BME280 test")); - if (! bme.begin(&Wire)) { + if (! bme.begin(0x77, &Wire)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } diff --git a/examples/bme280_unified/bme280_unified.ino b/examples/bme280_unified/bme280_unified.ino new file mode 100644 index 0000000..8a67af4 --- /dev/null +++ b/examples/bme280_unified/bme280_unified.ino @@ -0,0 +1,62 @@ +/*************************************************************************** + This is a library for the BME280 humidity, temperature & pressure sensor + This example shows how to take Sensor Events instead of direct readings + + Designed specifically to work with the Adafruit BME280 Breakout + ----> http://www.adafruit.com/products/2652 + + These sensors use I2C or SPI to communicate, 2 or 4 pins are required + to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products + from Adafruit! + + Written by Limor Fried & Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ + +#include +#include +#include + +Adafruit_BME280 bme; // use I2C interface +Adafruit_Sensor *bme_temp = bme.getTemperatureSensor(); +Adafruit_Sensor *bme_pressure = bme.getPressureSensor(); +Adafruit_Sensor *bme_humidity = bme.getHumiditySensor(); + +void setup() { + Serial.begin(9600); + Serial.println(F("BME280 Sensor event test")); + + if (!bme.begin()) { + Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); + while (1) delay(10); + } + + bme_temp->printSensorDetails(); + bme_pressure->printSensorDetails(); + bme_humidity->printSensorDetails(); +} + +void loop() { + sensors_event_t temp_event, pressure_event, humidity_event; + bme_temp->getEvent(&temp_event); + bme_pressure->getEvent(&pressure_event); + bme_humidity->getEvent(&humidity_event); + + Serial.print(F("Temperature = ")); + Serial.print(temp_event.temperature); + Serial.println(" *C"); + + Serial.print(F("Humidity = ")); + Serial.print(humidity_event.relative_humidity); + Serial.println(" %"); + + Serial.print(F("Pressure = ")); + Serial.print(pressure_event.pressure); + Serial.println(" hPa"); + + Serial.println(); + delay(1000); +} \ No newline at end of file diff --git a/library.properties b/library.properties index 28559a4..67d0164 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit BME280 Library -version=1.1.0 +version=2.0.0 author=Adafruit maintainer=Adafruit sentence=Arduino library for BME280 sensors.