From d7d71bf12fd28df817189ecda45f64410620e6f8 Mon Sep 17 00:00:00 2001 From: Adam Benhassen Date: Sun, 8 Mar 2026 01:37:01 +0100 Subject: [PATCH] feat: implement calibrated adc battery reading --- esp32_marauder/BatteryInterface.cpp | 19 +++++++++++++++++-- esp32_marauder/BatteryInterface.h | 1 + esp32_marauder/configs.h | 5 +---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esp32_marauder/BatteryInterface.cpp b/esp32_marauder/BatteryInterface.cpp index 6dc1863..3b53c29 100644 --- a/esp32_marauder/BatteryInterface.cpp +++ b/esp32_marauder/BatteryInterface.cpp @@ -26,7 +26,13 @@ void BatteryInterface::RunSetup() { #ifdef HAS_BATTERY - #ifndef HAS_AXP2101 + #ifdef BATTERY_ADC_PIN + analogReadResolution(12); + pinMode(BATTERY_ADC_PIN, INPUT); + this->has_adc_battery = true; + this->i2c_supported = true; + Serial.println(F("Battery: ADC mode")); + #elif !defined(HAS_AXP2101) Wire.begin(I2C_SDA, I2C_SCL); Wire.beginTransmission(IP5306_ADDR); @@ -59,13 +65,22 @@ void BatteryInterface::RunSetup() { this->i2c_supported = true; this->has_axp2101 = true; #endif - + this->initTime = millis(); #endif } int8_t BatteryInterface::getBatteryLevel() { + #ifdef BATTERY_ADC_PIN + if (this->has_adc_battery) { + int voltage_mv = analogReadMilliVolts(BATTERY_ADC_PIN) * 2; // voltage divider ratio 2:1 + if (voltage_mv <= 3300) return 0; + if (voltage_mv >= 4150) return 100; + return (int8_t)(((voltage_mv - 3300) * 100) / 850); + } + #endif + if (this->has_ip5306) { Wire.beginTransmission(IP5306_ADDR); Wire.write(0x78); diff --git a/esp32_marauder/BatteryInterface.h b/esp32_marauder/BatteryInterface.h index d5748c9..0d684c2 100644 --- a/esp32_marauder/BatteryInterface.h +++ b/esp32_marauder/BatteryInterface.h @@ -34,6 +34,7 @@ class BatteryInterface { bool has_max17048 = false; bool has_ip5306 = false; bool has_axp2101 = false; + bool has_adc_battery = false; BatteryInterface(); diff --git a/esp32_marauder/configs.h b/esp32_marauder/configs.h index 7e3ba79..85f7fc4 100644 --- a/esp32_marauder/configs.h +++ b/esp32_marauder/configs.h @@ -138,6 +138,7 @@ #define HAS_MINI_KB #ifdef MARAUDER_CARDPUTER_ADV #define HAS_BATTERY + #define BATTERY_ADC_PIN 10 #endif #define HAS_BT #define HAS_BUTTONS @@ -2673,10 +2674,6 @@ #define I2C_SDA 5 #endif - #ifdef MARAUDER_CARDPUTER_ADV - #define I2C_SDA 8 - #define I2C_SCL 9 - #endif #endif //// MARAUDER TITLE STUFF