Files
firmware/src/modules/Telemetry/Sensor/BMP3XXSensor.cpp

89 lines
2.9 KiB
C++
Raw Normal View History

2024-08-17 16:19:39 +10:00
#include "configuration.h"
#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#include "BMP3XXSensor.h"
2024-09-02 10:23:31 +02:00
BMP3XXSensor::BMP3XXSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_BMP3XX, "BMP3XX") {}
2024-08-17 16:19:39 +10:00
2024-08-31 18:15:33 +10:00
void BMP3XXSensor::setup() {}
2024-08-17 23:01:43 +10:00
2024-08-17 16:19:39 +10:00
int32_t BMP3XXSensor::runOnce()
{
LOG_INFO("Init sensor: %s\n", sensorName);
2024-09-02 10:23:31 +02:00
if (!hasSensor()) {
2024-08-17 16:19:39 +10:00
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
2024-08-31 18:15:33 +10:00
// Get a singleton instance and initialise the bmp3xx
if (bmp3xx == nullptr) {
bmp3xx = BMP3XXSingleton::GetInstance();
}
status = bmp3xx->begin_I2C(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
2024-08-17 16:19:39 +10:00
// set up oversampling and filter initialization
2024-08-31 18:15:33 +10:00
bmp3xx->setTemperatureOversampling(BMP3_OVERSAMPLING_4X);
bmp3xx->setPressureOversampling(BMP3_OVERSAMPLING_8X);
bmp3xx->setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp3xx->setOutputDataRate(BMP3_ODR_25_HZ);
2024-08-17 16:19:39 +10:00
// take a couple of initial readings to settle the sensor filters
2024-09-02 10:23:31 +02:00
for (int i = 0; i < 3; i++) {
2024-08-31 18:15:33 +10:00
bmp3xx->performReading();
2024-08-17 16:19:39 +10:00
}
return initI2CSensor();
}
bool BMP3XXSensor::getMetrics(meshtastic_Telemetry *measurement)
{
2024-08-31 18:15:33 +10:00
if (bmp3xx == nullptr) {
bmp3xx = BMP3XXSingleton::GetInstance();
}
2024-09-02 10:23:31 +02:00
if ((int)measurement->which_variant == meshtastic_Telemetry_environment_metrics_tag) {
2024-08-31 18:15:33 +10:00
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;
2024-09-02 10:23:31 +02:00
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 {
2024-08-17 16:19:39 +10:00
LOG_DEBUG("BMP3XXSensor::getMetrics id: %i\n", measurement->which_variant);
}
return true;
}
2024-08-31 18:15:33 +10:00
// Get a singleton wrapper for an Adafruit_bmp3xx
BMP3XXSingleton *BMP3XXSingleton::GetInstance()
{
2024-09-02 10:23:31 +02:00
if (pinstance == nullptr) {
2024-08-31 19:40:54 +10:00
pinstance = new BMP3XXSingleton();
2024-08-31 18:15:33 +10:00
}
2024-08-31 19:40:54 +10:00
return pinstance;
2024-08-31 18:15:33 +10:00
}
2024-09-02 10:23:31 +02:00
BMP3XXSingleton::BMP3XXSingleton() {}
2024-08-31 18:15:33 +10:00
2024-09-02 10:23:31 +02:00
BMP3XXSingleton::~BMP3XXSingleton() {}
2024-08-31 18:15:33 +10:00
2024-09-02 10:23:31 +02:00
BMP3XXSingleton *BMP3XXSingleton::pinstance{nullptr};
2024-08-31 18:15:33 +10:00
bool BMP3XXSingleton::performReading()
2024-08-17 16:19:39 +10:00
{
2024-08-31 18:15:33 +10:00
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;
2024-08-17 16:19:39 +10:00
}
#endif