Skip to content

Commit

Permalink
pybricks.tools: Document hub_menu.
Browse files Browse the repository at this point in the history
Fixes #144
  • Loading branch information
laurensvalk committed Oct 24, 2023
1 parent 8819446 commit faf1158
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/main/tools/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Input tools

.. autofunction:: pybricks.tools.read_input_byte

.. pybricks-requirements:: light-matrix

.. autofunction:: pybricks.tools.hub_menu

.. literalinclude::
../../../examples/pup/tools/hub_menu.py

Linear algebra tools
--------------------

Expand Down
16 changes: 16 additions & 0 deletions examples/pup/tools/hub_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pybricks.tools import hub_menu

# This example assumes that you have three other programs in Pybricks Code,
# called "fly_mission", "drive_mission", and "zigzag". This example creates a
# menu that lets you pick which one to run.

# Choose a letter.
selected = hub_menu("F", "D", "Z")

# Based on the selection, run a program.
if selected == "F":
import fly_mission
elif selected == "D":
import drive_mission
elif selected == "Z":
import zigzag
1 change: 1 addition & 0 deletions jedi/tests/test_complete_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def test_from_pybricks_tools_import():
assert [c["insertText"] for c in completions] == [
"cross",
"DataLog",
"hub_menu",
"Matrix",
"read_input_byte",
"StopWatch",
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

[flake8]
exclude = .venv/,*.pyi,jedi/
exclude = .venv/,*.pyi,jedi/,examples/pup/tools/hub_menu.py
max-line-length = 88
ignore = E203,E501,W503

Expand Down
21 changes: 21 additions & 0 deletions src/pybricks/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ def read_input_byte() -> Optional[int]:
"""


def hub_menu(*symbols: int | str) -> int | str:
"""
hub_menu(symbol1, symbol2, ...) -> int | str
Shows a menu on the hub display and waits for the user to select an item
using the buttons. Can be used in your own menu-program that lets you
choose which of your other programs to run.
Note that this is just a convenience function that combines the display,
buttons, and waits to make a simple menu. This means that it can be used
anywhere in a program, not just at the start.
Arguments:
symbol1 (int or str): The first symbol to show in the menu.
symbol2 (int or str): The second symbol, and so on...
Returns:
The selected symbol.
"""


# HACK: hide from jedi
if TYPE_CHECKING:
del Number
Expand Down

0 comments on commit faf1158

Please sign in to comment.