イベント#
導入#
イベントを使用すると、関数を非同期で開始できます。イベントがブロードキャストされると、イベントのコールバック関数が独自のタスク(スレッド)で開始されるため、メインコードは同時に実行を継続できます。
重要: event オブジェクトには1つのコールバックが格納されます。同じイベントオブジェクトに別の関数を登録すると、以前のコールバックは置き換えられます。
1つのトリガーから複数の動作を実行したい場合は、次のいずれかの方法を選択してください。
単一のコールバックから複数のヘルパー関数を呼び出す、または
複数のイベントオブジェクトを使用し、それぞれをブロードキャストする。
タスクを独立して実行するには、別の スレッド を使用します。
イベントを作成して使用するには、以下の手順を順番に実行してください。
eventを使用してイベントを作成します。Set the event’s callback using the event name (ex:
myEvent(myCallback);).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();
パラメータ#
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
イベントがブロードキャストされた際に実行されるように事前に定義された関数。 |
|
|
コピー元の既存のイベントオブジェクト。 |
|
|
voidポインタ引数を受け取り、イベントがブロードキャストされたときに実行される関数。 |
|
|
コールバック関数に渡されるデータへのポインタ。 |
注記#
An
eventobject stores one callback. If you register another function with the same event object, it replaces the previous callback.1つのトリガーから複数の動作を実行したい場合は、次のいずれかの方法を選択してください。
単一のコールバックから複数のヘルパー関数を呼び出す、または
複数のイベントオブジェクトを使用し、それぞれをブロードキャストする。
タスクを独立して実行するには、別々のスレッドを使用します。
例#
コールバック関数を定義します(
int main()の外側)// Create a callback function void onMyEvent() { Brain.Screen.print("Callback function executed!"); }コールバックを登録する
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create an event called myEvent // 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 Functionsvoid operator()( void (* callback)() );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
イベントがブロードキャストされたときに自動的に呼び出される、事前に定義されたコールバック関数。この関数は、必要なコールバックシグネチャに一致する必要があります。詳細については、コールバック関数を参照してください。 |
この関数は値を返しません。
Notesコールバックを再度設定すると、以前のコールバックは上書きされます。
コールバック関数は引数を取らず、戻り値はvoidでなければなりません。
コールバック関数を定義します(
int main()の外側)// Create a callback function 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()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // 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 はイベントのコールバックを開始し、すぐに続行します。
void broadcast();
この関数は引数を取りません。
Return Valuesこの関数は値を返しません。
Examplesコールバック関数を定義します(
int main()の外側)// Create a callback function void onMyEvent() { Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Event fired!"); }コールバックを登録する
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); Brain.Screen.print("Broadcasting in 2 seconds..."); // Create an event called myEvent // Set onMyEvent as the callback function event myEvent(onMyEvent); wait(2, seconds); // Trigger the event myEvent.broadcast(); }
broadcastAndWait#
コールバックを開始し、完了するまで(またはタイムアウトするまで)待機します。
Available Functionsvoid broadcastAndWait( int32_t timeout = 60000 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
コードが再び実行を開始するまでの待機時間(ミリ秒単位)(デフォルト値は60000)。 |
この関数は値を返しません。
Notesこれは、終了するコールバック関数(無限ループではないもの)でのみ使用してください。
これは、コールバックが完了するかタイムアウトするまで実行をブロックする待機関数です。
コールバック関数を定義します(
int main()の外側)// Create a callback function void startDriving() { Drivetrain.drive(forward); wait(2, seconds); }コールバックを登録する
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); 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!"); }