Skip to content

Commit f4bd4d0

Browse files
Improve wifi handling
1 parent 6137998 commit f4bd4d0

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

internal_filesystem/lib/mpos/battery_voltage.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,11 @@ def read_raw_adc(force_refresh=False):
8787
except ImportError:
8888
pass
8989

90-
# Check if WiFi operations are in progress
91-
if WifiService and WifiService.wifi_busy:
92-
raise RuntimeError("Cannot read battery voltage: WifiService is busy")
93-
94-
# Disable WiFi for ADC2 reading
95-
wifi_was_connected = False
90+
# Temporarily disable WiFi for ADC2 reading
91+
was_connected = False
9692
if needs_wifi_disable and WifiService:
97-
wifi_was_connected = WifiService.is_connected()
98-
WifiService.wifi_busy = True
99-
WifiService.disconnect()
93+
# This will raise RuntimeError if WiFi is already busy
94+
was_connected = WifiService.temporarily_disable()
10095
time.sleep(0.05) # Brief delay for WiFi to fully disable
10196

10297
try:
@@ -113,14 +108,7 @@ def read_raw_adc(force_refresh=False):
113108
finally:
114109
# Re-enable WiFi (only if we disabled it)
115110
if needs_wifi_disable and WifiService:
116-
WifiService.wifi_busy = False
117-
if wifi_was_connected:
118-
# Trigger reconnection in background thread
119-
try:
120-
import _thread
121-
_thread.start_new_thread(WifiService.auto_connect, ())
122-
except Exception as e:
123-
print(f"battery_voltage: Failed to start reconnect thread: {e}")
111+
WifiService.temporarily_enable(was_connected)
124112

125113

126114
def read_battery_voltage(force_refresh=False):

internal_filesystem/lib/mpos/board/fri3d_2024.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,24 @@ def keypad_read_cb(indev, data):
260260
# Battery voltage ADC measuring
261261
# NOTE: GPIO13 is on ADC2, which requires WiFi to be disabled during reading on ESP32-S3.
262262
# battery_voltage.py handles this automatically: disables WiFi, reads ADC, reconnects WiFi.
263-
# Readings are cached for 30 seconds to minimize WiFi interruptions.
264263
import mpos.battery_voltage
265-
mpos.battery_voltage.init_adc(13, 3.3 * 2 / 4095)
264+
"""
265+
best fit on battery power:
266+
2482 is 4.180
267+
2470 is 4.170
268+
2457 is 4.147
269+
2433 is 4.109
270+
2429 is 4.102
271+
2393 is 4.044
272+
2369 is 4.000
273+
2343 is 3.957
274+
2319 is 3.916
275+
2269 is 3.831
276+
"""
277+
def adc_to_voltage(adc_value):
278+
return (-0.0016237 * adc_value + 8.2035)
279+
#mpos.battery_voltage.init_adc(13, adc_to_voltage)
280+
mpos.battery_voltage.init_adc(13, 1/616) # simple scaling has an error of ~0.01V vs the adc_to_voltage() method
266281

267282
import mpos.sdcard
268283
mpos.sdcard.init(spi_bus, cs_pin=14)

internal_filesystem/lib/mpos/net/wifi_service.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,63 @@ def auto_connect(network_module=None, time_module=None):
197197
WifiService.wifi_busy = False
198198
print("WifiService: Auto-connect thread finished")
199199

200+
@staticmethod
201+
def temporarily_disable(network_module=None):
202+
"""
203+
Temporarily disable WiFi for operations that require it (e.g., ESP32-S3 ADC2).
204+
205+
This method sets wifi_busy flag and disconnects WiFi if connected.
206+
Caller must call temporarily_enable() in a finally block.
207+
208+
Args:
209+
network_module: Network module for dependency injection (testing)
210+
211+
Returns:
212+
bool: True if WiFi was connected before disabling, False otherwise
213+
214+
Raises:
215+
RuntimeError: If WiFi operations are already in progress
216+
"""
217+
if WifiService.wifi_busy:
218+
raise RuntimeError("Cannot disable WiFi: WifiService is already busy")
219+
220+
# Check actual connection status BEFORE setting wifi_busy
221+
was_connected = False
222+
if HAS_NETWORK_MODULE or network_module:
223+
try:
224+
net = network_module if network_module else network
225+
wlan = net.WLAN(net.STA_IF)
226+
was_connected = wlan.isconnected()
227+
except Exception as e:
228+
print(f"WifiService: Error checking connection: {e}")
229+
230+
# Now set busy flag and disconnect
231+
WifiService.wifi_busy = True
232+
WifiService.disconnect(network_module=network_module)
233+
234+
return was_connected
235+
236+
@staticmethod
237+
def temporarily_enable(was_connected, network_module=None):
238+
"""
239+
Re-enable WiFi after temporary disable operation.
240+
241+
Must be called in a finally block after temporarily_disable().
242+
243+
Args:
244+
was_connected: Return value from temporarily_disable()
245+
network_module: Network module for dependency injection (testing)
246+
"""
247+
WifiService.wifi_busy = False
248+
249+
# Only reconnect if WiFi was connected before we disabled it
250+
if was_connected:
251+
try:
252+
import _thread
253+
_thread.start_new_thread(WifiService.auto_connect, ())
254+
except Exception as e:
255+
print(f"WifiService: Failed to start reconnect thread: {e}")
256+
200257
@staticmethod
201258
def is_connected(network_module=None):
202259
"""

0 commit comments

Comments
 (0)