Add MAX17048 lipo fuel gauge (#4851)

* Initial commit

* Update MAX17048Sensor.cpp

* Update EnvironmentTelemetry.cpp

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
David
2024-09-25 20:34:53 +10:00
committed by GitHub
parent 1129c92974
commit 40b3dbaa70
11 changed files with 449 additions and 19 deletions

View File

@@ -77,6 +77,15 @@ INA219Sensor ina219Sensor;
INA3221Sensor ina3221Sensor;
#endif
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
#include "modules/Telemetry/Sensor/MAX17048Sensor.h"
#include <utility>
extern std::pair<uint8_t, TwoWire *> nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1];
#if HAS_TELEMETRY && (!MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR || !MESHTASTIC_EXCLUDE_POWER_TELEMETRY)
MAX17048Sensor max17048Sensor;
#endif
#endif
#if HAS_RAKPROT && !defined(ARCH_PORTDUINO)
RAK9154Sensor rak9154Sensor;
#endif
@@ -167,6 +176,7 @@ static void adcDisable()
*/
class AnalogBatteryLevel : public HasBatteryLevel
{
public:
/**
* Battery state of charge, from 0 to 100 or -1 for unknown
*/
@@ -553,7 +563,12 @@ bool Power::analogInit()
*/
bool Power::setup()
{
bool found = axpChipInit() || analogInit();
// initialise one power sensor (only)
bool found = axpChipInit();
if (!found)
found = lipoInit();
if (!found)
found = analogInit();
#ifdef NRF_APM
found = true;
@@ -1044,4 +1059,106 @@ bool Power::axpChipInit()
#else
return false;
#endif
}
}
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
/**
* Wrapper class for an I2C MAX17048 Lipo battery sensor. If there is no
* I2C sensor present, the class falls back to analog battery sensing
*/
class LipoBatteryLevel : public AnalogBatteryLevel
{
private:
MAX17048Singleton *max17048 = nullptr;
public:
/**
* Init the I2C MAX17048 Lipo battery level sensor
*/
bool runOnce()
{
if (max17048 == nullptr) {
max17048 = MAX17048Singleton::GetInstance();
}
// try to start if the sensor has been detected
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MAX17048].first != 0) {
return max17048->runOnce(nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_MAX17048].second);
}
return false;
}
/**
* Battery state of charge, from 0 to 100 or -1 for unknown
*/
virtual int getBatteryPercent() override
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::getBatteryPercent();
return max17048->getBusBatteryPercent();
}
/**
* The raw voltage of the battery in millivolts, or NAN if unknown
*/
virtual uint16_t getBattVoltage() override
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::getBattVoltage();
return max17048->getBusVoltageMv();
}
/**
* return true if there is a battery installed in this unit
*/
virtual bool isBatteryConnect() override
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isBatteryConnect();
return max17048->isBatteryConnected();
}
/**
* return true if there is an external power source detected
*/
virtual bool isVbusIn() override
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isVbusIn();
return max17048->isExternallyPowered();
}
/**
* return true if the battery is currently charging
*/
virtual bool isCharging() override
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isCharging();
return max17048->isBatteryCharging();
}
};
LipoBatteryLevel lipoLevel;
/**
* Init the Lipo battery level sensor
*/
bool Power::lipoInit()
{
bool result = lipoLevel.runOnce();
LOG_DEBUG("Power::lipoInit lipo sensor is %s\n", result ? "ready" : "not ready yet");
batteryLevel = &lipoLevel;
return true;
}
#else
/**
* The Lipo battery level sensor is unavailable - default to AnalogBatteryLevel
*/
bool Power::lipoInit()
{
return false;
}
#endif