ColorDistanceSensor

LEGO Color and Distance Sensor 88007

BrickLink listing

The LEGO® Color and Distance Sensor can sort between six different colors and objects within 5 to 10 cm range

NOTE: Support for this device is experimental and not all features are available yet.

class buildhat.ColorDistanceSensor(port)

Color Distance sensor

Parameters:

port – Port of device

Raises:

DeviceError – Occurs if there is no colordistance sensor attached to port

callback(func)

Set callback function

Parameters:

func – Callback function

property connected

Whether device is connected or not

Returns:

Connection status

static desc_for_id(typeid)

Translate integer type id to something more descriptive than the device name

Parameters:

typeid – Type of device

Returns:

Description of device

property description

Device on port info

Returns:

Device description

deselect()

Unselect data from mode

get()

Extract information from device

Returns:

Data from device

Raises:

DeviceError – Occurs if device not in valid mode

get_ambient_light()

Return the ambient light

Returns:

Ambient light

Return type:

int

get_color()

Return the color

Returns:

Name of the color as a string

Return type:

str

get_color_rgb()

Return the color

Returns:

RGBI representation

Return type:

list

get_distance()

Return the distance

Returns:

Distance

Return type:

int

get_reflected_light()

Return the reflected light

Returns:

Reflected light

Return type:

int

property interval

Interval between data points in milliseconds

Getter:

Gets interval

Setter:

Sets interval

Returns:

Device interval

Return type:

int

isconnected()

Whether it is connected or not

Raises:

DeviceError – Occurs if device no longer the same

mode(modev)

Set combimode or simple mode

Parameters:

modev – List of tuples for a combimode, or integer for simple mode

property name

Determine name of device on port

Returns:

Device name

static name_for_id(typeid)

Translate integer type id to device name (python class)

Parameters:

typeid – Type of device

Returns:

Name of device

off()

Turn off sensor

on()

Turn on the sensor and LED

reverse()

Reverse polarity

rgb_to_hsv(r, g, b)

Convert RGB to HSV

Based on https://www.rapidtables.com/convert/color/rgb-to-hsv.html algorithm

Parameters:
  • r – Red

  • g – Green

  • b – Blue

Returns:

HSV representation of color

Return type:

tuple

segment_color(r, g, b)

Return the color name from HSV

Parameters:
  • r – Red

  • g – Green

  • b – Blue

Returns:

Name of the color as a string

Return type:

str

select()

Request data from mode

Raises:

DeviceError – Occurs if device not in valid mode

property typeid

Type ID of device

Returns:

Type ID

property typeidcur

Type ID currently present

Returns:

Type ID

wait_for_new_color()

Wait for new color or returns immediately if first call

Returns:

Name of the color as a string

Return type:

str

wait_until_color(color)

Wait until specific color

Parameters:

color – Color to look for

Example

"""Example for color distance sensor"""

from buildhat import ColorDistanceSensor

color = ColorDistanceSensor('C')

print("Distance", color.get_distance())
print("RGBI", color.get_color_rgb())
print("Ambient", color.get_ambient_light())
print("Reflected", color.get_reflected_light())
print("Color", color.get_color())

print("Waiting for color black")
color.wait_until_color("black")
print("Found color black")

print("Waiting for color white")
color.wait_until_color("white")
print("Found color white")

while True:
    c = color.wait_for_new_color()
    print("Found new color", c)