Skip to content

Commit faf1158

Browse files
committed
pybricks.tools: Document hub_menu.
Fixes #144
1 parent 8819446 commit faf1158

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

doc/main/tools/index.rst

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Input tools
2727

2828
.. autofunction:: pybricks.tools.read_input_byte
2929

30+
.. pybricks-requirements:: light-matrix
31+
32+
.. autofunction:: pybricks.tools.hub_menu
33+
34+
.. literalinclude::
35+
../../../examples/pup/tools/hub_menu.py
36+
3037
Linear algebra tools
3138
--------------------
3239

examples/pup/tools/hub_menu.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pybricks.tools import hub_menu
2+
3+
# This example assumes that you have three other programs in Pybricks Code,
4+
# called "fly_mission", "drive_mission", and "zigzag". This example creates a
5+
# menu that lets you pick which one to run.
6+
7+
# Choose a letter.
8+
selected = hub_menu("F", "D", "Z")
9+
10+
# Based on the selection, run a program.
11+
if selected == "F":
12+
import fly_mission
13+
elif selected == "D":
14+
import drive_mission
15+
elif selected == "Z":
16+
import zigzag

jedi/tests/test_complete_import.py

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def test_from_pybricks_tools_import():
157157
assert [c["insertText"] for c in completions] == [
158158
"cross",
159159
"DataLog",
160+
"hub_menu",
160161
"Matrix",
161162
"read_input_byte",
162163
"StopWatch",

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

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

src/pybricks/tools.py

+21
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ def read_input_byte() -> Optional[int]:
234234
"""
235235

236236

237+
def hub_menu(*symbols: int | str) -> int | str:
238+
"""
239+
hub_menu(symbol1, symbol2, ...) -> int | str
240+
241+
Shows a menu on the hub display and waits for the user to select an item
242+
using the buttons. Can be used in your own menu-program that lets you
243+
choose which of your other programs to run.
244+
245+
Note that this is just a convenience function that combines the display,
246+
buttons, and waits to make a simple menu. This means that it can be used
247+
anywhere in a program, not just at the start.
248+
249+
Arguments:
250+
symbol1 (int or str): The first symbol to show in the menu.
251+
symbol2 (int or str): The second symbol, and so on...
252+
253+
Returns:
254+
The selected symbol.
255+
"""
256+
257+
237258
# HACK: hide from jedi
238259
if TYPE_CHECKING:
239260
del Number

0 commit comments

Comments
 (0)