タイマー#

導入#

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 クラスには、以下のメンバ関数が含まれています。

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

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

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

タイマー timer メンバ関数を呼び出す前に、以下に示すように brain インスタンスを作成する必要があります。

// Create the V5 Brain
brain Brain = brain();

clear#

タイマーの現在の値をゼロにリセットします。その後、タイマーは直ちにゼロからカウントアップを開始します。

Available Functions
void clear();

Parameters

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

Return Values

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

Examples
while (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 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", Brain.Timer.time());

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

  // Call turnOrange after 2 seconds
  Brain.Timer.event(turnOrange, 2000);
}