diff --git a/components/Monitoring/Monitoring/BatteryMonitor.cpp b/components/Monitoring/Monitoring/BatteryMonitor.cpp new file mode 100644 index 0000000..0be7d32 --- /dev/null +++ b/components/Monitoring/Monitoring/BatteryMonitor.cpp @@ -0,0 +1,44 @@ +#include "BatteryMonitor.hpp" + +#include + +static const char *TAG_BAT = "[BatteryMonitor]"; + +bool BatteryMonitor::setup() +{ +#if defined(CONFIG_IDF_TARGET_ESP32S3) + if (CONFIG_MONITORING_BATTERY_DIVIDER_R_TOP_OHM <= 0) + { + ESP_LOGE(TAG_BAT, "Invalid divider bottom resistor: %d", CONFIG_MONITORING_BATTERY_DIVIDER_R_TOP_OHM); + return false; + } + scale_ = 1.0f + static_cast(CONFIG_MONITORING_BATTERY_DIVIDER_R_TOP_OHM) / static_cast(CONFIG_MONITORING_BATTERY_DIVIDER_R_BOTTOM_OHM); + if (!adc_.init(CONFIG_MONITORING_BATTERY_ADC_GPIO, ADC_ATTEN_DB_12, ADC_BITWIDTH_DEFAULT, CONFIG_MONITORING_BATTERY_SAMPLES)) + { + ESP_LOGE(TAG_BAT, "Battery ADC init failed"); + return false; + } + ESP_LOGI(TAG_BAT, "Battery monitor enabled (GPIO=%d, scale=%.3f)", CONFIG_MONITORING_BATTERY_ADC_GPIO, scale_); + return true; +#else + ESP_LOGI(TAG_BAT, "Battery monitoring not supported on this target"); + return false; +#endif +} + +int BatteryMonitor::getBatteryMilliVolts() const +{ +#if defined(CONFIG_IDF_TARGET_ESP32S3) + if (!adc_.sampleOnce()) + return 0; + + const int mv_at_adc = adc_.getFilteredMilliVolts(); + if (mv_at_adc <= 0) + return 0; + + const float battery_mv = mv_at_adc * scale_; + return static_cast(std::lround(battery_mv)); +#else + return 0; +#endif +} diff --git a/components/Monitoring/Monitoring/BatteryMonitor.hpp b/components/Monitoring/Monitoring/BatteryMonitor.hpp new file mode 100644 index 0000000..fd552eb --- /dev/null +++ b/components/Monitoring/Monitoring/BatteryMonitor.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "AdcSampler.hpp" +#include + +class BatteryMonitor +{ +public: + BatteryMonitor() = default; + ~BatteryMonitor() = default; + + bool setup(); + + // Read once, update filter, and return battery voltage in mV (after divider compensation). + int getBatteryMilliVolts() const; + + // Whether monitoring is enabled by Kconfig + static constexpr bool isEnabled() + { +#ifdef CONFIG_MONITORING_BATTERY_ENABLE + return true; +#else + return false; +#endif + } + +private: + float scale_{1.0f}; + mutable AdcSampler adc_; +};