From 4d88fbbbd242fbe5aebf18974ca6307269c375da Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Mon, 10 Aug 2020 12:09:46 +0200 Subject: [PATCH] api/pupdevices/PFMotor: DCMotor functionality This replaces the on-off method in ColorDistanceSensor.remote() with a PFMotor class that works just like the DCMotor class. This provides much more functionality without having to invent a new API. --- doc/api/pupdevices.rst | 16 ++++++++++++++-- pybricks/pupdevices.py | 38 +++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/doc/api/pupdevices.rst b/doc/api/pupdevices.rst index 079c717..f771b7f 100644 --- a/doc/api/pupdevices.rst +++ b/doc/api/pupdevices.rst @@ -266,9 +266,21 @@ Color and Distance Sensor .. automethod:: pybricks.pupdevices::ColorDistanceSensor.light.off - .. rubric:: Power Functions 1.0 Control +Power Functions +^^^^^^^^^^^^^^^^^^^^^^^^^ - .. automethod:: pybricks.pupdevices.ColorDistanceSensor.remote +.. autoclass:: pybricks.pupdevices.PFMotor + :noindex: + :no-members: + + .. automethod:: pybricks.pupdevices.PFMotor.dc + :noindex: + + .. automethod:: pybricks.pupdevices.PFMotor.stop + :noindex: + + .. automethod:: pybricks.pupdevices.PFMotor.brake + :noindex: Color Sensor diff --git a/pybricks/pupdevices.py b/pybricks/pupdevices.py index 5f7a5eb..379ed3b 100644 --- a/pybricks/pupdevices.py +++ b/pybricks/pupdevices.py @@ -3,10 +3,12 @@ """LEGO® Powered Up motor, sensors, and lights.""" -from ._common import (KeyPad as _KeyPad, +from ._common import (KeyPad as _KeyPad, DCMotor as _DCMotor, ColorLight as _ColorLight, Motor as _Motor, LightArray as _LightArray, Light as _Light) +from .parameters import Direction as _Direction + class Motor(_Motor): """Generic class to control motors with built-in rotation sensors.""" @@ -150,25 +152,31 @@ class ColorDistanceSensor: """ pass - def remote(self, channel, button_1=None, button_2=None): - """Makes the sensor act like a Power Functions 1.0 IR remote. - Choose a channel and up to two buttons to "press". The infrared - receiver behaves just as if responding to the real remote. +class PFMotor(_DCMotor): + """Control Power Functions motors with the infrared functionality of the + :class:`ColorDistanceSensor `.""" - The sensor keeps sending the signal (as if you keep pressing the - buttons). It keeps going until you call this method again with - different buttons or no buttons at all. + def __init__(self, + sensor, + channel, + color, + positive_direction=_Direction.CLOCKWISE): + """ Arguments: - channel (int): Channel number of the remote. - button_1 (Button): Button of the Power Functions 1.0 IR remote. - Choose ``None`` if you don't want to press - any button. - button_2 (Button): Button of the Power Functions 1.0 IR remote. - Choose ``None`` if you don't want to press - any button. + sensor (ColorDistanceSensor): + Sensor object. + channel (int): + Channel number of the receiver: ``1``, ``2``, ``3``, or ``4``. + color (Color): + Color marker on the receiver: + :class:`Color.BLUE <.parameters.Color>` or + :class:`Color.RED <.parameters.Color>` + positive_direction (Direction): Which direction the motor should + turn when you give a positive duty cycle value. """ + pass class ColorSensor: