保险杠#
初始化保险杠类#
使用以下构造函数创建保险杠传感器:
Bumper(port)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 Bumper 类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct a Bumper Sensor "bumper" with the
# Bumper class.
bumper = Bumper(brain.three_wire_port.a)
This bumper
object will be used in all subsequent examples throughout this API documentation when referring to Bumper class methods.
类方法#
pressed()#
The pressed(callback, arg)
method registers a function to be called when the Bumper Sensor is pressed.
参数 |
描述 |
---|---|
打回来 |
当保险杠传感器被按下时将调用的函数。 |
arg |
**可选。**用于向回调函数传递参数的元组。 |
**返回:**事件类的一个实例。
# Define a function bumper_pressed().
def bumper_pressed():
# The Brain will print that the Bumper Sensor was pressed
# on the Brain's screen.
brain.screen.print("Bumper Sensor pressed")
# Run bumper_pressed when the Bumper Sensor is pressed.
bumper.pressed(bumper_pressed)
released()#
The released(callback, arg)
method registers a function to be called when the Bumper Sensor is released.
参数 |
描述 |
---|---|
打回来 |
当保险杠传感器被释放时将调用的函数。 |
arg |
**可选。**用于向回调函数传递参数的元组。 |
**返回:**事件类的一个实例。
# Define a function bumper_released.
def bumper_released():
# The Brain will print that the Bumper Sensor was released
# on the Brain's screen.
brain.screen.print("Bumper Sensor released")
# Run bumper_released when the Bumper Sensor is released.
bumper.released(bumper_released)
pressing()#
The pressing()
method checks if the Bumper Sensor is currently being pressed.
Returns: True
if the Bumper Sensor is currently being pressed. False
if it is not being pressed.