#

初始化灯光类#

使用以下构造函数创建光传感器:

Light(port)

此构造函数使用一个参数:

范围

描述

port

The 3-Wire Port that the Light Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Light Class constructor.

# Create the Brain.
brain = Brain()
# Construct a Light Sensor "light" with the Light class.
light = Light(brain.three_wire_port.a)

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

类方法#

brightness()#

The brightness(units) method returns the brightness level of light falling on the Light Sensor. The brightness of the Light Sensor is an estimation based on the raw value of the sensor.

亮度 0% 表示原始值大于或等于 900。亮度 100% 表示原始值等于 0。

参数

描述

units

Optional. The only valid unit for brightness is PERCENT.

**返回:**光传感器的亮度级别在 0% - 100% 范围内。

# Get Light Sensor brightness in range of 0% - 100%.
value = light.brightness()

changed()#

The changed(callback, arg) method registers a callback function for when the value of the Light Sensor changes.

这是一个非等待命令,允许下一个命令无延迟运行。

参数

描述

callback

当光传感器的值发生变化时调用的回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**无。

# Define a function light_changed()
def light_changed():
    # The Brain will print that the Light Sensor value changed 
    # on the Brain's screen.
    brain.screen.print("Light Sensor value changed")
# Run light_changed when the value of this Light Sensor changes.
light.changed(light_changed)