光学的#
初始化光学类#
使用以下构造函数创建光学传感器:
光学(端口)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
光学传感器连接到的有效 智能端口。 |
# Construct an Optical Sensor "optical" with the
# Optical class.
optical = Optical(Ports.PORT1)
当引用 Optical 类方法时,此“optical”对象将在整个 API 文档的所有后续示例中使用。
类方法#
hue()#
hue()
方法返回光学传感器检测到的色调值。
**返回:**光学传感器检测到的色调值,范围为 0 - 359.99 内的浮点数。
# Set a variable hue, to the value of the hue detected by
# the Optical Sensor.
hue = optical.hue()
brightness()#
brightness(readraw)
方法返回光学传感器检测到的亮度。
参数 |
描述 |
---|---|
读出 |
**可选。**用于读取原始亮度数据而不是百分比的布尔值。 |
**返回:**光学传感器检测到的亮度,范围为 0 - 100% 之间的浮点数。
# Set a variable brightness, to the value of the brightness
# detected by the Optical Sensor.
brightness = optical.brightness()
# Print the brightness detected by the Optical Sensor to
# the Brain's screen.
brain.screen.print(brightness)
color()#
color()
方法返回光学传感器检测到的颜色。
**返回:**光学传感器检测到的颜色,作为 颜色类 的实例。
# Set a variable, color, to the color detected by the
# Optical Sensor.
color = optical.color()
# Print the color detected by the Optical Sensor to the
# Brain's screen.
brain.screen.print(color)
is_near_object()#
is_near_object()
方法检查光学传感器是否检测到附近的物体。
返回: 如果检测到附近物体,则返回 True
。如果没有检测到,则返回 False
。
# If an object is detected yb the Optical Sensor, print "near object".
if optical.is_near_object():
brain.screen.print("near object")
set_light()#
set_light(value)
方法将光学传感器的 LED 设置为开或关。
参数 |
描述 |
---|---|
价值 |
设置灯光状态或功率百分比,范围为0-100。 |
**返回:**无。
# Turn on led with previous intensity.
optical.set_light(LedStateType.ON)
# Turn on led with new intensity.
optical.set_light(65)
set_light_power()#
set_light_power(value)
方法设置光学传感器 LED 的光功率。
参数 |
描述 |
---|---|
价值 |
将灯光的功率级别设置为 0 - 100 的百分比。 |
**返回:**无。
# Set the light power to 50 percent.
optical.set_light_power(50)
integration_time()#
integration_time(value)
方法将光学传感器的 LED 设置为请求的功率。
参数 |
描述 |
---|---|
价值 |
**可选。**积分时间(以毫秒为单位),范围为 5 - 700。 |
**返回:**无。
# Set the integration time to 50 milliseconds.
optical.integration_time(50)
rgb()#
rgb(raw)
方法返回光学传感器的 RGB 值。
参数 |
描述 |
---|---|
生的 |
**可选。**一个布尔值,用于确定是否返回原始值或处理后的值。 |
**返回:**一个包含红色、绿色、蓝色和亮度值的元组。
# Get the RGB value of the Optical Sensor.
rgb = optical.rgb()
object_detect_threshold()#
object_detect_threshold(value)
方法设置对象检测阈值。
参数 |
描述 |
---|---|
价值 |
0 - 255 范围内的数字。值为 0 时将仅返回当前值。 |
**返回:**物体检测阈值的当前值。
# Set the object detection threshold to 100.
optical.value = object_detect_threshold(100)
gesture_enable()#
gesture_enable()
方法启用手势模式。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
**返回:**无。
gesture_disable()#
gesture_disable()
方法禁用手势模式。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
**返回:**无。
get_gesture()#
get_gesture(newobject)
方法返回光学传感器检测到的手势。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
参数 |
描述 |
---|---|
新对象 |
**可选。**一个布尔值,决定是否创建一个新的手势对象。 |
**返回:**具有最后一个手势数据的对象。
object_detected()#
object_detected(callback, arg)
方法注册一个在检测到对象时回调的函数。
参数 |
描述 |
---|---|
打回来 |
当检测到物体时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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()#
object_lost(callback, arg)
方法注册一个当对象丢失时的回调函数。
参数 |
描述 |
---|---|
打回来 |
当对象丢失时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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()#
gesture_up(callback, arg)
方法注册一个回调函数,用于检测到向上手势时执行的操作。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
参数 |
描述 |
---|---|
打回来 |
当检测到向上手势时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# 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()#
gesture_down(callback, arg)
方法注册一个回调函数,用于在检测到向下手势时执行回调。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
参数 |
描述 |
---|---|
打回来 |
当检测到向下手势时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# 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()#
gesture_left(callback, arg)
方法注册一个回调函数,用于检测到向左手势时执行的操作。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
参数 |
描述 |
---|---|
打回来 |
当检测到向左手势时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# 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()#
gesture_right(callback, arg)
方法注册一个回调函数,用于检测到向右手势时执行的操作。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
参数 |
描述 |
---|---|
打回来 |
当检测到向右手势时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# 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()#
installed()
方法检查光学传感器是否已连接。
返回: 如果光学传感器已连接,则返回 True
。如果未连接,则返回 False
。
timestamp()#
timestamp()
方法返回从光学传感器接收到的最后一个状态包的时间戳。
**返回:**最后接收的状态包的时间戳(以毫秒为单位)。