diff --git a/src/Power.cpp b/src/Power.cpp index ab09cd08c..56fac6cd2 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -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) { diff --git a/src/modules/Telemetry/Sensor/INA219Sensor.cpp b/src/modules/Telemetry/Sensor/INA219Sensor.cpp index de69163b4..ea47e265d 100644 --- a/src/modules/Telemetry/Sensor/INA219Sensor.cpp +++ b/src/modules/Telemetry/Sensor/INA219Sensor.cpp @@ -45,4 +45,9 @@ uint16_t INA219Sensor::getBusVoltageMv() return lround(ina219.getBusVoltage_V() * 1000); } +int16_t INA219Sensor::getCurrentMa() +{ + return lround(ina219.getCurrent_mA()); +} + #endif \ No newline at end of file diff --git a/src/modules/Telemetry/Sensor/INA219Sensor.h b/src/modules/Telemetry/Sensor/INA219Sensor.h index 9dded067b..472174df7 100644 --- a/src/modules/Telemetry/Sensor/INA219Sensor.h +++ b/src/modules/Telemetry/Sensor/INA219Sensor.h @@ -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 \ No newline at end of file