1818import argcomplete
1919import questionary
2020from argcomplete .completers import FilesCompleter
21+ from packaging .version import Version
2122
2223from pybricksdev import __name__ as MODULE_NAME
2324from pybricksdev import __version__ as MODULE_VERSION
@@ -243,8 +244,9 @@ def is_pybricks_usb(dev):
243244 class ResponseOptions (IntEnum ):
244245 RECOMPILE_RUN = 0
245246 RECOMPILE_DOWNLOAD = 1
246- CHANGE_TARGET_FILE = 2
247- EXIT = 3
247+ RUN_STORED = 2
248+ CHANGE_TARGET_FILE = 3
249+ EXIT = 4
248250
249251 async def reconnect_hub ():
250252 if not await questionary .confirm (
@@ -271,9 +273,19 @@ async def reconnect_hub():
271273 response_options = [
272274 "Recompile and Run" ,
273275 "Recompile and Download" ,
276+ "Run Stored Program" ,
274277 "Change Target File" ,
275278 "Exit" ,
276279 ]
280+ # the entry that is selected by default when the menu opens
281+ # this is overridden after the user picks an option
282+ # so that the default option is always the one that was last chosen
283+ default_response_option = (
284+ ResponseOptions .RECOMPILE_RUN
285+ if args .start
286+ else ResponseOptions .RECOMPILE_DOWNLOAD
287+ )
288+
277289 while True :
278290 try :
279291 if args .file is sys .stdin :
@@ -290,17 +302,13 @@ async def reconnect_hub():
290302 questionary .select (
291303 f"Would you like to re-compile { os .path .basename (args .file .name )} ?" ,
292304 response_options ,
293- default = (
294- response_options [ResponseOptions .RECOMPILE_RUN ]
295- if args .start
296- else response_options [
297- ResponseOptions .RECOMPILE_DOWNLOAD
298- ]
299- ),
305+ default = (response_options [default_response_option ]),
300306 ).ask_async ()
301307 )
302308 )
303309
310+ default_response_option = response_options .index (response )
311+
304312 match response_options .index (response ):
305313
306314 case ResponseOptions .RECOMPILE_RUN :
@@ -311,6 +319,15 @@ async def reconnect_hub():
311319 with _get_script_path (args .file ) as script_path :
312320 await hub .download (script_path )
313321
322+ case ResponseOptions .RUN_STORED :
323+ if hub .fw_version < Version ("3.2.0-beta.4" ):
324+ print (
325+ "Running a stored program remotely is only supported in the hub firmware version >= v3.2.0."
326+ )
327+ else :
328+ await hub .start_user_program ()
329+ await hub ._wait_for_user_program_stop ()
330+
314331 case ResponseOptions .CHANGE_TARGET_FILE :
315332 args .file .close ()
316333 while True :
0 commit comments