カスタムタイマー#

導入#

timer クラスは、Brain のタイマー とは独立して時間を管理し、タイマーとやり取りするために使用されます。

クラスコンストラクタ#

timer();

クラスデストラクタ#

タイマー timer オブジェクトを破棄し、関連するリソースを解放します。

~timer();

パラメータ#

このコンストラクタはパラメータを受け付けません。

例#

// Create a timer instance
timer myTimer = timer();

メンバーの機能#

timer クラスには、以下のメンバ関数が含まれています。

  • clear — タイマーをゼロにリセットします。

  • time — タイマー開始からの経過時間を返します。

  • event — 指定されたミリ秒数後に、オプションの引数とともに関数を呼び出します。

タイマー 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 Functions
void clear();

Parameters

この関数は引数を受け付けません。

Return Values

この関数は値を返しません。

Examples
while (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 Functions

1 — 経過時間をミリ秒単位で返します。

uint32_t time() const;

2 — 指定された時間単位で経過時間を返します。

double time(
    timeUnits units ) const;

Parameters

パラメータ

タイプ

説明

units

timeUnits

経過時間を返す際に使用する単位:

  • seconds sec —秒
  • msec —ミリ秒

Return Values
  • 引数なしの関数(1)は、経過時間をミリ秒単位で表す uint32_t を返します。

  • パラメータ timeUnits (2) を持つ関数は、指定された単位での経過時間を表す double 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", myTimer.time(sec));

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

  timer myTimer = timer();
  // Call turnOrange after 2 seconds
  myTimer.event(turnOrange, 2000);
}