From 1a6e3626f60cc42f4daadacca34337236811cf20 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Thu, 14 Nov 2024 16:51:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=98=81=EF=B8=8F=20Makes=20barometer=20facade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/include/spot.h | 1 + .../ESP32-sveltekit/peripherals/barometer.h | 54 +++++++++++++++++++ .../peripherals/magnetometer.h | 6 ++- .../ESP32-sveltekit/peripherals/peripherals.h | 54 ++++--------------- 4 files changed, 70 insertions(+), 45 deletions(-) create mode 100644 esp32/lib/ESP32-sveltekit/peripherals/barometer.h diff --git a/esp32/include/spot.h b/esp32/include/spot.h index f28a7a0..1a1905b 100644 --- a/esp32/include/spot.h +++ b/esp32/include/spot.h @@ -51,6 +51,7 @@ class Spot { void readSensors() { _peripherals.readIMU(); _peripherals.readMag(); + _peripherals.readBMP(); } // plan diff --git a/esp32/lib/ESP32-sveltekit/peripherals/barometer.h b/esp32/lib/ESP32-sveltekit/peripherals/barometer.h new file mode 100644 index 0000000..e52973e --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/peripherals/barometer.h @@ -0,0 +1,54 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +class Barometer { + public: + Barometer() : _bmp(10085) {} + + bool initialize() { + bmp_success = _bmp.begin(); + return bmp_success; + } + + bool readBarometer() { + if (!bmp_success) return false; + _bmp.getTemperature(&temperature); + sensors_event_t event; + _bmp.getEvent(&event); + pressure = event.pressure; + altitude = _bmp.pressureToAltitude(seaLevelPressure, pressure); + return true; + } + + float getPressure() { return pressure; } + + float getAltitude() { return altitude; } + + float getTemperature() { return temperature; } + + void readBarometer(JsonObject& root) { + if (!bmp_success) return; + root["pressure"] = round2(getPressure()); + root["altitude"] = round2(getAltitude()); + root["bmp_temp"] = round2(getTemperature()); + } + + bool active() { return bmp_success; } + + private: + Adafruit_BMP085_Unified _bmp; + bool bmp_success {false}; + float pressure {0}; + float altitude {0}; + float temperature {0}; + + const float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; +}; \ No newline at end of file diff --git a/esp32/lib/ESP32-sveltekit/peripherals/magnetometer.h b/esp32/lib/ESP32-sveltekit/peripherals/magnetometer.h index 3d3abc5..ed32213 100644 --- a/esp32/lib/ESP32-sveltekit/peripherals/magnetometer.h +++ b/esp32/lib/ESP32-sveltekit/peripherals/magnetometer.h @@ -12,7 +12,11 @@ class Magnetometer { public: Magnetometer() : _mag(12345) {} - bool initialize() { return _mag.begin(); } + + bool initialize() { + mag_success = _mag.begin(); + return mag_success; + } bool readMagnetometer() { if (!mag_success) return false; diff --git a/esp32/lib/ESP32-sveltekit/peripherals/peripherals.h b/esp32/lib/ESP32-sveltekit/peripherals/peripherals.h index 06c8ab2..c600512 100644 --- a/esp32/lib/ESP32-sveltekit/peripherals/peripherals.h +++ b/esp32/lib/ESP32-sveltekit/peripherals/peripherals.h @@ -15,11 +15,10 @@ #include #include -#include -#include #include #include #include +#include #define EVENT_CONFIGURATION_SETTINGS "peripheralSettings" @@ -47,9 +46,6 @@ class Peripherals : public StatefulService { : endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this), _eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, EVENT_CONFIGURATION_SETTINGS), -#if FT_ENABLED(USE_BMP) - _bmp(10085), -#endif _persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) { _accessMutex = xSemaphoreCreateMutex(); addUpdateHandler([&](const String &originId) { updatePins(); }, false); @@ -78,12 +74,8 @@ class Peripherals : public StatefulService { if (!_mag.initialize()) ESP_LOGE("IMUService", "MAG initialize failed"); #endif #if FT_ENABLED(USE_BMP) - bmp_success = _bmp.begin(); - if (!bmp_success) { - ESP_LOGE("IMUService", "BMP initialize failed"); - } + if (!_bmp.initialize()) ESP_LOGE("IMUService", "BMP initialize failed"); #endif - #if FT_ENABLED(USE_USS) _left_sonar = new NewPing(USS_LEFT_PIN, USS_LEFT_PIN, MAX_DISTANCE); _right_sonar = new NewPing(USS_RIGHT_PIN, USS_RIGHT_PIN, MAX_DISTANCE); @@ -163,35 +155,14 @@ class Peripherals : public StatefulService { return updated; } - /* BMP FUNCTIONS */ - float getAltitude() { - float altitude = -1; -#if FT_ENABLED(USE_MAG) - sensors_event_t event; - _bmp.getEvent(&event); - float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; - altitude = bmp_success && event.pressure ? _bmp.pressureToAltitude(seaLevelPressure, event.pressure) : -1; -#endif - return altitude; - } - - float getPressure() { - float pressure = -1; + bool readBMP() { + bool updated = false; #if FT_ENABLED(USE_BMP) - sensors_event_t event; - _bmp.getEvent(&event); - pressure = bmp_success && event.pressure ? event.pressure : -1; + beginTransaction(); + updated = _bmp.readBarometer(); + endTransaction(); #endif - return pressure; - } - - float getTemperature() { - float temperature = 0; -#if FT_ENABLED(USE_BMP) - _bmp.getTemperature(&temperature); - temperature = bmp_success ? temperature : -1; -#endif - return temperature; + return updated; } void readSonar() { @@ -217,11 +188,7 @@ class Peripherals : public StatefulService { _mag.readMagnetometer(root); #endif #if FT_ENABLED(USE_BMP) - if (bmp_success) { - doc["pressure"] = round2(getPressure()); - doc["altitude"] = round2(getAltitude()); - doc["bmp_temp"] = round2(getTemperature()); - } + _bmp.readBarometer(root); #endif serializeJson(doc, message); socket.emit(EVENT_IMU, message); @@ -254,8 +221,7 @@ class Peripherals : public StatefulService { Magnetometer _mag; #endif #if FT_ENABLED(USE_BMP) - Adafruit_BMP085_Unified _bmp; - bool bmp_success {false}; + Barometer _bmp; #endif #if FT_ENABLED(USE_USS) NewPing *_left_sonar;