Skip to content

Commit

Permalink
api/pupdevices: initial color sensor hsv api
Browse files Browse the repository at this point in the history
Provide a simple yet powerful way to calibrate the color() method.
  • Loading branch information
laurensvalk committed May 25, 2020
1 parent c897564 commit a74672f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/api/pupdevices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ Color Sensor
.. autoclass:: pybricks.pupdevices.ColorSensor
:no-members:

.. automethod:: pybricks.pupdevices.ColorSensor.color

.. automethod:: pybricks.pupdevices.ColorSensor.hsv

.. automethod:: pybricks.pupdevices.ColorSensor.color_map

.. automethod:: pybricks.pupdevices.ColorSensor.ambient

.. automethod:: pybricks.pupdevices.ColorSensor.reflection

.. rubric:: Built-in lights

This sensor has 3 built-in lights. You can adjust the brightness of each
Expand Down
77 changes: 77 additions & 0 deletions pybricks/pupdevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,83 @@ class ColorSensor:

lights = _LightArray(3)

def color(self, illuminate=True):
"""Scans the color of a surface or an external light source.
Arguments:
illuminate (bool): Choose ``true`` to keep the sensor light on.
This is useful to scan the color of objects and surfaces.
Choose ``false`` to turn the sensor light off. This is useful
to scan the color of a light source or screen.
:returns:
Detected color.
:rtype: :class:`Color <.parameters.Color>`, or ``None`` if no color is
detected.
"""
pass

def color_map(self, hues, saturation, values):
"""Configures how :meth:`.color` selects a
:class:`Color <.parameters.Color>` based on a :meth:`.hsv` measurement.
Specify only colors that you wish to detect in your application.
This way, measurements are
rounded to the nearest expected color, and other colors are ignored.
If you give no arguments, the current settings will be returned as a
tuple.
Arguments:
hues (dict): A dictionary that
maps :class:`Color <.parameters.Color>` to hues. When the
saturation is high, :meth:`.color` will return one of these
colors, whichever has the nearest hue.
saturation (:ref:`percentage`): Minimum saturation of a proper
color.
values (:ref:`percentage`): A dictionary that
maps ``Color.WHITE``, ``Color.GRAY``, ``Color.BLACK`` and
``None`` to brightness values. When the saturation is
low, :meth:`.color` will return one of these colors, whichever
has the nearest value.
"""
pass

def hsv(self, illuminate=True):
"""Scans the hue, saturation and brightness value of a
surface or an external light source.
Arguments:
illuminate (bool): Choose ``true`` to keep the sensor light on.
This is useful to scan the color of objects and surfaces.
Choose ``false`` to turn the sensor light off. This is useful
to scan the color of a light source or screen.
:returns: Tuple with the hue, saturation, and value
(brightness) of the color.
:rtype: (:ref:`angle`, :ref:`percentage`, :ref:`percentage`)
"""
pass

def ambient(self):
"""Measures the ambient light intensity.
Returns:
:ref:`percentage`: Ambient light intensity, ranging from 0 (dark)
to 100 (bright).
"""
pass

def reflection(self):
"""Measures the reflection of a surface.
Returns:
:ref:`percentage`: Reflection, ranging from 0.0 (no reflection) to
100.0 (high reflection).
"""
pass


class UltrasonicSensor:
"""LEGO® SPIKE Color Sensor."""
Expand Down

0 comments on commit a74672f

Please sign in to comment.