Skip to content

Commit 51e8977

Browse files
battery_voltage: slow refresh for ADC2 because need to disable wifi
1 parent 6938650 commit 51e8977

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

internal_filesystem/lib/mpos/battery_voltage.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
# Cache to reduce WiFi interruptions (ADC2 requires WiFi to be disabled)
1111
_cached_raw_adc = None
1212
_last_read_time = 0
13-
CACHE_DURATION_MS = 30000 # 30 seconds
13+
CACHE_DURATION_ADC2_MS = 300000 # 300 seconds (expensive: requires WiFi disable)
14+
CACHE_DURATION_ADC1_MS = 30000 # 30 seconds (cheaper: no WiFi interference)
1415

1516

1617
def _is_adc2_pin(pin):
@@ -46,7 +47,7 @@ def init_adc(pinnr, sf):
4647

4748
def read_raw_adc(force_refresh=False):
4849
"""
49-
Read raw ADC value (0-4095) with caching.
50+
Read raw ADC value (0-4095) with adaptive caching.
5051
5152
On ESP32-S3 with ADC2, WiFi is temporarily disabled during reading.
5253
Raises RuntimeError if WifiService is busy (connecting/scanning) when using ADC2.
@@ -69,16 +70,19 @@ def read_raw_adc(force_refresh=False):
6970
int(MIN_VOLTAGE / scale_factor), int(MAX_VOLTAGE / scale_factor)
7071
)
7172

73+
# Check if this is an ADC2 pin (requires WiFi disable)
74+
needs_wifi_disable = adc_pin is not None and _is_adc2_pin(adc_pin)
75+
76+
# Use different cache durations based on cost
77+
cache_duration = CACHE_DURATION_ADC2_MS if needs_wifi_disable else CACHE_DURATION_ADC1_MS
78+
7279
# Check cache
7380
current_time = time.ticks_ms()
7481
if not force_refresh and _cached_raw_adc is not None:
7582
age = time.ticks_diff(current_time, _last_read_time)
76-
if age < CACHE_DURATION_MS:
83+
if age < cache_duration:
7784
return _cached_raw_adc
7885

79-
# Check if this is an ADC2 pin (requires WiFi disable)
80-
needs_wifi_disable = adc_pin is not None and _is_adc2_pin(adc_pin)
81-
8286
# Import WifiService only if needed
8387
WifiService = None
8488
if needs_wifi_disable:

internal_filesystem/lib/mpos/ui/topmenu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
CLOCK_UPDATE_INTERVAL = 1000 # 10 or even 1 ms doesn't seem to change the framerate but 100ms is enough
1313
WIFI_ICON_UPDATE_INTERVAL = 1500
14-
BATTERY_ICON_UPDATE_INTERVAL = 30000 # not too often, because on fri3d_2024, this briefly disables wifi
14+
BATTERY_ICON_UPDATE_INTERVAL = 15000 # not too often, but not too short, otherwise it takes a while to appear
1515
TEMPERATURE_UPDATE_INTERVAL = 2000
1616
MEMFREE_UPDATE_INTERVAL = 5000 # not too frequent because there's a forced gc.collect() to give it a reliable value
1717

0 commit comments

Comments
 (0)