定時器#
介紹#
timer 類衍生自 brain 基類,提供測量經過時間的功能,讓您在 VEX EXP 程式中追蹤持續時間和安排基於時間的事件。
使用權#
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 EXP 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 類別包含以下成員函數:
在呼叫 timer 成員函數之前,必須先建立一個 brain 實例,如下所示:
// Create the EXP Brain
brain Brain = brain();
clear#
將計時器的目前值重設為零。之後,計時器立即從零開始重新計數。
Available Functionsvoid clear();
此函數不接受任何參數。
Return Values此函數不傳回值。
Exampleswhile (true) {
// Reset the timer every time the check button is pressed
if (Brain.buttonCheck.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 Functions1 — 傳回經過的時間(以毫秒為單位)。
uint32_t time() const;
Parameters2 — 傳回以指定時間單位計算的經過時間。
double time( timeUnits units ) const;
範圍 |
類型 |
描述 |
|---|---|---|
|
|
The units used to return the elapsed time:
|
無參數函數 (1) 傳回
uint32_t,表示經過的時間(以毫秒為單位)。帶有
timeUnits參數 (2) 的函數傳回一個double表示以指定單位表示的經過時間。
專案開始時,計時器開始計時。
呼叫
clear將計時器值重設為零。
// 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 Functions1 — 註冊一個回呼函數,使其在指定的延遲後執行,並將使用者定義的值傳遞給該回呼函數。
static void event( void (*callback)(void*), uint32_t value );
Parameters2 — 註冊一個回呼函數,使其在指定的延遲後運作。
static void event( void (*callback)(void), uint32_t value );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
一個將在指定延遲後調用並接收用戶定義參數的函數。 |
|
|
將在指定延遲時間後呼叫的函數。 |
|
|
回呼函數執行前的延遲時間(以毫秒為單位)。 |
此函數不傳回值。
Notes此函數會安排一次性回調;它不會自動重複執行。
回呼函數必須傳回
void。可以同時安排多個活動。
定義函數(在
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); }