Fix PowerTelemetry initialization (#6106)

This commit is contained in:
GUVWAF 2025-02-20 22:28:01 +01:00 committed by GitHub
parent 4709d21df8
commit 4942c7b71f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,7 +31,6 @@ int32_t PowerTelemetryModule::runOnce()
doDeepSleep(nightyNightMs, true, false); doDeepSleep(nightyNightMs, true, false);
} }
uint32_t result = UINT32_MAX;
/* /*
Uncomment the preferences below if you want to use the module Uncomment the preferences below if you want to use the module
without having to configure it from the PythonAPI or WebUI. without having to configure it from the PythonAPI or WebUI.
@ -46,25 +45,33 @@ int32_t PowerTelemetryModule::runOnce()
return disable(); return disable();
} }
uint32_t sendToMeshIntervalMs = Default::getConfiguredOrDefaultMsScaled(
moduleConfig.telemetry.power_update_interval, default_telemetry_broadcast_interval_secs, numOnlineNodes);
if (firstTime) { if (firstTime) {
// This is the first time the OSThread library has called this function, so do some setup // This is the first time the OSThread library has called this function, so do some setup
firstTime = 0; firstTime = 0;
uint32_t result = UINT32_MAX;
#if HAS_TELEMETRY && !defined(ARCH_PORTDUINO) #if HAS_TELEMETRY && !defined(ARCH_PORTDUINO)
if (moduleConfig.telemetry.power_measurement_enabled) { if (moduleConfig.telemetry.power_measurement_enabled) {
LOG_INFO("Power Telemetry: init"); LOG_INFO("Power Telemetry: init");
// it's possible to have this module enabled, only for displaying values on the screen. // If sensor is already initialized by EnvironmentTelemetryModule, then we don't need to initialize it again,
// therefore, we should only enable the sensor loop if measurement is also enabled // but we need to set the result to != UINT32_MAX to avoid it being disabled
if (ina219Sensor.hasSensor() && !ina219Sensor.isInitialized()) if (ina219Sensor.hasSensor())
result = ina219Sensor.runOnce(); result = ina219Sensor.isInitialized() ? 0 : ina219Sensor.runOnce();
if (ina226Sensor.hasSensor() && !ina226Sensor.isInitialized()) if (ina226Sensor.hasSensor())
result = ina226Sensor.runOnce(); result = ina226Sensor.isInitialized() ? 0 : ina226Sensor.runOnce();
if (ina260Sensor.hasSensor() && !ina260Sensor.isInitialized()) if (ina260Sensor.hasSensor())
result = ina260Sensor.runOnce(); result = ina260Sensor.isInitialized() ? 0 : ina260Sensor.runOnce();
if (ina3221Sensor.hasSensor() && !ina3221Sensor.isInitialized()) if (ina3221Sensor.hasSensor())
result = ina3221Sensor.runOnce(); result = ina3221Sensor.isInitialized() ? 0 : ina3221Sensor.runOnce();
if (max17048Sensor.hasSensor() && !max17048Sensor.isInitialized()) if (max17048Sensor.hasSensor())
result = max17048Sensor.runOnce(); result = max17048Sensor.isInitialized() ? 0 : max17048Sensor.runOnce();
} }
// it's possible to have this module enabled, only for displaying values on the screen.
// therefore, we should only enable the sensor loop if measurement is also enabled
return result == UINT32_MAX ? disable() : setStartDelay(); return result == UINT32_MAX ? disable() : setStartDelay();
#else #else
return disable(); return disable();
@ -74,10 +81,7 @@ int32_t PowerTelemetryModule::runOnce()
if (!moduleConfig.telemetry.power_measurement_enabled) if (!moduleConfig.telemetry.power_measurement_enabled)
return disable(); return disable();
if (((lastSentToMesh == 0) || if (((lastSentToMesh == 0) || !Throttle::isWithinTimespanMs(lastSentToMesh, sendToMeshIntervalMs)) &&
!Throttle::isWithinTimespanMs(lastSentToMesh, Default::getConfiguredOrDefaultMsScaled(
moduleConfig.telemetry.power_update_interval,
default_telemetry_broadcast_interval_secs, numOnlineNodes))) &&
airTime->isTxAllowedAirUtil()) { airTime->isTxAllowedAirUtil()) {
sendTelemetry(); sendTelemetry();
lastSentToMesh = millis(); lastSentToMesh = millis();
@ -89,8 +93,9 @@ int32_t PowerTelemetryModule::runOnce()
lastSentToPhone = millis(); lastSentToPhone = millis();
} }
} }
return min(sendToPhoneIntervalMs, result); return min(sendToPhoneIntervalMs, sendToMeshIntervalMs);
} }
bool PowerTelemetryModule::wantUIFrame() bool PowerTelemetryModule::wantUIFrame()
{ {
return moduleConfig.telemetry.power_screen_enabled; return moduleConfig.telemetry.power_screen_enabled;