定時器#

介紹#

timer 類衍生自 brain 基類,提供測量經過時間的功能,讓您在 VEX V5 程式中追蹤持續時間和安排基於時間的事件。

使用權#

Brain.Timer

筆記#

  • Brain.Timer 是 timer 類別的一個實例,並提供相同的功能。

例子#

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

// Create the V5 Brain
brain Brain = brain();

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

  wait(50, msec);
}

成員功能#

timer 類別包含以下成員函數:

  • clear — 將計時器重設為零。

  • time — 返回自計時器開始以來經過的時間。

  • event — 在指定的毫秒數後呼叫函數,參數可選。

在呼叫 timer 成員函數之前,必須先建立一個 brain 實例,如下所示:

// Create the V5 Brain
brain Brain = brain();

clear#

將計時器的目前值重設為零。之後,計時器立即從零開始重新計數。

Available Functions
void clear();

Parameters

此函數不接受任何參數。

Return Values

此函數不傳回值。

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

time#

傳回計時器的目前值。

Available Functions

1 — 傳回經過的時間(以毫秒為單位)。

uint32_t time() const;

2 — 傳回以指定時間單位計算的經過時間。

double time(
    timeUnits units ) const;

Parameters

範圍

類型

描述

units

timeUnits

The units used to return the elapsed time:

  • seconds / sec — seconds
  • msec — milliseconds

Return Values
  • 無參數函數 (1) 傳回 uint32_t ,表示經過的時間(以毫秒為單位)。

  • 帶有 timeUnits 參數 (2) 的函數傳回一個 double 表示以指定單位表示的經過時間。

Notes
  • 專案開始時,計時器開始計時。

  • 呼叫 clear 將計時器值重設為零。

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

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

  // Call turnOrange after 2 seconds
  Brain.Timer.event(turnOrange, 2000);
}