From 51e8977b12f598121a71a1d19c9bfe60df7ece75 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 24 Nov 2025 19:49:03 +0100 Subject: [PATCH] battery_voltage: slow refresh for ADC2 because need to disable wifi --- internal_filesystem/lib/mpos/battery_voltage.py | 16 ++++++++++------ internal_filesystem/lib/mpos/ui/topmenu.py | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal_filesystem/lib/mpos/battery_voltage.py b/internal_filesystem/lib/mpos/battery_voltage.py index 9b943f63..bdc77fc0 100644 --- a/internal_filesystem/lib/mpos/battery_voltage.py +++ b/internal_filesystem/lib/mpos/battery_voltage.py @@ -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: diff --git a/internal_filesystem/lib/mpos/ui/topmenu.py b/internal_filesystem/lib/mpos/ui/topmenu.py index 715047a4..45169766 100644 --- a/internal_filesystem/lib/mpos/ui/topmenu.py +++ b/internal_filesystem/lib/mpos/ui/topmenu.py @@ -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