Instead of having LipoBatteryLevel forward requests to AnalogBatteryLevel if there's no Lipo sensor, just have lipoInit return false. The forwarding didn't work because it never called analogInit.

This commit is contained in:
David Huang 2024-09-27 23:35:57 -05:00
parent 36a66df923
commit 7f59cb54ef

View File

@ -1068,10 +1068,9 @@ bool Power::axpChipInit()
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
/** /**
* Wrapper class for an I2C MAX17048 Lipo battery sensor. If there is no * Wrapper class for an I2C MAX17048 Lipo battery sensor.
* I2C sensor present, the class falls back to analog battery sensing
*/ */
class LipoBatteryLevel : public AnalogBatteryLevel class LipoBatteryLevel : public HasBatteryLevel
{ {
private: private:
MAX17048Singleton *max17048 = nullptr; MAX17048Singleton *max17048 = nullptr;
@ -1096,52 +1095,27 @@ class LipoBatteryLevel : public AnalogBatteryLevel
/** /**
* Battery state of charge, from 0 to 100 or -1 for unknown * Battery state of charge, from 0 to 100 or -1 for unknown
*/ */
virtual int getBatteryPercent() override virtual int getBatteryPercent() override { return max17048->getBusBatteryPercent(); }
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::getBatteryPercent();
return max17048->getBusBatteryPercent();
}
/** /**
* The raw voltage of the battery in millivolts, or NAN if unknown * The raw voltage of the battery in millivolts, or NAN if unknown
*/ */
virtual uint16_t getBattVoltage() override virtual uint16_t getBattVoltage() override { return max17048->getBusVoltageMv(); }
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::getBattVoltage();
return max17048->getBusVoltageMv();
}
/** /**
* return true if there is a battery installed in this unit * return true if there is a battery installed in this unit
*/ */
virtual bool isBatteryConnect() override virtual bool isBatteryConnect() override { return max17048->isBatteryConnected(); }
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isBatteryConnect();
return max17048->isBatteryConnected();
}
/** /**
* return true if there is an external power source detected * return true if there is an external power source detected
*/ */
virtual bool isVbusIn() override virtual bool isVbusIn() override { return max17048->isExternallyPowered(); }
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isVbusIn();
return max17048->isExternallyPowered();
}
/** /**
* return true if the battery is currently charging * return true if the battery is currently charging
*/ */
virtual bool isCharging() override virtual bool isCharging() override { return max17048->isBatteryCharging(); }
{
if (!max17048->isInitialised())
return AnalogBatteryLevel::isCharging();
return max17048->isBatteryCharging();
}
}; };
LipoBatteryLevel lipoLevel; LipoBatteryLevel lipoLevel;
@ -1153,6 +1127,8 @@ bool Power::lipoInit()
{ {
bool result = lipoLevel.runOnce(); bool result = lipoLevel.runOnce();
LOG_DEBUG("Power::lipoInit lipo sensor is %s\n", result ? "ready" : "not ready yet"); LOG_DEBUG("Power::lipoInit lipo sensor is %s\n", result ? "ready" : "not ready yet");
if (!result)
return false;
batteryLevel = &lipoLevel; batteryLevel = &lipoLevel;
return true; return true;
} }