保险杠#
初始化保险杠类#
使用以下构造函数创建 Bumper Switch:
bumper
构造函数在指定的三线端口中创建一个 bumper 对象:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 Bumper 类构造函数创建对象。
// Create the Brain.
brain Brain;
// Construct a Bumper Switch "BumperA" with the
// bumper class.
bumper BumperA = bumper(Brain.ThreeWirePort.A)
当引用 Bumper 类方法时,此“BumperA”对象将在整个 API 文档的所有后续示例中使用。
类方法#
按下()#
pressed(callback)
方法注册一个当按下保险杠开关时要调用的函数。
参数 |
描述 |
---|---|
打回来 |
当保险杠被按下时将调用的函数。 |
**返回:**事件类的一个实例。
// Define the BumperPressed function with a void return type,
// showing it doesn't return a value.
void BumperPressed() {
// The Brain will print that the Bumper Switch was pressed on the
// Brain's screen.
Brain.Screen.print("Bumper pressed");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run BumperPressed when the value of the Bumper Switch is pressed.
BumperA.pressed(BumperPressed);
}
发布()#
released(callback)
方法注册一个在 Bumper Switch 被释放时调用的函数。
参数 |
描述 |
---|---|
打回来 |
当保险杠被释放时将调用的函数。 |
**返回:**事件类的一个实例。
// Define the BumperReleased function with a void return
// type, showing it doesn't return a value.
void BumperReleased() {
// The Brain will print that the Bumper Switch was released
// on the Brain's screen.
Brain.Screen.print("Bumper released");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run bumperReleased when the value of the Bumper Switch is released.
BumperA.released(bumperReleased);
}
紧迫()#
pressing()
方法检查保险杠开关当前是否被按下。
**返回值:**表示保险杠开关状态的整数值。如果保险杠开关被按下,则返回“1”。如果没有按下,则返回“0”。