活动#
介绍#
事件允许你异步启动函数。当一个事件被广播时,该事件的回调函数会在它自己的任务(线程)中启动,因此你的主代码可以同时继续运行。
重要提示:事件 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!"); }