カスタムタイマー#
導入#
timer クラスは、Brain のタイマー とは独立して時間を管理し、タイマーとやり取りするために使用されます。
クラスコンストラクタ#
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;
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
経過時間を返す際に使用する単位:
|
引数なしの関数(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", 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); }