自訂計時器#
介紹#
timer 類別用於保持時間並與獨立於 大腦的計時器 的計時器互動。
類別建構函數#
timer();
類別析構函數#
銷毀計時器 timer 物件並釋放相關資源。
~timer();
參數#
此建構函數不接受任何參數。
範例#
// Create a timer instance
timer myTimer = timer();
成員功能#
timer 類別包含以下成員函數:
在呼叫 timer 成員函數之前,必須先建立一個 timer 實例,如下所示:
/* This constructor is required to use a
custom timer. Replace the values
as needed. */
// Create a timer instance
timer myTimer = timer();
clear#
將計時器設定為零。
Available Functionsvoid clear();
此函數不接受任何參數。
Return Values此函數不傳回值。
Exampleswhile (true) {
// Clear the timer every time the screen is pressed
if (Brain.Screen.pressing()) {
myTimer.clear();
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", myTimer.time(msec));
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", myTimer.time(sec));
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(); timer myTimer = timer(); // Call turnOrange after 2 seconds myTimer.event(turnOrange, 2000); }