6666# mpremote helpers
6767# ---------------------------------------------------------------------------
6868
69+
6970def _mpremote (args : list , timeout : float = 30.0 ):
7071 """Run an mpremote command, return (stdout, stderr). Raises on timeout."""
7172 try :
@@ -214,7 +215,12 @@ def find_port() -> str:
214215 # Fall back to any USB serial port that isn't Bluetooth/wlan
215216 for line in stdout .splitlines ():
216217 parts = line .split ()
217- if parts and parts [0 ].startswith ("/dev/" ) and "Bluetooth" not in line and "wlan" not in line :
218+ if (
219+ parts
220+ and parts [0 ].startswith ("/dev/" )
221+ and "Bluetooth" not in line
222+ and "wlan" not in line
223+ ):
218224 return parts [0 ]
219225 raise RuntimeError (
220226 "No board detected. Connect the board and/or pass --port.\n "
@@ -225,16 +231,19 @@ def find_port() -> str:
225231def find_circuitpy () -> str | None :
226232 """Return the path to the mounted CIRCUITPY volume, or None if not found."""
227233 import platform
234+
228235 system = platform .system ()
229236
230237 if system == "Darwin" :
231238 # Glob so a second CIRCUITPY volume (mounted as "CIRCUITPY 1") is found.
232239 import glob
240+
233241 candidates = sorted (glob .glob ("/Volumes/CIRCUITPY*" ))
234242 elif system == "Windows" :
235243 # Scan all drive letters for a CIRCUITPY volume label.
236244 import string
237245 import ctypes
246+
238247 candidates = []
239248 kernel32 = ctypes .windll .kernel32 # type: ignore[attr-defined]
240249 buf = ctypes .create_unicode_buffer (256 )
@@ -248,6 +257,7 @@ def find_circuitpy() -> str | None:
248257 candidates = ["/media/CIRCUITPY" , "/run/media/CIRCUITPY" ]
249258 try :
250259 import pwd
260+
251261 user = pwd .getpwuid (os .getuid ()).pw_name
252262 candidates .insert (0 , f"/run/media/{ user } /CIRCUITPY" )
253263 candidates .insert (0 , f"/media/{ user } /CIRCUITPY" )
@@ -267,10 +277,9 @@ def copy_files(port: str, circuitpy: str | None = None):
267277 print (f"Copying files to board via { mount } ..." )
268278 else :
269279 print ("Copying files to board via mpremote ..." )
270- files = (
271- [(os .path .join (AUDIOCORE_DIR , w ), w ) for w in WAV_FILES ]
272- + [(os .path .join (SCRIPT_DIR , s ), s ) for s in TEST_SCRIPTS ]
273- )
280+ files = [(os .path .join (AUDIOCORE_DIR , w ), w ) for w in WAV_FILES ] + [
281+ (os .path .join (SCRIPT_DIR , s ), s ) for s in TEST_SCRIPTS
282+ ]
274283 # Check if device has enough space for test files
275284 if mount :
276285 needed = 0
@@ -285,11 +294,10 @@ def copy_files(port: str, circuitpy: str | None = None):
285294 if needed > free :
286295 short = needed - free
287296 print (f"ERROR: not enough space on { mount } ." )
288- print (f" need: { needed / 1024 :.1f} KiB" )
289- print (f" free: { free / 1024 :.1f} KiB" )
290- print (f" short: { short / 1024 :.1f} KiB" )
291- print (f" Delete unrelated files (e.g. an old code.py or "
292- f"stale WAVs) and re-run." )
297+ print (f" need: { needed / 1024 :.1f} KiB" )
298+ print (f" free: { free / 1024 :.1f} KiB" )
299+ print (f" short: { short / 1024 :.1f} KiB" )
300+ print (f" Delete unrelated files (e.g. an old code.py or stale WAVs) and re-run." )
293301 sys .exit (1 )
294302 missing = []
295303 for src , dst in files :
@@ -301,9 +309,7 @@ def copy_files(port: str, circuitpy: str | None = None):
301309 shutil .copy2 (src , dest_path )
302310 print (f" { dst } (copied)" )
303311 else :
304- stdout , stderr = _mpremote (
305- ["connect" , port , "fs" , "cp" , src , f":/{ dst } " ], timeout = 30
306- )
312+ stdout , stderr = _mpremote (["connect" , port , "fs" , "cp" , src , f":/{ dst } " ], timeout = 30 )
307313 if stderr and "Error" in stderr :
308314 print (f" { dst } (FAILED: { stderr .strip ()} )" )
309315 else :
@@ -349,9 +355,7 @@ def _run_exec(port: str, code: str, label: str, timeout: float, retries: int = 3
349355 print (f" [retry { attempt } /{ retries } ] port { port } not present — waited 10s" )
350356 continue
351357 try :
352- stdout , stderr = _mpremote (
353- ["connect" , port , "exec" , code ], timeout = timeout
354- )
358+ stdout , stderr = _mpremote (["connect" , port , "exec" , code ], timeout = timeout )
355359 except TimeoutError as exc :
356360 print (f" [FAIL] { exc } " )
357361 return False , "" , ""
@@ -397,6 +401,7 @@ def _settle_between_tests(port: str) -> None:
397401# Individual tests
398402# ---------------------------------------------------------------------------
399403
404+
400405def test1_wavefile_playback (port : str ) -> bool :
401406 code = 'exec(open("/wavefile_playback.py").read())'
402407 ok , stdout , stderr = _run_exec (
@@ -422,7 +427,9 @@ def test2_pause_resume(port: str) -> bool:
422427 return False
423428 passed = True
424429 for wav in sorted (WAV_FILES ):
425- passed &= _check (f"playing with pause/resume: { wav } " in stdout , f"pause/resume header for { wav } " )
430+ passed &= _check (
431+ f"playing with pause/resume: { wav } " in stdout , f"pause/resume header for { wav } "
432+ )
426433 passed &= _check ("paused" in stdout , "At least one 'paused' line printed" )
427434 passed &= _check ("resumed" in stdout , "At least one 'resumed' line printed" )
428435 passed &= _check ("TIMEOUT" not in stdout , "No pause/resume hang timeout" )
@@ -459,8 +466,14 @@ def test5_stereo_playback(port: str) -> bool:
459466 passed &= _check ("channel test: right only" in stdout , "Right-only channel tone played" )
460467 passed &= _check ("channel test: both channels" in stdout , "Both-channel tone played" )
461468 passed &= _check ("pan sweep: left to right" in stdout , "Pan sweep played" )
462- passed &= _check ("playing stereo: jeplayer-splash-44100-16bit-stereo-signed.wav" in stdout , "44100 Hz 16-bit stereo WAV played" )
463- passed &= _check ("playing stereo: jeplayer-splash-8000-16bit-stereo-signed.wav" in stdout , "8000 Hz 16-bit stereo WAV played" )
469+ passed &= _check (
470+ "playing stereo: jeplayer-splash-44100-16bit-stereo-signed.wav" in stdout ,
471+ "44100 Hz 16-bit stereo WAV played" ,
472+ )
473+ passed &= _check (
474+ "playing stereo: jeplayer-splash-8000-16bit-stereo-signed.wav" in stdout ,
475+ "8000 Hz 16-bit stereo WAV played" ,
476+ )
464477 passed &= _check ("done" in stdout , "Script completed with 'done'" )
465478 passed &= _check (not stderr , f"No exceptions (stderr={ stderr !r} )" )
466479 return passed
@@ -482,6 +495,7 @@ def test4_deinit(port: str) -> bool:
482495# Main
483496# ---------------------------------------------------------------------------
484497
498+
485499def main ():
486500 parser = argparse .ArgumentParser (
487501 description = __doc__ ,
0 commit comments