Added a singleton wrapper for bmp3xx

This commit is contained in:
David 2024-08-31 18:15:33 +10:00
parent a1d0af6636
commit 644e213b13
3 changed files with 91 additions and 33 deletions

View File

@ -55,7 +55,7 @@ AHT10Sensor aht10Sensor;
MLX90632Sensor mlx90632Sensor;
DFRobotLarkSensor dfRobotLarkSensor;
NAU7802Sensor nau7802Sensor;
extern BMP3XXSensor bmp3xxSensor;
BMP3XXSensor bmp3xxSensor;
#ifdef T1000X_SENSOR_EN
T1000xSensor t1000xSensor;
#endif

View File

@ -2,17 +2,11 @@
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "BMP3XXSensor.h"
#include "TelemetrySensor.h"
#include <Adafruit_BMP3XX.h>
#include <typeinfo>
BMP3XXSensor::BMP3XXSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BMP3XX, "BMP3XX"){}
BMP3XXSensor bmp3xxSensor;
void BMP3XXSensor::setup(){};
void BMP3XXSensor::setup() {}
int32_t BMP3XXSensor::runOnce()
{
@ -22,30 +16,44 @@ int32_t BMP3XXSensor::runOnce()
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
status = bmp3xx.begin_I2C(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
// Get a singleton instance and initialise the bmp3xx
if (bmp3xx == nullptr) {
bmp3xx = BMP3XXSingleton::GetInstance();
}
status = bmp3xx->begin_I2C(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
// set up oversampling and filter initialization
bmp3xx.setTemperatureOversampling(BMP3_OVERSAMPLING_4X);
bmp3xx.setPressureOversampling(BMP3_OVERSAMPLING_8X);
bmp3xx.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp3xx.setOutputDataRate(BMP3_ODR_25_HZ);
bmp3xx->setTemperatureOversampling(BMP3_OVERSAMPLING_4X);
bmp3xx->setPressureOversampling(BMP3_OVERSAMPLING_8X);
bmp3xx->setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp3xx->setOutputDataRate(BMP3_ODR_25_HZ);
// take a couple of initial readings to settle the sensor filters
for (int i = 0; i < 3; i++)
{
bmp3xx.performReading();
bmp3xx->performReading();
}
return initI2CSensor();
}
bool BMP3XXSensor::getMetrics(meshtastic_Telemetry *measurement)
{
if (bmp3xx == nullptr) {
bmp3xx = BMP3XXSingleton::GetInstance();
}
if ((int)measurement->which_variant == meshtastic_Telemetry_environment_metrics_tag)
{
bmp3xx.performReading();
measurement->variant.environment_metrics.temperature = bmp3xx.readTemperature();
measurement->variant.environment_metrics.barometric_pressure = bmp3xx.readPressure() / 100.0F;
LOG_DEBUG("BMP3XXSensor::getMetrics id: %i temp: %.1f press %.1f\n", measurement->which_variant, measurement->variant.environment_metrics.temperature, measurement->variant.environment_metrics.barometric_pressure);
bmp3xx->performReading();
measurement->variant.environment_metrics.has_temperature = true;
measurement->variant.environment_metrics.has_barometric_pressure = true;
measurement->variant.environment_metrics.has_relative_humidity = false;
measurement->variant.environment_metrics.temperature = static_cast<float>(bmp3xx->temperature);
measurement->variant.environment_metrics.barometric_pressure = static_cast<float>(bmp3xx->pressure) / 100.0F;
measurement->variant.environment_metrics.relative_humidity = 0.0f;
LOG_DEBUG("BMP3XXSensor::getMetrics id: %i temp: %.1f press %.1f\n", measurement->which_variant, measurement->variant.environment_metrics.temperature, measurement->variant.environment_metrics.barometric_pressure);
}
else
{
@ -54,9 +62,35 @@ bool BMP3XXSensor::getMetrics(meshtastic_Telemetry *measurement)
return true;
}
float BMP3XXSensor::getAltitudeAMSL()
// Get a singleton wrapper for an Adafruit_bmp3xx
BMP3XXSingleton *BMP3XXSingleton::GetInstance()
{
return bmp3xx.readAltitude(SEAL_LEVEL_HPA);
std::lock_guard<std::mutex> lock(mutex_);
if (pinstance_ == nullptr)
{
pinstance_ = new BMP3XXSingleton();
}
return pinstance_;
}
BMP3XXSingleton::BMP3XXSingleton(){}
BMP3XXSingleton::~BMP3XXSingleton(){}
BMP3XXSingleton* BMP3XXSingleton::pinstance_{nullptr};
std::mutex BMP3XXSingleton::mutex_;
bool BMP3XXSingleton::performReading()
{
bool result = Adafruit_BMP3XX::performReading();
if (result) {
double atmospheric = this->pressure / 100.0;
altitudeAmslMetres = 44330.0 * (1.0 - pow(atmospheric / SEAL_LEVEL_HPA, 0.1903));
} else {
altitudeAmslMetres = 0.0;
}
return result;
}
#endif

View File

@ -7,28 +7,52 @@
#define SEAL_LEVEL_HPA 1013.2f
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "TelemetrySensor.h"
#include <mutex>
#include <typeinfo>
#include <Adafruit_BMP3XX.h>
#include "TelemetrySensor.h"
// Singleton wrapper for the Adafruit_BMP3XX class
class BMP3XXSingleton : public Adafruit_BMP3XX
{
private:
static BMP3XXSingleton * pinstance_;
static std::mutex mutex_;
protected:
BMP3XXSingleton();
~BMP3XXSingleton();
public:
// Create a singleton instance (with lock for thread safety)
static BMP3XXSingleton *GetInstance();
// Singletons should not be cloneable.
BMP3XXSingleton(BMP3XXSingleton &other) = delete;
// Singletons should not be assignable.
void operator=(const BMP3XXSingleton &) = delete;
// Performs a full reading of all sensors in the BMP3XX. Assigns
// the internal temperature, pressure and altitudeAmsl variables
bool performReading();
// Altitude in metres above mean sea level, assigned after calling performReading()
double altitudeAmslMetres = 0.0f;
};
class BMP3XXSensor : public TelemetrySensor
{
protected:
Adafruit_BMP3XX bmp3xx;
float pressureHPa = 0.0f;
float temperatureCelcius = 0.0f;
float altitudeAmslMetres = 0.0f;
BMP3XXSingleton *bmp3xx = nullptr;
virtual void setup() override;
public:
BMP3XXSensor();
virtual void setup() override;
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
virtual float getAltitudeAMSL();
BMP3XXSensor();
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
};
extern BMP3XXSensor bmp3xxSensor;
#endif
#endif