Skip to content

Commit

Permalink
pybricks.pupdevices.Remote: Add more examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Jul 28, 2021
1 parent 8463206 commit 62bb262
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
38 changes: 36 additions & 2 deletions doc/main/pupdevices/remote.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,42 @@ Remote Control
Examples
-------------------

Check which buttons are pressed
*******************************
Checking which buttons are pressed
**********************************

.. literalinclude::
../../../examples/pup/remote/basics.py


Using the timeout setting
**********************************

You can use the ``timeout`` argument to change for how long the hub searches
for the remote. If you choose ``None``, it will search forever.

.. literalinclude::
../../../examples/pup/remote/timeout_none.py


If the remote was not found within the specified ``timeout``,
an :ref:`OSError <OSError>` is raised. You can catch this exception to run
other code if the remote is not available.


.. literalinclude::
../../../examples/pup/remote/timeout_exception.py

Changing the name of the remote
*******************************

You can change the Bluetooth name of the remote. The factory default name is
``Handset``.

.. literalinclude::
../../../examples/pup/remote/set_name.py

You can specify this name when connecting to the remote.
This lets you pick the right one if multiple remotes are nearby.

.. literalinclude::
../../../examples/pup/remote/use_name.py
12 changes: 12 additions & 0 deletions examples/pup/remote/set_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pybricks.pupdevices import Remote

# Connect to any remote.
my_remote = Remote()

# Print the current name of the remote.
print(my_remote.name())

# Choose a new name.
my_remote.name('truck2')

print("Done!")
16 changes: 16 additions & 0 deletions examples/pup/remote/timeout_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pybricks.pupdevices import Remote

try:
# Search for a remote for 5 seconds.
my_remote = Remote(timeout=5000)

print("Connected!")

# Here you can write code that uses the remote.

except OSError:

print("Could not find the remote.")

# Here you can make your robot do something
# without the remote.
6 changes: 6 additions & 0 deletions examples/pup/remote/timeout_none.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pybricks.pupdevices import Remote

# Connect to any remote. Search forever until we find one.
my_remote = Remote(timeout=None)

print("Connected!")
9 changes: 9 additions & 0 deletions examples/pup/remote/use_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pybricks.pupdevices import Remote
from pybricks.tools import wait

# Connect to a remote called truck2.
truck_remote = Remote('truck2', timeout=None)

print("Connected!")

wait(2000)

0 comments on commit 62bb262

Please sign in to comment.