battery_voltage: slow refresh for ADC2 because need to disable wifi

This commit is contained in:
Thomas Farstrike
2025-11-24 19:49:03 +01:00
parent 69386509e2
commit 51e8977b12
2 changed files with 11 additions and 7 deletions
@@ -10,7 +10,8 @@ adc_pin = None
# Cache to reduce WiFi interruptions (ADC2 requires WiFi to be disabled)
_cached_raw_adc = None
_last_read_time = 0
CACHE_DURATION_MS = 30000 # 30 seconds
CACHE_DURATION_ADC2_MS = 300000 # 300 seconds (expensive: requires WiFi disable)
CACHE_DURATION_ADC1_MS = 30000 # 30 seconds (cheaper: no WiFi interference)
def _is_adc2_pin(pin):
@@ -46,7 +47,7 @@ def init_adc(pinnr, sf):
def read_raw_adc(force_refresh=False):
"""
Read raw ADC value (0-4095) with caching.
Read raw ADC value (0-4095) with adaptive caching.
On ESP32-S3 with ADC2, WiFi is temporarily disabled during reading.
Raises RuntimeError if WifiService is busy (connecting/scanning) when using ADC2.
@@ -69,16 +70,19 @@ def read_raw_adc(force_refresh=False):
int(MIN_VOLTAGE / scale_factor), int(MAX_VOLTAGE / scale_factor)
)
# Check if this is an ADC2 pin (requires WiFi disable)
needs_wifi_disable = adc_pin is not None and _is_adc2_pin(adc_pin)
# Use different cache durations based on cost
cache_duration = CACHE_DURATION_ADC2_MS if needs_wifi_disable else CACHE_DURATION_ADC1_MS
# Check cache
current_time = time.ticks_ms()
if not force_refresh and _cached_raw_adc is not None:
age = time.ticks_diff(current_time, _last_read_time)
if age < CACHE_DURATION_MS:
if age < cache_duration:
return _cached_raw_adc
# Check if this is an ADC2 pin (requires WiFi disable)
needs_wifi_disable = adc_pin is not None and _is_adc2_pin(adc_pin)
# Import WifiService only if needed
WifiService = None
if needs_wifi_disable:
+1 -1
View File
@@ -11,7 +11,7 @@ NOTIFICATION_BAR_HEIGHT=24
CLOCK_UPDATE_INTERVAL = 1000 # 10 or even 1 ms doesn't seem to change the framerate but 100ms is enough
WIFI_ICON_UPDATE_INTERVAL = 1500
BATTERY_ICON_UPDATE_INTERVAL = 30000 # not too often, because on fri3d_2024, this briefly disables wifi
BATTERY_ICON_UPDATE_INTERVAL = 15000 # not too often, but not too short, otherwise it takes a while to appear
TEMPERATURE_UPDATE_INTERVAL = 2000
MEMFREE_UPDATE_INTERVAL = 5000 # not too frequent because there's a forced gc.collect() to give it a reliable value