INA219 charging detection

minimal implementation: if there is a configured INA219 sensor for battery monitoring we can take the current flow across the shunt resistor to know if we are charging the battery - negative milliamps indicate charging
This commit is contained in:
nebman 2024-11-02 00:46:26 +01:00 committed by Tom Fifield
parent bd3755bb33
commit f0a683985c
3 changed files with 19 additions and 0 deletions

View File

@ -414,6 +414,12 @@ class AnalogBatteryLevel : public HasBatteryLevel
#ifdef EXT_CHRG_DETECT
return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value;
#else
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
if (hasINA()) {
LOG_DEBUG("Using INA on I2C addr 0x%x for charging detection", config.power.device_battery_ina_address);
return getINACurrent() < 0;
}
#endif
return isBatteryConnect() && isVbusIn();
#endif
}
@ -461,6 +467,13 @@ class AnalogBatteryLevel : public HasBatteryLevel
return 0;
}
int16_t getINACurrent() {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getCurrentMa();
}
return 0;
}
bool hasINA()
{
if (!config.power.device_battery_ina_address) {

View File

@ -45,4 +45,9 @@ uint16_t INA219Sensor::getBusVoltageMv()
return lround(ina219.getBusVoltage_V() * 1000);
}
int16_t INA219Sensor::getCurrentMa()
{
return lround(ina219.getCurrent_mA());
}
#endif

View File

@ -20,6 +20,7 @@ class INA219Sensor : public TelemetrySensor, VoltageSensor
virtual int32_t runOnce() override;
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
virtual uint16_t getBusVoltageMv() override;
int16_t getCurrentMa();
};
#endif