api: generalize buttons

This makes the user code easier to read:

brick.buttons()

brick.buttons.pressed()

It also opens up possibility to add features in the future such as:
brick.buttons.released()  # return which buttons are not pressed
brick.buttons.clicked()  # return which buttons were clicked since we last checked

This follows the same style as the new light API: some devices have specific instances of a KeyPad class.

A good example that nicely demonstrates the concept is the LPF2 RemoteControl, which has instances of a ColorLight and a KeyPad. It's added in this commit.
This commit is contained in:
Laurens Valk
2019-06-11 21:30:46 +02:00
parent 12834ffc73
commit 0b67db897e
5 changed files with 58 additions and 16 deletions
+4 -4
View File
@@ -8,22 +8,22 @@
Buttons
-------
.. autofunction:: pybricks.ev3brick.buttons
.. automethod:: pybricks.ev3brick.buttons.pressed
Examples::
# Do something if the left button is pressed
if Button.LEFT in brick.buttons():
if Button.LEFT in brick.buttons.pressed():
print("The left button is pressed.")
::
# Wait until any of the buttons are pressed
while not any(brick.buttons()):
while not any(brick.buttons.pressed()):
wait(10)
# Wait until all buttons are released
while any(brick.buttons()):
while any(brick.buttons.pressed()):
wait(10)
+15
View File
@@ -122,3 +122,18 @@ Color and Distance Sensor
.. automethod:: pybricks._common.light.on
.. automethod:: pybricks._common.light.off
Other Devices
---------------
Remote Control
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: pybricks.lpf2devices.RemoteControl
:no-members:
.. automethod:: pybricks._common.light.on
.. automethod:: pybricks._common.light.off
.. automethod:: pybricks._common.buttons.pressed
+16
View File
@@ -557,6 +557,19 @@ class LightGrid():
pass
class KeyPad():
"""Get status of buttons on a keypad layout."""
def pressed(self):
"""Check which buttons are currently pressed.
:returns: List of pressed buttons.
:rtype: List of :class:`Button <.parameters.Button>`
"""
pass
class Battery():
"""Get the status of a battery."""
@@ -589,3 +602,6 @@ light.rgb = ColorLight.rgb
lights = ModuleType('LightArray')
lights.on = LightArray.on
lights.off = LightArray.off
buttons = ModuleType('KeyPad')
buttons.pressed = KeyPad.pressed
+2 -12
View File
@@ -1,19 +1,9 @@
"""LEGO® MINDSTORMS® EV3 Brick."""
from ._common import Speaker, Display, Battery, ColorLight
def buttons():
"""Check which buttons on the EV3 Brick are currently pressed.
:returns: List of pressed buttons.
:rtype: List of :class:`Button <parameters.Button>`
"""
pass
from ._common import Speaker, Display, Battery, ColorLight, KeyPad
sound = Speaker()
display = Display()
battery = Battery()
light = ColorLight()
buttons = KeyPad()
+21
View File
@@ -2,6 +2,9 @@
from ._common import Motor as CommonMotor, ColorLight
from ._common import KeyPad
# The above abviously needs fixing (but not until after fixing merge conflict)
class Motor(CommonMotor):
"""LEGO® Power Functions 2.0 motors"""
@@ -17,6 +20,24 @@ class Motor(CommonMotor):
pass
class RemoteControl():
"""LEGO® Power Functions 2.0 Bluetooth Remote Control/Handset (6214560)"""
def __init__(self, device=None, timeout=10000):
"""Connect the handset to the hub.
Arguments:
device (str): Bluetooth address of the handset (? TODO ?).
If you do not specify a device identifier, the hub will
attempt to pair with any handset that is currently in
advertising mode.
timeout (:ref:`time`): The amount of time before giving up
searching.
"""
self.buttons = KeyPad()
self.light = ColorLight()
class ColorDistanceSensor():
"""LEGO® Power Functions 2.0 Color and Distance Sensor (?/6182145)
"""