From 9c75a42a5c1264fa060d8762e8c13ae44fef2ce4 Mon Sep 17 00:00:00 2001 From: Lady Ada Date: Sun, 29 Dec 2019 02:54:44 -0500 Subject: [PATCH] clang & add temp/pressure Adafruit_Sensor components --- Adafruit_BME280.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++- Adafruit_BME280.h | 53 +++++++++++++++--------- 2 files changed, 131 insertions(+), 20 deletions(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index bc0bdad..e716c2a 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -153,7 +153,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 +527,100 @@ 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) { + 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) { + return pressure_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->max_value = -40.0; /* Temperature range -40 ~ +85 C */ + sensor->min_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->max_value = 300.0; /* 300 ~ 1100 hPa */ + sensor->min_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; +} diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index db1cdda..552b6b8 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -105,26 +105,35 @@ typedef struct { } bme280_calib_data; /*=========================================================================*/ -/* -class Adafruit_BME280_Unified : public Adafruit_Sensor -{ - public: - Adafruit_BME280_Unified(int32_t sensorID = -1); +class Adafruit_BME280; - 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*); +/** 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: - uint8_t _i2c_addr; - int32_t _sensorID; +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 = 0; + Adafruit_BME280 *_theBME280 = NULL; +}; /**************************************************************************/ /*! @@ -190,10 +199,9 @@ public: // 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); + Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); - bool begin(uint8_t addr=BME280_ADDRESS, TwoWire *theWire=&Wire); + bool begin(uint8_t addr = BME280_ADDRESS, TwoWire *theWire = &Wire); bool init(); void setSampling(sensor_mode mode = MODE_NORMAL, @@ -212,9 +220,16 @@ public: float seaLevelForAltitude(float altitude, float pressure); uint32_t sensorID(void); + Adafruit_Sensor *getTemperatureSensor(void); + Adafruit_Sensor *getPressureSensor(void); + protected: TwoWire *_wire; //!< pointer to a TwoWire object SPIClass *_spi; //!< pointer to SPI object + + Adafruit_BME280_Temp *temp_sensor = NULL; + Adafruit_BME280_Pressure *pressure_sensor = NULL; + void readCoefficients(void); bool isReadingCalibration(void); uint8_t spixfer(uint8_t x);