ColorDistanceSensor¶
LEGO Color and Distance Sensor 88007
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
- property ir_address¶
IR Address space of 0x0 for default PoweredUp or 0x1 for extra space
- property ir_channel¶
Get the IR channel for message transmission
- 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
- send_ir_combo_direct(port_b_output, port_a_output)¶
Send an IR message via Power Functions RC Protocol in Combo Direct mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Valid values for the output variables are: 0x0: Float output 0x1: Clockwise/Forward 0x2: Counterclockwise/Backwards 0x3: Brake then float
- Parameters:
port_b_output – 0-3 indicating the output to send to port B
port_a_output – 0-3 indicating the output to send to port A
- send_ir_combo_pwm(port_b_mode, port_a_mode)¶
Send an IR message via Power Functions RC Protocol in Combo PWM mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Valid values for the modes are: 0x0 Float 0x1 PWM Forward step 1 0x2 PWM Forward step 2 0x3 PWM Forward step 3 0x4 PWM Forward step 4 0x5 PWM Forward step 5 0x6 PWM Forward step 6 0x7 PWM Forward step 7 0x8 Brake (then float v1.20) 0x9 PWM Backward step 7 0xA PWM Backward step 6 0xB PWM Backward step 5 0xC PWM Backward step 4 0xD PWM Backward step 3 0xE PWM Backward step 2 0xF PWM Backward step 1
- Parameters:
port_b_mode – 0-15 indicating the command to send to port B
port_a_mode – 0-15 indicating the command to send to port A
- send_ir_extended(mode)¶
Send an IR message via Power Functions RC Protocol in Extended mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Valid values for the mode are: 0x0: Brake Port A (timeout) 0x1: Increment Speed on Port A 0x2: Decrement Speed on Port A
0x4: Toggle Forward/Clockwise/Float on Port B
0x6: Toggle Address bit 0x7: Align toggle bit
- Parameters:
mode – 0-2,4,6-7
- send_ir_single_pin(port, pin, mode, timeout)¶
Send an IR message via Power Functions RC Protocol in Single Pin mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Valid values for the mode are: 0x0: No-op 0x1: Clear 0x2: Set 0x3: Toggle
Note: The unlabeled IR receiver (vs the one labeled V2) has a “firmware bug in Single Pin mode” https://www.philohome.com/pfrec/pfrec.htm
- Parameters:
port – ‘A’ or ‘B’
pin – 1 or 2
mode – 0-3 indicating the pin’s mode to set
timeout – True or False
- send_ir_socstid(port, mode)¶
Send an IR message via Power Functions RC Protocol in Single Output Clear/Set/Toggle/Increment/Decrement mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Valid values for mode are: 0x0: Toggle full Clockwise/Forward (Stop to Clockwise, Clockwise to Stop, Counterclockwise to Clockwise) 0x1: Toggle direction 0x2: Increment numerical PWM 0x3: Decrement numerical PWM 0x4: Increment PWM 0x5: Decrement PWM 0x6: Full Clockwise/Forward 0x7: Full Counterclockwise/Backward 0x8: Toggle full (defaults to Forward, first) 0x9: Clear C1 (C1 to High) 0xA: Set C1 (C1 to Low) 0xB: Toggle C1 0xC: Clear C2 (C2 to High) 0xD: Set C2 (C2 to Low) 0xE: Toggle C2 0xF: Toggle full Counterclockwise/Backward (Stop to Clockwise, Counterclockwise to Stop, Clockwise to Counterclockwise)
- Parameters:
port – ‘A’ or ‘B’
mode – 0-15 indicating the port’s mode to set
- send_ir_sop(port, mode)¶
Send an IR message via Power Functions RC Protocol in Single Output PWM mode
PF IR RC Protocol documented at https://www.philohome.com/pf/pf.htm
Port B is blue
Valid values for mode are: 0x0: Float output 0x1: Forward/Clockwise at speed 1 0x2: Forward/Clockwise at speed 2 0x3: Forward/Clockwise at speed 3 0x4: Forward/Clockwise at speed 4 0x5: Forward/Clockwise at speed 5 0x6: Forward/Clockwise at speed 6 0x7: Forward/Clockwise at speed 7 0x8: Brake (then float v1.20) 0x9: Backwards/Counterclockwise at speed 7 0xA: Backwards/Counterclockwise at speed 6 0xB: Backwards/Counterclockwise at speed 5 0xC: Backwards/Counterclockwise at speed 4 0xD: Backwards/Counterclockwise at speed 3 0xE: Backwards/Counterclockwise at speed 2 0xF: Backwards/Counterclockwise at speed 1
- Parameters:
port – ‘A’ or ‘B’
mode – 0-15 indicating the port’s mode to set
- toggle_ir_toggle()¶
Toggle the IR toggle bit
- 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)