Optical#

Initializing the Optical Class#

The Optical constructor creates an Optical object in the specified Smart Port:

This constructor uses one parameter:

Parameter

Description

port

A valid Smart Port that the Optical Sensor is connected to.

# Construct an Optical Sensor "optical" with the
# Optical class.
optical = Optical(Ports.PORT1)

This optical object will be used in all subsequent examples throughout this API documentation when referring to Optical class methods.

Class Methods#

hue()#

The hue() method returns the value of the hue detected by the Optical Sensor.

Returns: The value of the hue detected by the Optical Sensor as a float in the range 0 - 359.99.

# Set a variable hue, to the value of the hue detected by
# the Optical Sensor.
optical.hue = hue()

brightness()#

The brightness(readraw) method returns the brightness detected by the Optical Sensor.

Parameters

Description

readraw

Optional. A boolean value to read raw brightness data instead of percentage.

Returns: The brightness detected by the Optical Sensor as a float in the range 0 - 100%.

# Set a variable brightness, to the value of the brightness
# detected by the Optical Sensor.      
optical.brightness = brightness()

# Print the brightness detected by the Optical Sensor to
# the Brain's screen.
brain.screen.print(brightness)

color()#

The color() method returns the color detected by the Optical Sensor.

Returns: The color detected by the Optical Sensor as an instance of the Color class.

# Set a variable, color, to the color detected by the
# Optical Sensor.
optical.color = color()

# Print the color detected by the Optical Sensor to the
# Brain's screen.
brain.screen.print(color)

is_near_object()#

The is_near_object() method checks if the Optical Sensor detects a nearby object.

Returns: True if a nearby object is detected. False if one is not.

# If an object is detected yb the Optical Sensor, print "near object".
if optical.is_near_object():
    brain.screen.print("near object")

set_light()#

The set_light(value) method sets Optical Sensor’s LED to on or off.

Parameters

Description

value

The state to set the light or the percentage of power in the range 0 - 100.

Returns: None.

# Turn on led with previous intensity.
optical.set_light(LedStateType.ON)

# Turn on led with new intensity.
optical.set_light(65)

set_light_power()#

The set_light_power(value) method sets the light power of the Optical Sensor’s LED.

Parameters

Description

value

The power level to set the light from 0 - 100.

Returns: None.

# Set the light power to 50 percent.
optical.set_light_power(50)

integration_time()#

The integration_time(value) method sets the Optical Sensor’s LED to the requested power.

Parameters

Description

value

Optional. The integration time in milliseconds from 5 - 700.

Returns: None.

# Set the integration time to 50 milliseconds.
optical.integration_time(50)

rgb()#

The rgb(raw) method returns the Optical Sensor’s RGB value.

Parameters

Description

raw

Optional. A boolean value to determine if you return raw or processed values.

Returns: A tuple with red, green, blue, and brightness values.

# Get the RGB value of the Optical Sensor.
optical.value = rgb()

object_detect_threshold()#

The object_detect_threshold(value) method sets the object detection threshold.

Parameters

Description

value

A number in the range 0 - 255. A value of 0 will just return the current value.

Returns: The current value of the object detection threshold.

# Set the object detection threshold to 100.
optical.value = object_detect_threshold(100)

gesture_enable()#

The gesture_enable() method enables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

gesture_disable()#

The gesture_disable() method disables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

get_gesture()#

The get_gesture(newobject) method returns the gesture detected by the Optical Sensor.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

newobject

Optional. A boolean value which determines whether or not to create a new Gesture object.

Returns: An object with the last gesture data.

object_detected()#

The object_detected(callback, arg) method registers a callback function for when an object is detected.

Parameters

Description

callback

The callback function to be called when an object is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function detected().
def detected():
    # The Brain will print that an object was detected on
    # the Brain's screen.
    brain.screen.print("object detected")

# Run detected when the Optical Sensor detects an object.
optical.object_detected(detected)

object_lost()#

The object_lost(callback, arg) method registers a callback function for when an object is lost.

Parameters

Description

callback

The callback function to be called when an object is lost.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function lost().
def lost():
    # The Brain will print that an object was lost on the
    # Brain's screen.
    brain.screen.print("object lost")

# Run lost when the Optical Sensor loses an object.
optical.object_lost(lost)

gesture_up()#

The gesture_up(callback, arg) method registers a callback function for when an upward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an upward gesture is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: None.

# Define a function detect_gesture_up()
def detect_gesture_up():
    # The Brain will print that the up gesture was detected 
    # on the Brain's screen.
    brain.screen.print("gesture up detected")

# Run detect_gesture_up when the Optical Sensor detects 
# an upward gesture.
optical.gesture_up(detect_gesture_up)

gesture_down()#

The gesture_down(callback, arg) method registers a callback function for when an downward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an downward gesture is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: None.

# Define a function detect_gesture_down().
def detect_gesture_down():
    # The Brain will print that the down gesture was detected 
    # on the Brain's screen.
    brain.screen.print("gesture down detected")

# Run detect_gesture_down when the Optical Sensor detects 
# a downward gesture.
optical.gesture_down(detect_gesture_down)

gesture_left()#

The gesture_left(callback, arg) method registers a callback function for when an leftward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an leftward gesture is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: None.

# Define a function detect_gesture_left().
def detect_gesture_left():
    # The Brain will print that the left gesture was detected 
    # on the Brain's screen.
    brain.screen.print("gesture left detected")

# Run detect_gesture_left when the Optical Sensor detects 
# a leftward gesture.
optical.gesture_left(detect_gesture_left)

gesture_right()#

The gesture_right(callback, arg) method registers a callback function for when an rightward gesture is detected.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an rightward gesture is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: None.

# Define a function detect_gesture_right()
def detect_gesture_right():
    # The Brain will print that the right gesture was
    # detected on the Brain's screen.
    brain.screen.print("gesture right detected")

# Run detect_gesture_right when the Optical Sensor detects 
# a rightward gesture.
optical.gesture_right(detect_gesture_right)

installed()#

The installed() method checks if the Optical Sensor is connected.

Returns: True if the Optical Sensor is connected. False if it is not.

timestamp()#

The timestamp() method returns the timestamp of the last received message from the Optical Sensor.

Returns: The timestamp of the last received message from the Optical Sensor in milliseconds.