This commit is contained in:
Michael Gjelsø 2025-06-07 23:09:19 +02:00 committed by GitHub
commit e36cf3a815
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 132 additions and 40 deletions

View File

@ -77,6 +77,8 @@ static const uint8_t ext_chrg_detect_value = EXT_CHRG_DETECT_VALUE;
#endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
#include "main.h";
uint8_t INA_ADDR_LOCAL;
#if __has_include(<Adafruit_INA219.h>)
INA219Sensor ina219Sensor;
#else
@ -461,7 +463,8 @@ class AnalogBatteryLevel : public HasBatteryLevel
if (hasINA()) {
// get current flow from INA sensor - negative value means power flowing into the battery
// default assuming BATTERY+ <--> INA_VIN+ <--> SHUNT RESISTOR <--> INA_VIN- <--> LOAD
LOG_DEBUG("Using INA on I2C addr 0x%x for charging detection", config.power.device_battery_ina_address);
// LOG_DEBUG("Using INA on I2C addr 0x%x for charging detection", config.power.device_battery_ina_address);
LOG_DEBUG("Using INA on I2C addr 0x%x for charging detection", INA_ADDR_LOCAL);
#if defined(INA_CHARGING_DETECTION_INVERT)
return getINACurrent() > 0;
#else
@ -504,8 +507,29 @@ class AnalogBatteryLevel : public HasBatteryLevel
#endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_STM32WL)
/*
Read Voltage from INA using autodetect of addr first else use addr in config.power.device_battery_ina_address
*/
uint16_t getINAVoltage()
{
if (!config.power.device_battery_ina_address) {
if (ina_found.type != ScanI2C::DeviceType::NONE) {
switch (ina_found.type) {
case ScanI2C::DeviceType::INA219:
return ina219Sensor.getBusVoltageMv();
break;
case ScanI2C::DeviceType::INA226:
return ina226Sensor.getBusVoltageMv();
break;
case ScanI2C::DeviceType::INA260:
return ina260Sensor.getBusVoltageMv();
break;
case ScanI2C::DeviceType::INA3221:
return ina3221Sensor.getBusVoltageMv();
break;
}
}
} else if (config.power.device_battery_ina_address) {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getBusVoltageMv();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA226].first ==
@ -518,11 +542,29 @@ class AnalogBatteryLevel : public HasBatteryLevel
config.power.device_battery_ina_address) {
return ina3221Sensor.getBusVoltageMv();
}
}
return 0;
}
/*
Read Current from INA using autodetect of addr first else use addr in config.power.device_battery_ina_address
*/
int16_t getINACurrent()
{
if (!config.power.device_battery_ina_address) {
if (ina_found.type != ScanI2C::DeviceType::NONE) {
switch (ina_found.type) {
case ScanI2C::DeviceType::INA219:
return ina219Sensor.getCurrentMa();
case ScanI2C::DeviceType::INA226:
return ina226Sensor.getCurrentMa();
case ScanI2C::DeviceType::INA3221:
return ina3221Sensor.getCurrentMa();
}
}
} else if (config.power.device_battery_ina_address) {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getCurrentMa();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA226].first ==
@ -532,14 +574,43 @@ class AnalogBatteryLevel : public HasBatteryLevel
config.power.device_battery_ina_address) {
return ina3221Sensor.getCurrentMa();
}
}
return 0;
}
bool hasINA()
/*
Use autodetect of addr first else use addr in config.power.device_battery_ina_address
Store INA addr in local variable for later use.
*/
{
if (!config.power.device_battery_ina_address) {
return false;
if (ina_found.type != ScanI2C::DeviceType::NONE) {
INA_ADDR_LOCAL = ina_found.address.address;
switch (ina_found.type) {
case ScanI2C::DeviceType::INA219:
if (!ina219Sensor.isInitialized())
return ina219Sensor.runOnce() > 0;
return ina219Sensor.isRunning();
case ScanI2C::DeviceType::INA226:
if (!ina226Sensor.isInitialized())
return ina226Sensor.runOnce() > 0;
return ina226Sensor.isRunning();
case ScanI2C::DeviceType::INA260:
if (!ina260Sensor.isInitialized())
return ina260Sensor.runOnce() > 0;
return ina260Sensor.isRunning();
case ScanI2C::DeviceType::INA3221:
if (!ina3221Sensor.isInitialized())
return ina3221Sensor.runOnce() > 0;
return ina3221Sensor.isRunning();
}
}
} else if (config.power.device_battery_ina_address) {
INA_ADDR_LOCAL = config.power.device_battery_ina_address;
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
if (!ina219Sensor.isInitialized())
return ina219Sensor.runOnce() > 0;
@ -559,6 +630,8 @@ class AnalogBatteryLevel : public HasBatteryLevel
return ina3221Sensor.runOnce() > 0;
return ina3221Sensor.isRunning();
}
}
INA_ADDR_LOCAL = 0;
return false;
}
#endif

View File

@ -41,6 +41,12 @@ ScanI2C::FoundDevice ScanI2C::firstAccelerometer() const
return firstOfOrNONE(8, types);
}
ScanI2C::FoundDevice ScanI2C::firstINA() const
{
ScanI2C::DeviceType types[] = {INA219, INA226, INA260, INA3221};
return firstOfOrNONE(4, types);
}
ScanI2C::FoundDevice ScanI2C::firstRGBLED() const
{
ScanI2C::DeviceType types[] = {NCP5623, LP5562};

View File

@ -124,6 +124,8 @@ class ScanI2C
FoundDevice firstAccelerometer() const;
FoundDevice firstINA() const;
FoundDevice firstRGBLED() const;
virtual FoundDevice find(DeviceType) const;

View File

@ -176,6 +176,9 @@ ScanI2C::DeviceAddress rtc_found = ScanI2C::ADDRESS_NONE;
ScanI2C::DeviceAddress accelerometer_found = ScanI2C::ADDRESS_NONE;
// The I2C address of the RGB LED (if found)
ScanI2C::FoundDevice rgb_found = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ScanI2C::ADDRESS_NONE);
// The I2C address of the INA Module (if found)
ScanI2C::FoundDevice ina_found = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ScanI2C::ADDRESS_NONE);
ScanI2C::DeviceAddress ina_Address = ScanI2C::ADDRESS_NONE;
#ifdef T_WATCH_S3
Adafruit_DRV2605 drv;
@ -685,6 +688,12 @@ void setup()
LOG_DEBUG("acc_info = %i", acc_info.type);
#endif
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && !defined(ARCH_PORTDUINO)
auto ina_Info = i2cScanner->firstINA();
// ina_Address = ina_Info.type != ScanI2C::DeviceType::NONE ? ina_Info.address : ina_Address;
LOG_DEBUG("ina_info = %i", ina_Info.type);
#endif
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::BME_680, meshtastic_TelemetrySensorType_BME680);
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::BME_280, meshtastic_TelemetrySensorType_BME280);
scannerToSensorsMap(i2cScanner, ScanI2C::DeviceType::BMP_280, meshtastic_TelemetrySensorType_BMP280);

View File

@ -34,6 +34,8 @@ extern uint8_t kb_model;
extern ScanI2C::DeviceAddress rtc_found;
extern ScanI2C::DeviceAddress accelerometer_found;
extern ScanI2C::FoundDevice rgb_found;
// extern ScanI2C::DeviceAddress ina_Address;
extern ScanI2C::FoundDevice ina_found;
extern bool eink_found;
extern bool pmu_found;