feat: implement calibrated adc battery reading

This commit is contained in:
Adam Benhassen
2026-03-08 01:37:01 +01:00
parent cafa589fae
commit d7d71bf12f
3 changed files with 19 additions and 6 deletions

View File

@@ -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);

View File

@@ -34,6 +34,7 @@ class BatteryInterface {
bool has_max17048 = false;
bool has_ip5306 = false;
bool has_axp2101 = false;
bool has_adc_battery = false;
BatteryInterface();

View File

@@ -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