-
Notifications
You must be signed in to change notification settings - Fork 22
Add the abilty to resend code though bluetooth in the same session #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
07cacfc
9dfd844
018af5a
e1683b1
aa342f4
1180a0d
e83e1e9
004b3f5
4e878ee
41e7a77
dc1ecac
c4e0745
f76600c
3a595e4
87ce5bc
39d6675
31561dc
888aa70
2b2ace0
5bc6a3b
f4dede9
52f055f
745a588
0633bfd
476a1cd
2fa86cb
3bec955
f088c1a
7429c39
76a0b89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,15 @@ | |
| from typing import ContextManager, TextIO | ||
|
|
||
| import argcomplete | ||
| import questionary | ||
| from argcomplete.completers import FilesCompleter | ||
|
|
||
| from pybricksdev import __name__ as MODULE_NAME | ||
| from pybricksdev import __version__ as MODULE_VERSION | ||
| from pybricksdev.connections.pybricks import ( | ||
| HubDisconnectError, | ||
| HubPowerButtonPressedError, | ||
| ) | ||
|
|
||
| PROG_NAME = ( | ||
| f"{path.basename(sys.executable)} -m {MODULE_NAME}" | ||
|
|
@@ -160,6 +165,13 @@ def add_parser(self, subparsers: argparse._SubParsersAction): | |
| default=True, | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--stay-connected", | ||
| help="Add a menu option to resend the code with bluetooth instead of disconnecting from the robot after the program ends.", | ||
| action=argparse.BooleanOptionalAction, | ||
| default=False, | ||
| ) | ||
|
|
||
| async def run(self, args: argparse.Namespace): | ||
|
|
||
| # Pick the right connection | ||
|
|
@@ -215,14 +227,84 @@ def is_pybricks_usb(dev): | |
| try: | ||
| with _get_script_path(args.file) as script_path: | ||
| if args.start: | ||
| await hub.run(script_path, args.wait) | ||
| await hub.run(script_path, args.wait or args.stay_connected) | ||
| else: | ||
| if args.stay_connected: | ||
| hub.print_output = True | ||
| hub._enable_line_handler = True | ||
dlech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await hub.download(script_path) | ||
|
|
||
| if args.stay_connected: | ||
shaggysa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| async def reconnect_hub(): | ||
| if not await questionary.confirm( | ||
| "\nThe hub has been disconnected. Would you like to re-connect?" | ||
| ).ask_async(): | ||
| exit() | ||
|
|
||
| if args.conntype == "ble": | ||
| print( | ||
| f"Searching for {args.name or 'any hub with Pybricks service'}..." | ||
| ) | ||
| device_or_address = await find_ble(args.name) | ||
| hub = PybricksHubBLE(device_or_address) | ||
| elif args.conntype == "usb": | ||
| device_or_address = find_usb(custom_match=is_pybricks_usb) | ||
| hub = PybricksHubUSB(device_or_address) | ||
|
|
||
| await hub.connect() | ||
| # re-enable echoing of the hub's stdout | ||
| hub.log_file = None | ||
| hub.output = [] | ||
| hub._stdout_buf.clear() | ||
| hub._stdout_line_queue = asyncio.Queue() | ||
shaggysa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| hub.print_output = True | ||
| hub._enable_line_handler = True | ||
| return hub | ||
|
|
||
| response_options = [ | ||
| "Recompile and Run", | ||
| "Recompile and Download", | ||
| "Exit", | ||
| ] | ||
| while True: | ||
| try: | ||
| response = await hub.race_power_button_press( | ||
dlech marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| questionary.select( | ||
| "Would you like to re-compile your code?", | ||
| response_options, | ||
| default=( | ||
| response_options[0] | ||
| if args.start | ||
| else response_options[1] | ||
| ), | ||
| ).ask_async() | ||
| ) | ||
| with _get_script_path(args.file) as script_path: | ||
| if response == response_options[0]: | ||
| await hub.run(script_path, True) | ||
shaggysa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| elif response == response_options[1]: | ||
| await hub.download(script_path) | ||
| else: | ||
| exit() | ||
|
|
||
| except HubPowerButtonPressedError: | ||
| try: | ||
shaggysa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await hub._wait_for_user_program_stop(5) | ||
|
||
| except HubDisconnectError: | ||
| hub = await reconnect_hub() | ||
|
|
||
| except HubDisconnectError: | ||
| # let terminal cool off before making a new prompt | ||
| await asyncio.sleep(0.3) | ||
| hub = await reconnect_hub() | ||
|
|
||
| finally: | ||
| await hub.disconnect() | ||
|
|
||
|
|
||
| class Flash(Tool): | ||
|
|
||
| def add_parser(self, subparsers: argparse._SubParsersAction): | ||
| parser = subparsers.add_parser( | ||
| "flash", help="flash firmware on a LEGO Powered Up device" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.