模拟#

初始化模拟类#

使用以下构造函数创建 Analog 对象:

Analog(port)

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

范围

描述

port

The 3-Wire Port that the Analog device 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 Analog Class constructor.

# Create the Brain.
brain = Brain()
# Construct an Analog "analog" with the
# Analog class.
analog = Analog(brain.three_wire_port.a)

当引用 Analog 类方法时,此 analog 对象将在整个 API 文档的所有后续示例中使用。

类方法#

changed()#

changed(callback, arg) 方法注册一个当模拟输入的值发生变化时要调用的函数。

参数

描述

callback

当模拟输入值发生变化时调用的函数。

arg

**可选。**用于向回调函数传递参数的元组。

**返回:**事件类的一个实例。

analog = Analog(brain.three_wire_port.a)

# Define the function analog_changed.
def analog_changed():
    # The Brain will print that the analog input 
    # changed on the Brain's screen.
    brain.screen.print("analog changed")
    brain.screen.next_row()
# Run analog_changed when the value of the
# analog input changes.
analog.changed(analog_changed)

value()#

The value() method returns the value of the analog device.

analog = Analog(brain.three_wire_port.a)

# Display the analog values
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Default:", analog.value())
    brain.screen.next_row()
    brain.screen.print("TENBIT:", analog.value(AnalogUnits.TENBIT))
    brain.screen.next_row()
    wait(.5, SECONDS)