模拟输入#
初始化 AnalogIn 类#
使用以下构造函数创建 AnalogIn 对象:
AnalogIn(port)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 AnalogIn 类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct an AnalogIn "analog_in" with the
# AnalogIn class.
analog_in = AnalogIn(brain.three_wire_port.a)
This analog_in
object will be used in all subsequent examples throughout this API documentation when referring to AnagloIn class methods.
类方法#
changed()#
The changed(callback, arg)
method registers a function to be called when the value of the analog input changes.
参数 |
描述 |
---|---|
打回来 |
当模拟输入值发生变化时调用的函数。 |
arg |
**可选。**用于向回调函数传递参数的元组。 |
**返回:**事件类的一个实例。
# Define the function analogIn_changed.
def analogIn_changed():
# The Brain will print that the analog input
# changed on the Brain's screen.
brain.screen.print("analog input changed")
# Run analogIn_changed when the value of the
# analog input changes.
analog_in.changed(analogIn_changed)
# Get the value of analog input in range 0 - 4095.
value = analog_in.value()
# Get the value of analog input in range 0 - 1023.
value = analog_in.value(AnalogUnits.TENBIT)