活動#
介紹#
事件允許你異步啟動函數。當一個事件被廣播時,該事件的回呼函數會在它自己的任務(線程)中啟動,因此你的主程式碼可以同時繼續運行。
重要提示:事件 event 儲存一個回呼函數。如果您使用同一個事件物件註冊另一個函數,它將替換先前的回呼函數。
如果你希望一個觸發器觸發多個事件,那麼:
從單一回呼函數呼叫多個輔助函數,或者
使用多個事件物件並分別廣播它們。
使用單獨的執行緒獨立運行任務。
若要建立和使用事件,請依照下列步驟操作:
使用
event建立事件。設定事件回呼 使用事件名稱(例如:
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 指標參數並在事件廣播時執行的函數。 |
|
|
指向將要傳遞給回呼函數的資料的指標。 |
筆記#
一個事件
event儲存一個回呼函數。如果您使用同一個事件物件註冊另一個函數,它將替換先前的回呼函數。如果你希望一個觸發器觸發多個事件,那麼:
從單一回呼函數呼叫多個輔助函數,或者
使用多個事件物件並分別廣播它們。
使用單獨的執行緒獨立運行任務。
例子#
定義回呼函數(在
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!"); }