定時器#

介紹#

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#

註冊一個回調函數,該函數將在指定的延遲後執行。

Available Functions

1 — 註冊一個回呼函數,使其在指定的延遲後執行,並將使用者定義的值傳遞給該回呼函數。

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

2 — 註冊一個回呼函數,使其在指定的延遲後運作。

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);
}