限制#
初始化限制类#
使用以下构造函数创建限位开关:
限制(端口)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 Limit Class 构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct a Limit Switch "limit" with the Limit class.
limit = Limit(brain.three_wire_port.a)
当引用 Limit 类方法时,此“limit”对象将在整个 API 文档的所有后续示例中使用。
类方法#
pressed()#
pressed(callback, arg)
方法注册了限位开关被按下时的回调函数。
参数 |
描述 |
---|---|
打回来 |
按下限位开关时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# Define a function switch_pressed().
def switch_pressed():
# The Brain will print that the Limit Switch was pressed
# on the Brain's screen.
brain.screen.print("switch pressed")
# Run switch_pressed when the Limit Switch is pressed.
limit.pressed(switch_pressed)
released()#
released(callback, arg)
方法注册了限位开关释放时的回调函数。
参数 |
描述 |
---|---|
打回来 |
当限位开关被释放时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# Define a function switch_released().
def switch_released():
# The Brain will print that the Limit Switch was released
# on the Brain's screen.
brain.screen.print("switch released")
# Run switch_released when the Limit Switch is released.
limit.released(switch_released)
pressing()#
pressing()
方法检查限位开关当前是否被按下。
返回: 如果限位开关当前被按下,则返回 True
。否则,返回 False
。