电位器#
初始化电位器类#
使用以下构造函数创建电位器:
电位器(端口)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用电位器类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct a Potentiometer "pot" with the Potentiometer class.
pot = Potentiometer(brain.three_wire_port.a)
当引用电位器类方法时,此“pot”对象将在整个 API 文档的所有后续示例中使用。
类方法#
angle()#
angle(units)
方法返回电位器测量的角度。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型或 |
**返回:**电位器测量的角度
# Print the current angle of the Potentiometer to the
# Brain's screen.
brain.screen.print(pot.angle())
changed()#
changed(callback, arg)
方法注册一个回调函数,用于当电位器测量的值发生变化时执行。
参数 |
描述 |
---|---|
打回来 |
当电位器测量的值发生变化时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# Define a function potentiometer_changed().
def potentiometer_changed():
# The Brain will print that the Potentiometer value changed
# on the Brain's screen.
brain.screen.print("potentiometer changed")
# Run potentiometer_changed when the value measured by
# the Potentiometer changes.
pot.changed(potentiometer_changed)