イベント#

導入#

イベントを使用すると、関数を非同期で開始できます。イベントがブロードキャストされると、イベントのコールバック関数が独自のタスク(スレッド)で開始されるため、メインコードは同時に実行を継続できます。

重要: event オブジェクトには1つのコールバックが格納されます。同じイベントオブジェクトに別の関数を登録すると、以前のコールバックは置き換えられます。

1つのトリガーから複数の動作を実行したい場合は、次のいずれかの方法を選択してください。

  • 単一のコールバックから複数のヘルパー関数を呼び出す、または

  • 複数のイベントオブジェクトを使用し、それぞれをブロードキャストする。

  • タスクを独立して実行するには、別の スレッド を使用します。

イベントを作成して使用するには、以下の手順を順番に実行してください。

  1. event を使用してイベントを作成します。

  2. Set the event’s callback using the event name (ex: myEvent(myCallback);).

  3. Broadcast an event:

    • broadcast — イベント オブジェクトに登録されているすべての関数を並列実行するようにトリガーします。

    • broadcastAndWait — イベント オブジェクトに登録されているすべての関数をトリガーし、それらが完了するまで待機してから処理を続行します。

クラスコンストラクタ#

1 — コールバック関数なしで event オブジェクトを作成します。

event();

2 — コールバック関数を持つ event オブジェクトを作成します。

event( void (* callback)(void) );

3 — コールバック関数を使用して、別のイベントから event オブジェクトを作成します。

event( 
  event v, 
  void (* callback)(void) );

4 — 引数を受け取るコールバック関数を持つ event オブジェクトを作成します。

event( void (* callback)(void *), void *arg );

5 — 引数を受け取るコールバック関数を使用して、別のイベントから event オブジェクトを作成します。

event( event v, void (* callback)(void *), void *arg );

クラスデストラクタ#

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

~event();

パラメータ#

パラメータ

タイプ

説明

callback

void (*)(void)

イベントがブロードキャストされた際に実行されるように事前に定義された関数。

v

event

コピー元の既存のイベントオブジェクト。

callback

void (*)(void *)

voidポインタ引数を受け取り、イベントがブロードキャストされたときに実行される関数。

arg

void *

コールバック関数に渡されるデータへのポインタ。

注記#

  • An event object stores one callback. If you register another function with the same event object, it replaces the previous callback.

  • 1つのトリガーから複数の動作を実行したい場合は、次のいずれかの方法を選択してください。

    • 単一のコールバックから複数のヘルパー関数を呼び出す、または

    • 複数のイベントオブジェクトを使用し、それぞれをブロードキャストする。

    • タスクを独立して実行するには、別々のスレッドを使用します。

例#

// Create a callback function
void onMyEvent() {
  Brain.Screen.print("Callback function executed!");
}

int main() {
  // Create an event called myEvent and set onMyEvent
  // as the callback function.
  event myEvent(onMyEvent);
}

メンバーの機能#

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

  • operator — イベントのコールバック関数を設定します。

  • broadcast — イベントのコールバック関数を並列実行するようにトリガーします。

  • broadcastAndWait — イベントのコールバック関数をトリガーし、完了を待ちます。

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

/* This constructor is required when using VS Code.
Event configuration can be created with or without
a callback function as shown in the examples above. */

// Create an event without a callback
event myEvent;

// Or create an event with a callback
event myEvent(onMyEvent);

operator#

関数呼び出し演算子を使用して、イベントのコールバック関数を設定します。

Available Functions
void operator()( void (* callback)() );

Parameters

パラメータ

タイプ

説明

callback

void (*)(void)

イベントがブロードキャストされたときに自動的に呼び出される、事前に定義されたコールバック関数。この関数は、必要なコールバックシグネチャに一致する必要があります。詳細については、コールバック関数を参照してください。

Return Values

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

Notes
  • コールバックを再度設定すると、以前のコールバックは上書きされます。

  • コールバック関数は引数を取らず、戻り値はvoidでなければなりません。

Examples
void drawSquare() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawRectangle(40, 40, 120, 120);
}

void drawCircle() {
  Brain.Screen.clearScreen();
  Brain.Screen.drawCircle(100, 100, 60);
}

int main() {
  // Create the event drawEvent.
  event drawEvent;

  Brain.Screen.print("Drawing a square...");

  // Set the event to drawSquare.
  drawEvent(drawSquare);

  wait(1, seconds);

  // Trigger the event.
  drawEvent.broadcast();

  wait(1, seconds);

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Drawing a circle...");

  // Replace drawSquare with drawCircle.
  drawEvent(drawCircle);

  wait(1, seconds);

  // Trigger the event a second time.
  drawEvent.broadcast();
}

broadcast#

broadcast はイベントのコールバックを開始し、すぐに続行します。

Member Functions
void broadcast();

Parameters

この関数は引数を取りません。

Return Values

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

Examples
// Create a callback function.
void onMyEvent() {
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Event fired!");
}

int main() {
  Brain.Screen.print("Broadcasting in 2 seconds...");

  // Create an event called myEvent and set onMyEvent
  // as the callback function.
  event myEvent(onMyEvent);

  wait(2, seconds);

  // Trigger the event.
  myEvent.broadcast();
}

broadcastAndWait#

コールバックを開始し、完了するまで(またはタイムアウトするまで)待機します。

Available Functions
void broadcastAndWait( int32_t timeout = 60000 );

Parameters

パラメータ

タイプ

説明

timeout

int32_t

コードが再び実行を開始するまでの待機時間(ミリ秒単位)(デフォルト値は60000)。

Return Values

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

Notes
  • これは、終了するコールバック関数(無限ループではないもの)でのみ使用してください。

  • これは、コールバックが完了するかタイムアウトするまで実行をブロックする待機関数です。

Examples
void startDriving() {
  Drivetrain.drive(forward);
  wait(2, seconds);
}

int main() {
  Brain.Screen.print("Starting drive...");

  event driveEvent(startDriving);

  // Give the event time to be set.
  wait(0.25, seconds);

  // Run the event and wait until it finishes.
  driveEvent.broadcastAndWait();

  // Stop driving after 2 seconds.
  Drivetrain.stop();
  Brain.Screen.clearScreen();
  Brain.Screen.print("Drive complete!");
}