ตัวจับเวลา#

การแนะนำ#

คลาส timer ใช้สำหรับรักษาเวลาและโต้ตอบกับตัวจับเวลาโดยอิสระจาก ตัวจับเวลาของสมอง

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

timer();

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

ทำลายอ็อบเจ็กต์ timer และปล่อยทรัพยากรที่เกี่ยวข้อง

~timer();

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

คอนสตรัคเตอร์นี้ไม่รับพารามิเตอร์ใดๆ

ตัวอย่าง#

// Create a timer instance
timer myTimer = timer();

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

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

  • reset — Resets the timer to zero.

  • value — Returns the elapsed time since the timer started.

  • event — เรียกฟังก์ชันหลังจากจำนวนมิลลิวินาทีที่ระบุ โดยมีอาร์กิวเมนต์เสริม

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

/* This constructor is required to use a 
custom timer. Replace the values
as needed. */

// Create a timer instance
timer myTimer = timer();

/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */

// Create the Brain to use Brain.Timer
brain Brain = brain();

reset#

ตั้งค่าตัวจับเวลาเป็นศูนย์

Available Functions
void reset();

Parameters

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

Return Values

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

Examples
while (true) {
  // Clear the timer every time the screen is pressed
  if (Brain.Screen.pressing()) {
    myTimer.reset();
  }
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", myTimer.value());
  wait(50, msec);
}

value#

ส่งคืนค่าปัจจุบันของตัวจับเวลา

Available Functions
double value();

Parameters

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

Return Values

ตัวเลขทศนิยมที่แสดงปริมาณเวลาที่ผ่านไปในหน่วยวินาที

Notes
  • ตัวจับเวลาจะเริ่มนับเมื่อสร้างตัวจับเวลาเสร็จแล้ว

  • Calling reset resets the timer value to zero.

Examples
// Display the timer as it ticks up
while (true) {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Time: %.2f", myTimer.value());

  wait(50, msec);
}

event#

ลงทะเบียนฟังก์ชันเรียกกลับ (callback function) เพื่อให้ทำงานหลังจากระยะเวลาหน่วงที่กำหนดไว้

Available Functions

1 — ลงทะเบียนฟังก์ชันเรียกกลับ (callback) เพื่อให้ทำงานหลังจากหน่วงเวลาที่กำหนด และส่งค่าที่ผู้ใช้กำหนดไปยังฟังก์ชันเรียกกลับนั้น

static void event(
    void (*callback)(void *),
    uint32_t value );

2 — ลงทะเบียนฟังก์ชันเรียกกลับ (callback) เพื่อให้ทำงานหลังจากหน่วงเวลาที่กำหนด

static void event(
    void (*callback)(void),
    uint32_t value );

Parameters

พารามิเตอร์

พิมพ์

คำอธิบาย

callback

void (*)(void *)

ฟังก์ชันที่จะถูกเรียกใช้หลังจากระยะเวลาหน่วงที่ระบุไว้ และรับอาร์กิวเมนต์ที่ผู้ใช้กำหนด

callback

void (*)(void)

ฟังก์ชันที่จะถูกเรียกใช้หลังจากระยะเวลาหน่วงที่ระบุไว้

value

uint32_t

ระยะเวลาหน่วง (หน่วยเป็นมิลลิวินาที) ก่อนที่ฟังก์ชันเรียกกลับจะถูกเรียกใช้งาน

Return Values

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

Notes
  • ฟังก์ชันนี้จะกำหนดเวลาเรียกกลับเพียงครั้งเดียวเท่านั้น และจะไม่เรียกซ้ำโดยอัตโนมัติ

  • ฟังก์ชันเรียกกลับต้องส่งคืนค่า void

  • อาจมีการจัดกิจกรรมหลายอย่างพร้อมกัน

Examples

กำหนดฟังก์ชัน (นอก int main())

// Create a function to turn the screen orange
void turnOrange() {
  Brain.Screen.clearScreen(orange);
}

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

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

  timer myTimer = timer();
  // Call turnOrange after 2 seconds
  myTimer.event(turnOrange, 2000);
}