|
20 | 20 |
|
21 | 21 | from pybricksdev import __name__ as MODULE_NAME |
22 | 22 | from pybricksdev import __version__ as MODULE_VERSION |
| 23 | +from pybricksdev.ble.pybricks import StatusFlag |
| 24 | +from pybricksdev.connections import ConnectionState |
23 | 25 |
|
24 | 26 | PROG_NAME = ( |
25 | 27 | f"{path.basename(sys.executable)} -m {MODULE_NAME}" |
@@ -221,32 +223,80 @@ def is_pybricks_usb(dev): |
221 | 223 | # Connect to the address and run the script |
222 | 224 | await hub.connect() |
223 | 225 | try: |
224 | | - while True: |
225 | | - with _get_script_path(args.file) as script_path: |
226 | | - if args.start: |
227 | | - await hub.run(script_path, args.wait) |
228 | | - else: |
229 | | - await hub.download(script_path) |
230 | | - |
231 | | - if not args.wait or not args.stay_connected: |
232 | | - break |
233 | | - |
234 | | - resend = await questionary.select( |
235 | | - "Would you like to resend your code?", choices=["Resend", "Exit"] |
236 | | - ).ask_async() |
237 | | - |
238 | | - if resend == "Exit": |
239 | | - break |
240 | | - |
241 | | - except RuntimeError: |
242 | | - print("The hub is no longer connected.") |
| 226 | + with _get_script_path(args.file) as script_path: |
| 227 | + if args.start: |
| 228 | + await hub.run(script_path, args.wait or args.stay_connected) |
| 229 | + else: |
| 230 | + if args.stay_connected: |
| 231 | + hub.print_output = True |
| 232 | + hub._enable_line_handler = True |
| 233 | + await hub.download(script_path) |
| 234 | + |
| 235 | + if args.stay_connected: |
| 236 | + response_options = ["Recompile and Run", "Recompile and Download", "Exit"] |
| 237 | + while True: |
| 238 | + try: |
| 239 | + response = await hub.race_user_program_start(questionary.select("Would you like to re-compile your code?", response_options).ask_async()) |
| 240 | + except RuntimeError as e: |
| 241 | + |
| 242 | + async def reconnect_hub(): |
| 243 | + if await questionary.confirm( |
| 244 | + "\nThe hub has been disconnected. Would you like to re-connect?").ask_async(): |
| 245 | + if args.conntype == "ble": |
| 246 | + print(f"Searching for {args.name or 'any hub with Pybricks service'}...") |
| 247 | + device_or_address = await find_ble(args.name) |
| 248 | + hub = PybricksHubBLE(device_or_address) |
| 249 | + elif args.conntype == "usb": |
| 250 | + device_or_address = find_usb(custom_match=is_pybricks_usb) |
| 251 | + hub = PybricksHubUSB(device_or_address) |
| 252 | + |
| 253 | + await hub.connect() |
| 254 | + # re-enable echoing of the hub's stdout |
| 255 | + hub.print_output = True |
| 256 | + hub._enable_line_handler = True |
| 257 | + return hub |
| 258 | + |
| 259 | + else: |
| 260 | + exit() |
| 261 | + |
| 262 | + if hub.status_observable.value & StatusFlag.POWER_BUTTON_PRESSED: |
| 263 | + try: |
| 264 | + await hub._wait_for_user_program_stop(2.1) |
| 265 | + continue |
| 266 | + |
| 267 | + except RuntimeError as e: |
| 268 | + if hub.connection_state_observable.value == ConnectionState.DISCONNECTED: |
| 269 | + hub = await reconnect_hub() |
| 270 | + continue |
| 271 | + |
| 272 | + else: |
| 273 | + raise e |
| 274 | + |
| 275 | + elif hub.connection_state_observable.value == ConnectionState.DISCONNECTED: |
| 276 | + # let terminal cool off before making a new prompt |
| 277 | + await asyncio.sleep(0.3) |
| 278 | + |
| 279 | + hub = await reconnect_hub() |
| 280 | + continue |
| 281 | + |
| 282 | + else: |
| 283 | + raise e |
| 284 | + |
| 285 | + with _get_script_path(args.file) as script_path: |
| 286 | + if response == response_options[0]: |
| 287 | + await hub.run(script_path, True) |
| 288 | + elif response == response_options[1]: |
| 289 | + await hub.download(script_path) |
| 290 | + else: |
| 291 | + exit(1) |
243 | 292 |
|
244 | 293 | finally: |
245 | 294 | await hub.disconnect() |
246 | 295 |
|
247 | 296 |
|
248 | 297 | class Flash(Tool): |
249 | 298 |
|
| 299 | + |
250 | 300 | def add_parser(self, subparsers: argparse._SubParsersAction): |
251 | 301 | parser = subparsers.add_parser( |
252 | 302 | "flash", help="flash firmware on a LEGO Powered Up device" |
|
0 commit comments