diff --git a/src/batterymonitor.cpp b/src/batterymonitor.cpp index 45066a2c..dff37913 100644 --- a/src/batterymonitor.cpp +++ b/src/batterymonitor.cpp @@ -43,11 +43,17 @@ void BatteryMonitor::Setup() m_Logger.error("MCP3021 not found on I2C bus"); } #endif +#if BATTERY_MONITOR == BAT_MAX17048 + if (address == 0) + { + m_Logger.error("MAX17048 not found on I2C bus"); + } +#endif } void BatteryMonitor::Loop() { - #if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 + #if BATTERY_MONITOR == BAT_EXTERNAL || BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048 auto now_ms = millis(); if (now_ms - last_battery_sample >= batterySampleRate) { @@ -99,6 +105,20 @@ void BatteryMonitor::Loop() } } #endif + #if BATTERY_MONITOR == BAT_MAX17048 + if (address > 0) + { + // Cell voltage register address 0x02 + uint16_t data; + auto status = I2Cdev::readWord(address, 0x02, &data); + if (status == 0) + { + float v = data * (78.125 / 1000000); + voltage = (voltage > 0) ? min(voltage, v) : v; + m_Logger.info("MAX17048 voltage %f", voltage); + } + } + #endif if (voltage > 0) //valid measurement { // Estimate battery level, 3.2V is 0%, 4.17V is 100% (1.0) diff --git a/src/batterymonitor.h b/src/batterymonitor.h index 7d6dc667..6572c365 100644 --- a/src/batterymonitor.h +++ b/src/batterymonitor.h @@ -74,10 +74,9 @@ class BatteryMonitor private: unsigned long last_battery_sample = 0; -#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 - // TODO: The Feather has a MAX17048 (or LC709203F on older ones) - // uint8_t address = 0x36; - uint8_t address = 0; +#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 || BATTERY_MONITOR == BAT_MAX17048 + // The Feather has a MAX17048 at 0x36 (LC709203F is used on older boards though) + uint8_t address = 0x36; #endif #if BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 uint16_t voltage_3_3 = 3000; diff --git a/src/consts.h b/src/consts.h index 8bdeba86..46f0a1d0 100644 --- a/src/consts.h +++ b/src/consts.h @@ -62,6 +62,7 @@ #define BAT_INTERNAL 2 #define BAT_MCP3021 3 #define BAT_INTERNAL_MCP3021 4 +#define BAT_MAX17048 5 #define LED_OFF 255 diff --git a/src/defines.h b/src/defines.h index c25d1f37..36a8123c 100644 --- a/src/defines.h +++ b/src/defines.h @@ -56,7 +56,9 @@ IMU_DESC_ENTRY(IMU_BMP160, PRIMARY_IMU_ADDRESS_ONE, IMU_ROTATION, PIN_IMU_SCL, P // BAT_EXTERNAL for ADC pin, // BAT_INTERNAL for internal - can detect only low battery, // BAT_MCP3021 for external ADC connected over I2C -#define BATTERY_MONITOR BAT_INTERNAL +// BAT_MAX17048 for the MAX17048 LiPo fuel gauge connected over I2C +// #define BATTERY_MONITOR BAT_MAX17048 +#define BATTERY_MONITOR BAT_MAX17048 // BAT_EXTERNAL definition override // D1 Mini boards with ESP8266 have internal resistors. For these boards you only have to adjust BATTERY_SHIELD_RESISTANCE.