สวิตช์กันชน#

การแนะนำ#

คลาส bumper ใช้สำหรับตรวจจับเมื่อกดและปล่อยสวิตช์กันชน

คอนสตรัคเตอร์คลาส#

bumper( 
  int32_t index );

ตัวทำลายคลาส#

ทำลายวัตถุ bumper และปล่อยทรัพยากรที่เกี่ยวข้อง

virtual ~bumper();

พารามิเตอร์#

พารามิเตอร์

พิมพ์

คำอธิบาย

index

int32_t

พอร์ตอัจฉริยะที่เชื่อมต่อกับสวิตช์กันชน จะเขียนในรูปแบบ PORTxโดยที่ x คือหมายเลขพอร์ต (ตัวอย่างเช่น PORT1)

ตัวอย่าง#

// Create a bumper instance in Port 1
bumper Bumper1 = bumper(PORT1);

ฟังก์ชันสมาชิก#

คลาส bumper ประกอบด้วยฟังก์ชันสมาชิกต่อไปนี้:

  • pressing — ส่งกลับหากกดสวิตช์กันชน

  • pressed — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อกดสวิตช์กันชน

  • released — ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อปล่อยสวิตช์กันชน

ก่อนเรียกใช้ฟังก์ชันสมาชิก bumper ใดๆ จะต้องสร้างอินสแตนซ์ bumper ก่อน ดังแสดงด้านล่าง:

/* This constructor is required when using VS Code.
Bumper Switch configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a bumper instance in Port 1
bumper Bumper1 = bumper(PORT1);

pressing#

ส่งคืนค่าหากขณะนี้กำลังกดสวิตช์กันชนอยู่

Available Functions
int32_t pressing();

Parameters

ฟังก์ชันนี้ไม่รับพารามิเตอร์ใดๆ

Return Values

ส่งคืนค่า int32_t เพื่อระบุว่าสวิตช์กันชนถูกกดอยู่หรือไม่

  • 1 — สวิตช์กันชนกำลังถูกกด

  • 0 — ไม่ได้กดสวิตช์กันชน

pressed#

ลงทะเบียนฟังก์ชันที่จะถูกเรียกใช้เมื่อกดสวิตช์กันชน

Available Functions
void pressed( void (* callback)(void) );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void)

ตัวชี้ไปยังฟังก์ชันที่จะถูกเรียกเมื่อกดปุ่มกันชน ฟังก์ชันนี้ต้องไม่มีพารามิเตอร์และส่งคืนค่าว่าง (void)

Return Values

ฟังก์ชันนี้ไม่ส่งค่ากลับ

Examples

กำหนดฟังก์ชันเรียกกลับ (นอก int main())

// Display a message when bumper is pressed
void BumperPressed() {
  Brain.Screen.print("Bumper pressed");
}

ลงทะเบียนฟังก์ชันเรียกกลับภายใน int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run BumperPressed when the value of the Bumper Switch is pressed.
  Bumper1.pressed(BumperPressed);
}

released#

ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อปล่อยสวิตช์กันชน

Available Functions
void released( void (* callback)(void) );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void)

ตัวชี้ไปยังฟังก์ชันที่จะถูกเรียกเมื่อปล่อยกันชน ฟังก์ชันนี้ต้องไม่มีพารามิเตอร์และส่งคืนค่าว่าง (void)

Return Values

ฟังก์ชันนี้ไม่ส่งค่ากลับ

Examples

กำหนดฟังก์ชันเรียกกลับ (นอก int main())

// Display a message when bumper is released
void BumperReleased() {
  Brain.Screen.print("Bumper released");
}

ลงทะเบียนฟังก์ชันเรียกกลับภายใน int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run BumperReleased when the value of the Bumper Switch is released.
  Bumper1.released(BumperReleased);
}