|
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,25 +223,95 @@ 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 = [ |
| 237 | + "Recompile and Run", |
| 238 | + "Recompile and Download", |
| 239 | + "Exit", |
| 240 | + ] |
| 241 | + while True: |
| 242 | + try: |
| 243 | + response = await hub.race_power_button_press( |
| 244 | + questionary.select( |
| 245 | + "Would you like to re-compile your code?", |
| 246 | + response_options, |
| 247 | + ).ask_async() |
| 248 | + ) |
| 249 | + except RuntimeError as e: |
| 250 | + |
| 251 | + async def reconnect_hub(): |
| 252 | + if await questionary.confirm( |
| 253 | + "\nThe hub has been disconnected. Would you like to re-connect?" |
| 254 | + ).ask_async(): |
| 255 | + if args.conntype == "ble": |
| 256 | + print( |
| 257 | + f"Searching for {args.name or 'any hub with Pybricks service'}..." |
| 258 | + ) |
| 259 | + device_or_address = await find_ble(args.name) |
| 260 | + hub = PybricksHubBLE(device_or_address) |
| 261 | + elif args.conntype == "usb": |
| 262 | + device_or_address = find_usb( |
| 263 | + custom_match=is_pybricks_usb |
| 264 | + ) |
| 265 | + hub = PybricksHubUSB(device_or_address) |
| 266 | + |
| 267 | + await hub.connect() |
| 268 | + # re-enable echoing of the hub's stdout |
| 269 | + hub.print_output = True |
| 270 | + hub._enable_line_handler = True |
| 271 | + return hub |
| 272 | + |
| 273 | + else: |
| 274 | + exit() |
| 275 | + |
| 276 | + if ( |
| 277 | + hub.status_observable.value |
| 278 | + & StatusFlag.POWER_BUTTON_PRESSED |
| 279 | + ): |
| 280 | + try: |
| 281 | + await hub._wait_for_user_program_stop(2.1) |
| 282 | + continue |
| 283 | + |
| 284 | + except RuntimeError as e: |
| 285 | + if ( |
| 286 | + hub.connection_state_observable.value |
| 287 | + == ConnectionState.DISCONNECTED |
| 288 | + ): |
| 289 | + hub = await reconnect_hub() |
| 290 | + continue |
| 291 | + |
| 292 | + else: |
| 293 | + raise e |
| 294 | + |
| 295 | + elif ( |
| 296 | + hub.connection_state_observable.value |
| 297 | + == ConnectionState.DISCONNECTED |
| 298 | + ): |
| 299 | + # let terminal cool off before making a new prompt |
| 300 | + await asyncio.sleep(0.3) |
| 301 | + |
| 302 | + hub = await reconnect_hub() |
| 303 | + continue |
| 304 | + |
| 305 | + else: |
| 306 | + raise e |
| 307 | + |
| 308 | + with _get_script_path(args.file) as script_path: |
| 309 | + if response == response_options[0]: |
| 310 | + await hub.run(script_path, True) |
| 311 | + elif response == response_options[1]: |
| 312 | + await hub.download(script_path) |
| 313 | + else: |
| 314 | + exit(1) |
243 | 315 |
|
244 | 316 | finally: |
245 | 317 | await hub.disconnect() |
|
0 commit comments