タイマー#
導入#
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 クラスには、以下のメンバ関数が含まれています。
タイマー timer メンバ関数を呼び出す前に、以下に示すように brain インスタンスを作成する必要があります。
// Create the V5 Brain
brain Brain = brain();
clear#
タイマーの現在の値をゼロにリセットします。その後、タイマーは直ちにゼロからカウントアップを開始します。
Available Functionsvoid clear();
この関数は引数を受け付けません。
Return Valuesこの関数は値を返しません。
Exampleswhile (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 Functions1 — 経過時間をミリ秒単位で返します。
uint32_t time() const;
Parameters2 — 指定された時間単位で経過時間を返します。
double time( timeUnits units ) const;
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
経過時間を返す際に使用する単位:
|
引数なしの関数(1)は、経過時間をミリ秒単位で表す
uint32_tを返します。パラメータ
timeUnits(2) を持つ関数は、指定された単位での経過時間を表す doubledoubleを返します。
タイマーはプロジェクト開始と同時にカウントを開始します。
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); }